--- projects/cms/source/ihost/ihost.c 2003/03/08 20:41:46 1.33 +++ projects/cms/source/ihost/ihost.c 2004/05/30 17:13:05 1.50 @@ -1,6 +1,6 @@ /* * i-scream central monitoring system - * http://www.i-scream.org.uk + * http://www.i-scream.org * Copyright (C) 2000-2002 i-scream * * This program is free software; you can redistribute it and/or @@ -31,9 +31,10 @@ #include #include #include +#include -#include "ukcprog.h" -#include "statgrab.h" +#include +#include #define LOG_CRIT 0 #define LOG_ERR 1 @@ -46,6 +47,8 @@ typedef struct{ char *host_ip; char *host_fqdn; + int preset_fqdn; + int preset_ip; char *server_fqdn; int server_udp_port; @@ -77,8 +80,8 @@ ihost_config_t ihost_config; extern int errno; -/* Taken from the openssh code. Its licence included in function.*/ -//#ifndef HAVE_STRLCAT +/* Taken from the OpenSSH code. Its licence included in function.*/ +#ifndef HAVE_STRLCAT /* * Copyright (c) 1998 Todd C. Miller @@ -106,6 +109,7 @@ extern int errno; * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + /* * Appends src to string dst of size siz (unlike strncat, siz is the * full size of dst, not space left). At most siz-1 characters @@ -144,9 +148,9 @@ strlcat(dst, src, siz) return(dlen + (s - src)); /* count does not include NUL */ } -//#endif +#endif +/* End strlcat function taken from OpenSSH */ - void log_msg(int level, char *format, ...){ int cur_errno; va_list ap; @@ -162,27 +166,10 @@ void log_msg(int level, char *format, ...){ }else{ fprintf(ihost_config.log, "\n"); } + fflush(ihost_config.log); } } -/* Takes many pointers, checks if they are NULL or not, and then free's them */ -/* Deprciated - and i only wrote it today! :) -void m_free(int num_pointers, ...){ - int x=0; - va_list ap; - void *p; - - va_start(ap, num_pointers); - for(;xfiltermanager_host, ihost_state->filtermanager_port))==NULL){ - goto error; + return -1; } if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ @@ -380,7 +369,6 @@ int ihost_getconfig(ihost_state_t *ihost_state){ goto error; } - printf("string : %s\n", response_ptr); server_udp_port=atoi(response_ptr); if (server_udp_port==0){ @@ -401,14 +389,25 @@ int ihost_getconfig(ihost_state_t *ihost_state){ * 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->host_fqdn!=NULL) free(ihost_state->host_fqdn); if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); - if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); + if(ihost_state->preset_fqdn){ + if(host_fqdn != NULL) free(host_fqdn); + }else{ + if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); + ihost_state->host_fqdn=host_fqdn; + } + + if(ihost_state->preset_ip){ + if(host_ip != NULL) free(host_ip); + }else{ + if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); + ihost_state->host_ip=host_ip; + } + + ihost_state->file_list=file_list; ihost_state->last_modified=last_modified; - ihost_state->host_fqdn=host_fqdn; - ihost_state->host_ip=host_ip; ihost_state->server_fqdn=server_fqdn; ihost_state->server_udp_port=server_udp_port; ihost_state->udp_update_time=udp_update_time; @@ -433,17 +432,17 @@ error: int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ char tmp[size]; - cpu_percent_t *cpu_percent; - mem_stat_t *mem_stats; - load_stat_t *load_stats; - user_stat_t *user_stats; - swap_stat_t *swap_stats; - general_stat_t *general_stats; - disk_stat_t *disk_stats; - diskio_stat_t *diskio_stats; - process_stat_t *process_stats; - network_stat_t *network_stats; - page_stat_t *page_stats; + sg_cpu_percents *cpu_percent; + sg_mem_stats *mem_stats; + sg_load_stats *load_stats; + sg_user_stats *user_stats; + sg_swap_stats *swap_stats; + sg_host_info *general_stats; + sg_fs_stats *disk_stats; + sg_disk_io_stats *diskio_stats; + sg_process_count *process_stats; + sg_network_io_stats *network_stats; + sg_page_stats *page_stats; int disk_entries=0; int diskio_entries=0; int network_entries=0; @@ -457,7 +456,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); /* Get cpu stats, check it is correct, then fill in its entry for the xml */ - if((cpu_percent=cpu_percent_usage())==NULL){ + if((cpu_percent=sg_get_cpu_percents())==NULL){ log_msg(LOG_CRIT, "Failed to get cpu statistics"); }else{ snprintf(tmp, size, \ @@ -473,7 +472,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /*Get mem stats, and fill in xml */ - if((mem_stats=get_memory_stats())==NULL){ + if((mem_stats=sg_get_mem_stats())==NULL){ log_msg(LOG_CRIT, "Failed to get memory statistics"); }else{ snprintf(tmp, size, \ @@ -488,7 +487,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* Get load stats */ - if((load_stats=get_load_stats())==NULL){ + if((load_stats=sg_get_load_stats())==NULL){ log_msg(LOG_CRIT, "Failed to get load statistics"); }else{ snprintf(tmp, size, \ @@ -502,7 +501,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* get user stats */ - if((user_stats=get_user_stats())==NULL){ + if((user_stats=sg_get_user_stats())==NULL){ log_msg(LOG_CRIT, "Failed to get user statistics"); }else{ @@ -516,7 +515,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* swap stats */ - if((swap_stats=get_swap_stats())==NULL){ + if((swap_stats=sg_get_swap_stats())==NULL){ log_msg(LOG_CRIT, "Failed to get swap statistics"); }else{ snprintf(tmp, size, \ @@ -531,7 +530,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* general stats */ - if((general_stats=get_general_stats())==NULL){ + if((general_stats=sg_get_host_info())==NULL){ log_msg(LOG_CRIT, "Failed to get general statistics"); }else{ snprintf(tmp, size, \ @@ -549,7 +548,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* process stats */ - if((process_stats=get_process_stats())==NULL){ + if((process_stats=sg_get_process_count())==NULL){ log_msg(LOG_CRIT, "Failed to get general statistics"); }else{ snprintf(tmp, size, \ @@ -566,7 +565,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* Get paging stats */ - if((page_stats=get_page_stats_diff())==NULL){ + if((page_stats=sg_get_page_stats_diff())==NULL){ log_msg(LOG_CRIT, "Failed to get paging statistics"); }else{ if(page_stats->systime!=0){ @@ -577,7 +576,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ y=page_stats->pages_pageout; } snprintf(tmp, size, \ - "%lld%lld", \ + "%lld%lld", \ x, \ y); @@ -587,7 +586,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* get diskio stats */ - if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){ + if((diskio_stats=sg_get_disk_io_stats_diff(&diskio_entries))==NULL){ log_msg(LOG_CRIT, "Failed to get diskio statistics"); }else{ strlcat(xml, "", size); @@ -620,7 +619,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* get networks stats */ - if((network_stats=get_network_stats_diff(&network_entries))==NULL){ + if((network_stats=sg_get_network_io_stats_diff(&network_entries))==NULL){ log_msg(LOG_CRIT, "Failed to get network statistics"); }else{ strlcat(xml, "", size); @@ -652,7 +651,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ /* get disk stats */ - if((disk_stats=get_disk_stats(&disk_entries))==NULL){ + if((disk_stats=sg_get_fs_stats(&disk_entries))==NULL){ log_msg(LOG_CRIT, "Failed to get disk statistics"); }else{ strlcat(xml, "", size); @@ -694,12 +693,18 @@ too_big_error: void usage(char *progname){ - fprintf(stderr, "Usage %s [options] server port\n", progname); - fprintf(stderr, "Options\n"); - fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); - fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); - fprintf(stderr, " -V Print version number\n"); - fprintf(stderr, " -h Prints this help page\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, " -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, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); exit(1); } @@ -726,14 +731,17 @@ int main(int argc, char **argv){ ihost_config.log=stderr; /* Blank ihost_state to default settings */ - ihost_state.filtermanager_host=NULL; + ihost_state.filtermanager_host=DEF_SERVER_NAME; + ihost_state.filtermanager_port=DEF_SERVER_PORT; ihost_state.host_fqdn=NULL; ihost_state.host_ip=NULL; + ihost_state.preset_fqdn = 0; + ihost_state.preset_ip = 0; ihost_state.server_fqdn=NULL; ihost_state.file_list=NULL; ihost_state.last_modified=NULL; - while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ + while((cmdopt=getopt(argc, argv, "vfVn:i:s:p:h")) != -1){ switch(cmdopt){ case 'v': ihost_config.verbose++; @@ -744,28 +752,42 @@ int main(int argc, char **argv){ ihost_config.daemon=0; break; - case 'h': - usage(argv[0]); - 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"); + usage(argv[0]); + } + break; + case 'i': + /* Hmm.. Someone could set any string to be the IP, and it will let it */ + ihost_state.preset_ip = 1; + ihost_state.host_ip = strdup(optarg); + if(ihost_state.host_ip == NULL){ + fprintf(stderr, "Missing ip\n"); + usage(argv[0]); + } + break; + case 's': + ihost_state.filtermanager_host=strdup(optarg); + break; + + case 'p': + ihost_state.filtermanager_port=atoi(optarg); + break; + + case 'h': default: usage(argv[0]); exit(1); } } - if(argc!=optind+2){ - usage(argv[0]); - exit(1); - } - - ihost_state.filtermanager_host=strdup(argv[optind]); - ihost_state.filtermanager_port=atoi(argv[optind+1]); - if(gethostbyname(ihost_state.filtermanager_host)==NULL){ log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); exit(1); @@ -802,7 +824,13 @@ int main(int argc, char **argv){ } - log_msg(LOG_INFO, "Starting ihost"); + log_msg(LOG_INFO, "Starting ihost..."); + + log_msg(LOG_DEBUG, "Running statgrab_init()"); + if(statgrab_init()){ + log_msg(LOG_CRIT, "statgrab_init failed (%m)"); + exit(1); + } log_msg(LOG_DEBUG,"Writing PID FILE"); @@ -830,7 +858,6 @@ int main(int argc, char **argv){ sleep(10); } - printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); 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"); sleep(10);