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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines