4 |
|
#include <unistd.h> |
5 |
|
#include <syslog.h> |
6 |
|
#include <netinet/in.h> |
7 |
< |
#include <ukcprog.h> |
7 |
> |
#include "ukcprog.h" |
8 |
|
#include <netdb.h> |
9 |
|
#include <strings.h> |
10 |
+ |
#include "statgrab.h" |
11 |
|
|
12 |
|
#define RECONFIGURE_RETURN_CODE 2 |
13 |
+ |
#define UDP_MAX_PACKET_SIZE 8192 |
14 |
|
|
15 |
|
typedef struct{ |
16 |
|
int fm_port; |
61 |
|
return -1; |
62 |
|
} |
63 |
|
|
64 |
< |
memset((char *)&addr, 0, sizeof addr); |
64 |
> |
memset(&addr, 0, sizeof addr); |
65 |
|
addr.sin_family = AF_INET; |
66 |
< |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
66 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
67 |
|
addr.sin_port = htons(ihost_state->fm_port); |
68 |
|
|
69 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
73 |
|
|
74 |
|
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
75 |
|
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
76 |
< |
errf("Failed to open stream (%m)"); |
76 |
> |
errf("Failed to open read stream (%m)"); |
77 |
|
return -1; |
78 |
|
} |
79 |
|
|
80 |
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
81 |
< |
errf("Failed to open stream (%m)"); |
81 |
> |
errf("Failed to open write stream (%m)"); |
82 |
|
return -1; |
83 |
|
} |
84 |
|
|
144 |
|
|
145 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
146 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
147 |
< |
errf("Server error (%m)"); |
147 |
> |
errf("Server error FILTER failed (%m)"); |
148 |
|
return -1; |
149 |
|
} |
150 |
|
reply_ptr=strchr(reply,';'); |
157 |
|
errf("strdup failed (%m)"); |
158 |
|
return -1; |
159 |
|
} |
160 |
< |
reply=++reply_ptr; |
160 |
> |
reply=reply_ptr + 1; |
161 |
|
reply_ptr=strchr(reply,';'); |
162 |
|
if (reply_ptr==NULL){ |
163 |
|
errf("Incorrect data returned 2"); |
165 |
|
} |
166 |
|
*reply_ptr='\0'; |
167 |
|
ihost_state->server_udp_port=atoi(reply); |
168 |
< |
reply=++reply_ptr; |
168 |
> |
reply=reply_ptr+1; |
169 |
|
ihost_state->server_tcp_port=atoi(reply); |
170 |
|
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
171 |
|
errf("Incorrect data returned 3 "); |
179 |
|
} |
180 |
|
|
181 |
|
if(fclose(fm_fd_r) !=0){ |
182 |
< |
errf("Failed to close FD (%m)"); |
182 |
> |
errf("Failed to close read FD (%m)"); |
183 |
|
return -1; |
184 |
|
} |
185 |
|
if(fclose(fm_fd_w) !=0){ |
186 |
< |
errf("Failed to close FD (%m)"); |
186 |
> |
errf("Failed to close write FD (%m)"); |
187 |
|
return -1; |
188 |
|
} |
189 |
|
|
208 |
|
return -1; |
209 |
|
} |
210 |
|
|
211 |
< |
memset((char *)&addr, 0, sizeof addr); |
211 |
> |
memset(&addr, 0, sizeof addr); |
212 |
|
addr.sin_family = AF_INET; |
213 |
< |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
213 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
214 |
|
addr.sin_port = htons(ihost_state->server_tcp_port); |
215 |
|
|
216 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
234 |
|
errf("Server error"); |
235 |
|
return -1; |
236 |
|
} |
237 |
< |
if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); |
237 |
> |
|
238 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
239 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
240 |
|
errf("Server error"); |
274 |
|
return -1; |
275 |
|
} |
276 |
|
|
277 |
+ |
fflush(fm_fd_r); |
278 |
+ |
fflush(fm_fd_w); |
279 |
+ |
|
280 |
|
if(fclose(fm_fd_r) !=0){ |
281 |
< |
errf("Failed to close FD (%m)"); |
281 |
> |
errf("Failed to close read FD (%m)"); |
282 |
|
return -1; |
283 |
|
} |
284 |
|
if(fclose(fm_fd_w) !=0){ |
285 |
< |
errf("Failed to close FD (%m)"); |
285 |
> |
errf("Failed to close write FD (%m)"); |
286 |
|
return -1; |
287 |
|
} |
288 |
|
|
289 |
|
return exitcode; |
290 |
|
} |
291 |
|
|
292 |
+ |
char *stat_grab(ihost_state_t *ihost_state, int counter){ |
293 |
+ |
#define NUM_STATS 9 |
294 |
+ |
char *stats[NUM_STATS]; |
295 |
+ |
char *xml_data=NULL; |
296 |
+ |
char *xml_data_p; |
297 |
+ |
int x=0; |
298 |
+ |
|
299 |
+ |
stats[0]=get_cpu_stats(); |
300 |
+ |
stats[1]=get_disk_stats(); |
301 |
+ |
stats[2]=get_load_stats(); |
302 |
+ |
stats[3]=get_memory_stats(); |
303 |
+ |
stats[4]=get_os_info(); |
304 |
+ |
stats[5]=get_page_stats(); |
305 |
+ |
stats[6]=get_process_stats(); |
306 |
+ |
stats[7]=get_swap_stats(); |
307 |
+ |
stats[8]=get_user_stats(); |
308 |
|
|
309 |
+ |
for(;x<NUM_STATS;x++){ |
310 |
+ |
if(stats[x]==NULL){ |
311 |
+ |
return NULL; |
312 |
+ |
} |
313 |
+ |
if(xml_data==NULL){ |
314 |
+ |
if((xml_data=strf("%s", stats[x])) == NULL){ |
315 |
+ |
errf("str failed (%m)"); |
316 |
+ |
return NULL; |
317 |
+ |
} |
318 |
+ |
}else{ |
319 |
+ |
xml_data_p=xml_data; |
320 |
+ |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
321 |
+ |
errf("str failed (%m)"); |
322 |
+ |
return NULL; |
323 |
+ |
} |
324 |
+ |
free(xml_data_p); |
325 |
+ |
} |
326 |
+ |
free(stats[x]); |
327 |
+ |
} |
328 |
+ |
xml_data_p=xml_data; |
329 |
+ |
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); |
330 |
+ |
free(xml_data_p); |
331 |
+ |
|
332 |
+ |
return xml_data; |
333 |
+ |
} |
334 |
+ |
|
335 |
+ |
int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
336 |
+ |
struct sockaddr_in addr; |
337 |
+ |
struct in_addr haddr; |
338 |
+ |
|
339 |
+ |
int sd; |
340 |
+ |
size_t len; |
341 |
+ |
|
342 |
+ |
len=strlen(data_stream); |
343 |
+ |
if(len>UDP_MAX_PACKET_SIZE){ |
344 |
+ |
errf("Too big to send to server. Please reconfigure client and server and recompile"); |
345 |
+ |
exit(1); |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
349 |
+ |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
350 |
+ |
return -1; |
351 |
+ |
} |
352 |
+ |
|
353 |
+ |
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
354 |
+ |
errf("failed to create UDP socket (%m)"); |
355 |
+ |
return -1; |
356 |
+ |
} |
357 |
+ |
|
358 |
+ |
memset(&addr, 0, sizeof(addr)); |
359 |
+ |
addr.sin_family=AF_INET; |
360 |
+ |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
361 |
+ |
addr.sin_port = htons(ihost_state->server_udp_port); |
362 |
+ |
|
363 |
+ |
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
364 |
+ |
errf("Send the wrong number of bytes (%m)"); |
365 |
+ |
return -1; |
366 |
+ |
} |
367 |
+ |
|
368 |
+ |
close(sd); |
369 |
+ |
|
370 |
+ |
return 0; |
371 |
+ |
} |
372 |
+ |
|
373 |
|
int main(int argc, char **argv){ |
374 |
|
ihost_state_t ihost_state; |
375 |
|
int heartbeat_exit; |
376 |
|
int counter=0; |
377 |
+ |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
378 |
+ |
int sleep_delay=0; |
379 |
+ |
char *xml_stats; |
380 |
|
|
293 |
– |
|
381 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
382 |
|
ihost_state.fm_host=NULL; |
383 |
|
ihost_state.my_fqdn=NULL; |
402 |
|
exit(1); |
403 |
|
} |
404 |
|
|
405 |
< |
while(TRUE){ |
405 |
> |
for(;;){ |
406 |
> |
cur_time=time(NULL); |
407 |
> |
if(cur_time>=tcp_time){ |
408 |
> |
/*printf("sending TCP\n");*/ |
409 |
> |
heartbeat_exit=heartbeat(&ihost_state); |
410 |
> |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
411 |
> |
/*errf("heartbeat needs to be reconfigured");*/ |
412 |
> |
ihost_configure(&ihost_state); |
413 |
> |
/* So udp doesn't wait til next sending before updating */ |
414 |
> |
udp_time=0; |
415 |
> |
} |
416 |
> |
if(heartbeat_exit==-1){ |
417 |
> |
errf("ah crap"); |
418 |
> |
exit(1); |
419 |
> |
} |
420 |
> |
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
421 |
> |
} |
422 |
|
|
423 |
< |
heartbeat_exit=heartbeat(&ihost_state); |
424 |
< |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
425 |
< |
errf("heartbeat needs to be reconfigured"); |
426 |
< |
ihost_configure(&ihost_state); |
423 |
> |
if(cur_time>=udp_time){ |
424 |
> |
/*printf("sending UDP\n");*/ |
425 |
> |
stat_grab_time=time(NULL); |
426 |
> |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
427 |
> |
errf("Failed to get stats (%m)"); |
428 |
> |
exit(1); |
429 |
> |
} |
430 |
> |
stat_grab_time=time(NULL)-stat_grab_time; |
431 |
> |
send_stats(&ihost_state, xml_stats); |
432 |
> |
free(xml_stats); |
433 |
> |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
434 |
|
} |
435 |
< |
if(heartbeat_exit==-1){ |
436 |
< |
errf("ah crap"); |
437 |
< |
exit(1); |
438 |
< |
} |
439 |
< |
printf("Count : %d\n",counter++); |
440 |
< |
printf("waiting %d\n",ihost_state.tcp_update_time); |
441 |
< |
sleep(ihost_state.tcp_update_time); |
435 |
> |
|
436 |
> |
if(tcp_time<udp_time){ |
437 |
> |
sleep_delay=tcp_time-time(NULL); |
438 |
> |
}else{ |
439 |
> |
sleep_delay=udp_time-time(NULL); |
440 |
> |
} |
441 |
> |
|
442 |
> |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
443 |
> |
if(sleep_delay>0) sleep(sleep_delay); |
444 |
|
} |
445 |
|
return 0; |
446 |
|
} |