ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.11
Committed: Sat May 18 18:15:56 2002 UTC (22 years ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.10: +19 -0 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

# User Rev Content
1 tdb 1.11 /*
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 pajs 1.1 #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 pajs 1.9 #include "ukcprog.h"
27 pajs 1.1 #include <netdb.h>
28     #include <strings.h>
29 pajs 1.9 #include "statgrab.h"
30 pajs 1.1
31 pajs 1.4 #define RECONFIGURE_RETURN_CODE 2
32 pajs 1.9 #define UDP_MAX_PACKET_SIZE 8192
33 pajs 1.4
34 pajs 1.1 typedef struct{
35     int fm_port;
36     char *fm_host;
37    
38     char *my_fqdn;
39     char *server_fqdn;
40     int server_udp_port;
41     int server_tcp_port;
42 pajs 1.4 char *last_modified;
43 pajs 1.1 char *files_list;
44     char *key;
45     int udp_update_time;
46     int tcp_update_time;
47    
48     }ihost_state_t;
49    
50 pajs 1.7 char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
51 pajs 1.1 char *reply;
52 pajs 1.7 fprintf(f_w, "%s\n", sendString);
53 pajs 1.2 fflush(f_w);
54     reply=fpgetline(f_r);
55 pajs 1.1 /* Returns pointer to static buffer */
56     return reply;
57     }
58    
59     int ihost_configure(ihost_state_t *ihost_state){
60     struct sockaddr_in addr;
61     struct in_addr haddr;
62     int sd;
63 pajs 1.2 FILE *fm_fd_r, *fm_fd_w;
64 pajs 1.1 char *reply;
65 pajs 1.2 char *reply_ptr;
66 pajs 1.1
67 pajs 1.5 /* 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 pajs 1.1 if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
74     errf("Can't create AF_INET socket (%m)");
75     return -1;
76     }
77    
78     if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
79     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
80     return -1;
81     }
82    
83 pajs 1.9 memset(&addr, 0, sizeof addr);
84 pajs 1.1 addr.sin_family = AF_INET;
85 pajs 1.9 memcpy(&addr.sin_addr, &haddr, sizeof haddr);
86 pajs 1.1 addr.sin_port = htons(ihost_state->fm_port);
87    
88     if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
89     errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
90     return -1;
91     }
92    
93 pajs 1.2 /* 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 pajs 1.8 errf("Failed to open read stream (%m)");
96 pajs 1.2 return -1;
97     }
98    
99     if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
100 pajs 1.8 errf("Failed to open write stream (%m)");
101 pajs 1.1 return -1;
102     }
103 pajs 1.2
104 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
105 pajs 1.1 if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
106     errf("Server error");
107     return -1;
108     }
109    
110 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
111 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
112 pajs 1.1 errf("Server error (%m)");
113     return -1;
114     }
115 pajs 1.7 if((ihost_state->last_modified=strdup(reply)) == NULL){
116     errf("strdup failed (%m)");
117 pajs 1.4 return -1;
118     }
119 pajs 1.1
120 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
121 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
122 pajs 1.1 errf("Server error (%m)");
123     return -1;
124     }
125 pajs 1.7 if((ihost_state->files_list=strdup(reply)) == NULL){
126     errf("strdup failed (%m)");
127 pajs 1.6 return -1;
128     }
129 pajs 1.1
130 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
131 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
132 pajs 1.1 errf("Server error (%m)");
133     return -1;
134     }
135     if((ihost_state->my_fqdn=strdup(reply)) == NULL){
136     errf("strdup failed (%m)");
137     return -1;
138     }
139    
140 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
141 pajs 1.1 if(reply== NULL){
142     errf("Server error (%m)");
143     return -1;
144     }
145     if (strncasecmp(reply, "ERROR", 5) != 0){
146     ihost_state->udp_update_time=atoi(reply);
147     }
148    
149 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
150 pajs 1.1 if(reply== NULL){
151     errf("Server error (%m)");
152     return -1;
153     }
154     if (strncasecmp(reply, "ERROR", 5) != 0){
155     ihost_state->tcp_update_time=atoi(reply);
156     }
157    
158 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
159 pajs 1.1 if(reply== NULL){
160     errf("Server error (%m)");
161     return -1;
162     }
163    
164 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
165 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
166 pajs 1.9 errf("Server error FILTER failed (%m)");
167 pajs 1.2 return -1;
168     }
169     reply_ptr=strchr(reply,';');
170     if (reply_ptr==NULL){
171     errf("Incorrect data returned");
172     return -1;
173     }
174     *reply_ptr='\0';
175     if((ihost_state->server_fqdn=strdup(reply)) == NULL){
176     errf("strdup failed (%m)");
177     return -1;
178     }
179 pajs 1.8 reply=reply_ptr + 1;
180 pajs 1.2 reply_ptr=strchr(reply,';');
181     if (reply_ptr==NULL){
182     errf("Incorrect data returned 2");
183     return -1;
184     }
185     *reply_ptr='\0';
186     ihost_state->server_udp_port=atoi(reply);
187 pajs 1.8 reply=reply_ptr+1;
188 pajs 1.2 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 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "END");
195 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
196     errf("Server error (%m)");
197     return -1;
198     }
199 pajs 1.1
200 pajs 1.2 if(fclose(fm_fd_r) !=0){
201 pajs 1.8 errf("Failed to close read FD (%m)");
202 pajs 1.2 return -1;
203     }
204     if(fclose(fm_fd_w) !=0){
205 pajs 1.8 errf("Failed to close write FD (%m)");
206 pajs 1.2 return -1;
207     }
208 pajs 1.1
209     return 0;
210     }
211 pajs 1.4
212     int heartbeat(ihost_state_t *ihost_state){
213     struct sockaddr_in addr;
214     struct in_addr haddr;
215     int sd;
216     FILE *fm_fd_r, *fm_fd_w;
217     char *reply;
218     int exitcode=0;
219    
220     if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
221     errf("Can't create AF_INET socket (%m)");
222     return -1;
223     }
224    
225     if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
226     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
227     return -1;
228     }
229    
230 pajs 1.9 memset(&addr, 0, sizeof addr);
231 pajs 1.4 addr.sin_family = AF_INET;
232 pajs 1.9 memcpy(&addr.sin_addr, &haddr, sizeof haddr);
233 pajs 1.4 addr.sin_port = htons(ihost_state->server_tcp_port);
234    
235     if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
236     errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
237     return -1;
238     }
239    
240     /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
241     if ((fm_fd_r=fdopen(sd,"r")) == NULL){
242     errf("Failed to open stream (%m)");
243     return -1;
244     }
245    
246     if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
247     errf("Failed to open stream (%m)");
248     return -1;
249     }
250    
251 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
252 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
253 pajs 1.4 errf("Server error");
254     return -1;
255     }
256 pajs 1.9
257 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
258 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
259 pajs 1.4 errf("Server error");
260     return -1;
261     }
262    
263     reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
264     if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
265     errf("Server error");
266     return -1;
267     }
268    
269     reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
270     if (reply==NULL) {
271     errf("Server error");
272     return -1;
273     }
274 pajs 1.6 if (strncasecmp(reply, "ERROR", 5) == 0){
275 pajs 1.4 /* Means the config has changed */
276     exitcode=RECONFIGURE_RETURN_CODE;
277     }
278 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
279 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
280 pajs 1.4 errf("Server error");
281     return -1;
282     }
283 pajs 1.5 if (ihost_state->key!=NULL) free(ihost_state->key);
284    
285 pajs 1.7 if((ihost_state->key=strdup(reply)) == NULL){
286     errf("strdup failed (%m)");
287 pajs 1.4 return -1;
288     }
289    
290 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
291 pajs 1.4 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
292     errf("Server error (%m)");
293     return -1;
294     }
295    
296 pajs 1.10 fflush(fm_fd_r);
297     fflush(fm_fd_w);
298    
299 pajs 1.6 if(fclose(fm_fd_r) !=0){
300 pajs 1.8 errf("Failed to close read FD (%m)");
301 pajs 1.6 return -1;
302     }
303     if(fclose(fm_fd_w) !=0){
304 pajs 1.8 errf("Failed to close write FD (%m)");
305 pajs 1.6 return -1;
306     }
307    
308 pajs 1.4 return exitcode;
309     }
310    
311 pajs 1.9 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 pajs 1.10 close(sd);
388    
389 pajs 1.9 return 0;
390     }
391 pajs 1.1
392 pajs 1.3 int main(int argc, char **argv){
393 pajs 1.2 ihost_state_t ihost_state;
394 pajs 1.6 int heartbeat_exit;
395     int counter=0;
396 pajs 1.10 long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0;
397 pajs 1.9 int sleep_delay=0;
398     char *xml_stats;
399 pajs 1.5
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 pajs 1.3 errf_set_progname(argv[0]);
409     if(argc!=3){
410     errf_usage("<host> <port>");
411     exit(1);
412     }
413 pajs 1.2
414 pajs 1.3 ihost_state.fm_host=argv[1];
415     ihost_state.fm_port=atoi(argv[2]);
416 pajs 1.2
417     if(ihost_configure(&ihost_state)!=0){
418     errf("configure failed");
419 pajs 1.6 /* 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 pajs 1.2 }
423 pajs 1.5
424 pajs 1.9 for(;;){
425     cur_time=time(NULL);
426     if(cur_time>=tcp_time){
427 pajs 1.10 /*printf("sending TCP\n");*/
428 pajs 1.9 heartbeat_exit=heartbeat(&ihost_state);
429     if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
430 pajs 1.10 /*errf("heartbeat needs to be reconfigured");*/
431 pajs 1.9 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 pajs 1.10
442 pajs 1.9 if(cur_time>=udp_time){
443 pajs 1.10 /*printf("sending UDP\n");*/
444     stat_grab_time=time(NULL);
445 pajs 1.9 if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){
446     errf("Failed to get stats (%m)");
447     exit(1);
448     }
449 pajs 1.10 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 pajs 1.9 }
454 pajs 1.10
455     if(tcp_time<udp_time){
456     sleep_delay=tcp_time-time(NULL);
457 pajs 1.9 }else{
458 pajs 1.10 sleep_delay=udp_time-time(NULL);
459 pajs 1.6 }
460 pajs 1.10
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 pajs 1.6 }
464 pajs 1.1 return 0;
465     }
466