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" |
36 |
> |
#include <ukcprog.h> |
37 |
> |
#include <statgrab.h> |
38 |
|
|
39 |
|
#define LOG_CRIT 0 |
40 |
|
#define LOG_ERR 1 |
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, ...){ |
79 |
– |
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 |
|
|
97 |
– |
/* Takes many pointers, checks if they are NULL or not, and then free's them */ |
98 |
– |
/* Deprciated - and i only wrote it today! :) |
99 |
– |
void m_free(int num_pointers, ...){ |
100 |
– |
int x=0; |
101 |
– |
va_list ap; |
102 |
– |
void *p; |
103 |
– |
|
104 |
– |
va_start(ap, num_pointers); |
105 |
– |
for(;x<num_pointers;x++){ |
106 |
– |
p=va_arg(ap, void*); |
107 |
– |
if(p!=NULL){ |
108 |
– |
free(p); |
109 |
– |
} |
110 |
– |
} |
111 |
– |
va_end(ap); |
112 |
– |
} |
113 |
– |
*/ |
114 |
– |
|
173 |
|
int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ |
174 |
|
|
175 |
|
struct in_addr haddr; |
208 |
|
|
209 |
|
if((get_host_addr(hostname, &haddr))!=0){ |
210 |
|
log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
211 |
+ |
close(sock); |
212 |
|
return NULL; |
213 |
|
} |
214 |
|
|
220 |
|
log_msg(LOG_DEBUG, "Creating a tcp connection"); |
221 |
|
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
222 |
|
log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
223 |
+ |
close(sock); |
224 |
|
return NULL; |
225 |
|
} |
226 |
|
|
281 |
|
time_t config_ttl=0; |
282 |
|
|
283 |
|
if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ |
284 |
< |
goto error; |
284 |
> |
return -1; |
285 |
|
} |
286 |
|
|
287 |
|
if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
369 |
|
goto error; |
370 |
|
} |
371 |
|
|
312 |
– |
printf("string : %s\n", response_ptr); |
372 |
|
server_udp_port=atoi(response_ptr); |
373 |
|
|
374 |
|
if (server_udp_port==0){ |
389 |
|
* this already be NULL */ |
390 |
|
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
391 |
|
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
333 |
– |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
392 |
|
if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
335 |
– |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
393 |
|
|
394 |
+ |
if(ihost_state->preset_fqdn){ |
395 |
+ |
if(host_fqdn != NULL) free(host_fqdn); |
396 |
+ |
}else{ |
397 |
+ |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
398 |
+ |
ihost_state->host_fqdn=host_fqdn; |
399 |
+ |
} |
400 |
+ |
|
401 |
+ |
if(ihost_state->preset_ip){ |
402 |
+ |
if(host_ip != NULL) free(host_ip); |
403 |
+ |
}else{ |
404 |
+ |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
405 |
+ |
ihost_state->host_ip=host_ip; |
406 |
+ |
} |
407 |
+ |
|
408 |
+ |
|
409 |
|
ihost_state->file_list=file_list; |
410 |
|
ihost_state->last_modified=last_modified; |
339 |
– |
ihost_state->host_fqdn=host_fqdn; |
340 |
– |
ihost_state->host_ip=host_ip; |
411 |
|
ihost_state->server_fqdn=server_fqdn; |
412 |
|
ihost_state->server_udp_port=server_udp_port; |
413 |
|
ihost_state->udp_update_time=udp_update_time; |
576 |
|
y=page_stats->pages_pageout; |
577 |
|
} |
578 |
|
snprintf(tmp, size, \ |
579 |
< |
"<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \ |
579 |
> |
"<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \ |
580 |
|
x, \ |
581 |
|
y); |
582 |
|
|
693 |
|
|
694 |
|
|
695 |
|
void usage(char *progname){ |
696 |
< |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
697 |
< |
fprintf(stderr, "Options\n"); |
698 |
< |
fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); |
699 |
< |
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
700 |
< |
fprintf(stderr, " -V Print version number\n"); |
701 |
< |
fprintf(stderr, " -h Prints this help page\n"); |
696 |
> |
fprintf(stderr, "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n", progname); |
697 |
> |
fprintf(stderr, " -v Verbose mode, -vv would make even more verbose\n"); |
698 |
> |
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
699 |
> |
fprintf(stderr, " -n Set the machine name to be reported as\n"); |
700 |
> |
fprintf(stderr, " -i Set the IP to be reported as\n"); |
701 |
> |
fprintf(stderr, " -s Specifies the i-scream server to connect to\n"); |
702 |
> |
fprintf(stderr, " default: %s\n", DEF_SERVER_NAME); |
703 |
> |
fprintf(stderr, " -p Specifies the i-scream server port\n"); |
704 |
> |
fprintf(stderr, " default: %d\n", DEF_SERVER_PORT); |
705 |
> |
fprintf(stderr, " -V Print version number\n"); |
706 |
> |
fprintf(stderr, " -h Prints this help page\n"); |
707 |
> |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
708 |
|
exit(1); |
709 |
|
} |
710 |
|
|
731 |
|
ihost_config.log=stderr; |
732 |
|
|
733 |
|
/* Blank ihost_state to default settings */ |
734 |
< |
ihost_state.filtermanager_host=NULL; |
734 |
> |
ihost_state.filtermanager_host=DEF_SERVER_NAME; |
735 |
> |
ihost_state.filtermanager_port=DEF_SERVER_PORT; |
736 |
|
ihost_state.host_fqdn=NULL; |
737 |
|
ihost_state.host_ip=NULL; |
738 |
+ |
ihost_state.preset_fqdn = 0; |
739 |
+ |
ihost_state.preset_ip = 0; |
740 |
|
ihost_state.server_fqdn=NULL; |
741 |
|
ihost_state.file_list=NULL; |
742 |
|
ihost_state.last_modified=NULL; |
743 |
|
|
744 |
< |
while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ |
744 |
> |
while((cmdopt=getopt(argc, argv, "vfVn:i:s:p:h")) != -1){ |
745 |
|
switch(cmdopt){ |
746 |
|
case 'v': |
747 |
|
ihost_config.verbose++; |
752 |
|
ihost_config.daemon=0; |
753 |
|
break; |
754 |
|
|
676 |
– |
case 'h': |
677 |
– |
usage(argv[0]); |
678 |
– |
break; |
679 |
– |
|
755 |
|
case 'V': |
756 |
|
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
757 |
|
break; |
758 |
+ |
case 'n': |
759 |
+ |
ihost_state.preset_fqdn = 1; |
760 |
+ |
ihost_state.host_fqdn = strdup(optarg); |
761 |
+ |
if(ihost_state.host_fqdn == NULL){ |
762 |
+ |
fprintf(stderr, "Missing hostname\n"); |
763 |
+ |
usage(argv[0]); |
764 |
+ |
} |
765 |
+ |
break; |
766 |
+ |
case 'i': |
767 |
+ |
/* Hmm.. Someone could set any string to be the IP, and it will let it */ |
768 |
+ |
ihost_state.preset_ip = 1; |
769 |
+ |
ihost_state.host_ip = strdup(optarg); |
770 |
+ |
if(ihost_state.host_ip == NULL){ |
771 |
+ |
fprintf(stderr, "Missing ip\n"); |
772 |
+ |
usage(argv[0]); |
773 |
+ |
} |
774 |
+ |
break; |
775 |
|
|
776 |
+ |
case 's': |
777 |
+ |
ihost_state.filtermanager_host=strdup(optarg); |
778 |
+ |
break; |
779 |
+ |
|
780 |
+ |
case 'p': |
781 |
+ |
ihost_state.filtermanager_port=atoi(optarg); |
782 |
+ |
break; |
783 |
+ |
|
784 |
+ |
case 'h': |
785 |
|
default: |
786 |
|
usage(argv[0]); |
787 |
|
exit(1); |
788 |
|
} |
789 |
|
} |
790 |
|
|
690 |
– |
if(argc!=optind+2){ |
691 |
– |
usage(argv[0]); |
692 |
– |
exit(1); |
693 |
– |
} |
694 |
– |
|
695 |
– |
ihost_state.filtermanager_host=strdup(argv[optind]); |
696 |
– |
ihost_state.filtermanager_port=atoi(argv[optind+1]); |
697 |
– |
|
791 |
|
if(gethostbyname(ihost_state.filtermanager_host)==NULL){ |
792 |
|
log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); |
793 |
|
exit(1); |
824 |
|
|
825 |
|
} |
826 |
|
|
827 |
< |
log_msg(LOG_INFO, "Starting ihost"); |
827 |
> |
log_msg(LOG_INFO, "Starting ihost..."); |
828 |
> |
|
829 |
> |
log_msg(LOG_DEBUG, "Running statgrab_init()"); |
830 |
> |
if(!statgrab_init()){ |
831 |
> |
log_msg(LOG_CRIT, "statgrab_init failed (%m)"); |
832 |
> |
exit(1); |
833 |
> |
} |
834 |
|
|
835 |
|
log_msg(LOG_DEBUG,"Writing PID FILE"); |
836 |
|
|
858 |
|
sleep(10); |
859 |
|
} |
860 |
|
|
762 |
– |
printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); |
861 |
|
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
862 |
|
log_msg(LOG_ERR, "Failed to create udp socket"); |
863 |
|
sleep(10); |