ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.39
Committed: Sat Mar 29 18:49:49 2003 UTC (21 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.38: +2 -2 lines
Log Message:
Change #include's to correctly use <> rather than "".

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