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 |
18 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
*/ |
20 |
|
|
21 |
+ |
#ifdef HAVE_CONFIG_H |
22 |
+ |
#include "config.h" |
23 |
+ |
#endif |
24 |
+ |
|
25 |
|
#include <stdio.h> |
26 |
|
#include <stdlib.h> |
27 |
|
#include <sys/types.h> |
34 |
|
#include "statgrab.h" |
35 |
|
#include <time.h> |
36 |
|
#include <sys/socket.h> |
32 |
– |
#include <netinet/in.h> |
37 |
|
#include <arpa/inet.h> |
38 |
|
#include <syslog.h> |
39 |
|
|
36 |
– |
#define versionNo 0.8 |
40 |
|
#define RECONFIGURE_RETURN_CODE 2 |
38 |
– |
#define UDP_MAX_PACKET_SIZE 8192 |
41 |
|
|
42 |
|
#define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0) |
43 |
|
|
74 |
|
|
75 |
|
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
76 |
|
char *reply; |
77 |
+ |
logmessage(LOG_DEBUG, "Sending %s",sendString); |
78 |
|
fprintf(f_w, "%s\n", sendString); |
79 |
|
fflush(f_w); |
80 |
|
reply=fpgetline(f_r); |
81 |
+ |
if (reply!=NULL) logmessage(LOG_DEBUG, "Received %s", reply); |
82 |
|
/* Returns pointer to static buffer */ |
83 |
|
return reply; |
84 |
|
} |
140 |
|
logmessage(LOG_ERR, "sa family is wrong type"); |
141 |
|
return -1; |
142 |
|
} |
143 |
< |
|
144 |
< |
if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
143 |
> |
|
144 |
> |
if(ihost_state->my_ip!=NULL) free(ihost_state->my_ip); |
145 |
> |
if((ihost_state->my_ip=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){ |
146 |
|
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
147 |
|
return -1; |
148 |
|
} |
149 |
|
|
145 |
– |
logmessage(LOG_DEBUG, "Sending STARTCONIG"); |
150 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
151 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
152 |
|
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
153 |
|
return -1; |
154 |
|
} |
155 |
|
|
152 |
– |
logmessage(LOG_DEBUG, "Sending LASTMODIFIED"); |
156 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
157 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
158 |
|
logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
163 |
|
return -1; |
164 |
|
} |
165 |
|
|
163 |
– |
logmessage(LOG_DEBUG, "Sending FILELIST"); |
166 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
167 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
168 |
|
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
173 |
|
return -1; |
174 |
|
} |
175 |
|
|
174 |
– |
logmessage(LOG_DEBUG, "Sending FQDN"); |
176 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
177 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
178 |
|
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
183 |
|
return -1; |
184 |
|
} |
185 |
|
|
185 |
– |
logmessage(LOG_DEBUG, "Sending FQDN"); |
186 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
187 |
|
if(reply== NULL){ |
188 |
|
logmessage(LOG_ERR, "Server error (%m)"); |
192 |
|
ihost_state->udp_update_time=atoi(reply); |
193 |
|
} |
194 |
|
|
195 |
– |
logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); |
195 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
196 |
|
if(reply== NULL){ |
197 |
|
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
201 |
|
ihost_state->tcp_update_time=atoi(reply); |
202 |
|
} |
203 |
|
|
205 |
– |
logmessage(LOG_DEBUG, "Sending ENDCONFIG"); |
204 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
205 |
|
if(reply== NULL){ |
206 |
|
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
207 |
|
return -1; |
208 |
|
} |
209 |
|
|
212 |
– |
logmessage(LOG_DEBUG, "Sending FILTER"); |
210 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
211 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
212 |
|
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
237 |
|
return -1; |
238 |
|
} |
239 |
|
|
243 |
– |
logmessage(LOG_DEBUG, "Sending END"); |
240 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
241 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
242 |
|
logmessage(LOG_ERR, "Server error on END (%m)"); |
295 |
|
return -1; |
296 |
|
} |
297 |
|
|
302 |
– |
logmessage(LOG_DEBUG, "Sending HEARTBEAT"); |
298 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
299 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
300 |
|
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
301 |
|
return -1; |
302 |
|
} |
303 |
|
|
309 |
– |
logmessage(LOG_DEBUG, "Sending CONFIG"); |
304 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
305 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
306 |
|
logmessage(LOG_ERR, "Server error on CONFIG"); |
307 |
|
return -1; |
308 |
|
} |
309 |
|
|
316 |
– |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); |
310 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
311 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
312 |
|
logmessage(LOG_ERR, "Server error on fileslist"); |
313 |
|
return -1; |
314 |
|
} |
315 |
|
|
323 |
– |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); |
316 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
317 |
|
if (reply==NULL) { |
318 |
|
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
324 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
325 |
|
} |
326 |
|
|
335 |
– |
logmessage(LOG_DEBUG,"Sending KEY"); |
327 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
328 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
329 |
|
logmessage(LOG_ERR, "Server error on KEY"); |
336 |
|
return -1; |
337 |
|
} |
338 |
|
|
348 |
– |
logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); |
339 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
340 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
341 |
|
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
362 |
|
char *stats[NUM_STATS]; |
363 |
|
char *xml_data=NULL; |
364 |
|
char *xml_data_p; |
365 |
+ |
int xml_size=0; |
366 |
|
int x=0; |
367 |
|
|
368 |
|
logmessage(LOG_DEBUG,"get_cpu_stats"); |
383 |
|
stats[7]=get_swap_stats(); |
384 |
|
logmessage(LOG_DEBUG,"get_user_stats"); |
385 |
|
stats[8]=get_user_stats(); |
386 |
+ |
|
387 |
|
|
388 |
< |
for(;x<NUM_STATS;x++){ |
388 |
> |
for(x=0;x<NUM_STATS;x++){ |
389 |
|
if(stats[x]==NULL){ |
390 |
|
logmessage(LOG_ERR,"Function returned NULL"); |
391 |
|
return NULL; |
392 |
|
} |
393 |
< |
if(xml_data==NULL){ |
394 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
395 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
396 |
< |
return NULL; |
397 |
< |
} |
398 |
< |
}else{ |
399 |
< |
xml_data_p=xml_data; |
400 |
< |
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 |
< |
} |
393 |
> |
xml_size+=strlen(stats[x]); |
394 |
> |
} |
395 |
> |
|
396 |
> |
xml_data=malloc(xml_size+1); |
397 |
> |
xml_data=strcpy(xml_data, stats[0]); |
398 |
> |
free(stats[0]); |
399 |
> |
for(x=1;x<NUM_STATS;x++){ |
400 |
> |
strcat(xml_data, stats[x]); |
401 |
|
free(stats[x]); |
402 |
|
} |
403 |
+ |
|
404 |
|
xml_data_p=xml_data; |
405 |
|
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); |
406 |
|
free(xml_data_p); |
426 |
|
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
427 |
|
return -1; |
428 |
|
} |
429 |
< |
logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
429 |
> |
logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
430 |
|
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
431 |
|
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
432 |
|
return -1; |
473 |
|
extern int syslog_logging; |
474 |
|
extern int log_level; |
475 |
|
extern int cur_level; |
476 |
+ |
FILE *f; |
477 |
|
|
478 |
|
log_level=1; |
479 |
|
cur_level=1; |
487 |
|
ihost_state.last_modified=NULL; |
488 |
|
ihost_state.files_list=NULL; |
489 |
|
ihost_state.key=NULL; |
490 |
+ |
ihost_state.my_ip=NULL; |
491 |
|
|
492 |
|
errf_set_progname(argv[0]); |
493 |
|
|
513 |
|
break; |
514 |
|
|
515 |
|
case 'V': |
516 |
< |
errf("%s version %f",argv[0], versionNo); |
516 |
> |
errf("%s version %s",argv[0], VERSION); |
517 |
|
break; |
518 |
|
|
519 |
|
default: |
577 |
|
break; |
578 |
|
} |
579 |
|
|
580 |
+ |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
581 |
+ |
pid=getpid(); |
582 |
+ |
if((f=fopen(PID_FILE,"w")) == NULL){ |
583 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
584 |
+ |
}else{ |
585 |
+ |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
586 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
587 |
+ |
} |
588 |
+ |
if((fclose(f))!=0){ |
589 |
+ |
logmessage(LOG_ERR, "failed to close PID file"); |
590 |
+ |
exit(1); |
591 |
+ |
} |
592 |
+ |
} |
593 |
+ |
|
594 |
|
if(ihost_configure(&ihost_state)!=0){ |
595 |
|
logmessage(LOG_ERR,"configure failed"); |
596 |
|
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
618 |
|
|
619 |
|
if(cur_time>=udp_time){ |
620 |
|
logmessage(LOG_DEBUG,"Sending udp data"); |
621 |
< |
|
621 |
> |
/* Work out how long it takes to get the stats for next time round |
622 |
> |
so the sleep time can be adjusted accordingly */ |
623 |
|
stat_grab_time=time(NULL); |
624 |
|
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
625 |
|
logmessage(LOG_ERR,"Failed to get stats (%m)"); |