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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines