ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
(Generate patch)

Comparing projects/cms/source/ihost/ihost.c (file contents):
Revision 1.2 by pajs, Fri May 10 20:05:05 2002 UTC vs.
Revision 1.38 by pajs, Wed Mar 19 21:51:50 2003 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * Copyright (C) 2000-2002 i-scream
5 + *
6 + * This program is free software; you can redistribute it and/or
7 + * modify it under the terms of the GNU General Public License
8 + * as published by the Free Software Foundation; either version 2
9 + * of the License, or (at your option) any later version.
10 + *
11 + * This program is distributed in the hope that it will be useful,
12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + * GNU General Public License for more details.
15 + *
16 + * You should have received a copy of the GNU General Public License
17 + * along with this program; if not, write to the Free Software
18 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + */
20 +
21 + #ifdef HAVE_CONFIG_H
22 + #include "config.h"
23 + #endif
24 +
25   #include <stdio.h>
2 #include <sys/socket.h>
26   #include <stdlib.h>
27   #include <unistd.h>
28 < #include <syslog.h>
29 < #include <netinet/in.h>
30 < #include <ukcprog.h>
28 > #include <string.h>
29 > #include <sys/types.h>
30 > #include <sys/socket.h>
31 > #include <stdarg.h>
32 > #include <errno.h>
33   #include <netdb.h>
9 #include <strings.h>
34  
35 + #include "ukcprog.h"
36 + #include "statgrab.h"
37 +
38 + #define LOG_CRIT 0
39 + #define LOG_ERR 1
40 + #define LOG_INFO 2
41 + #define LOG_DEBUG 3
42 +
43   typedef struct{
44 <        int fm_port;
45 <        char *fm_host;
46 <        
47 <        char *my_fqdn;
44 >        int filtermanager_port;
45 >        char *filtermanager_host;
46 >
47 >        char *host_ip;
48 >        char *host_fqdn;
49 >
50          char *server_fqdn;
51          int server_udp_port;
52 <        int server_tcp_port;
53 <        long last_modified;
54 <        char *files_list;
55 <        char *key;
52 >        
53 >        /* Weird stuff iscream wants sent to it */
54 >        char *last_modified;
55 >        char *file_list;
56 >
57          int udp_update_time;
58 <        int tcp_update_time;
58 > //      int config_ttl;
59  
60 +        time_t config_ttl;
61 +
62   }ihost_state_t;
63  
64 < char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){
65 <        char *reply;
66 <        fprintf(f_w, "%s", sendString);
30 <        fflush(f_w);
31 <        reply=fpgetline(f_r);
32 <        /* Returns pointer to static buffer */
33 <        return reply;
34 < }      
64 > typedef struct{
65 >        int verbose;
66 >        int daemon;
67  
68 < int ihost_configure(ihost_state_t *ihost_state){
68 >        FILE *log;
69 > }ihost_config_t;        
70 >
71 > typedef struct{
72          struct sockaddr_in addr;
73 <        struct in_addr haddr;
74 <        int sd;
40 <        FILE *fm_fd_r, *fm_fd_w;
41 <        char *reply;
42 <        char *reply_ptr;
73 >        int sock;
74 > }udp_sockinfo_t;
75  
76 <        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
45 <                errf("Can't create AF_INET socket (%m)");
46 <                return -1;
47 <        }
76 > ihost_config_t ihost_config;
77  
78 <        if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
79 <                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
80 <                return -1;
78 > extern int errno;
79 >
80 > /* Taken from the OpenSSH code. Its licence included in function.*/
81 > #ifndef HAVE_STRLCAT
82 >
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 >
110 > /*
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 > #endif
149 > /* End strlcat function taken from OpenSSH */
150 >
151 > 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 >                }else{
164 >                        fprintf(ihost_config.log, "\n");
165 >                }
166 >                fflush(ihost_config.log);
167          }
168 + }
169  
170 <        memset((char *)&addr, 0, sizeof addr);
171 <        addr.sin_family = AF_INET;
172 <        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
173 <        addr.sin_port =  htons(ihost_state->fm_port);
170 > /* 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  
177 <        if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
178 <                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
179 <                return -1;
177 >        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 <        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
189 <        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
190 <                errf("Failed to open stream (%m)");
191 <                return -1;
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  
198 <        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
199 <                errf("Failed to open stream (%m)");
200 <                return -1;
198 >        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          }
202  
203 <        reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n");
204 <        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
205 <                errf("Server error");  
206 <                return -1;
203 >        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 >
208 >        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          }
223  
224 <        reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n");
225 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
226 <                errf("Server error (%m)");
227 <                return -1;
224 >        if((get_host_addr(hostname, &haddr))!=0){
225 >                log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname);
226 >                close(sock);
227 >                return NULL;
228          }
229 <        ihost_state->last_modified=atol(reply);
229 >
230 >        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 >                close(sock);
239 >                return NULL;
240 >        }
241 >
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 >        }
247 >
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 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
264 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
265 <                errf("Server error (%m)");
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                  return -1;
274          }
275 <        if((ihost_state->files_list=strdup(reply)) == NULL){
276 <                errf("strdup failed (%m)");
275 >
276 >        *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 >        if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){
299                  return -1;
300          }
301  
302 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n");
303 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
304 <                errf("Server error (%m)");
305 <                return -1;
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 >        
315 >                if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){
316 >                        if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
317 >                                goto error;
318 >                        }
319 >                        fclose(tcp_con);
320 >                        return 0;
321 >                }else{
322 >                        if((strcmp(response, "EXPIRED"))!=0){
323 >                                goto error;
324 >                        }
325 >                }
326          }
327 <        if((ihost_state->my_fqdn=strdup(reply)) == NULL){
328 <                errf("strdup failed (%m)");
329 <                return -1;
327 >
328 >        /* If we got to here, the config must of expired */
329 >
330 >        if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){
331 >                goto error;
332          }
333  
334 <        reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n");
335 <        if(reply== NULL){
110 <                errf("Server error (%m)");
111 <                return -1;
334 >        if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){
335 >                goto error;
336          }
337 <        if (strncasecmp(reply, "ERROR", 5) != 0){
338 <                ihost_state->udp_update_time=atoi(reply);
337 >        last_modified=response;
338 >                        
339 >        if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){
340 >                goto error;
341          }
342 +        file_list=response;
343  
344 <        reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n");
345 <        if(reply== NULL){
119 <                errf("Server error (%m)");
120 <                return -1;
344 >        if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){
345 >                goto error;
346          }
347 <        if (strncasecmp(reply, "ERROR", 5) != 0){
348 <                ihost_state->tcp_update_time=atoi(reply);
347 >        host_fqdn=response;
348 >
349 >        if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){
350 >                goto error;
351          }
352 +        host_ip=response;
353  
354 <        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n");
355 <        if(reply== NULL){
128 <                errf("Server error (%m)");
129 <                return -1;
354 >        if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){
355 >                goto error;
356          }
357 +        udp_update_time=atoi(response);
358  
359 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n");
360 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
134 <                errf("Server error (%m)");
135 <                return -1;
359 >        if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){
360 >                goto error;
361          }
362 <        reply_ptr=strchr(reply,';');
363 <        if (reply_ptr==NULL){
364 <                errf("Incorrect data returned");
365 <                return -1;
362 >        config_ttl=atoi(response);
363 >
364 >        if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){
365 >                goto error;
366          }
367 <        *reply_ptr='\0';
368 <        if((ihost_state->server_fqdn=strdup(reply)) == NULL){
369 <                errf("strdup failed (%m)");
370 <                return -1;
367 >
368 >        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          }
395 <        reply=++reply_ptr;
396 <        reply_ptr=strchr(reply,';');
397 <        if (reply_ptr==NULL){
150 <                errf("Incorrect data returned 2");
151 <                return -1;
395 >        
396 >        if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){
397 >                goto error;
398          }
399 <        *reply_ptr='\0';
400 <        ihost_state->server_udp_port=atoi(reply);
401 <        reply=++reply_ptr;
402 <        ihost_state->server_tcp_port=atoi(reply);
403 <        if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
404 <                errf("Incorrect data returned 3 ");
405 <                return -1;
399 >
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          }
477  
478 <        reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
479 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
480 <                errf("Server error (%m)");
481 <                return -1;
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          }
492  
493 <        if(fclose(fm_fd_r) !=0){
494 <                errf("Failed to close FD (%m)");
495 <                return -1;
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          }
505 <        if(fclose(fm_fd_w) !=0){
506 <                errf("Failed to close FD (%m)");
507 <                return -1;
505 >
506 >
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          }
520  
521 +
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 +        }
534 +
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 +        }
553 +
554 +        
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 +
569 +        }
570 +
571 +
572 +        /* 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 +                        "<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \
585 +                        x, \
586 +                        y);
587 +        
588 +                if(strlcat(xml, tmp, size) >= size) goto too_big_error;
589 +        }
590 +
591 +
592 +        /* get diskio stats */
593 +        
594 +        if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){
595 +                log_msg(LOG_CRIT, "Failed to get diskio statistics");
596 +        }else{
597 +                strlcat(xml, "<diskio>", size);
598 +                for(counter=0;counter<diskio_entries;counter++){
599 +
600 +                        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 +
616 +                        strlcat(xml, tmp, size);
617 +                        diskio_stats++;
618 +                }
619 +
620 +                if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error;
621 +
622 +        }
623 +
624 +        
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 +
640 +                        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 +
648 +                        strlcat(xml, tmp, size);
649 +                        network_stats++;
650 +                }
651 +
652 +                if(strlcat(xml, "</net>", size) >= size) goto too_big_error;
653 +
654 +        }
655 +
656 +
657 +        /* 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 +
678 +                        strlcat(xml, tmp, size);
679 +
680 +                        disk_stats++;
681 +                }
682 +
683 +                if(strlcat(xml, "</disk>", size) >= size) goto too_big_error;
684 +
685 +        }
686 +        
687 +
688 +        if(strlcat(xml, "</packet>", size) >= size) goto too_big_error;
689 +
690 +        /*If we got to here, it should of all been filled in nicely now */
691          return 0;
692  
693 + too_big_error:
694 +        log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet");
695 +        return -1;
696   }
697  
698 < int main(){
698 >
699 >        
700 > void usage(char *progname){
701 >        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 > }
709 >
710 > int main(int argc, char **argv){
711 >
712          ihost_state_t ihost_state;
713 +        udp_sockinfo_t udp_sockinfo;
714 +        
715 +        int cmdopt;
716 +        extern int optind;
717 +        pid_t pid;
718 +        FILE *f;
719 +        int packet_num=0;
720 +        int len;
721  
722 <        ihost_state.fm_host=strdup("kernow.ukc.ac.uk");
185 <        ihost_state.fm_port=4567;
722 >        char packet[MAX_UDP_PACKET_SIZE];
723  
724 <        if(ihost_configure(&ihost_state)!=0){
725 <                errf("configure failed");
724 >        time_t cur_time, sleep_delay, udp_time=0, config_time=0;
725 >
726 >        /* 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 >                                fprintf(stderr, "%s version %s\n", argv[0], VERSION);
757 >                                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 >
770 >        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 >                exit(1);
776          }
777 +        if(ihost_state.filtermanager_port==0){
778 +                log_msg(LOG_ERR, "Invalid port number");
779 +                exit(1);
780 +        }
781  
782 <        return 0;
783 < }
782 >        if(ihost_config.daemon){
783 >                pid=fork();
784 >                if(pid==-1){
785 >                        log_msg(LOG_CRIT, "Failed to background exiting");
786 >                        exit(1);        
787 >                }else if(pid!=0){
788 >                        /* 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 >                if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){
798 >                        ihost_config.log=stderr;
799 >                        log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE);
800 >                        exit(1);
801 >                }
802  
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 +        }
836 +
837 +        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 +        }
842 +
843 +        config_time=time(NULL)+ihost_state.config_ttl;
844 +
845 +        /* Now have config.. collect data and send as often as required */
846 +        for(;;){
847 +                cur_time=time(NULL);
848 +
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 +                        }
860 +                        udp_time=cur_time+ihost_state.udp_update_time;
861 +                        log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time);
862 +                }
863 +                
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 +
879 +                                log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl);
880 +                        }
881 +                }
882 +
883 +                sleep_delay=udp_time-time(NULL);
884 +                log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay);
885 +                if(sleep_delay>0) sleep(sleep_delay);
886 +        }
887 +                        
888 +        return(0);
889 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines