ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.52
Committed: Mon May 31 13:52:27 2004 UTC (20 years, 5 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: IHOST_1_5_8
Changes since 1.51: +1 -1 lines
Log Message:
Prepare new release of ihost.

File Contents

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