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.23 by tdb, Tue May 21 16:47:12 2002 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines