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