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.7 by pajs, Mon May 13 09:22:02 2002 UTC vs.
Revision 1.15 by pajs, Sun May 19 15:14:31 2002 UTC

# Line 1 | Line 1
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>
2 #include <sys/socket.h>
21   #include <stdlib.h>
22   #include <unistd.h>
23   #include <syslog.h>
24   #include <netinet/in.h>
25 < #include <ukcprog.h>
25 > #include "ukcprog.h"
26   #include <netdb.h>
27 < #include <strings.h>
27 > #include <string.h>
28 > #include "statgrab.h"
29 > #include <time.h>
30 > #include <sys/socket.h>
31  
32   #define RECONFIGURE_RETURN_CODE 2
33 + #define UDP_MAX_PACKET_SIZE 8192
34  
35   typedef struct{
36          int fm_port;
# Line 59 | Line 81 | int ihost_configure(ihost_state_t *ihost_state){
81                  return -1;
82          }
83  
84 <        memset((char *)&addr, 0, sizeof addr);
84 >        memset(&addr, 0, sizeof addr);
85          addr.sin_family = AF_INET;
86 <        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
86 >        memcpy(&addr.sin_addr, &haddr, sizeof haddr);
87          addr.sin_port =  htons(ihost_state->fm_port);
88  
89          if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
# Line 71 | Line 93 | int ihost_configure(ihost_state_t *ihost_state){
93  
94          /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
95          if ((fm_fd_r=fdopen(sd,"r")) == NULL){
96 <                errf("Failed to open stream (%m)");
96 >                errf("Failed to open read stream (%m)");
97                  return -1;
98          }
99  
100          if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
101 <                errf("Failed to open stream (%m)");
101 >                errf("Failed to open write stream (%m)");
102                  return -1;
103          }
104  
# Line 142 | Line 164 | int ihost_configure(ihost_state_t *ihost_state){
164  
165          reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
166          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
167 <                errf("Server error (%m)");
167 >                errf("Server error FILTER failed (%m)");
168                  return -1;
169          }
170          reply_ptr=strchr(reply,';');
# Line 155 | Line 177 | int ihost_configure(ihost_state_t *ihost_state){
177                  errf("strdup failed (%m)");
178                  return -1;
179          }
180 <        reply=++reply_ptr;
180 >        reply=reply_ptr + 1;
181          reply_ptr=strchr(reply,';');
182          if (reply_ptr==NULL){
183                  errf("Incorrect data returned 2");
# Line 163 | Line 185 | int ihost_configure(ihost_state_t *ihost_state){
185          }
186          *reply_ptr='\0';
187          ihost_state->server_udp_port=atoi(reply);
188 <        reply=++reply_ptr;
188 >        reply=reply_ptr+1;
189          ihost_state->server_tcp_port=atoi(reply);
190          if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
191                  errf("Incorrect data returned 3 ");
# Line 177 | Line 199 | int ihost_configure(ihost_state_t *ihost_state){
199          }
200  
201          if(fclose(fm_fd_r) !=0){
202 <                errf("Failed to close FD (%m)");
202 >                errf("Failed to close read FD (%m)");
203                  return -1;
204          }
205          if(fclose(fm_fd_w) !=0){
206 <                errf("Failed to close FD (%m)");
206 >                errf("Failed to close write FD (%m)");
207                  return -1;
208          }
209  
# Line 206 | Line 228 | int heartbeat(ihost_state_t *ihost_state){
228                  return -1;
229          }
230  
231 <        memset((char *)&addr, 0, sizeof addr);
231 >        memset(&addr, 0, sizeof addr);
232          addr.sin_family = AF_INET;
233 <        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
233 >        memcpy(&addr.sin_addr, &haddr, sizeof haddr);
234          addr.sin_port =  htons(ihost_state->server_tcp_port);
235  
236          if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
# Line 232 | Line 254 | int heartbeat(ihost_state_t *ihost_state){
254                  errf("Server error");
255                  return -1;
256          }
257 <        if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host);
257 >
258          reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
259          if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
260                  errf("Server error");
# Line 272 | Line 294 | int heartbeat(ihost_state_t *ihost_state){
294                  return -1;
295          }
296  
297 +        fflush(fm_fd_r);
298 +        fflush(fm_fd_w);
299 +
300          if(fclose(fm_fd_r) !=0){
301 <                errf("Failed to close FD (%m)");
301 >                errf("Failed to close read FD (%m)");
302                  return -1;
303          }
304          if(fclose(fm_fd_w) !=0){
305 <                errf("Failed to close FD (%m)");
305 >                errf("Failed to close write FD (%m)");
306                  return -1;
307          }
308  
309          return exitcode;                
310   }
311  
312 + char *stat_grab(ihost_state_t *ihost_state, int counter){
313 + #define NUM_STATS 9
314 +        char *stats[NUM_STATS];
315 +        char *xml_data=NULL;
316 +        char *xml_data_p;
317 +        int x=0;
318 +        
319 +        stats[0]=get_cpu_stats();
320 +        stats[1]=get_disk_stats();
321 +        stats[2]=get_load_stats();      
322 +        stats[3]=get_memory_stats();
323 +        stats[4]=get_os_info();
324 +        stats[5]=get_page_stats();
325 +        stats[6]=get_process_stats();
326 +        stats[7]=get_swap_stats();
327 +        stats[8]=get_user_stats();
328  
329 +        for(;x<NUM_STATS;x++){
330 +                if(stats[x]==NULL){
331 +                        return NULL;
332 +                }
333 +                if(xml_data==NULL){
334 +                        if((xml_data=strf("%s", stats[x])) == NULL){
335 +                                errf("str failed (%m)");
336 +                                return NULL;
337 +                        }
338 +                }else{
339 +                        xml_data_p=xml_data;
340 +                        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 +                }
346 +                free(stats[x]);
347 +        }
348 +        xml_data_p=xml_data;
349 +        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);
350 +        free(xml_data_p);
351 +        
352 +        return xml_data;
353 + }
354 +
355 + int send_stats(ihost_state_t *ihost_state, char *data_stream){
356 +        struct sockaddr_in addr;
357 +        struct in_addr haddr;
358 +
359 +        int sd;
360 +        size_t len;
361 +
362 +        len=strlen(data_stream);
363 +        if(len>UDP_MAX_PACKET_SIZE){
364 +                errf("Too big to send to server. Please reconfigure client and server and recompile");
365 +                exit(1);
366 +        }
367 +        
368 +        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
369 +                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
370 +                return -1;
371 +        }
372 +
373 +        if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
374 +                errf("failed to create UDP socket (%m)");
375 +                return -1;
376 +        }
377 +
378 +        memset(&addr, 0, sizeof(addr));
379 +        addr.sin_family=AF_INET;
380 +        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
381 +        addr.sin_port =  htons(ihost_state->server_udp_port);
382 +
383 +        if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){
384 +                errf("Send the wrong number of bytes (%m)");
385 +                return -1;
386 +        }
387 +
388 +        close(sd);
389 +
390 +        return 0;      
391 + }
392 +
393   int main(int argc, char **argv){
394          ihost_state_t ihost_state;
395          int heartbeat_exit;
396          int counter=0;
397 +        long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0;
398 +        int sleep_delay=0;
399 +        char *xml_stats;
400  
293
401          /* NULL'ify so i can tell if i need to free it or not */
402          ihost_state.fm_host=NULL;
403          ihost_state.my_fqdn=NULL;
# Line 315 | Line 422 | int main(int argc, char **argv){
422                  exit(1);
423          }
424  
425 <        while(TRUE){
425 >        for(;;){
426 >                cur_time=time(NULL);
427 >                if(cur_time>=tcp_time){
428 >                        /*printf("sending TCP\n");*/
429 >                        heartbeat_exit=heartbeat(&ihost_state);
430 >                        if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
431 >                                /*errf("heartbeat needs to be reconfigured");*/
432 >                                ihost_configure(&ihost_state);
433 >                                /* So udp doesn't wait til next sending before updating */
434 >                                udp_time=0;
435 >                        }
436 >                        if(heartbeat_exit==-1){
437 >                                errf("ah crap");
438 >                                exit(1);
439 >                        }
440 >                        tcp_time=time(NULL)+ihost_state.tcp_update_time;
441 >                }
442  
443 <                heartbeat_exit=heartbeat(&ihost_state);
444 <                if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
445 <                        errf("heartbeat needs to be reconfigured");
446 <                        ihost_configure(&ihost_state);
443 >                if(cur_time>=udp_time){
444 >                        /*printf("sending UDP\n");*/
445 >                        stat_grab_time=time(NULL);
446 >                        if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){
447 >                                errf("Failed to get stats (%m)");
448 >                                exit(1);
449 >                        }
450 >                        stat_grab_time=time(NULL)-stat_grab_time;
451 >                        send_stats(&ihost_state, xml_stats);
452 >                        free(xml_stats);
453 >                        udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time;
454                  }
455 <                if(heartbeat_exit==-1){
456 <                        errf("ah crap");
457 <                        exit(1);
458 <                }
459 <                printf("Count : %d\n",counter++);
460 <                printf("waiting %d\n",ihost_state.tcp_update_time);
461 <                sleep(ihost_state.tcp_update_time);
455 >
456 >                if(tcp_time<udp_time){
457 >                        sleep_delay=tcp_time-time(NULL);
458 >                }else{
459 >                        sleep_delay=udp_time-time(NULL);
460 >                }
461 >
462 >                /*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/
463 >                if(sleep_delay>0) sleep(sleep_delay);
464          }
465          return 0;
466   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines