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 |
18 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
*/ |
20 |
|
|
21 |
+ |
#ifdef HAVE_CONFIG_H |
22 |
+ |
#include "config.h" |
23 |
+ |
#endif |
24 |
+ |
|
25 |
|
#include <stdio.h> |
26 |
|
#include <stdlib.h> |
27 |
+ |
#include <sys/types.h> |
28 |
|
#include <unistd.h> |
29 |
|
#include <syslog.h> |
30 |
|
#include <netinet/in.h> |
34 |
|
#include "statgrab.h" |
35 |
|
#include <time.h> |
36 |
|
#include <sys/socket.h> |
37 |
+ |
#include <arpa/inet.h> |
38 |
+ |
#include <syslog.h> |
39 |
|
|
40 |
|
#define RECONFIGURE_RETURN_CODE 2 |
41 |
|
#define UDP_MAX_PACKET_SIZE 8192 |
42 |
+ |
#define PID_FILE "/var/tmp/.ihost.pid" |
43 |
|
|
44 |
+ |
#define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0) |
45 |
+ |
|
46 |
|
typedef struct{ |
47 |
|
int fm_port; |
48 |
|
char *fm_host; |
49 |
< |
|
49 |
> |
|
50 |
> |
char *my_ip; |
51 |
|
char *my_fqdn; |
52 |
|
char *server_fqdn; |
53 |
|
int server_udp_port; |
60 |
|
|
61 |
|
}ihost_state_t; |
62 |
|
|
63 |
+ |
static int log_level; |
64 |
+ |
static int cur_level; |
65 |
+ |
static int syslog_logging; |
66 |
+ |
|
67 |
+ |
void log_errors(const char *message){ |
68 |
+ |
if(log_level>=cur_level){ |
69 |
+ |
if (syslog_logging==1){ |
70 |
+ |
syslog(cur_level, "%s\n", message); |
71 |
+ |
}else{ |
72 |
+ |
fprintf(stderr, "%s\n", message); |
73 |
+ |
} |
74 |
+ |
} |
75 |
+ |
} |
76 |
+ |
|
77 |
|
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
78 |
|
char *reply; |
79 |
+ |
logmessage(LOG_DEBUG, "Sending %s",sendString); |
80 |
|
fprintf(f_w, "%s\n", sendString); |
81 |
|
fflush(f_w); |
82 |
|
reply=fpgetline(f_r); |
83 |
+ |
if (reply!=NULL) logmessage(LOG_DEBUG, "Received %s", reply); |
84 |
|
/* Returns pointer to static buffer */ |
85 |
|
return reply; |
86 |
|
} |
88 |
|
int ihost_configure(ihost_state_t *ihost_state){ |
89 |
|
struct sockaddr_in addr; |
90 |
|
struct in_addr haddr; |
91 |
+ |
struct sockaddr ip; |
92 |
+ |
int ip_len; |
93 |
|
int sd; |
94 |
|
FILE *fm_fd_r, *fm_fd_w; |
95 |
|
char *reply; |
101 |
|
if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
102 |
|
if (ihost_state->files_list!=NULL) free(ihost_state->files_list); |
103 |
|
|
104 |
+ |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->fm_host, ihost_state->fm_port); |
105 |
|
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
106 |
< |
errf("Can't create AF_INET socket (%m)"); |
106 |
> |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
107 |
|
return -1; |
108 |
|
} |
109 |
|
|
110 |
|
if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
111 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
111 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host); |
112 |
|
return -1; |
113 |
|
} |
114 |
|
|
118 |
|
addr.sin_port = htons(ihost_state->fm_port); |
119 |
|
|
120 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
121 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
121 |
> |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
122 |
|
return -1; |
123 |
|
} |
124 |
|
|
125 |
|
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
126 |
|
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
127 |
< |
errf("Failed to open read stream (%m)"); |
127 |
> |
logmessage(LOG_ERR, "Failed to open read stream (%m)"); |
128 |
|
return -1; |
129 |
|
} |
130 |
|
|
131 |
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
132 |
< |
errf("Failed to open write stream (%m)"); |
132 |
> |
logmessage(LOG_ERR, "Failed to open write stream (%m)"); |
133 |
|
return -1; |
134 |
|
} |
135 |
+ |
ip_len=sizeof ip; |
136 |
+ |
memset(&ip, 0, ip_len); |
137 |
+ |
if((getsockname(sd, &ip, &ip_len)) != 0){ |
138 |
+ |
logmessage(LOG_ERR, "Failed to get IP address (%m)"); |
139 |
+ |
return -1; |
140 |
+ |
} |
141 |
+ |
if (ip.sa_family!=AF_INET){ |
142 |
+ |
logmessage(LOG_ERR, "sa family is wrong type"); |
143 |
+ |
return -1; |
144 |
+ |
} |
145 |
|
|
146 |
+ |
if(ihost_state->my_ip!=NULL) free(ihost_state->my_ip); |
147 |
+ |
if((ihost_state->my_ip=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){ |
148 |
+ |
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
149 |
+ |
return -1; |
150 |
+ |
} |
151 |
+ |
|
152 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
153 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
154 |
< |
errf("Server error"); |
154 |
> |
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
155 |
|
return -1; |
156 |
|
} |
157 |
|
|
158 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
159 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
160 |
< |
errf("Server error (%m)"); |
160 |
> |
logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
161 |
|
return -1; |
162 |
|
} |
163 |
|
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
164 |
< |
errf("strdup failed (%m)"); |
164 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
165 |
|
return -1; |
166 |
|
} |
167 |
< |
|
167 |
> |
|
168 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
169 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
170 |
< |
errf("Server error (%m)"); |
170 |
> |
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
171 |
|
return -1; |
172 |
|
} |
173 |
|
if((ihost_state->files_list=strdup(reply)) == NULL){ |
174 |
< |
errf("strdup failed (%m)"); |
174 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
175 |
|
return -1; |
176 |
|
} |
177 |
|
|
178 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
179 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
180 |
< |
errf("Server error (%m)"); |
180 |
> |
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
181 |
|
return -1; |
182 |
|
} |
183 |
|
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
184 |
< |
errf("strdup failed (%m)"); |
184 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
185 |
|
return -1; |
186 |
|
} |
187 |
|
|
188 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
189 |
|
if(reply== NULL){ |
190 |
< |
errf("Server error (%m)"); |
190 |
> |
logmessage(LOG_ERR, "Server error (%m)"); |
191 |
|
return -1; |
192 |
|
} |
193 |
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
194 |
|
ihost_state->udp_update_time=atoi(reply); |
195 |
|
} |
196 |
< |
|
196 |
> |
|
197 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
198 |
|
if(reply== NULL){ |
199 |
< |
errf("Server error (%m)"); |
199 |
> |
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
200 |
|
return -1; |
201 |
|
} |
202 |
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
205 |
|
|
206 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
207 |
|
if(reply== NULL){ |
208 |
< |
errf("Server error (%m)"); |
208 |
> |
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
209 |
|
return -1; |
210 |
|
} |
211 |
|
|
212 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
213 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
214 |
< |
errf("Server error FILTER failed (%m)"); |
214 |
> |
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
215 |
|
return -1; |
216 |
|
} |
217 |
|
reply_ptr=strchr(reply,';'); |
218 |
|
if (reply_ptr==NULL){ |
219 |
< |
errf("Incorrect data returned"); |
219 |
> |
logmessage(LOG_ERR, "Incorrect data returned"); |
220 |
|
return -1; |
221 |
|
} |
222 |
|
*reply_ptr='\0'; |
223 |
|
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
224 |
< |
errf("strdup failed (%m)"); |
224 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
225 |
|
return -1; |
226 |
|
} |
227 |
|
reply=reply_ptr + 1; |
228 |
|
reply_ptr=strchr(reply,';'); |
229 |
|
if (reply_ptr==NULL){ |
230 |
< |
errf("Incorrect data returned 2"); |
230 |
> |
logmessage(LOG_ERR, "Incorrect data returned 2"); |
231 |
|
return -1; |
232 |
|
} |
233 |
|
*reply_ptr='\0'; |
235 |
|
reply=reply_ptr+1; |
236 |
|
ihost_state->server_tcp_port=atoi(reply); |
237 |
|
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
238 |
< |
errf("Incorrect data returned 3 "); |
238 |
> |
logmessage(LOG_ERR, "Incorrect data returned 3 "); |
239 |
|
return -1; |
240 |
|
} |
241 |
|
|
242 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
243 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
244 |
< |
errf("Server error (%m)"); |
244 |
> |
logmessage(LOG_ERR, "Server error on END (%m)"); |
245 |
|
return -1; |
246 |
|
} |
247 |
|
|
248 |
|
if(fclose(fm_fd_r) !=0){ |
249 |
< |
errf("Failed to close read FD (%m)"); |
249 |
> |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
250 |
|
return -1; |
251 |
|
} |
252 |
|
if(fclose(fm_fd_w) !=0){ |
253 |
< |
errf("Failed to close write FD (%m)"); |
253 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
254 |
|
return -1; |
255 |
|
} |
256 |
|
|
265 |
|
char *reply; |
266 |
|
int exitcode=0; |
267 |
|
|
268 |
+ |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
269 |
|
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
270 |
< |
errf("Can't create AF_INET socket (%m)"); |
270 |
> |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
271 |
|
return -1; |
272 |
|
} |
273 |
|
|
274 |
|
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
275 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
275 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
276 |
|
return -1; |
277 |
|
} |
278 |
|
|
282 |
|
addr.sin_port = htons(ihost_state->server_tcp_port); |
283 |
|
|
284 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
285 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
285 |
> |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
286 |
|
return -1; |
287 |
|
} |
288 |
|
|
289 |
|
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
290 |
|
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
291 |
< |
errf("Failed to open stream (%m)"); |
291 |
> |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
292 |
|
return -1; |
293 |
|
} |
294 |
|
|
295 |
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
296 |
< |
errf("Failed to open stream (%m)"); |
296 |
> |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
297 |
|
return -1; |
298 |
|
} |
299 |
|
|
300 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
301 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
302 |
< |
errf("Server error"); |
302 |
> |
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
303 |
|
return -1; |
304 |
|
} |
305 |
|
|
306 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
307 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
308 |
< |
errf("Server error"); |
308 |
> |
logmessage(LOG_ERR, "Server error on CONFIG"); |
309 |
|
return -1; |
310 |
|
} |
311 |
|
|
312 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
313 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
314 |
< |
errf("Server error"); |
314 |
> |
logmessage(LOG_ERR, "Server error on fileslist"); |
315 |
|
return -1; |
316 |
|
} |
317 |
|
|
318 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
319 |
|
if (reply==NULL) { |
320 |
< |
errf("Server error"); |
320 |
> |
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
321 |
|
return -1; |
322 |
|
} |
323 |
|
if (strncasecmp(reply, "ERROR", 5) == 0){ |
324 |
|
/* Means the config has changed */ |
325 |
+ |
logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); |
326 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
327 |
|
} |
328 |
+ |
|
329 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
330 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
331 |
< |
errf("Server error"); |
331 |
> |
logmessage(LOG_ERR, "Server error on KEY"); |
332 |
|
return -1; |
333 |
|
} |
334 |
|
if (ihost_state->key!=NULL) free(ihost_state->key); |
335 |
|
|
336 |
|
if((ihost_state->key=strdup(reply)) == NULL){ |
337 |
< |
errf("strdup failed (%m)"); |
337 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
338 |
|
return -1; |
339 |
|
} |
340 |
|
|
341 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
342 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
343 |
< |
errf("Server error (%m)"); |
343 |
> |
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
344 |
|
return -1; |
345 |
|
} |
346 |
|
|
348 |
|
fflush(fm_fd_w); |
349 |
|
|
350 |
|
if(fclose(fm_fd_r) !=0){ |
351 |
< |
errf("Failed to close read FD (%m)"); |
351 |
> |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
352 |
|
return -1; |
353 |
|
} |
354 |
|
if(fclose(fm_fd_w) !=0){ |
355 |
< |
errf("Failed to close write FD (%m)"); |
355 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
356 |
|
return -1; |
357 |
|
} |
358 |
|
|
364 |
|
char *stats[NUM_STATS]; |
365 |
|
char *xml_data=NULL; |
366 |
|
char *xml_data_p; |
367 |
+ |
int xml_size=0; |
368 |
|
int x=0; |
369 |
< |
|
369 |
> |
|
370 |
> |
logmessage(LOG_DEBUG,"get_cpu_stats"); |
371 |
|
stats[0]=get_cpu_stats(); |
372 |
+ |
logmessage(LOG_DEBUG,"get_disk_stats"); |
373 |
|
stats[1]=get_disk_stats(); |
374 |
+ |
logmessage(LOG_DEBUG,"get_load_stats"); |
375 |
|
stats[2]=get_load_stats(); |
376 |
+ |
logmessage(LOG_DEBUG,"get_memory_stats"); |
377 |
|
stats[3]=get_memory_stats(); |
378 |
+ |
logmessage(LOG_DEBUG,"get_os_info"); |
379 |
|
stats[4]=get_os_info(); |
380 |
+ |
logmessage(LOG_DEBUG,"get_page_stats"); |
381 |
|
stats[5]=get_page_stats(); |
382 |
+ |
logmessage(LOG_DEBUG,"get_process_stats"); |
383 |
|
stats[6]=get_process_stats(); |
384 |
+ |
logmessage(LOG_DEBUG,"get_swap_stats"); |
385 |
|
stats[7]=get_swap_stats(); |
386 |
+ |
logmessage(LOG_DEBUG,"get_user_stats"); |
387 |
|
stats[8]=get_user_stats(); |
388 |
+ |
|
389 |
|
|
390 |
< |
for(;x<NUM_STATS;x++){ |
390 |
> |
for(x=0;x<NUM_STATS;x++){ |
391 |
|
if(stats[x]==NULL){ |
392 |
+ |
logmessage(LOG_ERR,"Function returned NULL"); |
393 |
|
return NULL; |
394 |
|
} |
395 |
< |
if(xml_data==NULL){ |
396 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
397 |
< |
errf("str failed (%m)"); |
398 |
< |
return NULL; |
399 |
< |
} |
400 |
< |
}else{ |
401 |
< |
xml_data_p=xml_data; |
402 |
< |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
341 |
< |
errf("str failed (%m)"); |
342 |
< |
return NULL; |
343 |
< |
} |
344 |
< |
free(xml_data_p); |
345 |
< |
} |
395 |
> |
xml_size+=strlen(stats[x]); |
396 |
> |
} |
397 |
> |
|
398 |
> |
xml_data=malloc(xml_size+1); |
399 |
> |
xml_data=strcpy(xml_data, stats[0]); |
400 |
> |
free(stats[0]); |
401 |
> |
for(x=1;x<NUM_STATS;x++){ |
402 |
> |
strcat(xml_data, stats[x]); |
403 |
|
free(stats[x]); |
404 |
|
} |
405 |
+ |
|
406 |
|
xml_data_p=xml_data; |
407 |
< |
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), "127.0.0.1", ihost_state->key, xml_data); |
407 |
> |
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); |
408 |
|
free(xml_data_p); |
409 |
< |
|
409 |
> |
|
410 |
> |
logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
411 |
|
return xml_data; |
412 |
|
} |
413 |
|
|
420 |
|
|
421 |
|
len=strlen(data_stream); |
422 |
|
if(len>UDP_MAX_PACKET_SIZE){ |
423 |
< |
errf("Too big to send to server. Please reconfigure client and server and recompile"); |
423 |
> |
logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
424 |
|
exit(1); |
425 |
|
} |
426 |
< |
|
426 |
> |
logmessage(LOG_DEBUG,"Resolving IP of server"); |
427 |
|
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
428 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
428 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
429 |
|
return -1; |
430 |
|
} |
431 |
< |
|
431 |
> |
logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
432 |
|
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
433 |
< |
errf("failed to create UDP socket (%m)"); |
433 |
> |
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
434 |
|
return -1; |
435 |
|
} |
436 |
|
|
439 |
|
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
440 |
|
addr.sin_port = htons(ihost_state->server_udp_port); |
441 |
|
|
442 |
+ |
logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
443 |
|
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
444 |
< |
errf("Send the wrong number of bytes (%m)"); |
444 |
> |
logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
445 |
|
return -1; |
446 |
|
} |
447 |
|
|
450 |
|
return 0; |
451 |
|
} |
452 |
|
|
453 |
+ |
void usage(char *progname){ |
454 |
+ |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
455 |
+ |
fprintf(stderr, "Options\n"); |
456 |
+ |
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
457 |
+ |
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
458 |
+ |
fprintf(stderr, " -s Send errors to syslog\n"); |
459 |
+ |
fprintf(stderr, " -V Print version number\n"); |
460 |
+ |
fprintf(stderr, " -h Prints this help page\n"); |
461 |
+ |
exit(1); |
462 |
+ |
} |
463 |
+ |
|
464 |
|
int main(int argc, char **argv){ |
465 |
|
ihost_state_t ihost_state; |
466 |
|
int heartbeat_exit; |
468 |
|
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
469 |
|
int sleep_delay=0; |
470 |
|
char *xml_stats; |
471 |
+ |
pid_t pid; |
472 |
+ |
int cmdopt; |
473 |
+ |
extern int optind; |
474 |
+ |
int verbose=0, daemon=0; |
475 |
+ |
extern int syslog_logging; |
476 |
+ |
extern int log_level; |
477 |
+ |
extern int cur_level; |
478 |
+ |
FILE *f; |
479 |
|
|
480 |
+ |
log_level=1; |
481 |
+ |
cur_level=1; |
482 |
+ |
syslog_logging=0; |
483 |
+ |
|
484 |
+ |
errf_set_ofunc(log_errors); |
485 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
486 |
|
ihost_state.fm_host=NULL; |
487 |
|
ihost_state.my_fqdn=NULL; |
489 |
|
ihost_state.last_modified=NULL; |
490 |
|
ihost_state.files_list=NULL; |
491 |
|
ihost_state.key=NULL; |
492 |
+ |
ihost_state.my_ip=NULL; |
493 |
|
|
494 |
|
errf_set_progname(argv[0]); |
495 |
< |
if(argc!=3){ |
496 |
< |
errf_usage("<host> <port>"); |
495 |
> |
|
496 |
> |
|
497 |
> |
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
498 |
> |
switch(cmdopt){ |
499 |
> |
case 'v': |
500 |
> |
verbose++; |
501 |
> |
break; |
502 |
> |
|
503 |
> |
case 'd': |
504 |
> |
/* Force syslog logging since stderr will be closed in this case */ |
505 |
> |
syslog_logging=1; |
506 |
> |
daemon=1; |
507 |
> |
break; |
508 |
> |
|
509 |
> |
case 's': |
510 |
> |
syslog_logging=1; |
511 |
> |
break; |
512 |
> |
|
513 |
> |
case 'h': |
514 |
> |
usage(argv[0]); |
515 |
> |
break; |
516 |
> |
|
517 |
> |
case 'V': |
518 |
> |
errf("%s version %s",argv[0], VERSION); |
519 |
> |
break; |
520 |
> |
|
521 |
> |
default: |
522 |
> |
usage(argv[0]); |
523 |
> |
exit(1); |
524 |
> |
} |
525 |
> |
} |
526 |
> |
|
527 |
> |
if(argc!=optind+2){ |
528 |
> |
usage(argv[0]); |
529 |
|
exit(1); |
530 |
|
} |
531 |
+ |
ihost_state.fm_host=argv[optind]; |
532 |
+ |
ihost_state.fm_port=atoi(argv[optind+1]); |
533 |
+ |
if(ihost_state.fm_port==0){ |
534 |
+ |
errf("Invalid port number"); |
535 |
+ |
usage(argv[0]); |
536 |
+ |
} |
537 |
|
|
538 |
< |
ihost_state.fm_host=argv[1]; |
539 |
< |
ihost_state.fm_port=atoi(argv[2]); |
538 |
> |
if(daemon==1){ |
539 |
> |
pid=fork(); |
540 |
> |
if(pid==-1){ |
541 |
> |
errf("Fork failed, can't background. Exiting"); |
542 |
> |
exit(1); |
543 |
> |
}else if(pid!=0){ |
544 |
> |
/* Parent process */ |
545 |
> |
return 0; |
546 |
> |
} |
547 |
> |
/* We should now be in the background*/ |
548 |
> |
if(setsid()==-1){ |
549 |
> |
errf("setsid failed (%m)"); |
550 |
> |
exit(1); |
551 |
> |
} |
552 |
> |
fclose(stdin); |
553 |
> |
fclose(stdout); |
554 |
> |
fclose(stderr); |
555 |
> |
} |
556 |
|
|
557 |
+ |
if(syslog_logging==1){ |
558 |
+ |
openlog(errf_get_progname(),0,LOG_ERR); |
559 |
+ |
setlogmask(LOG_UPTO(LOG_DEBUG)); |
560 |
+ |
} |
561 |
+ |
|
562 |
+ |
switch(verbose){ |
563 |
+ |
case 0: |
564 |
+ |
/* Critical errors + */ |
565 |
+ |
log_level=LOG_ERR; |
566 |
+ |
break; |
567 |
+ |
case 1: |
568 |
+ |
/* Recoverable errors */ |
569 |
+ |
log_level=LOG_WARNING; |
570 |
+ |
break; |
571 |
+ |
case 2: |
572 |
+ |
/* Print stuff like the XML packets */ |
573 |
+ |
log_level=LOG_INFO; |
574 |
+ |
break; |
575 |
+ |
default: |
576 |
+ |
/* Must have lots of v's */ |
577 |
+ |
/* Print out everything its doing */ |
578 |
+ |
log_level=LOG_DEBUG; |
579 |
+ |
break; |
580 |
+ |
} |
581 |
+ |
|
582 |
+ |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
583 |
+ |
pid=getpid(); |
584 |
+ |
if((f=fopen(PID_FILE,"w")) == NULL){ |
585 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
586 |
+ |
}else{ |
587 |
+ |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
588 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
589 |
+ |
} |
590 |
+ |
if((fclose(f))!=0){ |
591 |
+ |
logmessage(LOG_ERR, "failed to close PID file"); |
592 |
+ |
exit(1); |
593 |
+ |
} |
594 |
+ |
} |
595 |
+ |
|
596 |
|
if(ihost_configure(&ihost_state)!=0){ |
597 |
< |
errf("configure failed"); |
597 |
> |
logmessage(LOG_ERR,"configure failed"); |
598 |
|
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
599 |
|
happens.. But we dont :) (at the moment) */ |
600 |
|
exit(1); |
603 |
|
for(;;){ |
604 |
|
cur_time=time(NULL); |
605 |
|
if(cur_time>=tcp_time){ |
606 |
< |
/*printf("sending TCP\n");*/ |
606 |
> |
logmessage(LOG_DEBUG,"Sending heartbeat"); |
607 |
|
heartbeat_exit=heartbeat(&ihost_state); |
608 |
|
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
609 |
< |
/*errf("heartbeat needs to be reconfigured");*/ |
609 |
> |
logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
610 |
|
ihost_configure(&ihost_state); |
433 |
– |
/* So udp doesn't wait til next sending before updating */ |
611 |
|
udp_time=0; |
612 |
|
} |
613 |
|
if(heartbeat_exit==-1){ |
614 |
< |
errf("ah crap"); |
614 |
> |
logmessage(LOG_ERR,"Heartbeat failed"); |
615 |
|
exit(1); |
616 |
|
} |
617 |
|
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
618 |
+ |
logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
619 |
|
} |
620 |
|
|
621 |
|
if(cur_time>=udp_time){ |
622 |
< |
/*printf("sending UDP\n");*/ |
622 |
> |
logmessage(LOG_DEBUG,"Sending udp data"); |
623 |
> |
/* Work out how long it takes to get the stats for next time round |
624 |
> |
so the sleep time can be adjusted accordingly */ |
625 |
|
stat_grab_time=time(NULL); |
626 |
|
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
627 |
< |
errf("Failed to get stats (%m)"); |
627 |
> |
logmessage(LOG_ERR,"Failed to get stats (%m)"); |
628 |
|
exit(1); |
629 |
|
} |
630 |
|
stat_grab_time=time(NULL)-stat_grab_time; |
631 |
|
send_stats(&ihost_state, xml_stats); |
632 |
|
free(xml_stats); |
633 |
|
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
634 |
+ |
logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
635 |
|
} |
636 |
|
|
637 |
|
if(tcp_time<udp_time){ |
639 |
|
}else{ |
640 |
|
sleep_delay=udp_time-time(NULL); |
641 |
|
} |
642 |
< |
|
462 |
< |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
642 |
> |
logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
643 |
|
if(sleep_delay>0) sleep(sleep_delay); |
644 |
|
} |
645 |
|
return 0; |