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 |
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 "1.0rc1" |
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 |
|
|
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 |
|
} |
139 |
|
logmessage(LOG_ERR, "sa family is wrong type"); |
140 |
|
return -1; |
141 |
|
} |
142 |
< |
|
143 |
< |
if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
142 |
> |
|
143 |
> |
if(ihost_state->my_ip!=NULL) free(ihost_state->my_ip); |
144 |
> |
if((ihost_state->my_ip=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){ |
145 |
|
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
146 |
|
return -1; |
147 |
|
} |
148 |
|
|
145 |
– |
logmessage(LOG_DEBUG, "Sending STARTCONIG"); |
149 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
150 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
151 |
|
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
152 |
|
return -1; |
153 |
|
} |
154 |
|
|
152 |
– |
logmessage(LOG_DEBUG, "Sending LASTMODIFIED"); |
155 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
156 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
157 |
|
logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
162 |
|
return -1; |
163 |
|
} |
164 |
|
|
163 |
– |
logmessage(LOG_DEBUG, "Sending FILELIST"); |
165 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
166 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
167 |
|
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
172 |
|
return -1; |
173 |
|
} |
174 |
|
|
174 |
– |
logmessage(LOG_DEBUG, "Sending FQDN"); |
175 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
176 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
177 |
|
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
182 |
|
return -1; |
183 |
|
} |
184 |
|
|
185 |
– |
logmessage(LOG_DEBUG, "Sending FQDN"); |
185 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
186 |
|
if(reply== NULL){ |
187 |
|
logmessage(LOG_ERR, "Server error (%m)"); |
191 |
|
ihost_state->udp_update_time=atoi(reply); |
192 |
|
} |
193 |
|
|
195 |
– |
logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); |
194 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
195 |
|
if(reply== NULL){ |
196 |
|
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
200 |
|
ihost_state->tcp_update_time=atoi(reply); |
201 |
|
} |
202 |
|
|
205 |
– |
logmessage(LOG_DEBUG, "Sending ENDCONFIG"); |
203 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
204 |
|
if(reply== NULL){ |
205 |
|
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
206 |
|
return -1; |
207 |
|
} |
208 |
|
|
212 |
– |
logmessage(LOG_DEBUG, "Sending FILTER"); |
209 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
210 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
211 |
|
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
236 |
|
return -1; |
237 |
|
} |
238 |
|
|
243 |
– |
logmessage(LOG_DEBUG, "Sending END"); |
239 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
240 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
241 |
|
logmessage(LOG_ERR, "Server error on END (%m)"); |
294 |
|
return -1; |
295 |
|
} |
296 |
|
|
302 |
– |
logmessage(LOG_DEBUG, "Sending HEARTBEAT"); |
297 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
298 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
299 |
|
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
300 |
|
return -1; |
301 |
|
} |
302 |
|
|
309 |
– |
logmessage(LOG_DEBUG, "Sending CONFIG"); |
303 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
304 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
305 |
|
logmessage(LOG_ERR, "Server error on CONFIG"); |
306 |
|
return -1; |
307 |
|
} |
308 |
|
|
316 |
– |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); |
309 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
310 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
311 |
|
logmessage(LOG_ERR, "Server error on fileslist"); |
312 |
|
return -1; |
313 |
|
} |
314 |
|
|
323 |
– |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); |
315 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
316 |
|
if (reply==NULL) { |
317 |
|
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
323 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
324 |
|
} |
325 |
|
|
335 |
– |
logmessage(LOG_DEBUG,"Sending KEY"); |
326 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
327 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
328 |
|
logmessage(LOG_ERR, "Server error on KEY"); |
335 |
|
return -1; |
336 |
|
} |
337 |
|
|
348 |
– |
logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); |
338 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
339 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
340 |
|
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
361 |
|
char *stats[NUM_STATS]; |
362 |
|
char *xml_data=NULL; |
363 |
|
char *xml_data_p; |
364 |
+ |
int xml_size=0; |
365 |
|
int x=0; |
366 |
|
|
367 |
|
logmessage(LOG_DEBUG,"get_cpu_stats"); |
382 |
|
stats[7]=get_swap_stats(); |
383 |
|
logmessage(LOG_DEBUG,"get_user_stats"); |
384 |
|
stats[8]=get_user_stats(); |
385 |
+ |
|
386 |
|
|
387 |
< |
for(;x<NUM_STATS;x++){ |
387 |
> |
for(x=0;x<NUM_STATS;x++){ |
388 |
|
if(stats[x]==NULL){ |
389 |
|
logmessage(LOG_ERR,"Function returned NULL"); |
390 |
|
return NULL; |
391 |
|
} |
392 |
< |
if(xml_data==NULL){ |
393 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
394 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
395 |
< |
return NULL; |
396 |
< |
} |
397 |
< |
}else{ |
398 |
< |
xml_data_p=xml_data; |
399 |
< |
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 |
< |
} |
392 |
> |
xml_size+=strlen(stats[x]); |
393 |
> |
} |
394 |
> |
|
395 |
> |
xml_data=malloc(xml_size+1); |
396 |
> |
xml_data=strcpy(xml_data, stats[0]); |
397 |
> |
free(stats[0]); |
398 |
> |
for(x=1;x<NUM_STATS;x++){ |
399 |
> |
strcat(xml_data, stats[x]); |
400 |
|
free(stats[x]); |
401 |
|
} |
402 |
+ |
|
403 |
|
xml_data_p=xml_data; |
404 |
|
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); |
405 |
|
free(xml_data_p); |
425 |
|
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
426 |
|
return -1; |
427 |
|
} |
428 |
< |
logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
428 |
> |
logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
429 |
|
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
430 |
|
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
431 |
|
return -1; |
472 |
|
extern int syslog_logging; |
473 |
|
extern int log_level; |
474 |
|
extern int cur_level; |
475 |
+ |
FILE *f; |
476 |
|
|
477 |
|
log_level=1; |
478 |
|
cur_level=1; |
486 |
|
ihost_state.last_modified=NULL; |
487 |
|
ihost_state.files_list=NULL; |
488 |
|
ihost_state.key=NULL; |
489 |
+ |
ihost_state.my_ip=NULL; |
490 |
|
|
491 |
|
errf_set_progname(argv[0]); |
492 |
|
|
512 |
|
break; |
513 |
|
|
514 |
|
case 'V': |
515 |
< |
errf("%s version %f",argv[0], versionNo); |
515 |
> |
errf("%s version %s",argv[0], VERSION_NO); |
516 |
|
break; |
517 |
|
|
518 |
|
default: |
576 |
|
break; |
577 |
|
} |
578 |
|
|
579 |
+ |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
580 |
+ |
pid=getpid(); |
581 |
+ |
if((f=fopen(PID_FILE,"w")) == NULL){ |
582 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
583 |
+ |
}else{ |
584 |
+ |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
585 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
586 |
+ |
} |
587 |
+ |
if((fclose(f))!=0){ |
588 |
+ |
logmessage(LOG_ERR, "failed to close PID file"); |
589 |
+ |
exit(1); |
590 |
+ |
} |
591 |
+ |
} |
592 |
+ |
|
593 |
|
if(ihost_configure(&ihost_state)!=0){ |
594 |
|
logmessage(LOG_ERR,"configure failed"); |
595 |
|
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
617 |
|
|
618 |
|
if(cur_time>=udp_time){ |
619 |
|
logmessage(LOG_DEBUG,"Sending udp data"); |
620 |
< |
|
620 |
> |
/* Work out how long it takes to get the stats for next time round |
621 |
> |
so the sleep time can be adjusted accordingly */ |
622 |
|
stat_grab_time=time(NULL); |
623 |
|
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
624 |
|
logmessage(LOG_ERR,"Failed to get stats (%m)"); |