--- projects/cms/source/ihost/ihost.c 2002/05/21 14:07:32 1.18 +++ projects/cms/source/ihost/ihost.c 2002/05/21 16:47:12 1.23 @@ -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 @@ -29,13 +30,13 @@ #include "statgrab.h" #include #include -#include #include #include -#define versionNo 0.8 +#define VERSION_NO "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) @@ -72,9 +73,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,19 +140,17 @@ int ihost_configure(ihost_state_t *ihost_state){ return -1; } - if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ + 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)"); @@ -160,7 +161,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)"); @@ -171,7 +171,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)"); @@ -182,7 +181,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)"); @@ -192,7 +190,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)"); @@ -202,14 +199,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)"); @@ -240,7 +235,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)"); @@ -299,28 +293,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"); @@ -332,7 +322,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"); @@ -345,7 +334,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)"); @@ -372,6 +360,7 @@ char *stat_grab(ihost_state_t *ihost_state, int counte 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"); @@ -392,27 +381,24 @@ char *stat_grab(ihost_state_t *ihost_state, int counte stats[7]=get_swap_stats(); logmessage(LOG_DEBUG,"get_user_stats"); stats[8]=get_user_stats(); + - for(;x%s", counter, ihost_state->my_fqdn, time(NULL), ihost_state->my_ip, ihost_state->key, xml_data); free(xml_data_p); @@ -438,7 +424,7 @@ int send_stats(ihost_state_t *ihost_state, char *data_ logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_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; @@ -485,6 +471,7 @@ int main(int argc, char **argv){ extern int syslog_logging; extern int log_level; extern int cur_level; + FILE *f; log_level=1; cur_level=1; @@ -523,7 +510,7 @@ int main(int argc, char **argv){ break; case 'V': - errf("%s version %f",argv[0], versionNo); + errf("%s version %s",argv[0], VERSION_NO); break; default: @@ -587,6 +574,20 @@ int main(int argc, char **argv){ break; } + logmessage(LOG_DEBUG,"Writing PID FILE"); + pid=getpid(); + if((f=fopen(PID_FILE,"w")) == NULL){ + logmessage(LOG_WARNING, "Failed to write PID file"); + }else{ + if((fprintf(f,"%d",(int)pid)) > 0 ){ + logmessage(LOG_WARNING, "Failed to write PID file"); + } + if((fclose(f))!=0){ + logmessage(LOG_ERR, "failed to close PID file"); + exit(1); + } + } + if(ihost_configure(&ihost_state)!=0){ logmessage(LOG_ERR,"configure failed"); /* Ok, ideally we prob should have 2 copies of the structure and carry on if this @@ -614,7 +615,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)");