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.4 by pajs, Sun May 12 12:00:33 2002 UTC vs.
Revision 1.11 by tdb, Sat May 18 18:15:56 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>
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;
# Line 26 | Line 47 | typedef struct{
47  
48   }ihost_state_t;
49  
50 < char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){
50 > char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
51          char *reply;
52 <        fprintf(f_w, "%s", sendString);
52 >        fprintf(f_w, "%s\n", sendString);
53          fflush(f_w);
54          reply=fpgetline(f_r);
55          /* Returns pointer to static buffer */
# Line 43 | Line 64 | int ihost_configure(ihost_state_t *ihost_state){
64          char *reply;
65          char *reply_ptr;
66  
67 +        /* Check to see if anything needs to be free'd */
68 +        if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
69 +        if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
70 +        if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
71 +        if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
72 +
73          if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
74                  errf("Can't create AF_INET socket (%m)");
75                  return -1;
# Line 53 | Line 80 | int ihost_configure(ihost_state_t *ihost_state){
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) {
# Line 65 | Line 92 | int ihost_configure(ihost_state_t *ihost_state){
92  
93          /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
94          if ((fm_fd_r=fdopen(sd,"r")) == NULL){
95 <                errf("Failed to open stream (%m)");
95 >                errf("Failed to open read stream (%m)");
96                  return -1;
97          }
98  
99          if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
100 <                errf("Failed to open stream (%m)");
100 >                errf("Failed to open write stream (%m)");
101                  return -1;
102          }
103  
104 <        reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n");
104 >        reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
105          if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
106                  errf("Server error");  
107                  return -1;
108          }
109  
110 <        reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n");
110 >        reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
111          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
112                  errf("Server error (%m)");
113                  return -1;
# Line 90 | Line 117 | int ihost_configure(ihost_state_t *ihost_state){
117                  return -1;
118          }
119          
120 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
120 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
121          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
122                  errf("Server error (%m)");
123                  return -1;
124          }
125 <        if((ihost_state->files_list=strdup(reply)) == NULL){
126 <                errf("strdup failed (%m)");
127 <                return -1;
128 <        }
125 >        if((ihost_state->files_list=strdup(reply)) == NULL){
126 >                errf("strdup failed (%m)");
127 >                return -1;
128 >        }
129  
130 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n");
130 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
131          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
132                  errf("Server error (%m)");
133                  return -1;
# Line 110 | Line 137 | int ihost_configure(ihost_state_t *ihost_state){
137                  return -1;
138          }
139  
140 <        reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n");
140 >        reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
141          if(reply== NULL){
142                  errf("Server error (%m)");
143                  return -1;
# Line 119 | Line 146 | int ihost_configure(ihost_state_t *ihost_state){
146                  ihost_state->udp_update_time=atoi(reply);
147          }
148  
149 <        reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n");
149 >        reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
150          if(reply== NULL){
151                  errf("Server error (%m)");
152                  return -1;
# Line 128 | Line 155 | int ihost_configure(ihost_state_t *ihost_state){
155                  ihost_state->tcp_update_time=atoi(reply);
156          }
157  
158 <        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n");
158 >        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
159          if(reply== NULL){
160                  errf("Server error (%m)");
161                  return -1;
162          }
163  
164 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n");
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,';');
# Line 149 | Line 176 | int ihost_configure(ihost_state_t *ihost_state){
176                  errf("strdup failed (%m)");
177                  return -1;
178          }
179 <        reply=++reply_ptr;
179 >        reply=reply_ptr + 1;
180          reply_ptr=strchr(reply,';');
181          if (reply_ptr==NULL){
182                  errf("Incorrect data returned 2");
# Line 157 | Line 184 | int ihost_configure(ihost_state_t *ihost_state){
184          }
185          *reply_ptr='\0';
186          ihost_state->server_udp_port=atoi(reply);
187 <        reply=++reply_ptr;
187 >        reply=reply_ptr+1;
188          ihost_state->server_tcp_port=atoi(reply);
189          if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
190                  errf("Incorrect data returned 3 ");
191                  return -1;
192          }
193  
194 <        reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
194 >        reply=sock_comm(fm_fd_r, fm_fd_w, "END");
195          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
196                  errf("Server error (%m)");
197                  return -1;
198          }
199  
200          if(fclose(fm_fd_r) !=0){
201 <                errf("Failed to close FD (%m)");
201 >                errf("Failed to close read FD (%m)");
202                  return -1;
203          }
204          if(fclose(fm_fd_w) !=0){
205 <                errf("Failed to close FD (%m)");
205 >                errf("Failed to close write FD (%m)");
206                  return -1;
207          }
208  
# Line 200 | Line 227 | int heartbeat(ihost_state_t *ihost_state){
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) {
# Line 221 | Line 248 | int heartbeat(ihost_state_t *ihost_state){
248                  return -1;
249          }
250  
251 <        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n");
252 <        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
251 >        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
252 >        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
253                  errf("Server error");
254                  return -1;
255          }
256  
257 <        reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n");
258 <        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
257 >        reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
258 >        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
259                  errf("Server error");
260                  return -1;
261          }
# Line 244 | Line 271 | int heartbeat(ihost_state_t *ihost_state){
271                  errf("Server error");
272                  return -1;
273          }
274 <        if (strncasecmp(reply, "ERROR", 2) == 0){
274 >        if (strncasecmp(reply, "ERROR", 5) == 0){
275          /* Means the config has changed */
276                  exitcode=RECONFIGURE_RETURN_CODE;
277          }
278 <        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n");
279 <        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
278 >        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
279 >        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
280                  errf("Server error");
281                  return -1;
282          }
283 <        
283 >        if (ihost_state->key!=NULL) free(ihost_state->key);
284 >
285          if((ihost_state->key=strdup(reply)) == NULL){
286                  errf("strdup failed (%m)");
287                  return -1;
288          }
289  
290 <        reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
290 >        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
291          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
292                  errf("Server error (%m)");
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;
302 +        }
303 +        if(fclose(fm_fd_w) !=0){
304 +                errf("Failed to close write FD (%m)");
305 +                return -1;
306 +        }
307 +
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 +
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;
403 +        ihost_state.server_fqdn=NULL;
404 +        ihost_state.last_modified=NULL;
405 +        ihost_state.files_list=NULL;
406 +        ihost_state.key=NULL;
407 +
408          errf_set_progname(argv[0]);
409          if(argc!=3){
410                  errf_usage("<host> <port>");    
# Line 282 | Line 416 | int main(int argc, char **argv){
416  
417          if(ihost_configure(&ihost_state)!=0){
418                  errf("configure failed");
419 +                /* Ok, ideally we prob should have 2 copies of the structure and carry on if this
420 +                happens.. But we dont :) (at the moment) */
421 +                exit(1);
422          }
423  
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 +                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 +
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   }
466  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines