--- projects/cms/source/ihost/ihost.c 2004/05/30 17:13:05 1.50 +++ projects/cms/source/ihost/ihost.c 2004/05/31 13:35:23 1.51 @@ -32,13 +32,14 @@ #include #include #include +#include #include #include #define LOG_CRIT 0 #define LOG_ERR 1 -#define LOG_INFO 2 +#define LOG_INFO 2 #define LOG_DEBUG 3 typedef struct{ @@ -52,24 +53,23 @@ typedef struct{ char *server_fqdn; int server_udp_port; - + /* Weird stuff iscream wants sent to it */ char *last_modified; char *file_list; int udp_update_time; -// int config_ttl; time_t config_ttl; }ihost_state_t; typedef struct{ - int verbose; - int daemon; + int verbose; + int daemon; FILE *log; -}ihost_config_t; +}ihost_config_t; typedef struct{ struct sockaddr_in addr; @@ -119,33 +119,33 @@ extern int errno; */ size_t strlcat(dst, src, siz) - char *dst; - const char *src; - size_t siz; + char *dst; + const char *src; + size_t siz; { - register char *d = dst; - register const char *s = src; - register size_t n = siz; - size_t dlen; + register char *d = dst; + register const char *s = src; + register size_t n = siz; + size_t dlen; - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = siz - dlen; + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = '\0'; + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; - return(dlen + (s - src)); /* count does not include NUL */ + return(dlen + (s - src)); /* count does not include NUL */ } #endif @@ -200,7 +200,7 @@ FILE *create_tcp_connection(char *hostname, int port){ struct in_addr haddr; FILE *f; - log_msg(LOG_DEBUG, "Creating tcp socket"); + log_msg(LOG_DEBUG, "Creating tcp socket"); if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ log_msg(LOG_CRIT, "Failed to make TCP Socket"); return NULL; @@ -244,7 +244,7 @@ int tcp_comm(FILE *f, char *send, char **response, cha if(*response!=NULL) log_msg(LOG_DEBUG, "Received %s", *response); if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; - + if(expected==NULL) return 0; if((strcmp(expected, *response))==0) return 0; @@ -254,7 +254,7 @@ int tcp_comm(FILE *f, char *send, char **response, cha } int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){ - if((tcp_comm(f, send, response, expected))!=0){ + if((tcp_comm(f, send, response, expected))!=0){ return -1; } @@ -263,7 +263,7 @@ int tcp_comm_strdup(FILE *f, char *send, char **respon return 0; } - + int ihost_getconfig(ihost_state_t *ihost_state){ FILE *tcp_con; @@ -271,7 +271,7 @@ int ihost_getconfig(ihost_state_t *ihost_state){ char *response_ptr; /* Keep these in case of a failure and so it can keep running on the old config */ - char *file_list=NULL; + char *file_list=NULL; char *last_modified=NULL; char *host_fqdn=NULL; char *host_ip=NULL; @@ -287,16 +287,16 @@ int ihost_getconfig(ihost_state_t *ihost_state){ if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ if(tcp_con==NULL){ goto error; - } - + } + if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ goto error; } - + if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ goto error; } - + if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ goto error; @@ -320,7 +320,7 @@ int ihost_getconfig(ihost_state_t *ihost_state){ goto error; } last_modified=response; - + if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ goto error; } @@ -365,18 +365,18 @@ int ihost_getconfig(ihost_state_t *ihost_state){ } response_ptr++; if(response_ptr==NULL){ - log_msg(LOG_ERR, "Incorrect data sent by server"); - goto error; - } + log_msg(LOG_ERR, "Incorrect data sent by server"); + goto error; + } - server_udp_port=atoi(response_ptr); + server_udp_port=atoi(response_ptr); if (server_udp_port==0){ log_msg(LOG_ERR, "Incorrect data sent by server"); goto error; } } - + if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ goto error; } @@ -387,14 +387,14 @@ int ihost_getconfig(ihost_state_t *ihost_state){ /* Free the old data before pointing them to the new data. m_free copes should * this already be NULL */ - if(ihost_state->file_list!=NULL) free(ihost_state->file_list); - if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); - if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); + if(ihost_state->file_list!=NULL) free(ihost_state->file_list); + if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); + if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); if(ihost_state->preset_fqdn){ if(host_fqdn != NULL) free(host_fqdn); }else{ - if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); + if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); ihost_state->host_fqdn=host_fqdn; } @@ -432,7 +432,7 @@ error: int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ char tmp[size]; - sg_cpu_percents *cpu_percent; + sg_cpu_percents *cpu_percent; sg_mem_stats *mem_stats; sg_load_stats *load_stats; sg_user_stats *user_stats; @@ -453,11 +453,12 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* Print start of the packet we want */ snprintf(xml, size, "", \ - seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); + seq_no, ihost_state->host_fqdn, (long) time(NULL), ihost_state->host_ip); /* Get cpu stats, check it is correct, then fill in its entry for the xml */ if((cpu_percent=sg_get_cpu_percents())==NULL){ - log_msg(LOG_CRIT, "Failed to get cpu statistics"); + log_msg(LOG_CRIT, "Failed to get cpu statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ snprintf(tmp, size, \ "%3.2f%3.2f%3.2f%3.2f%3.2f", \ @@ -470,10 +471,11 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ if(strlcat(xml, tmp, size) >= size) goto too_big_error; } - - /*Get mem stats, and fill in xml */ + + /*Get mem stats, and fill in xml */ if((mem_stats=sg_get_mem_stats())==NULL){ - log_msg(LOG_CRIT, "Failed to get memory statistics"); + log_msg(LOG_CRIT, "Failed to get memory statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ snprintf(tmp, size, \ "%lld%lld%lld%lld", \ @@ -481,14 +483,15 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ mem_stats->free, \ mem_stats->used, \ mem_stats->cache); - + if(strlcat(xml, tmp, size) >= size) goto too_big_error; } - /* Get load stats */ + /* Get load stats */ if((load_stats=sg_get_load_stats())==NULL){ - log_msg(LOG_CRIT, "Failed to get load statistics"); + log_msg(LOG_CRIT, "Failed to get load statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ snprintf(tmp, size, \ "%.2lf%.2lf%.2lf", \ @@ -500,38 +503,41 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* get user stats */ - + if((user_stats=sg_get_user_stats())==NULL){ - log_msg(LOG_CRIT, "Failed to get user statistics"); + log_msg(LOG_CRIT, "Failed to get user statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ snprintf(tmp, size, \ "%s%d", \ user_stats->name_list, \ user_stats->num_entries); - + if(strlcat(xml, tmp, size) >= size) goto too_big_error; } /* swap stats */ if((swap_stats=sg_get_swap_stats())==NULL){ - log_msg(LOG_CRIT, "Failed to get swap statistics"); + log_msg(LOG_CRIT, "Failed to get swap statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ snprintf(tmp, size, \ "%lld%lld%lld",\ swap_stats->total, \ swap_stats->used, \ swap_stats->free); - + if(strlcat(xml, tmp, size) >= size) goto too_big_error; } /* general stats */ - + if((general_stats=sg_get_host_info())==NULL){ - log_msg(LOG_CRIT, "Failed to get general statistics"); + log_msg(LOG_CRIT, "Failed to get host info statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ snprintf(tmp, size, \ "%s%s%s%s%s%ld", \ @@ -541,15 +547,16 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ general_stats->hostname, \ general_stats->platform, \ (long)general_stats->uptime); - + if(strlcat(xml, tmp, size) >= size) goto too_big_error; - + } - + /* process stats */ - if((process_stats=sg_get_process_count())==NULL){ - log_msg(LOG_CRIT, "Failed to get general statistics"); + if((process_stats=sg_get_process_count())==NULL){ + log_msg(LOG_CRIT, "Failed to get process statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ snprintf(tmp, size, \ "%d%d%d%d%d",\ @@ -566,7 +573,8 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* Get paging stats */ if((page_stats=sg_get_page_stats_diff())==NULL){ - log_msg(LOG_CRIT, "Failed to get paging statistics"); + log_msg(LOG_CRIT, "Failed to get paging statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ if(page_stats->systime!=0){ x=page_stats->pages_pagein / page_stats->systime; @@ -579,15 +587,16 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ "%lld%lld", \ x, \ y); - + if(strlcat(xml, tmp, size) >= size) goto too_big_error; } /* get diskio stats */ - + if((diskio_stats=sg_get_disk_io_stats_diff(&diskio_entries))==NULL){ - log_msg(LOG_CRIT, "Failed to get diskio statistics"); + log_msg(LOG_CRIT, "Failed to get disk io statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ strlcat(xml, "", size); for(counter=0;counterwrite_bytes / diskio_stats->systime; }else{ x=diskio_stats->read_bytes; - y=diskio_stats->write_bytes; + y=diskio_stats->write_bytes; } - + snprintf(tmp, size, \ "", \ counter, \ @@ -616,11 +625,12 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ } - + /* get networks stats */ - + if((network_stats=sg_get_network_io_stats_diff(&network_entries))==NULL){ - log_msg(LOG_CRIT, "Failed to get network statistics"); + log_msg(LOG_CRIT, "Failed to get network io statistics: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); }else{ strlcat(xml, "", size); for(counter=0;counter", size); for(counter=0;counter", size) >= size) goto too_big_error; } - + if(strlcat(xml, "", size) >= size) goto too_big_error; /*If we got to here, it should of all been filled in nicely now */ @@ -691,28 +702,28 @@ too_big_error: } - + void usage(char *progname){ - fprintf(stderr, "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n", progname); - fprintf(stderr, " -v Verbose mode, -vv would make even more verbose\n"); - fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); + fprintf(stderr, "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n", progname); + fprintf(stderr, " -v Verbose mode, -vv would make even more verbose\n"); + fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); fprintf(stderr, " -n Set the machine name to be reported as\n"); fprintf(stderr, " -i Set the IP to be reported as\n"); fprintf(stderr, " -s Specifies the i-scream server to connect to\n"); - fprintf(stderr, " default: %s\n", DEF_SERVER_NAME); + fprintf(stderr, " default: %s\n", DEF_SERVER_NAME); fprintf(stderr, " -p Specifies the i-scream server port\n"); - fprintf(stderr, " default: %d\n", DEF_SERVER_PORT); - fprintf(stderr, " -V Print version number\n"); - fprintf(stderr, " -h Prints this help page\n"); + fprintf(stderr, " default: %d\n", DEF_SERVER_PORT); + fprintf(stderr, " -V Print version number\n"); + fprintf(stderr, " -h Prints this help page\n"); fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); - exit(1); + exit(1); } int main(int argc, char **argv){ ihost_state_t ihost_state; udp_sockinfo_t udp_sockinfo; - + int cmdopt; extern int optind; pid_t pid; @@ -724,7 +735,7 @@ int main(int argc, char **argv){ time_t cur_time, sleep_delay, udp_time=0, config_time=0; - /* Set default settings */ + /* Set default settings */ ihost_config.verbose=1; ihost_config.daemon=1; /* Set all errors to go down stderr until told otherwise */ @@ -741,25 +752,25 @@ int main(int argc, char **argv){ ihost_state.file_list=NULL; ihost_state.last_modified=NULL; - while((cmdopt=getopt(argc, argv, "vfVn:i:s:p:h")) != -1){ - switch(cmdopt){ - case 'v': - ihost_config.verbose++; - break; + while((cmdopt=getopt(argc, argv, "vfVn:i:s:p:h")) != -1){ + switch(cmdopt){ + case 'v': + ihost_config.verbose++; + break; - case 'f': - /* Force syslog logging since stderr will be closed in this case */ - ihost_config.daemon=0; - break; + case 'f': + /* Force syslog logging since stderr will be closed in this case */ + ihost_config.daemon=0; + break; - case 'V': - fprintf(stderr, "%s version %s\n", argv[0], VERSION); - break; + case 'V': + fprintf(stderr, "%s version %s\n", argv[0], VERSION); + break; case 'n': ihost_state.preset_fqdn = 1; ihost_state.host_fqdn = strdup(optarg); if(ihost_state.host_fqdn == NULL){ - fprintf(stderr, "Missing hostname\n"); + fprintf(stderr, "Missing hostname\n"); usage(argv[0]); } break; @@ -768,7 +779,7 @@ int main(int argc, char **argv){ ihost_state.preset_ip = 1; ihost_state.host_ip = strdup(optarg); if(ihost_state.host_ip == NULL){ - fprintf(stderr, "Missing ip\n"); + fprintf(stderr, "Missing ip\n"); usage(argv[0]); } break; @@ -781,12 +792,12 @@ int main(int argc, char **argv){ ihost_state.filtermanager_port=atoi(optarg); break; - case 'h': - default: - usage(argv[0]); - exit(1); - } - } + case 'h': + default: + usage(argv[0]); + exit(1); + } + } if(gethostbyname(ihost_state.filtermanager_host)==NULL){ log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); @@ -801,56 +812,57 @@ int main(int argc, char **argv){ pid=fork(); if(pid==-1){ log_msg(LOG_CRIT, "Failed to background exiting"); - exit(1); + exit(1); }else if(pid!=0){ - /* Parent process */ - return 0; - } - /* We should now be in the background*/ - if(setsid()==-1){ - log_msg(LOG_CRIT, "setsid failed"); - exit(1); - } - + /* Parent process */ + return 0; + } + /* We should now be in the background*/ + if(setsid()==-1){ + log_msg(LOG_CRIT, "setsid failed"); + exit(1); + } + if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ ihost_config.log=stderr; log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); exit(1); } - fclose(stdin); - fclose(stdout); - fclose(stderr); + fclose(stdin); + fclose(stdout); + fclose(stderr); - } + } log_msg(LOG_INFO, "Starting ihost..."); - log_msg(LOG_DEBUG, "Running statgrab_init()"); - if(statgrab_init()){ - log_msg(LOG_CRIT, "statgrab_init failed (%m)"); + log_msg(LOG_DEBUG, "Running sg_init()"); + if(sg_init()){ + log_msg(LOG_CRIT, "sg_init failed: %s (%s)", + sg_str_error(sg_get_error()), sg_get_error_arg()); exit(1); } - - log_msg(LOG_DEBUG,"Writing PID FILE"); - pid=getpid(); + log_msg(LOG_DEBUG,"Writing PID FILE"); - if((f=fopen(PID_FILE,"w")) == NULL){ - log_msg(LOG_CRIT, "Failed to write PID file"); - }else{ - if((fprintf(f,"%d",(int)pid)) <= 0 ){ - log_msg(LOG_CRIT, "Failed to write PID file"); - } - if((fclose(f))!=0){ - log_msg(LOG_CRIT, "failed to close PID file"); - } - } + pid=getpid(); - /* Get the initial config from the filter manager. Should this fail, + if((f=fopen(PID_FILE,"w")) == NULL){ + log_msg(LOG_CRIT, "Failed to write PID file"); + }else{ + if((fprintf(f,"%d",(int)pid)) <= 0 ){ + log_msg(LOG_CRIT, "Failed to write PID file"); + } + if((fclose(f))!=0){ + log_msg(LOG_CRIT, "failed to close PID file"); + } + } + + /* Get the initial config from the filter manager. Should this fail, * wait, and then try again. */ - get_diskio_stats_diff(&packet_num); + sg_get_disk_io_stats_diff(&packet_num); packet_num=0; while(ihost_getconfig(&ihost_state)!=0){ @@ -859,7 +871,7 @@ int main(int argc, char **argv){ } while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ - log_msg(LOG_ERR, "Failed to create udp socket"); + log_msg(LOG_ERR, "Failed to create udp socket"); sleep(10); } @@ -876,14 +888,14 @@ int main(int argc, char **argv){ len=strlen(packet); log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); - + if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ log_msg(LOG_CRIT, "Failed to send packet"); } udp_time=cur_time+ihost_state.udp_update_time; log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); } - + if(cur_time>=config_time){ if(ihost_getconfig(&ihost_state)!=0){ /* If we can't get the config, try again 5 minutes time */ @@ -892,9 +904,9 @@ int main(int argc, char **argv){ }else{ close(udp_sockinfo.sock); - while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ - log_msg(LOG_CRIT, "Failed to create udp socket"); - sleep(10); + while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ + log_msg(LOG_CRIT, "Failed to create udp socket"); + sleep(10); } config_time=time(NULL)+ihost_state.config_ttl; @@ -906,7 +918,7 @@ int main(int argc, char **argv){ sleep_delay=udp_time-time(NULL); log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); if(sleep_delay>0) sleep(sleep_delay); - } - + } + return(0); }