| 1 |
|
/* |
| 2 |
|
* i-scream central monitoring system |
| 3 |
< |
* http://www.i-scream.org.uk |
| 3 |
> |
* http://www.i-scream.org |
| 4 |
|
* Copyright (C) 2000-2002 i-scream |
| 5 |
|
* |
| 6 |
|
* This program is free software; you can redistribute it and/or |
| 31 |
|
#include <stdarg.h> |
| 32 |
|
#include <errno.h> |
| 33 |
|
#include <netdb.h> |
| 34 |
+ |
#include <netinet/in.h> |
| 35 |
|
|
| 36 |
|
#include <ukcprog.h> |
| 37 |
< |
#include "statgrab.h" |
| 37 |
> |
#include <statgrab.h> |
| 38 |
|
|
| 39 |
|
#define LOG_CRIT 0 |
| 40 |
|
#define LOG_ERR 1 |
| 41 |
|
#define LOG_INFO 2 |
| 42 |
|
#define LOG_DEBUG 3 |
| 43 |
|
|
| 43 |
– |
#define PID_FILE "/var/run/ihost.pid" |
| 44 |
– |
#define LOGFILE_PATH "/var/log/ihost.log" |
| 45 |
– |
#define VERSION ".03" |
| 46 |
– |
|
| 47 |
– |
#define MAX_UDP_PACKET_SIZE 8192 |
| 48 |
– |
|
| 44 |
|
typedef struct{ |
| 45 |
|
int filtermanager_port; |
| 46 |
|
char *filtermanager_host; |
| 47 |
|
|
| 48 |
|
char *host_ip; |
| 49 |
|
char *host_fqdn; |
| 50 |
+ |
int preset_fqdn; |
| 51 |
+ |
int preset_ip; |
| 52 |
|
|
| 53 |
|
char *server_fqdn; |
| 54 |
|
int server_udp_port; |
| 78 |
|
|
| 79 |
|
ihost_config_t ihost_config; |
| 80 |
|
|
| 81 |
+ |
extern int errno; |
| 82 |
+ |
|
| 83 |
+ |
/* Taken from the OpenSSH code. Its licence included in function.*/ |
| 84 |
+ |
#ifndef HAVE_STRLCAT |
| 85 |
+ |
|
| 86 |
+ |
/* |
| 87 |
+ |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
| 88 |
+ |
* All rights reserved. |
| 89 |
+ |
* |
| 90 |
+ |
* Redistribution and use in source and binary forms, with or without |
| 91 |
+ |
* modification, are permitted provided that the following conditions |
| 92 |
+ |
* are met: |
| 93 |
+ |
* 1. Redistributions of source code must retain the above copyright |
| 94 |
+ |
* notice, this list of conditions and the following disclaimer. |
| 95 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
| 96 |
+ |
* notice, this list of conditions and the following disclaimer in the |
| 97 |
+ |
* documentation and/or other materials provided with the distribution. |
| 98 |
+ |
* 3. The name of the author may not be used to endorse or promote products |
| 99 |
+ |
* derived from this software without specific prior written permission. |
| 100 |
+ |
* |
| 101 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 102 |
+ |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 103 |
+ |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 104 |
+ |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 105 |
+ |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 106 |
+ |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 107 |
+ |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 108 |
+ |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 109 |
+ |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 110 |
+ |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 111 |
+ |
*/ |
| 112 |
+ |
|
| 113 |
+ |
/* |
| 114 |
+ |
* Appends src to string dst of size siz (unlike strncat, siz is the |
| 115 |
+ |
* full size of dst, not space left). At most siz-1 characters |
| 116 |
+ |
* will be copied. Always NUL terminates (unless siz <= strlen(dst)). |
| 117 |
+ |
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
| 118 |
+ |
* If retval >= siz, truncation occurred. |
| 119 |
+ |
*/ |
| 120 |
+ |
size_t |
| 121 |
+ |
strlcat(dst, src, siz) |
| 122 |
+ |
char *dst; |
| 123 |
+ |
const char *src; |
| 124 |
+ |
size_t siz; |
| 125 |
+ |
{ |
| 126 |
+ |
register char *d = dst; |
| 127 |
+ |
register const char *s = src; |
| 128 |
+ |
register size_t n = siz; |
| 129 |
+ |
size_t dlen; |
| 130 |
+ |
|
| 131 |
+ |
/* Find the end of dst and adjust bytes left but don't go past end */ |
| 132 |
+ |
while (n-- != 0 && *d != '\0') |
| 133 |
+ |
d++; |
| 134 |
+ |
dlen = d - dst; |
| 135 |
+ |
n = siz - dlen; |
| 136 |
+ |
|
| 137 |
+ |
if (n == 0) |
| 138 |
+ |
return(dlen + strlen(s)); |
| 139 |
+ |
while (*s != '\0') { |
| 140 |
+ |
if (n != 1) { |
| 141 |
+ |
*d++ = *s; |
| 142 |
+ |
n--; |
| 143 |
+ |
} |
| 144 |
+ |
s++; |
| 145 |
+ |
} |
| 146 |
+ |
*d = '\0'; |
| 147 |
+ |
|
| 148 |
+ |
return(dlen + (s - src)); /* count does not include NUL */ |
| 149 |
+ |
} |
| 150 |
+ |
|
| 151 |
+ |
#endif |
| 152 |
+ |
/* End strlcat function taken from OpenSSH */ |
| 153 |
+ |
|
| 154 |
|
void log_msg(int level, char *format, ...){ |
| 85 |
– |
extern int errno; |
| 155 |
|
int cur_errno; |
| 156 |
|
va_list ap; |
| 157 |
|
|
| 166 |
|
}else{ |
| 167 |
|
fprintf(ihost_config.log, "\n"); |
| 168 |
|
} |
| 169 |
+ |
fflush(ihost_config.log); |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
|
| 226 |
|
|
| 227 |
|
if((get_host_addr(hostname, &haddr))!=0){ |
| 228 |
|
log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
| 229 |
+ |
close(sock); |
| 230 |
|
return NULL; |
| 231 |
|
} |
| 232 |
|
|
| 238 |
|
log_msg(LOG_DEBUG, "Creating a tcp connection"); |
| 239 |
|
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
| 240 |
|
log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
| 241 |
+ |
close(sock); |
| 242 |
|
return NULL; |
| 243 |
|
} |
| 244 |
|
|
| 298 |
|
int server_udp_port=0; |
| 299 |
|
time_t config_ttl=0; |
| 300 |
|
|
| 301 |
< |
tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port); |
| 301 |
> |
if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ |
| 302 |
> |
return -1; |
| 303 |
> |
} |
| 304 |
|
|
| 305 |
|
if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
| 306 |
|
if(tcp_con==NULL){ |
| 316 |
|
} |
| 317 |
|
|
| 318 |
|
if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ |
| 319 |
< |
if((tcp_comm(tcp_con, "END", &response, "OK"))==0){ |
| 319 |
> |
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
| 320 |
|
goto error; |
| 321 |
|
} |
| 322 |
|
fclose(tcp_con); |
| 408 |
|
* this already be NULL */ |
| 409 |
|
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
| 410 |
|
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
| 337 |
– |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
| 411 |
|
if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
| 339 |
– |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
| 412 |
|
|
| 413 |
+ |
if(ihost_state->preset_fqdn){ |
| 414 |
+ |
if(host_fqdn != NULL) free(host_fqdn); |
| 415 |
+ |
}else{ |
| 416 |
+ |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
| 417 |
+ |
ihost_state->host_fqdn=host_fqdn; |
| 418 |
+ |
} |
| 419 |
+ |
|
| 420 |
+ |
if(ihost_state->preset_ip){ |
| 421 |
+ |
if(host_ip != NULL) free(host_ip); |
| 422 |
+ |
}else{ |
| 423 |
+ |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
| 424 |
+ |
ihost_state->host_ip=host_ip; |
| 425 |
+ |
} |
| 426 |
+ |
|
| 427 |
+ |
|
| 428 |
|
ihost_state->file_list=file_list; |
| 429 |
|
ihost_state->last_modified=last_modified; |
| 343 |
– |
ihost_state->host_fqdn=host_fqdn; |
| 344 |
– |
ihost_state->host_ip=host_ip; |
| 430 |
|
ihost_state->server_fqdn=server_fqdn; |
| 431 |
|
ihost_state->server_udp_port=server_udp_port; |
| 432 |
|
ihost_state->udp_update_time=udp_update_time; |
| 595 |
|
y=page_stats->pages_pageout; |
| 596 |
|
} |
| 597 |
|
snprintf(tmp, size, \ |
| 598 |
< |
"<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \ |
| 598 |
> |
"<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \ |
| 599 |
|
x, \ |
| 600 |
|
y); |
| 601 |
|
|
| 716 |
|
fprintf(stderr, "Options\n"); |
| 717 |
|
fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); |
| 718 |
|
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
| 719 |
+ |
fprintf(stderr, " -s Set the machine name to be reported as\n"); |
| 720 |
+ |
fprintf(stderr, " -i Set the IP to be reported as\n"); |
| 721 |
|
fprintf(stderr, " -V Print version number\n"); |
| 722 |
|
fprintf(stderr, " -h Prints this help page\n"); |
| 723 |
|
exit(1); |
| 749 |
|
ihost_state.filtermanager_host=NULL; |
| 750 |
|
ihost_state.host_fqdn=NULL; |
| 751 |
|
ihost_state.host_ip=NULL; |
| 752 |
+ |
ihost_state.preset_fqdn = 0; |
| 753 |
+ |
ihost_state.preset_ip = 0; |
| 754 |
|
ihost_state.server_fqdn=NULL; |
| 755 |
|
ihost_state.file_list=NULL; |
| 756 |
|
ihost_state.last_modified=NULL; |
| 757 |
|
|
| 758 |
< |
while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ |
| 758 |
> |
while((cmdopt=getopt(argc, argv, "vfhVs:i:")) != -1){ |
| 759 |
|
switch(cmdopt){ |
| 760 |
|
case 'v': |
| 761 |
|
ihost_config.verbose++; |
| 771 |
|
break; |
| 772 |
|
|
| 773 |
|
case 'V': |
| 774 |
< |
fprintf(stderr, "%s version %s", argv[0], VERSION); |
| 774 |
> |
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
| 775 |
|
break; |
| 776 |
+ |
case 's': |
| 777 |
+ |
ihost_state.preset_fqdn = 1; |
| 778 |
+ |
ihost_state.host_fqdn = strdup(optarg); |
| 779 |
+ |
if(ihost_state.host_fqdn == NULL){ |
| 780 |
+ |
fprintf(stderr, "Missing hostname\n"); |
| 781 |
+ |
usage(argv[0]); |
| 782 |
+ |
} |
| 783 |
+ |
break; |
| 784 |
+ |
case 'i': |
| 785 |
+ |
/* Hmm.. Someone could set any string to be the IP, and it will let it */ |
| 786 |
+ |
ihost_state.preset_ip = 1; |
| 787 |
+ |
ihost_state.host_ip = strdup(optarg); |
| 788 |
+ |
if(ihost_state.host_ip == NULL){ |
| 789 |
+ |
fprintf(stderr, "Missing ip\n"); |
| 790 |
+ |
usage(argv[0]); |
| 791 |
+ |
} |
| 792 |
+ |
break; |
| 793 |
|
|
| 794 |
|
default: |
| 795 |
|
usage(argv[0]); |
| 829 |
|
exit(1); |
| 830 |
|
} |
| 831 |
|
|
| 832 |
< |
if((ihost_config.log=fopen(LOGFILE_PATH, "a"))==NULL){ |
| 832 |
> |
if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
| 833 |
|
ihost_config.log=stderr; |
| 834 |
< |
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOGFILE_PATH); |
| 834 |
> |
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
| 835 |
|
exit(1); |
| 836 |
|
} |
| 837 |
|
|