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, 8 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

# User Rev Content
1 tdb 1.11 /*
2     * i-scream central monitoring system
3 tdb 1.23 * http://www.i-scream.org.uk
4 tdb 1.11 * 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 tdb 1.26 #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24    
25 pajs 1.1 #include <stdio.h>
26     #include <stdlib.h>
27 pajs 1.28 #include <unistd.h>
28     #include <string.h>
29 pajs 1.18 #include <sys/types.h>
30 pajs 1.28 #include <sys/socket.h>
31     #include <stdarg.h>
32     #include <errno.h>
33 pajs 1.1 #include <netdb.h>
34 pajs 1.40 #include <netinet/in.h>
35 pajs 1.28
36 tdb 1.39 #include <ukcprog.h>
37     #include <statgrab.h>
38 pajs 1.1
39 pajs 1.28 #define LOG_CRIT 0
40     #define LOG_ERR 1
41     #define LOG_INFO 2
42     #define LOG_DEBUG 3
43    
44 pajs 1.1 typedef struct{
45 pajs 1.28 int filtermanager_port;
46     char *filtermanager_host;
47    
48     char *host_ip;
49     char *host_fqdn;
50 pajs 1.16
51 pajs 1.1 char *server_fqdn;
52     int server_udp_port;
53 pajs 1.28
54     /* Weird stuff iscream wants sent to it */
55 pajs 1.4 char *last_modified;
56 pajs 1.28 char *file_list;
57    
58 pajs 1.1 int udp_update_time;
59 pajs 1.28 // int config_ttl;
60    
61     time_t config_ttl;
62 pajs 1.1
63     }ihost_state_t;
64    
65 pajs 1.28 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 pajs 1.33 extern int errno;
80    
81 tdb 1.34 /* Taken from the OpenSSH code. Its licence included in function.*/
82     #ifndef HAVE_STRLCAT
83 pajs 1.33
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 tdb 1.34
111 pajs 1.33 /*
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 tdb 1.34 #endif
150     /* End strlcat function taken from OpenSSH */
151 pajs 1.33
152 pajs 1.28 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 pajs 1.18 }else{
165 pajs 1.28 fprintf(ihost_config.log, "\n");
166 pajs 1.18 }
167 pajs 1.37 fflush(ihost_config.log);
168 pajs 1.28 }
169 pajs 1.18 }
170    
171 pajs 1.28 /* 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 pajs 1.1
178 pajs 1.28 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 pajs 1.1
199 pajs 1.28 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 pajs 1.1 }
203    
204 pajs 1.28 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 pajs 1.1
209 pajs 1.28 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 pajs 1.1 }
224    
225 pajs 1.28 if((get_host_addr(hostname, &haddr))!=0){
226     log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname);
227 pajs 1.38 close(sock);
228 pajs 1.28 return NULL;
229 pajs 1.2 }
230    
231 pajs 1.28 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 pajs 1.38 close(sock);
240 pajs 1.28 return NULL;
241 pajs 1.1 }
242 pajs 1.28
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 pajs 1.16 }
248 pajs 1.28
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 pajs 1.16 return -1;
275     }
276 pajs 1.25
277 pajs 1.28 *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 pajs 1.30 if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){
300 pajs 1.35 return -1;
301 pajs 1.30 }
302 pajs 1.28
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 pajs 1.18
316 pajs 1.28 if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){
317 pajs 1.32 if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
318 pajs 1.28 goto error;
319     }
320     fclose(tcp_con);
321     return 0;
322     }else{
323     if((strcmp(response, "EXPIRED"))!=0){
324     goto error;
325     }
326     }
327 pajs 1.1 }
328    
329 pajs 1.28 /* If we got to here, the config must of expired */
330 pajs 1.18
331 pajs 1.28 if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){
332     goto error;
333 pajs 1.1 }
334    
335 pajs 1.28 if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){
336     goto error;
337 pajs 1.1 }
338 pajs 1.28 last_modified=response;
339    
340     if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){
341     goto error;
342 pajs 1.1 }
343 pajs 1.28 file_list=response;
344 pajs 1.1
345 pajs 1.28 if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){
346     goto error;
347 pajs 1.1 }
348 pajs 1.28 host_fqdn=response;
349    
350     if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){
351     goto error;
352 pajs 1.1 }
353 pajs 1.28 host_ip=response;
354    
355     if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){
356     goto error;
357 pajs 1.1 }
358 pajs 1.28 udp_update_time=atoi(response);
359    
360     if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){
361     goto error;
362 pajs 1.1 }
363 pajs 1.28 config_ttl=atoi(response);
364 pajs 1.1
365 pajs 1.28 if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){
366     goto error;
367 pajs 1.1 }
368    
369 pajs 1.28 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 pajs 1.2 }
396 pajs 1.28
397     if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
398     goto error;
399 pajs 1.2 }
400 pajs 1.28
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 pajs 1.2 }
478 pajs 1.28
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 pajs 1.2 }
493 pajs 1.28
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 pajs 1.2 }
506    
507 pajs 1.28
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 pajs 1.2 }
521 pajs 1.1
522 pajs 1.28
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 pajs 1.2 }
535 pajs 1.28
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 pajs 1.2 }
554 pajs 1.1
555 pajs 1.28
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 pajs 1.4
570 pajs 1.28 }
571 pajs 1.4
572    
573 pajs 1.28 /* 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 tdb 1.36 "<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \
586 pajs 1.28 x, \
587     y);
588    
589     if(strlcat(xml, tmp, size) >= size) goto too_big_error;
590     }
591 pajs 1.4
592    
593 pajs 1.28 /* get diskio stats */
594    
595     if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){
596 pajs 1.31 log_msg(LOG_CRIT, "Failed to get diskio statistics");
597 pajs 1.28 }else{
598     strlcat(xml, "<diskio>", size);
599     for(counter=0;counter<diskio_entries;counter++){
600 pajs 1.4
601 pajs 1.28 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 pajs 1.9
617 pajs 1.28 strlcat(xml, tmp, size);
618     diskio_stats++;
619     }
620 pajs 1.4
621 pajs 1.28 if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error;
622 pajs 1.4
623     }
624 pajs 1.18
625 pajs 1.28
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 pajs 1.4
641 pajs 1.28 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 pajs 1.4
649 pajs 1.28 strlcat(xml, tmp, size);
650     network_stats++;
651     }
652 pajs 1.10
653 pajs 1.28 if(strlcat(xml, "</net>", size) >= size) goto too_big_error;
654 pajs 1.6
655 pajs 1.28 }
656 pajs 1.4
657 pajs 1.18
658 pajs 1.28 /* 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 pajs 1.18
679 pajs 1.28 strlcat(xml, tmp, size);
680 pajs 1.9
681 pajs 1.28 disk_stats++;
682     }
683 pajs 1.9
684 pajs 1.28 if(strlcat(xml, "</disk>", size) >= size) goto too_big_error;
685 pajs 1.9
686     }
687 pajs 1.28
688 pajs 1.9
689 pajs 1.28 if(strlcat(xml, "</packet>", size) >= size) goto too_big_error;
690 pajs 1.9
691 pajs 1.28 /*If we got to here, it should of all been filled in nicely now */
692     return 0;
693 pajs 1.10
694 pajs 1.28 too_big_error:
695     log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet");
696     return -1;
697 pajs 1.9 }
698 pajs 1.1
699 pajs 1.28
700    
701 pajs 1.17 void usage(char *progname){
702 pajs 1.28 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 pajs 1.17 }
710    
711 pajs 1.3 int main(int argc, char **argv){
712 pajs 1.28
713 pajs 1.2 ihost_state_t ihost_state;
714 pajs 1.28 udp_sockinfo_t udp_sockinfo;
715    
716 pajs 1.17 int cmdopt;
717     extern int optind;
718 pajs 1.28 pid_t pid;
719 pajs 1.19 FILE *f;
720 pajs 1.28 int packet_num=0;
721     int len;
722 pajs 1.18
723 pajs 1.28 char packet[MAX_UDP_PACKET_SIZE];
724 pajs 1.18
725 pajs 1.28 time_t cur_time, sleep_delay, udp_time=0, config_time=0;
726 pajs 1.18
727 pajs 1.28 /* 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 tdb 1.29 fprintf(stderr, "%s version %s\n", argv[0], VERSION);
758 pajs 1.28 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 pajs 1.17
771 pajs 1.28 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 pajs 1.3 exit(1);
777     }
778 pajs 1.28 if(ihost_state.filtermanager_port==0){
779     log_msg(LOG_ERR, "Invalid port number");
780     exit(1);
781 pajs 1.17 }
782 pajs 1.18
783 pajs 1.28 if(ihost_config.daemon){
784 pajs 1.18 pid=fork();
785     if(pid==-1){
786 pajs 1.28 log_msg(LOG_CRIT, "Failed to background exiting");
787     exit(1);
788 pajs 1.18 }else if(pid!=0){
789 pajs 1.28 /* 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 tdb 1.29 if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){
799 pajs 1.28 ihost_config.log=stderr;
800 tdb 1.29 log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE);
801 pajs 1.18 exit(1);
802     }
803 pajs 1.28
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 pajs 1.18 }
837    
838 pajs 1.28 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 pajs 1.18 }
843 pajs 1.2
844 pajs 1.28 config_time=time(NULL)+ihost_state.config_ttl;
845 pajs 1.5
846 pajs 1.28 /* Now have config.. collect data and send as often as required */
847 pajs 1.9 for(;;){
848     cur_time=time(NULL);
849 pajs 1.28
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 pajs 1.9 }
861 pajs 1.28 udp_time=cur_time+ihost_state.udp_update_time;
862     log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time);
863 pajs 1.9 }
864 pajs 1.28
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 pajs 1.10
880 pajs 1.28 log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl);
881 pajs 1.9 }
882     }
883 pajs 1.10
884 pajs 1.28 sleep_delay=udp_time-time(NULL);
885     log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay);
886 pajs 1.10 if(sleep_delay>0) sleep(sleep_delay);
887 pajs 1.28 }
888    
889     return(0);
890 pajs 1.1 }