--- projects/cms/source/ihost/ihost.c 2002/05/13 11:01:27 1.8 +++ projects/cms/source/ihost/ihost.c 2002/05/29 23:03:53 1.27 @@ -1,19 +1,51 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include -#include #include +#include #include #include #include -#include +#include "ukcprog.h" #include -#include +#include +#include "statgrab.h" +#include +#include +#include +#include #define RECONFIGURE_RETURN_CODE 2 +#define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0) + typedef struct{ int fm_port; char *fm_host; - + + char *my_ip; char *my_fqdn; char *server_fqdn; int server_udp_port; @@ -26,11 +58,27 @@ typedef struct{ }ihost_state_t; +static int log_level; +static int cur_level; +static int syslog_logging; + +void log_errors(const char *message){ + if(log_level>=cur_level){ + if (syslog_logging==1){ + syslog(cur_level, "%s\n", message); + }else{ + fprintf(stderr, "%s\n", message); + } + } +} + char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ char *reply; + logmessage(LOG_DEBUG, "Sending %s",sendString); fprintf(f_w, "%s\n", sendString); fflush(f_w); reply=fpgetline(f_r); + if (reply!=NULL) logmessage(LOG_DEBUG, "Received %s", reply); /* Returns pointer to static buffer */ return reply; } @@ -38,6 +86,8 @@ char* sock_comm(FILE *f_r, FILE *f_w, char *sendString int ihost_configure(ihost_state_t *ihost_state){ struct sockaddr_in addr; struct in_addr haddr; + struct sockaddr ip; + int ip_len; int sd; FILE *fm_fd_r, *fm_fd_w; char *reply; @@ -49,85 +99,102 @@ int ihost_configure(ihost_state_t *ihost_state){ if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); if (ihost_state->files_list!=NULL) free(ihost_state->files_list); + logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->fm_host, ihost_state->fm_port); if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { - errf("Can't create AF_INET socket (%m)"); + logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); return -1; } if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ - errf("Failed to resolve address %s (%m)", ihost_state->fm_host); + logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host); return -1; } - memset((char *)&addr, 0, sizeof addr); + memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; - memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); + memcpy(&addr.sin_addr, &haddr, sizeof haddr); addr.sin_port = htons(ihost_state->fm_port); if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { - errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); + logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); return -1; } /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ if ((fm_fd_r=fdopen(sd,"r")) == NULL){ - errf("Failed to open read stream (%m)"); + logmessage(LOG_ERR, "Failed to open read stream (%m)"); return -1; } if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ - errf("Failed to open write stream (%m)"); + logmessage(LOG_ERR, "Failed to open write stream (%m)"); return -1; } + ip_len=sizeof ip; + memset(&ip, 0, ip_len); + if((getsockname(sd, &ip, &ip_len)) != 0){ + logmessage(LOG_ERR, "Failed to get IP address (%m)"); + return -1; + } + if (ip.sa_family!=AF_INET){ + logmessage(LOG_ERR, "sa family is wrong type"); + return -1; + } + if(ihost_state->my_ip!=NULL) free(ihost_state->my_ip); + if((ihost_state->my_ip=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){ + logmessage(LOG_ERR, "Failed to get IP (%m)"); + return -1; + } + reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { - errf("Server error"); + logmessage(LOG_ERR, "Server error on STARTCONFIG"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); return -1; } if((ihost_state->last_modified=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); + logmessage(LOG_ERR, "strdup failed (%m)"); return -1; } - + reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error on FILELIST (%m)"); return -1; } if((ihost_state->files_list=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); + logmessage(LOG_ERR, "strdup failed (%m)"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error on FQDN (%m)"); return -1; } if((ihost_state->my_fqdn=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); + logmessage(LOG_ERR, "strdup failed (%m)"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); if(reply== NULL){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error (%m)"); return -1; } if (strncasecmp(reply, "ERROR", 5) != 0){ ihost_state->udp_update_time=atoi(reply); } - + reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); if(reply== NULL){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); return -1; } if (strncasecmp(reply, "ERROR", 5) != 0){ @@ -136,29 +203,29 @@ int ihost_configure(ihost_state_t *ihost_state){ reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); if(reply== NULL){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error FILTER failed (%m)"); return -1; } reply_ptr=strchr(reply,';'); if (reply_ptr==NULL){ - errf("Incorrect data returned"); + logmessage(LOG_ERR, "Incorrect data returned"); return -1; } *reply_ptr='\0'; if((ihost_state->server_fqdn=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); + logmessage(LOG_ERR, "strdup failed (%m)"); return -1; } reply=reply_ptr + 1; reply_ptr=strchr(reply,';'); if (reply_ptr==NULL){ - errf("Incorrect data returned 2"); + logmessage(LOG_ERR, "Incorrect data returned 2"); return -1; } *reply_ptr='\0'; @@ -166,22 +233,22 @@ int ihost_configure(ihost_state_t *ihost_state){ reply=reply_ptr+1; ihost_state->server_tcp_port=atoi(reply); if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ - errf("Incorrect data returned 3 "); + logmessage(LOG_ERR, "Incorrect data returned 3 "); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, "END"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error on END (%m)"); return -1; } if(fclose(fm_fd_r) !=0){ - errf("Failed to close read FD (%m)"); + logmessage(LOG_ERR, "Failed to close read FD (%m)"); return -1; } if(fclose(fm_fd_w) !=0){ - errf("Failed to close write FD (%m)"); + logmessage(LOG_ERR, "Failed to close write FD (%m)"); return -1; } @@ -196,101 +263,223 @@ int heartbeat(ihost_state_t *ihost_state){ char *reply; int exitcode=0; + logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { - errf("Can't create AF_INET socket (%m)"); + logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); return -1; } if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ - errf("Failed to resolve address %s (%m)", ihost_state->fm_host); + logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); return -1; } - memset((char *)&addr, 0, sizeof addr); + memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; - memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); + memcpy(&addr.sin_addr, &haddr, sizeof haddr); addr.sin_port = htons(ihost_state->server_tcp_port); if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { - errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); + logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); return -1; } /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ if ((fm_fd_r=fdopen(sd,"r")) == NULL){ - errf("Failed to open stream (%m)"); + logmessage(LOG_ERR, "Failed to open stream (%m)"); return -1; } if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ - errf("Failed to open stream (%m)"); + logmessage(LOG_ERR, "Failed to open stream (%m)"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { - errf("Server error"); + logmessage(LOG_ERR, "Server error on HEARTBEAT"); return -1; } - if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); + reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { - errf("Server error"); + logmessage(LOG_ERR, "Server error on CONFIG"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { - errf("Server error"); + logmessage(LOG_ERR, "Server error on fileslist"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); if (reply==NULL) { - errf("Server error"); + logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); return -1; } if (strncasecmp(reply, "ERROR", 5) == 0){ /* Means the config has changed */ + logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); exitcode=RECONFIGURE_RETURN_CODE; } + reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { - errf("Server error"); + logmessage(LOG_ERR, "Server error on KEY"); return -1; } if (ihost_state->key!=NULL) free(ihost_state->key); if((ihost_state->key=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); + logmessage(LOG_ERR, "strdup failed (%m)"); return -1; } reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ - errf("Server error (%m)"); + logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); return -1; } + fflush(fm_fd_r); + fflush(fm_fd_w); + if(fclose(fm_fd_r) !=0){ - errf("Failed to close read FD (%m)"); + logmessage(LOG_ERR, "Failed to close read FD (%m)"); return -1; } if(fclose(fm_fd_w) !=0){ - errf("Failed to close write FD (%m)"); + logmessage(LOG_ERR, "Failed to close write FD (%m)"); return -1; } return exitcode; } +char *stat_grab(ihost_state_t *ihost_state, int counter){ +#define NUM_STATS 9 + char *stats[NUM_STATS]; + char *xml_data=NULL; + char *xml_data_p; + int xml_size=0; + int x=0; + logmessage(LOG_DEBUG,"get_cpu_stats"); + stats[0]=get_cpu_stats(); + logmessage(LOG_DEBUG,"get_disk_stats"); + stats[1]=get_disk_stats(); + logmessage(LOG_DEBUG,"get_load_stats"); + stats[2]=get_load_stats(); + logmessage(LOG_DEBUG,"get_memory_stats"); + stats[3]=get_memory_stats(); + logmessage(LOG_DEBUG,"get_os_info"); + stats[4]=get_os_info(); + logmessage(LOG_DEBUG,"get_page_stats"); + stats[5]=get_page_stats(); + logmessage(LOG_DEBUG,"get_process_stats"); + stats[6]=get_process_stats(); + logmessage(LOG_DEBUG,"get_swap_stats"); + stats[7]=get_swap_stats(); + logmessage(LOG_DEBUG,"get_user_stats"); + stats[8]=get_user_stats(); + + + for(x=0;x%s", counter, ihost_state->my_fqdn, time(NULL), ihost_state->my_ip, ihost_state->key, xml_data); + free(xml_data_p); + + logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); + return xml_data; +} + +int send_stats(ihost_state_t *ihost_state, char *data_stream){ + struct sockaddr_in addr; + struct in_addr haddr; + + int sd; + size_t len; + + len=strlen(data_stream); + if(len>UDP_MAX_PACKET_SIZE){ + logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); + exit(1); + } + logmessage(LOG_DEBUG,"Resolving IP of server"); + if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ + logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); + return -1; + } + logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); + if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ + logmessage(LOG_ERR, "failed to create UDP socket (%m)"); + return -1; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family=AF_INET; + memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); + addr.sin_port = htons(ihost_state->server_udp_port); + + logmessage(LOG_INFO,"Sending packet : %s", data_stream); + if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ + logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); + return -1; + } + + close(sd); + + return 0; +} + +void usage(char *progname){ + fprintf(stderr, "Usage %s [options] server port\n", progname); + fprintf(stderr, "Options\n"); + fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); + fprintf(stderr, " -d Daemon mode, self backgrounding\n"); + fprintf(stderr, " -s Send errors to syslog\n"); + fprintf(stderr, " -V Print version number\n"); + fprintf(stderr, " -h Prints this help page\n"); + exit(1); +} + int main(int argc, char **argv){ ihost_state_t ihost_state; int heartbeat_exit; int counter=0; + long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; + int sleep_delay=0; + char *xml_stats; + pid_t pid; + int cmdopt; + extern int optind; + int verbose=0, daemon=0; + extern int syslog_logging; + extern int log_level; + extern int cur_level; + FILE *f; + log_level=1; + cur_level=1; + syslog_logging=0; + errf_set_ofunc(log_errors); /* NULL'ify so i can tell if i need to free it or not */ ihost_state.fm_host=NULL; ihost_state.my_fqdn=NULL; @@ -298,37 +487,158 @@ int main(int argc, char **argv){ ihost_state.last_modified=NULL; ihost_state.files_list=NULL; ihost_state.key=NULL; + ihost_state.my_ip=NULL; errf_set_progname(argv[0]); - if(argc!=3){ - errf_usage(" "); + + + while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ + switch(cmdopt){ + case 'v': + verbose++; + break; + + case 'd': + /* Force syslog logging since stderr will be closed in this case */ + syslog_logging=1; + daemon=1; + break; + + case 's': + syslog_logging=1; + break; + + case 'h': + usage(argv[0]); + break; + + case 'V': + errf("%s version %s",argv[0], VERSION); + break; + + default: + usage(argv[0]); + exit(1); + } + } + + if(argc!=optind+2){ + usage(argv[0]); exit(1); } + ihost_state.fm_host=argv[optind]; + ihost_state.fm_port=atoi(argv[optind+1]); + if(ihost_state.fm_port==0){ + errf("Invalid port number"); + usage(argv[0]); + } - ihost_state.fm_host=argv[1]; - ihost_state.fm_port=atoi(argv[2]); + if(daemon==1){ + pid=fork(); + if(pid==-1){ + errf("Fork failed, can't background. Exiting"); + exit(1); + }else if(pid!=0){ + /* Parent process */ + return 0; + } + /* We should now be in the background*/ + if(setsid()==-1){ + errf("setsid failed (%m)"); + exit(1); + } + fclose(stdin); + fclose(stdout); + fclose(stderr); + } + if(syslog_logging==1){ + openlog(errf_get_progname(),0,LOG_ERR); + setlogmask(LOG_UPTO(LOG_DEBUG)); + } + + switch(verbose){ + case 0: + /* Critical errors + */ + log_level=LOG_ERR; + break; + case 1: + /* Recoverable errors */ + log_level=LOG_WARNING; + break; + case 2: + /* Print stuff like the XML packets */ + log_level=LOG_INFO; + break; + default: + /* Must have lots of v's */ + /* Print out everything its doing */ + log_level=LOG_DEBUG; + break; + } + + logmessage(LOG_DEBUG,"Writing PID FILE"); + pid=getpid(); + if((f=fopen(PID_FILE,"w")) == NULL){ + logmessage(LOG_WARNING, "Failed to write PID file (%m)"); + }else{ + if((fprintf(f,"%d",(int)pid)) <= 0 ){ + logmessage(LOG_WARNING, "Failed to write PID file (%m)"); + } + if((fclose(f))!=0){ + logmessage(LOG_ERR, "failed to close PID file"); + exit(1); + } + } + if(ihost_configure(&ihost_state)!=0){ - errf("configure failed"); + logmessage(LOG_ERR,"configure failed"); /* Ok, ideally we prob should have 2 copies of the structure and carry on if this happens.. But we dont :) (at the moment) */ exit(1); } - while(TRUE){ + for(;;){ + cur_time=time(NULL); + if(cur_time>=tcp_time){ + logmessage(LOG_DEBUG,"Sending heartbeat"); + heartbeat_exit=heartbeat(&ihost_state); + if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ + logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); + ihost_configure(&ihost_state); + udp_time=0; + } + if(heartbeat_exit==-1){ + logmessage(LOG_ERR,"Heartbeat failed"); + exit(1); + } + tcp_time=time(NULL)+ihost_state.tcp_update_time; + logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); + } - heartbeat_exit=heartbeat(&ihost_state); - if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ - errf("heartbeat needs to be reconfigured"); - ihost_configure(&ihost_state); + if(cur_time>=udp_time){ + logmessage(LOG_DEBUG,"Sending udp data"); + /* Work out how long it takes to get the stats for next time round + so the sleep time can be adjusted accordingly */ + stat_grab_time=time(NULL); + if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ + logmessage(LOG_ERR,"Failed to get stats (%m)"); + exit(1); + } + stat_grab_time=time(NULL)-stat_grab_time; + send_stats(&ihost_state, xml_stats); + free(xml_stats); + udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; + logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); } - if(heartbeat_exit==-1){ - errf("ah crap"); - exit(1); - } - printf("Count : %d\n",counter++); - printf("waiting %d\n",ihost_state.tcp_update_time); - sleep(ihost_state.tcp_update_time); + + if(tcp_time0) sleep(sleep_delay); } return 0; }