ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.28
Committed: Mon Mar 3 12:18:35 2003 UTC (21 years, 3 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.27: +669 -496 lines
Log Message:
New re-written ihost to use the new libstatgrab. Also now doesn't
die on errors, and should handle them nicely. Uses the new protocol too.

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