--- projects/cms/source/ihost/ihost.c 2003/03/03 12:32:35 1.29 +++ projects/cms/source/ihost/ihost.c 2004/01/05 15:22:17 1.44 @@ -1,6 +1,6 @@ /* * i-scream central monitoring system - * http://www.i-scream.org.uk + * http://www.i-scream.org * Copyright (C) 2000-2002 i-scream * * This program is free software; you can redistribute it and/or @@ -31,9 +31,10 @@ #include #include #include +#include -#include "ukcprog.h" -#include "statgrab.h" +#include +#include #define LOG_CRIT 0 #define LOG_ERR 1 @@ -46,6 +47,8 @@ typedef struct{ char *host_ip; char *host_fqdn; + int preset_fqdn; + int preset_ip; char *server_fqdn; int server_udp_port; @@ -75,8 +78,80 @@ typedef struct{ ihost_config_t ihost_config; +extern int errno; + +/* Taken from the OpenSSH code. Its licence included in function.*/ +#ifndef HAVE_STRLCAT + +/* + * Copyright (c) 1998 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} + +#endif +/* End strlcat function taken from OpenSSH */ + void log_msg(int level, char *format, ...){ - extern int errno; int cur_errno; va_list ap; @@ -91,6 +166,7 @@ void log_msg(int level, char *format, ...){ }else{ fprintf(ihost_config.log, "\n"); } + fflush(ihost_config.log); } } @@ -150,6 +226,7 @@ FILE *create_tcp_connection(char *hostname, int port){ if((get_host_addr(hostname, &haddr))!=0){ log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); + close(sock); return NULL; } @@ -161,6 +238,7 @@ FILE *create_tcp_connection(char *hostname, int port){ log_msg(LOG_DEBUG, "Creating a tcp connection"); if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); + close(sock); return NULL; } @@ -220,7 +298,9 @@ int ihost_getconfig(ihost_state_t *ihost_state){ int server_udp_port=0; time_t config_ttl=0; - tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port); + if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ + return -1; + } if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ if(tcp_con==NULL){ @@ -236,7 +316,7 @@ int ihost_getconfig(ihost_state_t *ihost_state){ } if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ - if((tcp_comm(tcp_con, "END", &response, "OK"))==0){ + if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ goto error; } fclose(tcp_con); @@ -307,7 +387,7 @@ int ihost_getconfig(ihost_state_t *ihost_state){ goto error; } - printf("string : %s\n", response_ptr); + /*printf("string : %s\n", response_ptr);*/ server_udp_port=atoi(response_ptr); if (server_udp_port==0){ @@ -328,14 +408,25 @@ int ihost_getconfig(ihost_state_t *ihost_state){ * this already be NULL */ if(ihost_state->file_list!=NULL) free(ihost_state->file_list); if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); - if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); - if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); + if(ihost_state->preset_fqdn){ + if(host_fqdn != NULL) free(host_fqdn); + }else{ + if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); + ihost_state->host_fqdn=host_fqdn; + } + + if(ihost_state->preset_ip){ + if(host_ip != NULL) free(host_ip); + }else{ + if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); + ihost_state->host_ip=host_ip; + } + + ihost_state->file_list=file_list; ihost_state->last_modified=last_modified; - ihost_state->host_fqdn=host_fqdn; - ihost_state->host_ip=host_ip; ihost_state->server_fqdn=server_fqdn; ihost_state->server_udp_port=server_udp_port; ihost_state->udp_update_time=udp_update_time; @@ -504,7 +595,7 @@ int get_system_stats(int seq_no, ihost_state_t *ihost_ y=page_stats->pages_pageout; } snprintf(tmp, size, \ - "%lld%lld", \ + "%lld%lld", \ x, \ y); @@ -621,12 +712,18 @@ too_big_error: void usage(char *progname){ - fprintf(stderr, "Usage %s [options] server port\n", progname); - fprintf(stderr, "Options\n"); - fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); - fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); - fprintf(stderr, " -V Print version number\n"); - fprintf(stderr, " -h Prints this help page\n"); + fprintf(stderr, "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n", progname); + fprintf(stderr, " -v Verbose mode, -vv would make even more verbose\n"); + fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); + fprintf(stderr, " -n Set the machine name to be reported as\n"); + fprintf(stderr, " -i Set the IP to be reported as\n"); + fprintf(stderr, " -s Specifies the i-scream server to connect to\n"); + fprintf(stderr, " default: %s\n", DEF_SERVER_NAME); + fprintf(stderr, " -p Specifies the i-scream server port\n"); + fprintf(stderr, " default: %d\n", DEF_SERVER_PORT); + fprintf(stderr, " -V Print version number\n"); + fprintf(stderr, " -h Prints this help page\n"); + fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); exit(1); } @@ -653,14 +750,17 @@ int main(int argc, char **argv){ ihost_config.log=stderr; /* Blank ihost_state to default settings */ - ihost_state.filtermanager_host=NULL; + ihost_state.filtermanager_host=DEF_SERVER_NAME; + ihost_state.filtermanager_port=DEF_SERVER_PORT; ihost_state.host_fqdn=NULL; ihost_state.host_ip=NULL; + ihost_state.preset_fqdn = 0; + ihost_state.preset_ip = 0; ihost_state.server_fqdn=NULL; ihost_state.file_list=NULL; ihost_state.last_modified=NULL; - while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ + while((cmdopt=getopt(argc, argv, "vfhVs:i:")) != -1){ switch(cmdopt){ case 'v': ihost_config.verbose++; @@ -671,28 +771,42 @@ int main(int argc, char **argv){ ihost_config.daemon=0; break; - case 'h': - usage(argv[0]); - break; - case 'V': fprintf(stderr, "%s version %s\n", argv[0], VERSION); break; + case 'n': + ihost_state.preset_fqdn = 1; + ihost_state.host_fqdn = strdup(optarg); + if(ihost_state.host_fqdn == NULL){ + fprintf(stderr, "Missing hostname\n"); + usage(argv[0]); + } + break; + case 'i': + /* Hmm.. Someone could set any string to be the IP, and it will let it */ + ihost_state.preset_ip = 1; + ihost_state.host_ip = strdup(optarg); + if(ihost_state.host_ip == NULL){ + fprintf(stderr, "Missing ip\n"); + usage(argv[0]); + } + break; + case 's': + ihost_state.filtermanager_host=strdup(optarg); + break; + + case 'p': + ihost_state.filtermanager_port=atoi(optarg); + break; + + case 'h': default: usage(argv[0]); exit(1); } } - if(argc!=optind+2){ - usage(argv[0]); - exit(1); - } - - ihost_state.filtermanager_host=strdup(argv[optind]); - ihost_state.filtermanager_port=atoi(argv[optind+1]); - if(gethostbyname(ihost_state.filtermanager_host)==NULL){ log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); exit(1); @@ -729,7 +843,13 @@ int main(int argc, char **argv){ } - log_msg(LOG_INFO, "Starting ihost"); + log_msg(LOG_INFO, "Starting ihost..."); + + log_msg(LOG_DEBUG, "Running statgrab_init()"); + if(!statgrab_init()){ + log_msg(LOG_CRIT, "statgrab_init failed (%m)"); + exit(1); + } log_msg(LOG_DEBUG,"Writing PID FILE"); @@ -746,6 +866,7 @@ int main(int argc, char **argv){ } } + /* Get the initial config from the filter manager. Should this fail, * wait, and then try again. */ @@ -757,7 +878,7 @@ int main(int argc, char **argv){ sleep(10); } - printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); + /*printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port);*/ while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ log_msg(LOG_ERR, "Failed to create udp socket"); sleep(10);