1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
#include <stdio.h> |
21 |
|
#include <sys/socket.h> |
22 |
|
#include <stdlib.h> |
23 |
|
#include <unistd.h> |
24 |
|
#include <syslog.h> |
25 |
|
#include <netinet/in.h> |
26 |
< |
#include <ukcprog.h> |
26 |
> |
#include "ukcprog.h" |
27 |
|
#include <netdb.h> |
28 |
|
#include <strings.h> |
29 |
+ |
#include "statgrab.h" |
30 |
|
|
31 |
|
#define RECONFIGURE_RETURN_CODE 2 |
32 |
+ |
#define UDP_MAX_PACKET_SIZE 8192 |
33 |
|
|
34 |
|
typedef struct{ |
35 |
|
int fm_port; |
80 |
|
return -1; |
81 |
|
} |
82 |
|
|
83 |
< |
memset((char *)&addr, 0, sizeof addr); |
83 |
> |
memset(&addr, 0, sizeof addr); |
84 |
|
addr.sin_family = AF_INET; |
85 |
< |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
85 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
86 |
|
addr.sin_port = htons(ihost_state->fm_port); |
87 |
|
|
88 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
163 |
|
|
164 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
165 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
166 |
< |
errf("Server error (%m)"); |
166 |
> |
errf("Server error FILTER failed (%m)"); |
167 |
|
return -1; |
168 |
|
} |
169 |
|
reply_ptr=strchr(reply,';'); |
227 |
|
return -1; |
228 |
|
} |
229 |
|
|
230 |
< |
memset((char *)&addr, 0, sizeof addr); |
230 |
> |
memset(&addr, 0, sizeof addr); |
231 |
|
addr.sin_family = AF_INET; |
232 |
< |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
232 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
233 |
|
addr.sin_port = htons(ihost_state->server_tcp_port); |
234 |
|
|
235 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
253 |
|
errf("Server error"); |
254 |
|
return -1; |
255 |
|
} |
256 |
< |
if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); |
256 |
> |
|
257 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
258 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
259 |
|
errf("Server error"); |
293 |
|
return -1; |
294 |
|
} |
295 |
|
|
296 |
+ |
fflush(fm_fd_r); |
297 |
+ |
fflush(fm_fd_w); |
298 |
+ |
|
299 |
|
if(fclose(fm_fd_r) !=0){ |
300 |
|
errf("Failed to close read FD (%m)"); |
301 |
|
return -1; |
308 |
|
return exitcode; |
309 |
|
} |
310 |
|
|
311 |
+ |
char *stat_grab(ihost_state_t *ihost_state, int counter){ |
312 |
+ |
#define NUM_STATS 9 |
313 |
+ |
char *stats[NUM_STATS]; |
314 |
+ |
char *xml_data=NULL; |
315 |
+ |
char *xml_data_p; |
316 |
+ |
int x=0; |
317 |
+ |
|
318 |
+ |
stats[0]=get_cpu_stats(); |
319 |
+ |
stats[1]=get_disk_stats(); |
320 |
+ |
stats[2]=get_load_stats(); |
321 |
+ |
stats[3]=get_memory_stats(); |
322 |
+ |
stats[4]=get_os_info(); |
323 |
+ |
stats[5]=get_page_stats(); |
324 |
+ |
stats[6]=get_process_stats(); |
325 |
+ |
stats[7]=get_swap_stats(); |
326 |
+ |
stats[8]=get_user_stats(); |
327 |
|
|
328 |
+ |
for(;x<NUM_STATS;x++){ |
329 |
+ |
if(stats[x]==NULL){ |
330 |
+ |
return NULL; |
331 |
+ |
} |
332 |
+ |
if(xml_data==NULL){ |
333 |
+ |
if((xml_data=strf("%s", stats[x])) == NULL){ |
334 |
+ |
errf("str failed (%m)"); |
335 |
+ |
return NULL; |
336 |
+ |
} |
337 |
+ |
}else{ |
338 |
+ |
xml_data_p=xml_data; |
339 |
+ |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
340 |
+ |
errf("str failed (%m)"); |
341 |
+ |
return NULL; |
342 |
+ |
} |
343 |
+ |
free(xml_data_p); |
344 |
+ |
} |
345 |
+ |
free(stats[x]); |
346 |
+ |
} |
347 |
+ |
xml_data_p=xml_data; |
348 |
+ |
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); |
349 |
+ |
free(xml_data_p); |
350 |
+ |
|
351 |
+ |
return xml_data; |
352 |
+ |
} |
353 |
+ |
|
354 |
+ |
int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
355 |
+ |
struct sockaddr_in addr; |
356 |
+ |
struct in_addr haddr; |
357 |
+ |
|
358 |
+ |
int sd; |
359 |
+ |
size_t len; |
360 |
+ |
|
361 |
+ |
len=strlen(data_stream); |
362 |
+ |
if(len>UDP_MAX_PACKET_SIZE){ |
363 |
+ |
errf("Too big to send to server. Please reconfigure client and server and recompile"); |
364 |
+ |
exit(1); |
365 |
+ |
} |
366 |
+ |
|
367 |
+ |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
368 |
+ |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
369 |
+ |
return -1; |
370 |
+ |
} |
371 |
+ |
|
372 |
+ |
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
373 |
+ |
errf("failed to create UDP socket (%m)"); |
374 |
+ |
return -1; |
375 |
+ |
} |
376 |
+ |
|
377 |
+ |
memset(&addr, 0, sizeof(addr)); |
378 |
+ |
addr.sin_family=AF_INET; |
379 |
+ |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
380 |
+ |
addr.sin_port = htons(ihost_state->server_udp_port); |
381 |
+ |
|
382 |
+ |
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
383 |
+ |
errf("Send the wrong number of bytes (%m)"); |
384 |
+ |
return -1; |
385 |
+ |
} |
386 |
+ |
|
387 |
+ |
close(sd); |
388 |
+ |
|
389 |
+ |
return 0; |
390 |
+ |
} |
391 |
+ |
|
392 |
|
int main(int argc, char **argv){ |
393 |
|
ihost_state_t ihost_state; |
394 |
|
int heartbeat_exit; |
395 |
|
int counter=0; |
396 |
+ |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
397 |
+ |
int sleep_delay=0; |
398 |
+ |
char *xml_stats; |
399 |
|
|
293 |
– |
|
400 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
401 |
|
ihost_state.fm_host=NULL; |
402 |
|
ihost_state.my_fqdn=NULL; |
421 |
|
exit(1); |
422 |
|
} |
423 |
|
|
424 |
< |
while(TRUE){ |
424 |
> |
for(;;){ |
425 |
> |
cur_time=time(NULL); |
426 |
> |
if(cur_time>=tcp_time){ |
427 |
> |
/*printf("sending TCP\n");*/ |
428 |
> |
heartbeat_exit=heartbeat(&ihost_state); |
429 |
> |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
430 |
> |
/*errf("heartbeat needs to be reconfigured");*/ |
431 |
> |
ihost_configure(&ihost_state); |
432 |
> |
/* So udp doesn't wait til next sending before updating */ |
433 |
> |
udp_time=0; |
434 |
> |
} |
435 |
> |
if(heartbeat_exit==-1){ |
436 |
> |
errf("ah crap"); |
437 |
> |
exit(1); |
438 |
> |
} |
439 |
> |
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
440 |
> |
} |
441 |
|
|
442 |
< |
heartbeat_exit=heartbeat(&ihost_state); |
443 |
< |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
444 |
< |
errf("heartbeat needs to be reconfigured"); |
445 |
< |
ihost_configure(&ihost_state); |
442 |
> |
if(cur_time>=udp_time){ |
443 |
> |
/*printf("sending UDP\n");*/ |
444 |
> |
stat_grab_time=time(NULL); |
445 |
> |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
446 |
> |
errf("Failed to get stats (%m)"); |
447 |
> |
exit(1); |
448 |
> |
} |
449 |
> |
stat_grab_time=time(NULL)-stat_grab_time; |
450 |
> |
send_stats(&ihost_state, xml_stats); |
451 |
> |
free(xml_stats); |
452 |
> |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
453 |
|
} |
454 |
< |
if(heartbeat_exit==-1){ |
455 |
< |
errf("ah crap"); |
456 |
< |
exit(1); |
457 |
< |
} |
458 |
< |
printf("Count : %d\n",counter++); |
459 |
< |
printf("waiting %d\n",ihost_state.tcp_update_time); |
460 |
< |
sleep(ihost_state.tcp_update_time); |
454 |
> |
|
455 |
> |
if(tcp_time<udp_time){ |
456 |
> |
sleep_delay=tcp_time-time(NULL); |
457 |
> |
}else{ |
458 |
> |
sleep_delay=udp_time-time(NULL); |
459 |
> |
} |
460 |
> |
|
461 |
> |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
462 |
> |
if(sleep_delay>0) sleep(sleep_delay); |
463 |
|
} |
464 |
|
return 0; |
465 |
|
} |