| 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 |
| 41 |
|
#define UDP_MAX_PACKET_SIZE 8192 |
| 42 |
|
#define PID_FILE "/var/tmp/.ihost.pid" |
| 76 |
|
|
| 77 |
|
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
| 78 |
|
char *reply; |
| 79 |
+ |
logmessage(LOG_DEBUG, "Sending %s",sendString); |
| 80 |
|
fprintf(f_w, "%s\n", sendString); |
| 81 |
|
fflush(f_w); |
| 82 |
|
reply=fpgetline(f_r); |
| 83 |
+ |
if (reply!=NULL) logmessage(LOG_DEBUG, "Received %s", reply); |
| 84 |
|
/* Returns pointer to static buffer */ |
| 85 |
|
return reply; |
| 86 |
|
} |
| 142 |
|
logmessage(LOG_ERR, "sa family is wrong type"); |
| 143 |
|
return -1; |
| 144 |
|
} |
| 145 |
< |
|
| 146 |
< |
if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
| 145 |
> |
|
| 146 |
> |
if(ihost_state->my_ip!=NULL) free(ihost_state->my_ip); |
| 147 |
> |
if((ihost_state->my_ip=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){ |
| 148 |
|
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
| 149 |
|
return -1; |
| 150 |
|
} |
| 151 |
|
|
| 146 |
– |
logmessage(LOG_DEBUG, "Sending STARTCONIG"); |
| 152 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
| 153 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
| 154 |
|
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
| 155 |
|
return -1; |
| 156 |
|
} |
| 157 |
|
|
| 153 |
– |
logmessage(LOG_DEBUG, "Sending LASTMODIFIED"); |
| 158 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
| 159 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
| 160 |
|
logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
| 165 |
|
return -1; |
| 166 |
|
} |
| 167 |
|
|
| 164 |
– |
logmessage(LOG_DEBUG, "Sending FILELIST"); |
| 168 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
| 169 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
| 170 |
|
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
| 175 |
|
return -1; |
| 176 |
|
} |
| 177 |
|
|
| 175 |
– |
logmessage(LOG_DEBUG, "Sending FQDN"); |
| 178 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
| 179 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
| 180 |
|
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
| 185 |
|
return -1; |
| 186 |
|
} |
| 187 |
|
|
| 186 |
– |
logmessage(LOG_DEBUG, "Sending FQDN"); |
| 188 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
| 189 |
|
if(reply== NULL){ |
| 190 |
|
logmessage(LOG_ERR, "Server error (%m)"); |
| 194 |
|
ihost_state->udp_update_time=atoi(reply); |
| 195 |
|
} |
| 196 |
|
|
| 196 |
– |
logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); |
| 197 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
| 198 |
|
if(reply== NULL){ |
| 199 |
|
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
| 203 |
|
ihost_state->tcp_update_time=atoi(reply); |
| 204 |
|
} |
| 205 |
|
|
| 206 |
– |
logmessage(LOG_DEBUG, "Sending ENDCONFIG"); |
| 206 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
| 207 |
|
if(reply== NULL){ |
| 208 |
|
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
| 209 |
|
return -1; |
| 210 |
|
} |
| 211 |
|
|
| 213 |
– |
logmessage(LOG_DEBUG, "Sending FILTER"); |
| 212 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
| 213 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
| 214 |
|
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
| 239 |
|
return -1; |
| 240 |
|
} |
| 241 |
|
|
| 244 |
– |
logmessage(LOG_DEBUG, "Sending END"); |
| 242 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
| 243 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
| 244 |
|
logmessage(LOG_ERR, "Server error on END (%m)"); |
| 297 |
|
return -1; |
| 298 |
|
} |
| 299 |
|
|
| 303 |
– |
logmessage(LOG_DEBUG, "Sending HEARTBEAT"); |
| 300 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
| 301 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
| 302 |
|
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
| 303 |
|
return -1; |
| 304 |
|
} |
| 305 |
|
|
| 310 |
– |
logmessage(LOG_DEBUG, "Sending CONFIG"); |
| 306 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
| 307 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
| 308 |
|
logmessage(LOG_ERR, "Server error on CONFIG"); |
| 309 |
|
return -1; |
| 310 |
|
} |
| 311 |
|
|
| 317 |
– |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); |
| 312 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
| 313 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
| 314 |
|
logmessage(LOG_ERR, "Server error on fileslist"); |
| 315 |
|
return -1; |
| 316 |
|
} |
| 317 |
|
|
| 324 |
– |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); |
| 318 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
| 319 |
|
if (reply==NULL) { |
| 320 |
|
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
| 326 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
| 327 |
|
} |
| 328 |
|
|
| 336 |
– |
logmessage(LOG_DEBUG,"Sending KEY"); |
| 329 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
| 330 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
| 331 |
|
logmessage(LOG_ERR, "Server error on KEY"); |
| 338 |
|
return -1; |
| 339 |
|
} |
| 340 |
|
|
| 349 |
– |
logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); |
| 341 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
| 342 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
| 343 |
|
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
| 364 |
|
char *stats[NUM_STATS]; |
| 365 |
|
char *xml_data=NULL; |
| 366 |
|
char *xml_data_p; |
| 367 |
+ |
int xml_size=0; |
| 368 |
|
int x=0; |
| 369 |
|
|
| 370 |
|
logmessage(LOG_DEBUG,"get_cpu_stats"); |
| 385 |
|
stats[7]=get_swap_stats(); |
| 386 |
|
logmessage(LOG_DEBUG,"get_user_stats"); |
| 387 |
|
stats[8]=get_user_stats(); |
| 388 |
+ |
|
| 389 |
|
|
| 390 |
< |
for(;x<NUM_STATS;x++){ |
| 390 |
> |
for(x=0;x<NUM_STATS;x++){ |
| 391 |
|
if(stats[x]==NULL){ |
| 392 |
|
logmessage(LOG_ERR,"Function returned NULL"); |
| 393 |
|
return NULL; |
| 394 |
|
} |
| 395 |
< |
if(xml_data==NULL){ |
| 396 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
| 397 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
| 398 |
< |
return NULL; |
| 399 |
< |
} |
| 400 |
< |
}else{ |
| 401 |
< |
xml_data_p=xml_data; |
| 402 |
< |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
| 410 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
| 411 |
< |
return NULL; |
| 412 |
< |
} |
| 413 |
< |
free(xml_data_p); |
| 414 |
< |
} |
| 395 |
> |
xml_size+=strlen(stats[x]); |
| 396 |
> |
} |
| 397 |
> |
|
| 398 |
> |
xml_data=malloc(xml_size+1); |
| 399 |
> |
xml_data=strcpy(xml_data, stats[0]); |
| 400 |
> |
free(stats[0]); |
| 401 |
> |
for(x=1;x<NUM_STATS;x++){ |
| 402 |
> |
strcat(xml_data, stats[x]); |
| 403 |
|
free(stats[x]); |
| 404 |
|
} |
| 405 |
+ |
|
| 406 |
|
xml_data_p=xml_data; |
| 407 |
|
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); |
| 408 |
|
free(xml_data_p); |
| 428 |
|
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
| 429 |
|
return -1; |
| 430 |
|
} |
| 431 |
< |
logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
| 431 |
> |
logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
| 432 |
|
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
| 433 |
|
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
| 434 |
|
return -1; |
| 489 |
|
ihost_state.last_modified=NULL; |
| 490 |
|
ihost_state.files_list=NULL; |
| 491 |
|
ihost_state.key=NULL; |
| 492 |
+ |
ihost_state.my_ip=NULL; |
| 493 |
|
|
| 494 |
|
errf_set_progname(argv[0]); |
| 495 |
|
|
| 515 |
|
break; |
| 516 |
|
|
| 517 |
|
case 'V': |
| 518 |
< |
errf("%s version %f",argv[0], versionNo); |
| 518 |
> |
errf("%s version %s",argv[0], VERSION); |
| 519 |
|
break; |
| 520 |
|
|
| 521 |
|
default: |
| 582 |
|
logmessage(LOG_DEBUG,"Writing PID FILE"); |
| 583 |
|
pid=getpid(); |
| 584 |
|
if((f=fopen(PID_FILE,"w")) == NULL){ |
| 585 |
< |
logmessage(LOG_WARNING, "Failed to write PID file"); |
| 585 |
> |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
| 586 |
|
}else{ |
| 587 |
< |
if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){ |
| 588 |
< |
logmessage(LOG_WARNING, "Failed to write PID file"); |
| 587 |
> |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
| 588 |
> |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
| 589 |
|
} |
| 590 |
|
if((fclose(f))!=0){ |
| 591 |
|
logmessage(LOG_ERR, "failed to close PID file"); |
| 620 |
|
|
| 621 |
|
if(cur_time>=udp_time){ |
| 622 |
|
logmessage(LOG_DEBUG,"Sending udp data"); |
| 623 |
< |
|
| 623 |
> |
/* Work out how long it takes to get the stats for next time round |
| 624 |
> |
so the sleep time can be adjusted accordingly */ |
| 625 |
|
stat_grab_time=time(NULL); |
| 626 |
|
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
| 627 |
|
logmessage(LOG_ERR,"Failed to get stats (%m)"); |