--- projects/cms/source/ihost/ihost.c 2003/03/10 16:01:09 1.37 +++ projects/cms/source/ihost/ihost.c 2003/09/26 21:22:44 1.42 @@ -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; @@ -223,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; } @@ -234,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; } @@ -403,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; @@ -700,6 +716,8 @@ void usage(char *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, " -s Set the machine name to be reported as\n"); + fprintf(stderr, " -i Set the IP to be reported as\n"); fprintf(stderr, " -V Print version number\n"); fprintf(stderr, " -h Prints this help page\n"); exit(1); @@ -731,11 +749,13 @@ int main(int argc, char **argv){ ihost_state.filtermanager_host=NULL; 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++; @@ -753,6 +773,23 @@ int main(int argc, char **argv){ case 'V': fprintf(stderr, "%s version %s\n", argv[0], VERSION); break; + case 's': + 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; default: usage(argv[0]);