--- projects/cms/source/ihost/ihost.c 2002/05/21 14:36:23 1.20 +++ projects/cms/source/ihost/ihost.c 2002/05/29 23:03:53 1.27 @@ -1,5 +1,6 @@ /* * 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 @@ -17,6 +18,10 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include @@ -29,14 +34,10 @@ #include "statgrab.h" #include #include -#include #include #include -#define versionNo 0.8 #define RECONFIGURE_RETURN_CODE 2 -#define UDP_MAX_PACKET_SIZE 8192 -#define PID_FILE "/var/tmp/.ihost.pid" #define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0) @@ -73,9 +74,11 @@ void log_errors(const char *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; } @@ -137,20 +140,19 @@ int ihost_configure(ihost_state_t *ihost_state){ logmessage(LOG_ERR, "sa family is wrong type"); return -1; } - - if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ + + 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; } - logmessage(LOG_DEBUG, "Sending STARTCONIG"); reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { logmessage(LOG_ERR, "Server error on STARTCONFIG"); return -1; } - logmessage(LOG_DEBUG, "Sending LASTMODIFIED"); reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); @@ -161,7 +163,6 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - logmessage(LOG_DEBUG, "Sending FILELIST"); reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ logmessage(LOG_ERR, "Server error on FILELIST (%m)"); @@ -172,7 +173,6 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - logmessage(LOG_DEBUG, "Sending FQDN"); reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ logmessage(LOG_ERR, "Server error on FQDN (%m)"); @@ -183,7 +183,6 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - logmessage(LOG_DEBUG, "Sending FQDN"); reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); if(reply== NULL){ logmessage(LOG_ERR, "Server error (%m)"); @@ -193,7 +192,6 @@ int ihost_configure(ihost_state_t *ihost_state){ ihost_state->udp_update_time=atoi(reply); } - logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); if(reply== NULL){ logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); @@ -203,14 +201,12 @@ int ihost_configure(ihost_state_t *ihost_state){ ihost_state->tcp_update_time=atoi(reply); } - logmessage(LOG_DEBUG, "Sending ENDCONFIG"); reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); if(reply== NULL){ logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); return -1; } - logmessage(LOG_DEBUG, "Sending FILTER"); reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ logmessage(LOG_ERR, "Server error FILTER failed (%m)"); @@ -241,7 +237,6 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - logmessage(LOG_DEBUG, "Sending END"); reply=sock_comm(fm_fd_r, fm_fd_w, "END"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ logmessage(LOG_ERR, "Server error on END (%m)"); @@ -300,28 +295,24 @@ int heartbeat(ihost_state_t *ihost_state){ return -1; } - logmessage(LOG_DEBUG, "Sending HEARTBEAT"); reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { logmessage(LOG_ERR, "Server error on HEARTBEAT"); return -1; } - logmessage(LOG_DEBUG, "Sending CONFIG"); reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { logmessage(LOG_ERR, "Server error on CONFIG"); return -1; } - logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { logmessage(LOG_ERR, "Server error on fileslist"); return -1; } - logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); if (reply==NULL) { logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); @@ -333,7 +324,6 @@ int heartbeat(ihost_state_t *ihost_state){ exitcode=RECONFIGURE_RETURN_CODE; } - logmessage(LOG_DEBUG,"Sending KEY"); reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { logmessage(LOG_ERR, "Server error on KEY"); @@ -346,7 +336,6 @@ int heartbeat(ihost_state_t *ihost_state){ return -1; } - logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); @@ -404,11 +393,12 @@ char *stat_grab(ihost_state_t *ihost_state, int counte xml_size+=strlen(stats[x]); } - xml_data=malloc(xml_size); + xml_data=malloc(xml_size+1); xml_data=strcpy(xml_data, stats[0]); - + free(stats[0]); for(x=1;xserver_fqdn); return -1; } - logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); + 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; @@ -497,6 +487,7 @@ 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]); @@ -522,7 +513,7 @@ int main(int argc, char **argv){ break; case 'V': - errf("%s version %f",argv[0], versionNo); + errf("%s version %s",argv[0], VERSION); break; default: @@ -589,10 +580,10 @@ int main(int argc, char **argv){ logmessage(LOG_DEBUG,"Writing PID FILE"); pid=getpid(); if((f=fopen(PID_FILE,"w")) == NULL){ - logmessage(LOG_WARNING, "Failed to write PID file"); + logmessage(LOG_WARNING, "Failed to write PID file (%m)"); }else{ - if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){ - logmessage(LOG_WARNING, "Failed to write PID file"); + 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"); @@ -627,7 +618,8 @@ int main(int argc, char **argv){ 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)");