ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
(Generate patch)

Comparing projects/cms/source/ihost/ihost.c (file contents):
Revision 1.18 by pajs, Tue May 21 14:07:32 2002 UTC vs.
Revision 1.22 by pajs, Tue May 21 16:04:34 2002 UTC

# Line 29 | Line 29
29   #include "statgrab.h"
30   #include <time.h>
31   #include <sys/socket.h>
32 #include <netinet/in.h>
32   #include <arpa/inet.h>
33   #include <syslog.h>
34  
35 < #define versionNo 0.8
35 > #define VERSION_NO "0.8"
36   #define RECONFIGURE_RETURN_CODE 2
37   #define UDP_MAX_PACKET_SIZE 8192
38 + #define PID_FILE "/var/tmp/.ihost.pid"
39  
40   #define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0)
41  
# Line 72 | Line 72 | void log_errors(const char *message){
72  
73   char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
74          char *reply;
75 +        logmessage(LOG_DEBUG, "Sending %s",sendString);
76          fprintf(f_w, "%s\n", sendString);
77          fflush(f_w);
78          reply=fpgetline(f_r);
79 +        if (reply!=NULL) logmessage(LOG_DEBUG, "Received %s", reply);
80          /* Returns pointer to static buffer */
81          return reply;
82   }      
# Line 137 | Line 139 | int ihost_configure(ihost_state_t *ihost_state){
139                  return -1;
140          }
141          
142 <        if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){
142 >        if((ihost_state->my_ip=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){
143                  logmessage(LOG_ERR, "Failed to get IP (%m)");
144                  return -1;
145          }      
146          
145        logmessage(LOG_DEBUG, "Sending STARTCONIG");
147          reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
148          if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
149                  logmessage(LOG_ERR, "Server error on STARTCONFIG");    
150                  return -1;
151          }
152  
152        logmessage(LOG_DEBUG, "Sending LASTMODIFIED");
153          reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
154          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
155                  logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)");
# Line 160 | Line 160 | int ihost_configure(ihost_state_t *ihost_state){
160                  return -1;
161          }
162  
163        logmessage(LOG_DEBUG, "Sending FILELIST");
163          reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
164          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
165                  logmessage(LOG_ERR, "Server error on FILELIST (%m)");
# Line 171 | Line 170 | int ihost_configure(ihost_state_t *ihost_state){
170                  return -1;
171          }
172  
174        logmessage(LOG_DEBUG, "Sending FQDN");
173          reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
174          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
175                  logmessage(LOG_ERR, "Server error on FQDN (%m)");
# Line 182 | Line 180 | int ihost_configure(ihost_state_t *ihost_state){
180                  return -1;
181          }
182  
185        logmessage(LOG_DEBUG, "Sending FQDN");
183          reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
184          if(reply== NULL){
185                  logmessage(LOG_ERR, "Server error (%m)");
# Line 192 | Line 189 | int ihost_configure(ihost_state_t *ihost_state){
189                  ihost_state->udp_update_time=atoi(reply);
190          }
191          
195        logmessage(LOG_DEBUG, "Sending TCPUpdateTime");
192          reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
193          if(reply== NULL){
194                  logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)");
# Line 202 | Line 198 | int ihost_configure(ihost_state_t *ihost_state){
198                  ihost_state->tcp_update_time=atoi(reply);
199          }
200  
205        logmessage(LOG_DEBUG, "Sending ENDCONFIG");
201          reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
202          if(reply== NULL){
203                  logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)");
204                  return -1;
205          }
206  
212        logmessage(LOG_DEBUG, "Sending FILTER");
207          reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
208          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
209                  logmessage(LOG_ERR, "Server error FILTER failed (%m)");
# Line 240 | Line 234 | int ihost_configure(ihost_state_t *ihost_state){
234                  return -1;
235          }
236  
243        logmessage(LOG_DEBUG, "Sending END");
237          reply=sock_comm(fm_fd_r, fm_fd_w, "END");
238          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
239                  logmessage(LOG_ERR, "Server error on END (%m)");
# Line 299 | Line 292 | int heartbeat(ihost_state_t *ihost_state){
292                  return -1;
293          }
294  
302        logmessage(LOG_DEBUG, "Sending HEARTBEAT");
295          reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
296          if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
297                  logmessage(LOG_ERR, "Server error on HEARTBEAT");
298                  return -1;
299          }
300  
309        logmessage(LOG_DEBUG, "Sending CONFIG");
301          reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
302          if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
303                  logmessage(LOG_ERR, "Server error on CONFIG");
304                  return -1;
305          }
306  
316        logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list);
307          reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
308          if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
309                  logmessage(LOG_ERR, "Server error on fileslist");
310                  return -1;
311          }
312  
323        logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified);
313          reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
314          if (reply==NULL) {
315                  logmessage(LOG_ERR, "Server error NULL recieved on lastmodified");
# Line 332 | Line 321 | int heartbeat(ihost_state_t *ihost_state){
321                  exitcode=RECONFIGURE_RETURN_CODE;
322          }
323  
335        logmessage(LOG_DEBUG,"Sending KEY");
324          reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
325          if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
326                  logmessage(LOG_ERR, "Server error on KEY");
# Line 345 | Line 333 | int heartbeat(ihost_state_t *ihost_state){
333                  return -1;
334          }
335  
348        logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT");
336          reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
337          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
338                  logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)");
# Line 372 | Line 359 | char *stat_grab(ihost_state_t *ihost_state, int counte
359          char *stats[NUM_STATS];
360          char *xml_data=NULL;
361          char *xml_data_p;
362 +        int xml_size=0;
363          int x=0;
364  
365          logmessage(LOG_DEBUG,"get_cpu_stats");  
# Line 392 | Line 380 | char *stat_grab(ihost_state_t *ihost_state, int counte
380          stats[7]=get_swap_stats();
381          logmessage(LOG_DEBUG,"get_user_stats");
382          stats[8]=get_user_stats();
383 +        
384  
385 <        for(;x<NUM_STATS;x++){
385 >        for(x=0;x<NUM_STATS;x++){
386                  if(stats[x]==NULL){
387                          logmessage(LOG_ERR,"Function returned NULL");
388                          return NULL;
389                  }
390 <                if(xml_data==NULL){
391 <                        if((xml_data=strf("%s", stats[x])) == NULL){
392 <                                logmessage(LOG_ERR, "str failed (%m)");
393 <                                return NULL;
394 <                        }
395 <                }else{
396 <                        xml_data_p=xml_data;
397 <                        if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){
409 <                                logmessage(LOG_ERR, "str failed (%m)");
410 <                                return NULL;
411 <                        }
412 <                        free(xml_data_p);
413 <                }
390 >                xml_size+=strlen(stats[x]);
391 >        }
392 >
393 >        xml_data=malloc(xml_size+1);
394 >        xml_data=strcpy(xml_data, stats[0]);
395 >        free(stats[0]);
396 >        for(x=1;x<NUM_STATS;x++){
397 >                strcat(xml_data, stats[x]);
398                  free(stats[x]);
399          }
400 +
401          xml_data_p=xml_data;
402          xml_data=strf("<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\" key=\"%s\">%s</packet>", counter, ihost_state->my_fqdn, time(NULL), ihost_state->my_ip, ihost_state->key, xml_data);
403          free(xml_data_p);
# Line 438 | Line 423 | int send_stats(ihost_state_t *ihost_state, char *data_
423                  logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn);
424                  return -1;
425          }
426 <        logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port);
426 >        logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port);
427          if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
428                  logmessage(LOG_ERR, "failed to create UDP socket (%m)");
429                  return -1;
# Line 485 | Line 470 | int main(int argc, char **argv){
470          extern int syslog_logging;
471          extern int log_level;
472          extern int cur_level;
473 +        FILE *f;
474  
475          log_level=1;
476          cur_level=1;
# Line 523 | Line 509 | int main(int argc, char **argv){
509                                  break;
510  
511                          case 'V':
512 <                                errf("%s version %f",argv[0], versionNo);
512 >                                errf("%s version %s",argv[0], VERSION_NO);
513                                  break;
514  
515                          default:
# Line 587 | Line 573 | int main(int argc, char **argv){
573                          break;
574          }
575  
576 +        logmessage(LOG_DEBUG,"Writing PID FILE");
577 +        pid=getpid();
578 +        if((f=fopen(PID_FILE,"w")) == NULL){
579 +                logmessage(LOG_WARNING, "Failed to write PID file");
580 +        }else{
581 +                if((fprintf(f,"%d",(int)pid)) > 0 ){
582 +                        logmessage(LOG_WARNING, "Failed to write PID file");
583 +                }
584 +                if((fclose(f))!=0){
585 +                        logmessage(LOG_ERR, "failed to close PID file");
586 +                        exit(1);
587 +                }
588 +        }
589 +
590          if(ihost_configure(&ihost_state)!=0){
591                  logmessage(LOG_ERR,"configure failed");
592                  /* Ok, ideally we prob should have 2 copies of the structure and carry on if this
# Line 614 | Line 614 | int main(int argc, char **argv){
614  
615                  if(cur_time>=udp_time){
616                          logmessage(LOG_DEBUG,"Sending udp data");
617 <
617 >                        /* Work out how long it takes to get the stats for next time round
618 >                           so the sleep time can be adjusted accordingly */
619                          stat_grab_time=time(NULL);
620                          if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){
621                                  logmessage(LOG_ERR,"Failed to get stats (%m)");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines