| 1 |
+ |
/* |
| 2 |
+ |
* i-scream central monitoring system |
| 3 |
+ |
* Copyright (C) 2000-2002 i-scream |
| 4 |
+ |
* |
| 5 |
+ |
* This program is free software; you can redistribute it and/or |
| 6 |
+ |
* modify it under the terms of the GNU General Public License |
| 7 |
+ |
* as published by the Free Software Foundation; either version 2 |
| 8 |
+ |
* of the License, or (at your option) any later version. |
| 9 |
+ |
* |
| 10 |
+ |
* This program is distributed in the hope that it will be useful, |
| 11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
+ |
* GNU General Public License for more details. |
| 14 |
+ |
* |
| 15 |
+ |
* You should have received a copy of the GNU General Public License |
| 16 |
+ |
* along with this program; if not, write to the Free Software |
| 17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 |
+ |
*/ |
| 19 |
+ |
|
| 20 |
|
#include <stdio.h> |
| 2 |
– |
#include <sys/socket.h> |
| 21 |
|
#include <stdlib.h> |
| 22 |
|
#include <unistd.h> |
| 23 |
|
#include <syslog.h> |
| 24 |
|
#include <netinet/in.h> |
| 25 |
|
#include "ukcprog.h" |
| 26 |
|
#include <netdb.h> |
| 27 |
< |
#include <strings.h> |
| 27 |
> |
#include <string.h> |
| 28 |
|
#include "statgrab.h" |
| 29 |
+ |
#include <time.h> |
| 30 |
+ |
#include <sys/socket.h> |
| 31 |
+ |
#include <netinet/in.h> |
| 32 |
+ |
#include <arpa/inet.h> |
| 33 |
|
|
| 34 |
+ |
#define versionNo 0.8 |
| 35 |
|
#define RECONFIGURE_RETURN_CODE 2 |
| 36 |
|
#define UDP_MAX_PACKET_SIZE 8192 |
| 37 |
|
|
| 38 |
|
typedef struct{ |
| 39 |
|
int fm_port; |
| 40 |
|
char *fm_host; |
| 41 |
< |
|
| 41 |
> |
|
| 42 |
> |
char *my_ip; |
| 43 |
|
char *my_fqdn; |
| 44 |
|
char *server_fqdn; |
| 45 |
|
int server_udp_port; |
| 64 |
|
int ihost_configure(ihost_state_t *ihost_state){ |
| 65 |
|
struct sockaddr_in addr; |
| 66 |
|
struct in_addr haddr; |
| 67 |
+ |
struct sockaddr ip; |
| 68 |
+ |
int ip_len; |
| 69 |
|
int sd; |
| 70 |
|
FILE *fm_fd_r, *fm_fd_w; |
| 71 |
|
char *reply; |
| 107 |
|
errf("Failed to open write stream (%m)"); |
| 108 |
|
return -1; |
| 109 |
|
} |
| 110 |
+ |
ip_len=sizeof ip; |
| 111 |
+ |
memset(&ip, 0, ip_len); |
| 112 |
+ |
if((getsockname(sd, &ip, &ip_len)) != 0){ |
| 113 |
+ |
errf("Failed to get IP address (%m)"); |
| 114 |
+ |
return -1; |
| 115 |
+ |
} |
| 116 |
+ |
if (ip.sa_family!=AF_INET){ |
| 117 |
+ |
errf("sa family is wrong type"); |
| 118 |
+ |
return -1; |
| 119 |
+ |
} |
| 120 |
+ |
|
| 121 |
+ |
if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
| 122 |
+ |
errf("Failed to get IP (%m)"); |
| 123 |
+ |
return -1; |
| 124 |
+ |
} |
| 125 |
|
|
| 126 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
| 127 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
| 315 |
|
return -1; |
| 316 |
|
} |
| 317 |
|
|
| 318 |
+ |
fflush(fm_fd_r); |
| 319 |
+ |
fflush(fm_fd_w); |
| 320 |
+ |
|
| 321 |
|
if(fclose(fm_fd_r) !=0){ |
| 322 |
|
errf("Failed to close read FD (%m)"); |
| 323 |
|
return -1; |
| 367 |
|
free(stats[x]); |
| 368 |
|
} |
| 369 |
|
xml_data_p=xml_data; |
| 370 |
< |
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), "127.0.0.1", ihost_state->key, xml_data); |
| 370 |
> |
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); |
| 371 |
|
free(xml_data_p); |
| 372 |
|
|
| 373 |
|
return xml_data; |
| 406 |
|
return -1; |
| 407 |
|
} |
| 408 |
|
|
| 409 |
+ |
close(sd); |
| 410 |
+ |
|
| 411 |
|
return 0; |
| 412 |
|
} |
| 413 |
|
|
| 414 |
+ |
void usage(char *progname){ |
| 415 |
+ |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
| 416 |
+ |
fprintf(stderr, "Options\n"); |
| 417 |
+ |
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
| 418 |
+ |
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
| 419 |
+ |
fprintf(stderr, " -s Send errors to syslog\n"); |
| 420 |
+ |
fprintf(stderr, " -V Print version number\n"); |
| 421 |
+ |
fprintf(stderr, " -h Prints this help page\n"); |
| 422 |
+ |
exit(1); |
| 423 |
+ |
} |
| 424 |
+ |
|
| 425 |
|
int main(int argc, char **argv){ |
| 426 |
|
ihost_state_t ihost_state; |
| 427 |
|
int heartbeat_exit; |
| 428 |
|
int counter=0; |
| 429 |
< |
long udp_time=0, tcp_time=0, cur_time=0; |
| 429 |
> |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
| 430 |
|
int sleep_delay=0; |
| 431 |
|
char *xml_stats; |
| 432 |
|
|
| 433 |
+ |
int cmdopt; |
| 434 |
+ |
extern int optind; |
| 435 |
+ |
int verbose=0, daemon=0, syslog=0 ; |
| 436 |
+ |
|
| 437 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
| 438 |
|
ihost_state.fm_host=NULL; |
| 439 |
|
ihost_state.my_fqdn=NULL; |
| 443 |
|
ihost_state.key=NULL; |
| 444 |
|
|
| 445 |
|
errf_set_progname(argv[0]); |
| 446 |
< |
if(argc!=3){ |
| 447 |
< |
errf_usage("<host> <port>"); |
| 446 |
> |
|
| 447 |
> |
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
| 448 |
> |
switch(cmdopt){ |
| 449 |
> |
case 'v': |
| 450 |
> |
verbose++; |
| 451 |
> |
break; |
| 452 |
> |
|
| 453 |
> |
case 'd': |
| 454 |
> |
daemon=1; |
| 455 |
> |
break; |
| 456 |
> |
|
| 457 |
> |
case 's': |
| 458 |
> |
syslog=1; |
| 459 |
> |
break; |
| 460 |
> |
|
| 461 |
> |
case 'h': |
| 462 |
> |
usage(argv[0]); |
| 463 |
> |
break; |
| 464 |
> |
case 'V': |
| 465 |
> |
errf("%s version %f",argv[0], versionNo); |
| 466 |
> |
break; |
| 467 |
> |
default: |
| 468 |
> |
usage(argv[0]); |
| 469 |
> |
exit(1); |
| 470 |
> |
} |
| 471 |
> |
} |
| 472 |
> |
|
| 473 |
> |
if(argc!=optind+2){ |
| 474 |
> |
usage(argv[0]); |
| 475 |
|
exit(1); |
| 476 |
|
} |
| 477 |
+ |
ihost_state.fm_host=argv[optind]; |
| 478 |
+ |
ihost_state.fm_port=atoi(argv[optind+1]); |
| 479 |
+ |
if(ihost_state.fm_port==0){ |
| 480 |
+ |
errf("Invalid port number"); |
| 481 |
+ |
usage(argv[0]); |
| 482 |
+ |
} |
| 483 |
+ |
printf("%s\n%d\n",ihost_state.fm_host, ihost_state.fm_port); |
| 484 |
|
|
| 390 |
– |
ihost_state.fm_host=argv[1]; |
| 391 |
– |
ihost_state.fm_port=atoi(argv[2]); |
| 392 |
– |
|
| 485 |
|
if(ihost_configure(&ihost_state)!=0){ |
| 486 |
|
errf("configure failed"); |
| 487 |
|
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
| 489 |
|
exit(1); |
| 490 |
|
} |
| 491 |
|
|
| 400 |
– |
/* get starting stats */ |
| 401 |
– |
if((heartbeat(&ihost_state)) == -1){ |
| 402 |
– |
errf("inital heartbeat failed (%m)"); |
| 403 |
– |
exit(1); |
| 404 |
– |
} |
| 405 |
– |
|
| 406 |
– |
|
| 407 |
– |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
| 408 |
– |
errf("Failed to get stats (%m)"); |
| 409 |
– |
exit(1); |
| 410 |
– |
} |
| 411 |
– |
|
| 412 |
– |
|
| 492 |
|
for(;;){ |
| 493 |
|
cur_time=time(NULL); |
| 494 |
|
if(cur_time>=tcp_time){ |
| 495 |
+ |
/*printf("sending TCP\n");*/ |
| 496 |
|
heartbeat_exit=heartbeat(&ihost_state); |
| 497 |
|
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
| 498 |
< |
errf("heartbeat needs to be reconfigured"); |
| 498 |
> |
/*errf("heartbeat needs to be reconfigured");*/ |
| 499 |
|
ihost_configure(&ihost_state); |
| 500 |
|
/* So udp doesn't wait til next sending before updating */ |
| 501 |
|
udp_time=0; |
| 506 |
|
} |
| 507 |
|
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
| 508 |
|
} |
| 509 |
+ |
|
| 510 |
|
if(cur_time>=udp_time){ |
| 511 |
< |
send_stats(&ihost_state, xml_stats); |
| 512 |
< |
free(xml_stats); |
| 513 |
< |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
| 511 |
> |
/*printf("sending UDP\n");*/ |
| 512 |
> |
stat_grab_time=time(NULL); |
| 513 |
> |
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
| 514 |
|
errf("Failed to get stats (%m)"); |
| 515 |
|
exit(1); |
| 516 |
|
} |
| 517 |
< |
udp_time=time(NULL)+ihost_state.udp_update_time; |
| 517 |
> |
stat_grab_time=time(NULL)-stat_grab_time; |
| 518 |
> |
send_stats(&ihost_state, xml_stats); |
| 519 |
> |
free(xml_stats); |
| 520 |
> |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
| 521 |
|
} |
| 522 |
< |
if(tcp_time>udp_time){ |
| 523 |
< |
sleep_delay=time(NULL)-tcp_time; |
| 522 |
> |
|
| 523 |
> |
if(tcp_time<udp_time){ |
| 524 |
> |
sleep_delay=tcp_time-time(NULL); |
| 525 |
|
}else{ |
| 526 |
< |
sleep_delay=time(NULL)-udp_time; |
| 526 |
> |
sleep_delay=udp_time-time(NULL); |
| 527 |
|
} |
| 528 |
< |
sleep(sleep_delay); |
| 528 |
> |
|
| 529 |
> |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
| 530 |
> |
if(sleep_delay>0) sleep(sleep_delay); |
| 531 |
|
} |
| 532 |
|
return 0; |
| 533 |
|
} |