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.1 by pajs, Fri May 10 17:11:30 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;
37          char *fm_host;
# Line 16 | Line 40 | typedef struct{
40          char *server_fqdn;
41          int server_udp_port;
42          int server_tcp_port;
43 <        long last_modified;
43 >        char *last_modified;
44          char *files_list;
45          char *key;
46          int udp_update_time;
# Line 24 | Line 48 | typedef struct{
48  
49   }ihost_state_t;
50  
51 < char* sock_comm(FILE *f, char* sendString){
51 > char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
52          char *reply;
53 <
54 <        fprintf(f, sendString);
55 <        fflush(f);
32 <        reply=fpgetline(f);
53 >        fprintf(f_w, "%s\n", sendString);
54 >        fflush(f_w);
55 >        reply=fpgetline(f_r);
56          /* Returns pointer to static buffer */
57          return reply;
58   }      
# Line 38 | Line 61 | int ihost_configure(ihost_state_t *ihost_state){
61          struct sockaddr_in addr;
62          struct in_addr haddr;
63          int sd;
64 <        FILE *fm_fd;
64 >        FILE *fm_fd_r, *fm_fd_w;
65          char *reply;
66 +        char *reply_ptr;
67  
68 +        /* Check to see if anything needs to be free'd */
69 +        if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
70 +        if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
71 +        if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
72 +        if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
73 +
74          if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
75                  errf("Can't create AF_INET socket (%m)");
76                  return -1;
# Line 51 | 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 61 | Line 91 | int ihost_configure(ihost_state_t *ihost_state){
91                  return -1;
92          }
93  
94 <        if ((fm_fd=fdopen(sd,"r+")) == NULL){
95 <                errf("Failed to open stream (%m)");
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 read stream (%m)");
97                  return -1;
98          }
99 <        
100 <        reply=sock_comm(fm_fd, "STARTCONFIG\n");
99 >
100 >        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
101 >                errf("Failed to open write stream (%m)");
102 >                return -1;
103 >        }
104 >
105 >        reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
106          if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
107                  errf("Server error");  
108                  return -1;
109          }
110  
111 <        reply=sock_comm(fm_fd, "LASTMODIFIED\n");
112 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){
111 >        reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
112 >        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
113                  errf("Server error (%m)");
114                  return -1;
115          }
116 <        ihost_state->last_modified=atol(reply);
116 >        if((ihost_state->last_modified=strdup(reply)) == NULL){
117 >                errf("strdup failed (%m)");
118 >                return -1;
119 >        }
120          
121 <        reply=sock_comm(fm_fd, "FILELIST\n");
122 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){
121 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
122 >        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
123                  errf("Server error (%m)");
124                  return -1;
125          }
126 <        if((ihost_state->files_list=strdup(reply)) == NULL){
127 <                errf("strdup failed (%m)");
128 <                return -1;
129 <        }
126 >        if((ihost_state->files_list=strdup(reply)) == NULL){
127 >                errf("strdup failed (%m)");
128 >                return -1;
129 >        }
130  
131 <        reply=sock_comm(fm_fd, "FQDN\n");
132 <        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5))){
131 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
132 >        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
133                  errf("Server error (%m)");
134                  return -1;
135          }
# Line 99 | Line 138 | int ihost_configure(ihost_state_t *ihost_state){
138                  return -1;
139          }
140  
141 <        reply=sock_comm(fm_fd, "UDPUpdateTime\n");
141 >        reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
142          if(reply== NULL){
143                  errf("Server error (%m)");
144                  return -1;
# Line 108 | Line 147 | int ihost_configure(ihost_state_t *ihost_state){
147                  ihost_state->udp_update_time=atoi(reply);
148          }
149  
150 <        reply=sock_comm(fm_fd, "TCPUpdateTime\n");
150 >        reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
151          if(reply== NULL){
152                  errf("Server error (%m)");
153                  return -1;
# Line 117 | Line 156 | int ihost_configure(ihost_state_t *ihost_state){
156                  ihost_state->tcp_update_time=atoi(reply);
157          }
158  
159 <        reply=sock_comm(fm_fd, "ENDCONFIG\n");
159 >        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
160          if(reply== NULL){
161                  errf("Server error (%m)");
162                  return -1;
163          }
164  
165 <                
165 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
166 >        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
167 >                errf("Server error FILTER failed (%m)");
168 >                return -1;
169 >        }
170 >        reply_ptr=strchr(reply,';');
171 >        if (reply_ptr==NULL){
172 >                errf("Incorrect data returned");
173 >                return -1;
174 >        }
175 >        *reply_ptr='\0';
176 >        if((ihost_state->server_fqdn=strdup(reply)) == NULL){
177 >                errf("strdup failed (%m)");
178 >                return -1;
179 >        }
180 >        reply=reply_ptr + 1;
181 >        reply_ptr=strchr(reply,';');
182 >        if (reply_ptr==NULL){
183 >                errf("Incorrect data returned 2");
184 >                return -1;
185 >        }
186 >        *reply_ptr='\0';
187 >        ihost_state->server_udp_port=atoi(reply);
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 ");
192 >                return -1;
193 >        }
194  
195 +        reply=sock_comm(fm_fd_r, fm_fd_w, "END");
196 +        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
197 +                errf("Server error (%m)");
198 +                return -1;
199 +        }
200  
201 +        if(fclose(fm_fd_r) !=0){
202 +                errf("Failed to close read FD (%m)");
203 +                return -1;
204 +        }
205 +        if(fclose(fm_fd_w) !=0){
206 +                errf("Failed to close write FD (%m)");
207 +                return -1;
208 +        }
209 +
210          return 0;
211 + }
212  
213 + int heartbeat(ihost_state_t *ihost_state){
214 +        struct sockaddr_in addr;
215 +        struct in_addr haddr;
216 +        int sd;
217 +        FILE *fm_fd_r, *fm_fd_w;
218 +        char *reply;
219 +        int exitcode=0;
220 +
221 +        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
222 +                errf("Can't create AF_INET socket (%m)");
223 +                return -1;
224 +        }
225 +
226 +        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
227 +                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
228 +                return -1;
229 +        }
230 +
231 +        memset(&addr, 0, sizeof addr);
232 +        addr.sin_family = AF_INET;
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) {
237 +                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
238 +                return -1;
239 +        }
240 +
241 +        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
242 +        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
243 +                errf("Failed to open stream (%m)");
244 +                return -1;
245 +        }
246 +
247 +        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
248 +                errf("Failed to open stream (%m)");
249 +                return -1;
250 +        }
251 +
252 +        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
253 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
254 +                errf("Server error");
255 +                return -1;
256 +        }
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");
261 +                return -1;
262 +        }
263 +
264 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
265 +        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
266 +                errf("Server error");
267 +                return -1;
268 +        }
269 +
270 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
271 +        if (reply==NULL) {
272 +                errf("Server error");
273 +                return -1;
274 +        }
275 +        if (strncasecmp(reply, "ERROR", 5) == 0){
276 +        /* Means the config has changed */
277 +                exitcode=RECONFIGURE_RETURN_CODE;
278 +        }
279 +        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
280 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
281 +                errf("Server error");
282 +                return -1;
283 +        }
284 +        if (ihost_state->key!=NULL) free(ihost_state->key);
285 +
286 +        if((ihost_state->key=strdup(reply)) == NULL){
287 +                errf("strdup failed (%m)");
288 +                return -1;
289 +        }
290 +
291 +        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
292 +        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
293 +                errf("Server error (%m)");
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 read FD (%m)");
302 +                return -1;
303 +        }
304 +        if(fclose(fm_fd_w) !=0){
305 +                errf("Failed to close write FD (%m)");
306 +                return -1;
307 +        }
308 +
309 +        return exitcode;                
310   }
311  
312 < int main(){
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 +
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;
404 +        ihost_state.server_fqdn=NULL;
405 +        ihost_state.last_modified=NULL;
406 +        ihost_state.files_list=NULL;
407 +        ihost_state.key=NULL;
408 +
409 +        errf_set_progname(argv[0]);
410 +        if(argc!=3){
411 +                errf_usage("<host> <port>");    
412 +                exit(1);
413 +        }
414 +
415 +        ihost_state.fm_host=argv[1];
416 +        ihost_state.fm_port=atoi(argv[2]);
417 +
418 +        if(ihost_configure(&ihost_state)!=0){
419 +                errf("configure failed");
420 +                /* Ok, ideally we prob should have 2 copies of the structure and carry on if this
421 +                happens.. But we dont :) (at the moment) */
422 +                exit(1);
423 +        }
424 +
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 +                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 +
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   }
467  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines