ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.15
Committed: Sun May 19 15:14:31 2002 UTC (22 years ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.14: +0 -1 lines
Log Message:
Didn't mean to commit with the printf in :)

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 <stdlib.h>
22     #include <unistd.h>
23     #include <syslog.h>
24     #include <netinet/in.h>
25 pajs 1.9 #include "ukcprog.h"
26 pajs 1.1 #include <netdb.h>
27 pajs 1.12 #include <string.h>
28 pajs 1.9 #include "statgrab.h"
29 pajs 1.12 #include <time.h>
30 pajs 1.14 #include <sys/socket.h>
31 pajs 1.1
32 pajs 1.4 #define RECONFIGURE_RETURN_CODE 2
33 pajs 1.9 #define UDP_MAX_PACKET_SIZE 8192
34 pajs 1.4
35 pajs 1.1 typedef struct{
36     int fm_port;
37     char *fm_host;
38    
39     char *my_fqdn;
40     char *server_fqdn;
41     int server_udp_port;
42     int server_tcp_port;
43 pajs 1.4 char *last_modified;
44 pajs 1.1 char *files_list;
45     char *key;
46     int udp_update_time;
47     int tcp_update_time;
48    
49     }ihost_state_t;
50    
51 pajs 1.7 char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
52 pajs 1.1 char *reply;
53 pajs 1.7 fprintf(f_w, "%s\n", sendString);
54 pajs 1.2 fflush(f_w);
55     reply=fpgetline(f_r);
56 pajs 1.1 /* Returns pointer to static buffer */
57     return reply;
58     }
59    
60     int ihost_configure(ihost_state_t *ihost_state){
61     struct sockaddr_in addr;
62     struct in_addr haddr;
63     int sd;
64 pajs 1.2 FILE *fm_fd_r, *fm_fd_w;
65 pajs 1.1 char *reply;
66 pajs 1.2 char *reply_ptr;
67 pajs 1.1
68 pajs 1.5 /* 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 pajs 1.1 if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
75     errf("Can't create AF_INET socket (%m)");
76     return -1;
77     }
78    
79     if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
80     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
81     return -1;
82     }
83    
84 pajs 1.9 memset(&addr, 0, sizeof addr);
85 pajs 1.1 addr.sin_family = AF_INET;
86 pajs 1.9 memcpy(&addr.sin_addr, &haddr, sizeof haddr);
87 pajs 1.1 addr.sin_port = htons(ihost_state->fm_port);
88    
89     if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
90     errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
91     return -1;
92     }
93    
94 pajs 1.2 /* 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 pajs 1.8 errf("Failed to open read stream (%m)");
97 pajs 1.2 return -1;
98     }
99    
100     if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
101 pajs 1.8 errf("Failed to open write stream (%m)");
102 pajs 1.1 return -1;
103     }
104 pajs 1.2
105 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
106 pajs 1.1 if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
107     errf("Server error");
108     return -1;
109     }
110    
111 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
112 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
113 pajs 1.1 errf("Server error (%m)");
114     return -1;
115     }
116 pajs 1.7 if((ihost_state->last_modified=strdup(reply)) == NULL){
117     errf("strdup failed (%m)");
118 pajs 1.4 return -1;
119     }
120 pajs 1.1
121 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
122 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
123 pajs 1.1 errf("Server error (%m)");
124     return -1;
125     }
126 pajs 1.7 if((ihost_state->files_list=strdup(reply)) == NULL){
127     errf("strdup failed (%m)");
128 pajs 1.6 return -1;
129     }
130 pajs 1.1
131 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
132 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
133 pajs 1.1 errf("Server error (%m)");
134     return -1;
135     }
136     if((ihost_state->my_fqdn=strdup(reply)) == NULL){
137     errf("strdup failed (%m)");
138     return -1;
139     }
140    
141 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
142 pajs 1.1 if(reply== NULL){
143     errf("Server error (%m)");
144     return -1;
145     }
146     if (strncasecmp(reply, "ERROR", 5) != 0){
147     ihost_state->udp_update_time=atoi(reply);
148     }
149    
150 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
151 pajs 1.1 if(reply== NULL){
152     errf("Server error (%m)");
153     return -1;
154     }
155     if (strncasecmp(reply, "ERROR", 5) != 0){
156     ihost_state->tcp_update_time=atoi(reply);
157     }
158    
159 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
160 pajs 1.1 if(reply== NULL){
161     errf("Server error (%m)");
162     return -1;
163     }
164    
165 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
166 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
167 pajs 1.9 errf("Server error FILTER failed (%m)");
168 pajs 1.2 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 pajs 1.8 reply=reply_ptr + 1;
181 pajs 1.2 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 pajs 1.8 reply=reply_ptr+1;
189 pajs 1.2 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 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "END");
196 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
197     errf("Server error (%m)");
198     return -1;
199     }
200 pajs 1.1
201 pajs 1.2 if(fclose(fm_fd_r) !=0){
202 pajs 1.8 errf("Failed to close read FD (%m)");
203 pajs 1.2 return -1;
204     }
205     if(fclose(fm_fd_w) !=0){
206 pajs 1.8 errf("Failed to close write FD (%m)");
207 pajs 1.2 return -1;
208     }
209 pajs 1.1
210     return 0;
211     }
212 pajs 1.4
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 pajs 1.9 memset(&addr, 0, sizeof addr);
232 pajs 1.4 addr.sin_family = AF_INET;
233 pajs 1.9 memcpy(&addr.sin_addr, &haddr, sizeof haddr);
234 pajs 1.4 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 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
253 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
254 pajs 1.4 errf("Server error");
255     return -1;
256     }
257 pajs 1.9
258 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
259 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
260 pajs 1.4 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 pajs 1.6 if (strncasecmp(reply, "ERROR", 5) == 0){
276 pajs 1.4 /* Means the config has changed */
277     exitcode=RECONFIGURE_RETURN_CODE;
278     }
279 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
280 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
281 pajs 1.4 errf("Server error");
282     return -1;
283     }
284 pajs 1.5 if (ihost_state->key!=NULL) free(ihost_state->key);
285    
286 pajs 1.7 if((ihost_state->key=strdup(reply)) == NULL){
287     errf("strdup failed (%m)");
288 pajs 1.4 return -1;
289     }
290    
291 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
292 pajs 1.4 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
293     errf("Server error (%m)");
294     return -1;
295     }
296    
297 pajs 1.10 fflush(fm_fd_r);
298     fflush(fm_fd_w);
299    
300 pajs 1.6 if(fclose(fm_fd_r) !=0){
301 pajs 1.8 errf("Failed to close read FD (%m)");
302 pajs 1.6 return -1;
303     }
304     if(fclose(fm_fd_w) !=0){
305 pajs 1.8 errf("Failed to close write FD (%m)");
306 pajs 1.6 return -1;
307     }
308    
309 pajs 1.4 return exitcode;
310     }
311    
312 pajs 1.9 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 pajs 1.10 close(sd);
389    
390 pajs 1.9 return 0;
391     }
392 pajs 1.1
393 pajs 1.3 int main(int argc, char **argv){
394 pajs 1.2 ihost_state_t ihost_state;
395 pajs 1.6 int heartbeat_exit;
396     int counter=0;
397 pajs 1.10 long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0;
398 pajs 1.9 int sleep_delay=0;
399     char *xml_stats;
400 pajs 1.5
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 pajs 1.3 errf_set_progname(argv[0]);
410     if(argc!=3){
411     errf_usage("<host> <port>");
412     exit(1);
413     }
414 pajs 1.2
415 pajs 1.3 ihost_state.fm_host=argv[1];
416     ihost_state.fm_port=atoi(argv[2]);
417 pajs 1.2
418     if(ihost_configure(&ihost_state)!=0){
419     errf("configure failed");
420 pajs 1.6 /* 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 pajs 1.2 }
424 pajs 1.5
425 pajs 1.9 for(;;){
426     cur_time=time(NULL);
427     if(cur_time>=tcp_time){
428 pajs 1.10 /*printf("sending TCP\n");*/
429 pajs 1.9 heartbeat_exit=heartbeat(&ihost_state);
430     if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
431 pajs 1.10 /*errf("heartbeat needs to be reconfigured");*/
432 pajs 1.9 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 pajs 1.10
443 pajs 1.9 if(cur_time>=udp_time){
444 pajs 1.10 /*printf("sending UDP\n");*/
445     stat_grab_time=time(NULL);
446 pajs 1.13 if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){
447 pajs 1.9 errf("Failed to get stats (%m)");
448     exit(1);
449     }
450 pajs 1.10 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 pajs 1.9 }
455 pajs 1.10
456     if(tcp_time<udp_time){
457     sleep_delay=tcp_time-time(NULL);
458 pajs 1.9 }else{
459 pajs 1.10 sleep_delay=udp_time-time(NULL);
460 pajs 1.6 }
461 pajs 1.10
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 pajs 1.6 }
465 pajs 1.1 return 0;
466     }
467