ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.29
Committed: Mon Mar 3 12:32:35 2003 UTC (21 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: IHOST_1_5
Changes since 1.28: +4 -10 lines
Log Message:
Following up on Pete's commit of the new ihost - the new configure stuff.
Also dropped the old libstatgrab.

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
35 #include "ukcprog.h"
36 #include "statgrab.h"
37
38 #define LOG_CRIT 0
39 #define LOG_ERR 1
40 #define LOG_INFO 2
41 #define LOG_DEBUG 3
42
43 typedef struct{
44 int filtermanager_port;
45 char *filtermanager_host;
46
47 char *host_ip;
48 char *host_fqdn;
49
50 char *server_fqdn;
51 int server_udp_port;
52
53 /* Weird stuff iscream wants sent to it */
54 char *last_modified;
55 char *file_list;
56
57 int udp_update_time;
58 // int config_ttl;
59
60 time_t config_ttl;
61
62 }ihost_state_t;
63
64 typedef struct{
65 int verbose;
66 int daemon;
67
68 FILE *log;
69 }ihost_config_t;
70
71 typedef struct{
72 struct sockaddr_in addr;
73 int sock;
74 }udp_sockinfo_t;
75
76 ihost_config_t ihost_config;
77
78 void log_msg(int level, char *format, ...){
79 extern int errno;
80 int cur_errno;
81 va_list ap;
82
83 cur_errno=errno;
84
85 if(level<=ihost_config.verbose){
86 va_start(ap, format);
87 vfprintf(ihost_config.log, format, ap);
88 va_end(ap);
89 if(level==LOG_CRIT){
90 fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno));
91 }else{
92 fprintf(ihost_config.log, "\n");
93 }
94 }
95 }
96
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
115 int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){
116
117 struct in_addr haddr;
118
119 log_msg(LOG_DEBUG, "Resolving name for udp connection");
120 if(get_host_addr(hostname, &haddr) != 0){
121 log_msg(LOG_CRIT, "Failed to lookup name");
122 return 1;
123 }
124
125 if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
126 log_msg(LOG_CRIT, "Failed to create UDP socket");
127 return 1;
128 }
129
130 memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in));
131 udp_sockinfo->addr.sin_family=AF_INET;
132 memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr));
133 udp_sockinfo->addr.sin_port = htons(port);
134
135 log_msg(LOG_DEBUG, "Socket created");
136 return 0;
137 }
138
139 FILE *create_tcp_connection(char *hostname, int port){
140 int sock;
141 struct sockaddr_in addr;
142 struct in_addr haddr;
143 FILE *f;
144
145 log_msg(LOG_DEBUG, "Creating tcp socket");
146 if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){
147 log_msg(LOG_CRIT, "Failed to make TCP Socket");
148 return NULL;
149 }
150
151 if((get_host_addr(hostname, &haddr))!=0){
152 log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname);
153 return NULL;
154 }
155
156 memset(&addr, 0, sizeof(addr));
157 addr.sin_family = AF_INET;
158 memcpy(&addr.sin_addr, &haddr, sizeof(haddr));
159 addr.sin_port = htons(port);
160
161 log_msg(LOG_DEBUG, "Creating a tcp connection");
162 if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){
163 log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port);
164 return NULL;
165 }
166
167 if((f=fdopen(sock, "r+"))==NULL){
168 log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection");
169 close(sock);
170 return NULL;
171 }
172
173 return f;
174 }
175
176 int tcp_comm(FILE *f, char *send, char **response, char *expected){
177
178 log_msg(LOG_DEBUG, "Sending %s", send);
179 fprintf(f, "%s\n", send);
180 fflush(f);
181 *response=fpgetline(f);
182 fseek(f, 0, SEEK_CUR);
183
184 if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response);
185
186 if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1;
187
188 if(expected==NULL) return 0;
189
190 if((strcmp(expected, *response))==0) return 0;
191
192 log_msg(LOG_DEBUG, "Did not get expected response");
193 return -1;
194 }
195
196 int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){
197 if((tcp_comm(f, send, response, expected))!=0){
198 return -1;
199 }
200
201 *response=strdup(*response);
202 if (response==NULL) return -1;
203
204 return 0;
205 }
206
207 int ihost_getconfig(ihost_state_t *ihost_state){
208
209 FILE *tcp_con;
210 char *response;
211 char *response_ptr;
212
213 /* Keep these in case of a failure and so it can keep running on the old config */
214 char *file_list=NULL;
215 char *last_modified=NULL;
216 char *host_fqdn=NULL;
217 char *host_ip=NULL;
218 int udp_update_time=0;
219 char *server_fqdn=NULL;
220 int server_udp_port=0;
221 time_t config_ttl=0;
222
223 tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port);
224
225 if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){
226 if(tcp_con==NULL){
227 goto error;
228 }
229
230 if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){
231 goto error;
232 }
233
234 if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){
235 goto error;
236 }
237
238 if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){
239 if((tcp_comm(tcp_con, "END", &response, "OK"))==0){
240 goto error;
241 }
242 fclose(tcp_con);
243 return 0;
244 }else{
245 if((strcmp(response, "EXPIRED"))!=0){
246 goto error;
247 }
248 }
249 }
250
251 /* If we got to here, the config must of expired */
252
253 if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){
254 goto error;
255 }
256
257 if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){
258 goto error;
259 }
260 last_modified=response;
261
262 if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){
263 goto error;
264 }
265 file_list=response;
266
267 if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){
268 goto error;
269 }
270 host_fqdn=response;
271
272 if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){
273 goto error;
274 }
275 host_ip=response;
276
277 if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){
278 goto error;
279 }
280 udp_update_time=atoi(response);
281
282 if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){
283 goto error;
284 }
285 config_ttl=atoi(response);
286
287 if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){
288 goto error;
289 }
290
291 if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){
292 goto error;
293 }else{
294 response_ptr=strchr(response,';');
295 if(response_ptr==NULL){
296 log_msg(LOG_ERR, "Incorrect data sent by server");
297 goto error;
298 }
299 *response_ptr='\0';
300 server_fqdn=strdup(response);
301 if(server_fqdn==NULL){
302 goto error;
303 }
304 response_ptr++;
305 if(response_ptr==NULL){
306 log_msg(LOG_ERR, "Incorrect data sent by server");
307 goto error;
308 }
309
310 printf("string : %s\n", response_ptr);
311 server_udp_port=atoi(response_ptr);
312
313 if (server_udp_port==0){
314 log_msg(LOG_ERR, "Incorrect data sent by server");
315 goto error;
316 }
317 }
318
319 if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
320 goto error;
321 }
322
323 fclose(tcp_con);
324
325 /* We have the data we need, and its all been read correctly */
326
327 /* Free the old data before pointing them to the new data. m_free copes should
328 * this already be NULL */
329 if(ihost_state->file_list!=NULL) free(ihost_state->file_list);
330 if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
331 if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn);
332 if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
333 if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip);
334
335 ihost_state->file_list=file_list;
336 ihost_state->last_modified=last_modified;
337 ihost_state->host_fqdn=host_fqdn;
338 ihost_state->host_ip=host_ip;
339 ihost_state->server_fqdn=server_fqdn;
340 ihost_state->server_udp_port=server_udp_port;
341 ihost_state->udp_update_time=udp_update_time;
342 ihost_state->config_ttl=config_ttl;
343
344 log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time);
345 log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl);
346
347 return 0;
348
349 error:
350
351 if(file_list!=NULL) free(file_list);
352 if(last_modified!=NULL) free(last_modified);
353 if(host_fqdn!=NULL) free(host_fqdn);
354 if(server_fqdn!=NULL) free(server_fqdn);
355 if(host_ip!=NULL) free(host_ip);
356 fclose(tcp_con);
357
358 return -1;
359 }
360
361 int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){
362 char tmp[size];
363 cpu_percent_t *cpu_percent;
364 mem_stat_t *mem_stats;
365 load_stat_t *load_stats;
366 user_stat_t *user_stats;
367 swap_stat_t *swap_stats;
368 general_stat_t *general_stats;
369 disk_stat_t *disk_stats;
370 diskio_stat_t *diskio_stats;
371 process_stat_t *process_stats;
372 network_stat_t *network_stats;
373 page_stat_t *page_stats;
374 int disk_entries=0;
375 int diskio_entries=0;
376 int network_entries=0;
377
378 int counter;
379 long long x;
380 long long y;
381
382 /* Print start of the packet we want */
383 snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \
384 seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip);
385
386 /* Get cpu stats, check it is correct, then fill in its entry for the xml */
387 if((cpu_percent=cpu_percent_usage())==NULL){
388 log_msg(LOG_CRIT, "Failed to get cpu statistics");
389 }else{
390 snprintf(tmp, size, \
391 "<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \
392 cpu_percent->user, \
393 cpu_percent->kernel, \
394 cpu_percent->idle, \
395 cpu_percent->iowait, \
396 cpu_percent->swap);
397
398 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
399 }
400
401
402 /*Get mem stats, and fill in xml */
403 if((mem_stats=get_memory_stats())==NULL){
404 log_msg(LOG_CRIT, "Failed to get memory statistics");
405 }else{
406 snprintf(tmp, size, \
407 "<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \
408 mem_stats->total, \
409 mem_stats->free, \
410 mem_stats->used, \
411 mem_stats->cache);
412
413 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
414 }
415
416
417 /* Get load stats */
418 if((load_stats=get_load_stats())==NULL){
419 log_msg(LOG_CRIT, "Failed to get load statistics");
420 }else{
421 snprintf(tmp, size, \
422 "<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \
423 load_stats->min1, \
424 load_stats->min5, \
425 load_stats->min15);
426 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
427 }
428
429
430 /* get user stats */
431
432 if((user_stats=get_user_stats())==NULL){
433 log_msg(LOG_CRIT, "Failed to get user statistics");
434 }else{
435
436 snprintf(tmp, size, \
437 "<users><list>%s</list><count>%d</count></users>", \
438 user_stats->name_list, \
439 user_stats->num_entries);
440
441 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
442 }
443
444
445 /* swap stats */
446 if((swap_stats=get_swap_stats())==NULL){
447 log_msg(LOG_CRIT, "Failed to get swap statistics");
448 }else{
449 snprintf(tmp, size, \
450 "<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\
451 swap_stats->total, \
452 swap_stats->used, \
453 swap_stats->free);
454
455 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
456 }
457
458
459 /* general stats */
460
461 if((general_stats=get_general_stats())==NULL){
462 log_msg(LOG_CRIT, "Failed to get general statistics");
463 }else{
464 snprintf(tmp, size, \
465 "<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \
466 general_stats->os_name, \
467 general_stats->os_release, \
468 general_stats->os_version, \
469 general_stats->hostname, \
470 general_stats->platform, \
471 (long)general_stats->uptime);
472
473 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
474
475 }
476
477
478 /* process stats */
479 if((process_stats=get_process_stats())==NULL){
480 log_msg(LOG_CRIT, "Failed to get general statistics");
481 }else{
482 snprintf(tmp, size, \
483 "<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\
484 process_stats->sleeping, \
485 process_stats->running, \
486 process_stats->zombie, \
487 process_stats->stopped, \
488 process_stats->total);
489
490 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
491
492 }
493
494
495 /* Get paging stats */
496 if((page_stats=get_page_stats_diff())==NULL){
497 log_msg(LOG_CRIT, "Failed to get paging statistics");
498 }else{
499 if(page_stats->systime!=0){
500 x=page_stats->pages_pagein / page_stats->systime;
501 y=page_stats->pages_pageout / page_stats->systime;
502 }else{
503 x=page_stats->pages_pagein;
504 y=page_stats->pages_pageout;
505 }
506 snprintf(tmp, size, \
507 "<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \
508 x, \
509 y);
510
511 if(strlcat(xml, tmp, size) >= size) goto too_big_error;
512 }
513
514
515 /* get diskio stats */
516
517 if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){
518 log_msg(LOG_CRIT, "Failed to get diskio statistics");
519 }else{
520 strlcat(xml, "<diskio>", size);
521 for(counter=0;counter<diskio_entries;counter++){
522
523 if(diskio_stats->systime!=0){
524 x=diskio_stats->read_bytes / diskio_stats->systime;
525 y=diskio_stats->write_bytes / diskio_stats->systime;
526 }else{
527 x=diskio_stats->read_bytes;
528 y=diskio_stats->write_bytes;
529 }
530
531 snprintf(tmp, size, \
532 "<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \
533 counter, \
534 diskio_stats->disk_name, \
535 x, \
536 y, \
537 counter);
538
539 strlcat(xml, tmp, size);
540 diskio_stats++;
541 }
542
543 if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error;
544
545 }
546
547
548 /* get networks stats */
549
550 if((network_stats=get_network_stats_diff(&network_entries))==NULL){
551 log_msg(LOG_CRIT, "Failed to get network statistics");
552 }else{
553 strlcat(xml, "<net>", size);
554 for(counter=0;counter<network_entries;counter++){
555 if(network_stats->systime!=0){
556 x=network_stats->rx / network_stats->systime;
557 y=network_stats->tx / network_stats->systime;
558 }else{
559 x=network_stats->rx;
560 y=network_stats->tx;
561 }
562
563 snprintf(tmp, size, \
564 "<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \
565 counter, \
566 network_stats->interface_name, \
567 x, \
568 y, \
569 counter);
570
571 strlcat(xml, tmp, size);
572 network_stats++;
573 }
574
575 if(strlcat(xml, "</net>", size) >= size) goto too_big_error;
576
577 }
578
579
580 /* get disk stats */
581
582 if((disk_stats=get_disk_stats(&disk_entries))==NULL){
583 log_msg(LOG_CRIT, "Failed to get disk statistics");
584 }else{
585 strlcat(xml, "<disk>", size);
586 for(counter=0;counter<disk_entries;counter++){
587 snprintf(tmp, size, \
588 "<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \
589 counter, \
590 disk_stats->device_name, \
591 disk_stats->mnt_point, \
592 disk_stats->fs_type, \
593 disk_stats->size, \
594 disk_stats->used, \
595 disk_stats->avail, \
596 disk_stats->total_inodes, \
597 disk_stats->used_inodes, \
598 disk_stats->free_inodes, \
599 counter);
600
601 strlcat(xml, tmp, size);
602
603 disk_stats++;
604 }
605
606 if(strlcat(xml, "</disk>", size) >= size) goto too_big_error;
607
608 }
609
610
611 if(strlcat(xml, "</packet>", size) >= size) goto too_big_error;
612
613 /*If we got to here, it should of all been filled in nicely now */
614 return 0;
615
616 too_big_error:
617 log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet");
618 return -1;
619 }
620
621
622
623 void usage(char *progname){
624 fprintf(stderr, "Usage %s [options] server port\n", progname);
625 fprintf(stderr, "Options\n");
626 fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n");
627 fprintf(stderr, " -f Foreground mode, print errors to stderr\n");
628 fprintf(stderr, " -V Print version number\n");
629 fprintf(stderr, " -h Prints this help page\n");
630 exit(1);
631 }
632
633 int main(int argc, char **argv){
634
635 ihost_state_t ihost_state;
636 udp_sockinfo_t udp_sockinfo;
637
638 int cmdopt;
639 extern int optind;
640 pid_t pid;
641 FILE *f;
642 int packet_num=0;
643 int len;
644
645 char packet[MAX_UDP_PACKET_SIZE];
646
647 time_t cur_time, sleep_delay, udp_time=0, config_time=0;
648
649 /* Set default settings */
650 ihost_config.verbose=1;
651 ihost_config.daemon=1;
652 /* Set all errors to go down stderr until told otherwise */
653 ihost_config.log=stderr;
654
655 /* Blank ihost_state to default settings */
656 ihost_state.filtermanager_host=NULL;
657 ihost_state.host_fqdn=NULL;
658 ihost_state.host_ip=NULL;
659 ihost_state.server_fqdn=NULL;
660 ihost_state.file_list=NULL;
661 ihost_state.last_modified=NULL;
662
663 while((cmdopt=getopt(argc, argv, "vfhV")) != -1){
664 switch(cmdopt){
665 case 'v':
666 ihost_config.verbose++;
667 break;
668
669 case 'f':
670 /* Force syslog logging since stderr will be closed in this case */
671 ihost_config.daemon=0;
672 break;
673
674 case 'h':
675 usage(argv[0]);
676 break;
677
678 case 'V':
679 fprintf(stderr, "%s version %s\n", argv[0], VERSION);
680 break;
681
682 default:
683 usage(argv[0]);
684 exit(1);
685 }
686 }
687
688 if(argc!=optind+2){
689 usage(argv[0]);
690 exit(1);
691 }
692
693 ihost_state.filtermanager_host=strdup(argv[optind]);
694 ihost_state.filtermanager_port=atoi(argv[optind+1]);
695
696 if(gethostbyname(ihost_state.filtermanager_host)==NULL){
697 log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings");
698 exit(1);
699 }
700 if(ihost_state.filtermanager_port==0){
701 log_msg(LOG_ERR, "Invalid port number");
702 exit(1);
703 }
704
705 if(ihost_config.daemon){
706 pid=fork();
707 if(pid==-1){
708 log_msg(LOG_CRIT, "Failed to background exiting");
709 exit(1);
710 }else if(pid!=0){
711 /* Parent process */
712 return 0;
713 }
714 /* We should now be in the background*/
715 if(setsid()==-1){
716 log_msg(LOG_CRIT, "setsid failed");
717 exit(1);
718 }
719
720 if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){
721 ihost_config.log=stderr;
722 log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE);
723 exit(1);
724 }
725
726 fclose(stdin);
727 fclose(stdout);
728 fclose(stderr);
729
730 }
731
732 log_msg(LOG_INFO, "Starting ihost");
733
734 log_msg(LOG_DEBUG,"Writing PID FILE");
735
736 pid=getpid();
737
738 if((f=fopen(PID_FILE,"w")) == NULL){
739 log_msg(LOG_CRIT, "Failed to write PID file");
740 }else{
741 if((fprintf(f,"%d",(int)pid)) <= 0 ){
742 log_msg(LOG_CRIT, "Failed to write PID file");
743 }
744 if((fclose(f))!=0){
745 log_msg(LOG_CRIT, "failed to close PID file");
746 }
747 }
748
749 /* Get the initial config from the filter manager. Should this fail,
750 * wait, and then try again. */
751
752 get_diskio_stats_diff(&packet_num);
753 packet_num=0;
754
755 while(ihost_getconfig(&ihost_state)!=0){
756 log_msg(LOG_ERR, "Failed to get ihost config");
757 sleep(10);
758 }
759
760 printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port);
761 while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
762 log_msg(LOG_ERR, "Failed to create udp socket");
763 sleep(10);
764 }
765
766 config_time=time(NULL)+ihost_state.config_ttl;
767
768 /* Now have config.. collect data and send as often as required */
769 for(;;){
770 cur_time=time(NULL);
771
772 if(cur_time>=udp_time){
773 if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){
774 log_msg(LOG_ERR, "Failed to get system stats");
775 }
776
777 len=strlen(packet);
778 log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet);
779
780 if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){
781 log_msg(LOG_CRIT, "Failed to send packet");
782 }
783 udp_time=cur_time+ihost_state.udp_update_time;
784 log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time);
785 }
786
787 if(cur_time>=config_time){
788 if(ihost_getconfig(&ihost_state)!=0){
789 /* If we can't get the config, try again 5 minutes time */
790 log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time");
791 config_time=time(NULL)+300;
792 }else{
793 close(udp_sockinfo.sock);
794
795 while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){
796 log_msg(LOG_CRIT, "Failed to create udp socket");
797 sleep(10);
798 }
799
800 config_time=time(NULL)+ihost_state.config_ttl;
801
802 log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl);
803 }
804 }
805
806 sleep_delay=udp_time-time(NULL);
807 log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay);
808 if(sleep_delay>0) sleep(sleep_delay);
809 }
810
811 return(0);
812 }