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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines