--- projects/cms/source/ihost/ihost.c 2002/05/19 12:03:43 1.12 +++ projects/cms/source/ihost/ihost.c 2002/05/20 12:28:46 1.17 @@ -18,7 +18,6 @@ */ #include -#include #include #include #include @@ -28,14 +27,19 @@ #include #include "statgrab.h" #include +#include +#include +#include +#define versionNo 0.8 #define RECONFIGURE_RETURN_CODE 2 #define UDP_MAX_PACKET_SIZE 8192 typedef struct{ int fm_port; char *fm_host; - + + char *my_ip; char *my_fqdn; char *server_fqdn; int server_udp_port; @@ -60,6 +64,8 @@ char* sock_comm(FILE *f_r, FILE *f_w, char *sendString int ihost_configure(ihost_state_t *ihost_state){ struct sockaddr_in addr; struct in_addr haddr; + struct sockaddr ip; + int ip_len; int sd; FILE *fm_fd_r, *fm_fd_w; char *reply; @@ -101,6 +107,21 @@ int ihost_configure(ihost_state_t *ihost_state){ errf("Failed to open write stream (%m)"); return -1; } + ip_len=sizeof ip; + memset(&ip, 0, ip_len); + if((getsockname(sd, &ip, &ip_len)) != 0){ + errf("Failed to get IP address (%m)"); + return -1; + } + if (ip.sa_family!=AF_INET){ + errf("sa family is wrong type"); + return -1; + } + + if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ + errf("Failed to get IP (%m)"); + return -1; + } reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { @@ -346,7 +367,7 @@ char *stat_grab(ihost_state_t *ihost_state, int counte free(stats[x]); } xml_data_p=xml_data; - xml_data=strf("%s", counter, ihost_state->my_fqdn, time(NULL), "127.0.0.1", ihost_state->key, xml_data); + xml_data=strf("%s", counter, ihost_state->my_fqdn, time(NULL), ihost_state->my_ip, ihost_state->key, xml_data); free(xml_data_p); return xml_data; @@ -390,6 +411,17 @@ int send_stats(ihost_state_t *ihost_state, char *data_ return 0; } +void usage(char *progname){ + fprintf(stderr, "Usage %s [options] server port\n", progname); + fprintf(stderr, "Options\n"); + fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); + fprintf(stderr, " -d Daemon mode, self backgrounding\n"); + fprintf(stderr, " -s Send errors to syslog\n"); + fprintf(stderr, " -V Print version number\n"); + fprintf(stderr, " -h Prints this help page\n"); + exit(1); +} + int main(int argc, char **argv){ ihost_state_t ihost_state; int heartbeat_exit; @@ -398,6 +430,10 @@ int main(int argc, char **argv){ int sleep_delay=0; char *xml_stats; + int cmdopt; + extern int optind; + int verbose=0, daemon=0, syslog=0 ; + /* NULL'ify so i can tell if i need to free it or not */ ihost_state.fm_host=NULL; ihost_state.my_fqdn=NULL; @@ -407,14 +443,45 @@ int main(int argc, char **argv){ ihost_state.key=NULL; errf_set_progname(argv[0]); - if(argc!=3){ - errf_usage(" "); + + while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ + switch(cmdopt){ + case 'v': + verbose++; + break; + + case 'd': + daemon=1; + break; + + case 's': + syslog=1; + break; + + case 'h': + usage(argv[0]); + break; + case 'V': + errf("%s version %f",argv[0], versionNo); + break; + default: + usage(argv[0]); + exit(1); + } + } + + if(argc!=optind+2){ + usage(argv[0]); exit(1); } + ihost_state.fm_host=argv[optind]; + ihost_state.fm_port=atoi(argv[optind+1]); + if(ihost_state.fm_port==0){ + errf("Invalid port number"); + usage(argv[0]); + } + printf("%s\n%d\n",ihost_state.fm_host, ihost_state.fm_port); - ihost_state.fm_host=argv[1]; - ihost_state.fm_port=atoi(argv[2]); - if(ihost_configure(&ihost_state)!=0){ errf("configure failed"); /* Ok, ideally we prob should have 2 copies of the structure and carry on if this @@ -443,7 +510,7 @@ int main(int argc, char **argv){ if(cur_time>=udp_time){ /*printf("sending UDP\n");*/ stat_grab_time=time(NULL); - if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ + if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ errf("Failed to get stats (%m)"); exit(1); }