ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.40
Committed: Sun Apr 6 12:08:37 2003 UTC (21 years, 2 months ago) by pajs
Content type: text/plain
Branch: MAIN
CVS Tags: IHOST_1_5_5
Changes since 1.39: +1 -0 lines
Log Message:
Fixed to compile on freebsd. ihost required an extra header.
configure.in needed -lkvm to be supplied before statgrab and i added
-ldevstat as statgrab requires this also.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 i-scream
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
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>
38
39 #define LOG_CRIT 0
40 #define LOG_ERR 1
41 #define LOG_INFO 2
42 #define LOG_DEBUG 3
43
44 typedef struct{
45 int filtermanager_port;
46 char *filtermanager_host;
47
48 char *host_ip;
49 char *host_fqdn;
50
51 char *server_fqdn;
52 int server_udp_port;
53
54 /* Weird stuff iscream wants sent to it */
55 char *last_modified;
56 char *file_list;
57
58 int udp_update_time;
59 // int config_ttl;
60
61 time_t config_ttl;
62
63 }ihost_state_t;
64
65 typedef struct{
66 int verbose;
67 int daemon;
68
69 FILE *log;
70 }ihost_config_t;
71
72 typedef struct{
73 struct sockaddr_in addr;
74 int sock;
75 }udp_sockinfo_t;
76
77 ihost_config_t ihost_config;
78
79 extern int errno;
80
81 /* Taken from the OpenSSH code. Its licence included in function.*/
82 #ifndef HAVE_STRLCAT
83
84 /*
85 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
86 * All rights reserved.
87 *
88 * Redistribution and use in source and binary forms, with or without
89 * modification, are permitted provided that the following conditions
90 * are met:
91 * 1. Redistributions of source code must retain the above copyright
92 * notice, this list of conditions and the following disclaimer.
93 * 2. Redistributions in binary form must reproduce the above copyright
94 * notice, this list of conditions and the following disclaimer in the
95 * documentation and/or other materials provided with the distribution.
96 * 3. The name of the author may not be used to endorse or promote products
97 * derived from this software without specific prior written permission.
98 *
99 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
100 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
101 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
102 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
103 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
104 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
105 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
106 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
107 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
108 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109 */
110
111 /*
112 * Appends src to string dst of size siz (unlike strncat, siz is the
113 * full size of dst, not space left). At most siz-1 characters
114 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
115 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
116 * If retval >= siz, truncation occurred.
117 */
118 size_t
119 strlcat(dst, src, siz)
120 char *dst;
121 const char *src;
122 size_t siz;
123 {
124 register char *d = dst;
125 register const char *s = src;
126 register size_t n = siz;
127 size_t dlen;
128
129 /* Find the end of dst and adjust bytes left but don't go past end */
130 while (n-- != 0 && *d != '\0')
131 d++;
132 dlen = d - dst;
133 n = siz - dlen;
134
135 if (n == 0)
136 return(dlen + strlen(s));
137 while (*s != '\0') {
138 if (n != 1) {
139 *d++ = *s;
140 n--;
141 }
142 s++;
143 }
144 *d = '\0';
145
146 return(dlen + (s - src)); /* count does not include NUL */
147 }
148
149 #endif
150 /* End strlcat function taken from OpenSSH */
151
152 void log_msg(int level, char *format, ...){
153 int cur_errno;
154 va_list ap;
155
156 cur_errno=errno;
157
158 if(level<=ihost_config.verbose){
159 va_start(ap, format);
160 vfprintf(ihost_config.log, format, ap);
161 va_end(ap);
162 if(level==LOG_CRIT){
163 fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno));
164 }else{
165 fprintf(ihost_config.log, "\n");
166 }
167 fflush(ihost_config.log);
168 }
169 }
170
171 /* Takes many pointers, checks if they are NULL or not, and then free's them */
172 /* Deprciated - and i only wrote it today! :)
173 void m_free(int num_pointers, ...){
174 int x=0;
175 va_list ap;
176 void *p;
177
178 va_start(ap, num_pointers);
179 for(;x<num_pointers;x++){
180 p=va_arg(ap, void*);
181 if(p!=NULL){
182 free(p);
183 }
184 }
185 va_end(ap);
186 }
187 */
188
189 int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){
190
191 struct in_addr haddr;
192
193 log_msg(LOG_DEBUG, "Resolving name for udp connection");
194 if(get_host_addr(hostname, &haddr) != 0){
195 log_msg(LOG_CRIT, "Failed to lookup name");
196 return 1;
197 }
198
199 if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
200 log_msg(LOG_CRIT, "Failed to create UDP socket");
201 return 1;
202 }
203
204 memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in));
205 udp_sockinfo->addr.sin_family=AF_INET;
206 memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr));
207 udp_sockinfo->addr.sin_port = htons(port);
208
209 log_msg(LOG_DEBUG, "Socket created");
210 return 0;
211 }
212
213 FILE *create_tcp_connection(char *hostname, int port){
214 int sock;
215 struct sockaddr_in addr;
216 struct in_addr haddr;
217 FILE *f;
218
219 log_msg(LOG_DEBUG, "Creating tcp socket");
220 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){
221 log_msg(LOG_CRIT, "Failed to make TCP Socket");
222 return NULL;
223 }
224
225 if((get_host_addr(hostname, &haddr))!=0){
226 log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname);
227 close(sock);
228 return NULL;
229 }
230
231 memset(&addr, 0, sizeof(addr));
232 addr.sin_family = AF_INET;
233 memcpy(&addr.sin_addr, &haddr, sizeof(haddr));
234 addr.sin_port = htons(port);
235
236 log_msg(LOG_DEBUG, "Creating a tcp connection");
237 if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){
238 log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port);
239 close(sock);
240 return NULL;
241 }
242
243 if((f=fdopen(sock, "r+"))==NULL){
244 log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection");
245 close(sock);
246 return NULL;
247 }
248
249 return f;
250 }
251
252 int tcp_comm(FILE *f, char *send, char **response, char *expected){
253
254 log_msg(LOG_DEBUG, "Sending %s", send);
255 fprintf(f, "%s\n", send);
256 fflush(f);
257 *response=fpgetline(f);
258 fseek(f, 0, SEEK_CUR);
259
260 if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response);
261
262 if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1;
263
264 if(expected==NULL) return 0;
265
266 if((strcmp(expected, *response))==0) return 0;
267
268 log_msg(LOG_DEBUG, "Did not get expected response");
269 return -1;
270 }
271
272 int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){
273 if((tcp_comm(f, send, response, expected))!=0){
274 return -1;
275 }
276
277 *response=strdup(*response);
278 if (response==NULL) return -1;
279
280 return 0;
281 }
282
283 int ihost_getconfig(ihost_state_t *ihost_state){
284
285 FILE *tcp_con;
286 char *response;
287 char *response_ptr;
288
289 /* Keep these in case of a failure and so it can keep running on the old config */
290 char *file_list=NULL;
291 char *last_modified=NULL;
292 char *host_fqdn=NULL;
293 char *host_ip=NULL;
294 int udp_update_time=0;
295 char *server_fqdn=NULL;
296 int server_udp_port=0;
297 time_t config_ttl=0;
298
299 if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){
300 return -1;
301 }
302
303 if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){
304 if(tcp_con==NULL){
305 goto error;
306 }
307
308 if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){
309 goto error;
310 }
311
312 if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){
313 goto error;
314 }
315
316 if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){
317 if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
318 goto error;
319 }
320 fclose(tcp_con);
321 return 0;
322 }else{
323 if((strcmp(response, "EXPIRED"))!=0){
324 goto error;
325 }
326 }
327 }
328
329 /* If we got to here, the config must of expired */
330
331 if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){
332 goto error;
333 }
334
335 if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){
336 goto error;
337 }
338 last_modified=response;
339
340 if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){
341 goto error;
342 }
343 file_list=response;
344
345 if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){
346 goto error;
347 }
348 host_fqdn=response;
349
350 if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){
351 goto error;
352 }
353 host_ip=response;
354
355 if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){
356 goto error;
357 }
358 udp_update_time=atoi(response);
359
360 if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){
361 goto error;
362 }
363 config_ttl=atoi(response);
364
365 if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){
366 goto error;
367 }
368
369 if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){
370 goto error;
371 }else{
372 response_ptr=strchr(response,';');
373 if(response_ptr==NULL){
374 log_msg(LOG_ERR, "Incorrect data sent by server");
375 goto error;
376 }
377 *response_ptr='\0';
378 server_fqdn=strdup(response);
379 if(server_fqdn==NULL){
380 goto error;
381 }
382 response_ptr++;
383 if(response_ptr==NULL){
384 log_msg(LOG_ERR, "Incorrect data sent by server");
385 goto error;
386 }
387
388 printf("string : %s\n", response_ptr);
389 server_udp_port=atoi(response_ptr);
390
391 if (server_udp_port==0){
392 log_msg(LOG_ERR, "Incorrect data sent by server");
393 goto error;
394 }
395 }
396
397 if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
398 goto error;
399 }
400
401 fclose(tcp_con);
402
403 /* We have the data we need, and its all been read correctly */
404
405 /* Free the old data before pointing them to the new data. m_free copes should
406 * this already be NULL */
407 if(ihost_state->file_list!=NULL) free(ihost_state->file_list);
408 if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
409 if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn);
410 if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
411 if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip);
412
413 ihost_state->file_list=file_list;
414 ihost_state->last_modified=last_modified;
415 ihost_state->host_fqdn=host_fqdn;
416 ihost_state->host_ip=host_ip;
417 ihost_state->server_fqdn=server_fqdn;
418 ihost_state->server_udp_port=server_udp_port;
419 ihost_state->udp_update_time=udp_update_time;
420 ihost_state->config_ttl=config_ttl;
421
422 log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time);
423 log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl);
424
425 return 0;
426
427 error:
428
429 if(file_list!=NULL) free(file_list);
430 if(last_modified!=NULL) free(last_modified);
431 if(host_fqdn!=NULL) free(host_fqdn);
432 if(server_fqdn!=NULL) free(server_fqdn);
433 if(host_ip!=NULL) free(host_ip);
434 fclose(tcp_con);
435
436 return -1;
437 }
438
439 int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){
440 char tmp[size];
441 cpu_percent_t *cpu_percent;
442 mem_stat_t *mem_stats;
443 load_stat_t *load_stats;
444 user_stat_t *user_stats;
445 swap_stat_t *swap_stats;
446 general_stat_t *general_stats;
447 disk_stat_t *disk_stats;
448 diskio_stat_t *diskio_stats;
449 process_stat_t *process_stats;
450 network_stat_t *network_stats;
451 page_stat_t *page_stats;
452 int disk_entries=0;
453 int diskio_entries=0;
454 int network_entries=0;
455
456 int counter;
457 long long x;
458 long long y;
459
460 /* Print start of the packet we want */
461 snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \
462 seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip);
463
464 /* Get cpu stats, check it is correct, then fill in its entry for the xml */
465 if((cpu_percent=cpu_percent_usage())==NULL){
466 log_msg(LOG_CRIT, "Failed to get cpu statistics");
467 }else{
468 snprintf(tmp, size, \
469 "<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \
470 cpu_percent->user, \
471 cpu_percent->kernel, \
472 cpu_percent->idle, \
473 cpu_percent->iowait, \
474 cpu_percent->swap);
475
476 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
477 }
478
479
480 /*Get mem stats, and fill in xml */
481 if((mem_stats=get_memory_stats())==NULL){
482 log_msg(LOG_CRIT, "Failed to get memory statistics");
483 }else{
484 snprintf(tmp, size, \
485 "<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \
486 mem_stats->total, \
487 mem_stats->free, \
488 mem_stats->used, \
489 mem_stats->cache);
490
491 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
492 }
493
494
495 /* Get load stats */
496 if((load_stats=get_load_stats())==NULL){
497 log_msg(LOG_CRIT, "Failed to get load statistics");
498 }else{
499 snprintf(tmp, size, \
500 "<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \
501 load_stats->min1, \
502 load_stats->min5, \
503 load_stats->min15);
504 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
505 }
506
507
508 /* get user stats */
509
510 if((user_stats=get_user_stats())==NULL){
511 log_msg(LOG_CRIT, "Failed to get user statistics");
512 }else{
513
514 snprintf(tmp, size, \
515 "<users><list>%s</list><count>%d</count></users>", \
516 user_stats->name_list, \
517 user_stats->num_entries);
518
519 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
520 }
521
522
523 /* swap stats */
524 if((swap_stats=get_swap_stats())==NULL){
525 log_msg(LOG_CRIT, "Failed to get swap statistics");
526 }else{
527 snprintf(tmp, size, \
528 "<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\
529 swap_stats->total, \
530 swap_stats->used, \
531 swap_stats->free);
532
533 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
534 }
535
536
537 /* general stats */
538
539 if((general_stats=get_general_stats())==NULL){
540 log_msg(LOG_CRIT, "Failed to get general statistics");
541 }else{
542 snprintf(tmp, size, \
543 "<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \
544 general_stats->os_name, \
545 general_stats->os_release, \
546 general_stats->os_version, \
547 general_stats->hostname, \
548 general_stats->platform, \
549 (long)general_stats->uptime);
550
551 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
552
553 }
554
555
556 /* process stats */
557 if((process_stats=get_process_stats())==NULL){
558 log_msg(LOG_CRIT, "Failed to get general statistics");
559 }else{
560 snprintf(tmp, size, \
561 "<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\
562 process_stats->sleeping, \
563 process_stats->running, \
564 process_stats->zombie, \
565 process_stats->stopped, \
566 process_stats->total);
567
568 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
569
570 }
571
572
573 /* Get paging stats */
574 if((page_stats=get_page_stats_diff())==NULL){
575 log_msg(LOG_CRIT, "Failed to get paging statistics");
576 }else{
577 if(page_stats->systime!=0){
578 x=page_stats->pages_pagein / page_stats->systime;
579 y=page_stats->pages_pageout / page_stats->systime;
580 }else{
581 x=page_stats->pages_pagein;
582 y=page_stats->pages_pageout;
583 }
584 snprintf(tmp, size, \
585 "<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \
586 x, \
587 y);
588
589 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
590 }
591
592
593 /* get diskio stats */
594
595 if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){
596 log_msg(LOG_CRIT, "Failed to get diskio statistics");
597 }else{
598 strlcat(xml, "<diskio>", size);
599 for(counter=0;counter<diskio_entries;counter++){
600
601 if(diskio_stats->systime!=0){
602 x=diskio_stats->read_bytes / diskio_stats->systime;
603 y=diskio_stats->write_bytes / diskio_stats->systime;
604 }else{
605 x=diskio_stats->read_bytes;
606 y=diskio_stats->write_bytes;
607 }
608
609 snprintf(tmp, size, \
610 "<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \
611 counter, \
612 diskio_stats->disk_name, \
613 x, \
614 y, \
615 counter);
616
617 strlcat(xml, tmp, size);
618 diskio_stats++;
619 }
620
621 if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error;
622
623 }
624
625
626 /* get networks stats */
627
628 if((network_stats=get_network_stats_diff(&network_entries))==NULL){
629 log_msg(LOG_CRIT, "Failed to get network statistics");
630 }else{
631 strlcat(xml, "<net>", size);
632 for(counter=0;counter<network_entries;counter++){
633 if(network_stats->systime!=0){
634 x=network_stats->rx / network_stats->systime;
635 y=network_stats->tx / network_stats->systime;
636 }else{
637 x=network_stats->rx;
638 y=network_stats->tx;
639 }
640
641 snprintf(tmp, size, \
642 "<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \
643 counter, \
644 network_stats->interface_name, \
645 x, \
646 y, \
647 counter);
648
649 strlcat(xml, tmp, size);
650 network_stats++;
651 }
652
653 if(strlcat(xml, "</net>", size) >= size) goto too_big_error;
654
655 }
656
657
658 /* get disk stats */
659
660 if((disk_stats=get_disk_stats(&disk_entries))==NULL){
661 log_msg(LOG_CRIT, "Failed to get disk statistics");
662 }else{
663 strlcat(xml, "<disk>", size);
664 for(counter=0;counter<disk_entries;counter++){
665 snprintf(tmp, size, \
666 "<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \
667 counter, \
668 disk_stats->device_name, \
669 disk_stats->mnt_point, \
670 disk_stats->fs_type, \
671 disk_stats->size, \
672 disk_stats->used, \
673 disk_stats->avail, \
674 disk_stats->total_inodes, \
675 disk_stats->used_inodes, \
676 disk_stats->free_inodes, \
677 counter);
678
679 strlcat(xml, tmp, size);
680
681 disk_stats++;
682 }
683
684 if(strlcat(xml, "</disk>", size) >= size) goto too_big_error;
685
686 }
687
688
689 if(strlcat(xml, "</packet>", size) >= size) goto too_big_error;
690
691 /*If we got to here, it should of all been filled in nicely now */
692 return 0;
693
694 too_big_error:
695 log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet");
696 return -1;
697 }
698
699
700
701 void usage(char *progname){
702 fprintf(stderr, "Usage %s [options] server port\n", progname);
703 fprintf(stderr, "Options\n");
704 fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n");
705 fprintf(stderr, " -f Foreground mode, print errors to stderr\n");
706 fprintf(stderr, " -V Print version number\n");
707 fprintf(stderr, " -h Prints this help page\n");
708 exit(1);
709 }
710
711 int main(int argc, char **argv){
712
713 ihost_state_t ihost_state;
714 udp_sockinfo_t udp_sockinfo;
715
716 int cmdopt;
717 extern int optind;
718 pid_t pid;
719 FILE *f;
720 int packet_num=0;
721 int len;
722
723 char packet[MAX_UDP_PACKET_SIZE];
724
725 time_t cur_time, sleep_delay, udp_time=0, config_time=0;
726
727 /* Set default settings */
728 ihost_config.verbose=1;
729 ihost_config.daemon=1;
730 /* Set all errors to go down stderr until told otherwise */
731 ihost_config.log=stderr;
732
733 /* Blank ihost_state to default settings */
734 ihost_state.filtermanager_host=NULL;
735 ihost_state.host_fqdn=NULL;
736 ihost_state.host_ip=NULL;
737 ihost_state.server_fqdn=NULL;
738 ihost_state.file_list=NULL;
739 ihost_state.last_modified=NULL;
740
741 while((cmdopt=getopt(argc, argv, "vfhV")) != -1){
742 switch(cmdopt){
743 case 'v':
744 ihost_config.verbose++;
745 break;
746
747 case 'f':
748 /* Force syslog logging since stderr will be closed in this case */
749 ihost_config.daemon=0;
750 break;
751
752 case 'h':
753 usage(argv[0]);
754 break;
755
756 case 'V':
757 fprintf(stderr, "%s version %s\n", argv[0], VERSION);
758 break;
759
760 default:
761 usage(argv[0]);
762 exit(1);
763 }
764 }
765
766 if(argc!=optind+2){
767 usage(argv[0]);
768 exit(1);
769 }
770
771 ihost_state.filtermanager_host=strdup(argv[optind]);
772 ihost_state.filtermanager_port=atoi(argv[optind+1]);
773
774 if(gethostbyname(ihost_state.filtermanager_host)==NULL){
775 log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings");
776 exit(1);
777 }
778 if(ihost_state.filtermanager_port==0){
779 log_msg(LOG_ERR, "Invalid port number");
780 exit(1);
781 }
782
783 if(ihost_config.daemon){
784 pid=fork();
785 if(pid==-1){
786 log_msg(LOG_CRIT, "Failed to background exiting");
787 exit(1);
788 }else if(pid!=0){
789 /* Parent process */
790 return 0;
791 }
792 /* We should now be in the background*/
793 if(setsid()==-1){
794 log_msg(LOG_CRIT, "setsid failed");
795 exit(1);
796 }
797
798 if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){
799 ihost_config.log=stderr;
800 log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE);
801 exit(1);
802 }
803
804 fclose(stdin);
805 fclose(stdout);
806 fclose(stderr);
807
808 }
809
810 log_msg(LOG_INFO, "Starting ihost");
811
812 log_msg(LOG_DEBUG,"Writing PID FILE");
813
814 pid=getpid();
815
816 if((f=fopen(PID_FILE,"w")) == NULL){
817 log_msg(LOG_CRIT, "Failed to write PID file");
818 }else{
819 if((fprintf(f,"%d",(int)pid)) <= 0 ){
820 log_msg(LOG_CRIT, "Failed to write PID file");
821 }
822 if((fclose(f))!=0){
823 log_msg(LOG_CRIT, "failed to close PID file");
824 }
825 }
826
827 /* Get the initial config from the filter manager. Should this fail,
828 * wait, and then try again. */
829
830 get_diskio_stats_diff(&packet_num);
831 packet_num=0;
832
833 while(ihost_getconfig(&ihost_state)!=0){
834 log_msg(LOG_ERR, "Failed to get ihost config");
835 sleep(10);
836 }
837
838 printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port);
839 while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
840 log_msg(LOG_ERR, "Failed to create udp socket");
841 sleep(10);
842 }
843
844 config_time=time(NULL)+ihost_state.config_ttl;
845
846 /* Now have config.. collect data and send as often as required */
847 for(;;){
848 cur_time=time(NULL);
849
850 if(cur_time>=udp_time){
851 if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){
852 log_msg(LOG_ERR, "Failed to get system stats");
853 }
854
855 len=strlen(packet);
856 log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet);
857
858 if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){
859 log_msg(LOG_CRIT, "Failed to send packet");
860 }
861 udp_time=cur_time+ihost_state.udp_update_time;
862 log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time);
863 }
864
865 if(cur_time>=config_time){
866 if(ihost_getconfig(&ihost_state)!=0){
867 /* If we can't get the config, try again 5 minutes time */
868 log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time");
869 config_time=time(NULL)+300;
870 }else{
871 close(udp_sockinfo.sock);
872
873 while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
874 log_msg(LOG_CRIT, "Failed to create udp socket");
875 sleep(10);
876 }
877
878 config_time=time(NULL)+ihost_state.config_ttl;
879
880 log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl);
881 }
882 }
883
884 sleep_delay=udp_time-time(NULL);
885 log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay);
886 if(sleep_delay>0) sleep(sleep_delay);
887 }
888
889 return(0);
890 }