ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.16
Committed: Sun May 19 19:42:41 2002 UTC (22 years, 7 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.15: +22 -2 lines
Log Message:
Now sends ip address of host. Doesn't cope with ip changing without at least
a reconfigure signal from the server. Only tested on solaris.

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.16 #include <netinet/in.h>
32     #include <arpa/inet.h>
33 pajs 1.1
34 pajs 1.4 #define RECONFIGURE_RETURN_CODE 2
35 pajs 1.9 #define UDP_MAX_PACKET_SIZE 8192
36 pajs 1.4
37 pajs 1.1 typedef struct{
38     int fm_port;
39     char *fm_host;
40 pajs 1.16
41     char *my_ip;
42 pajs 1.1 char *my_fqdn;
43     char *server_fqdn;
44     int server_udp_port;
45     int server_tcp_port;
46 pajs 1.4 char *last_modified;
47 pajs 1.1 char *files_list;
48     char *key;
49     int udp_update_time;
50     int tcp_update_time;
51    
52     }ihost_state_t;
53    
54 pajs 1.7 char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
55 pajs 1.1 char *reply;
56 pajs 1.7 fprintf(f_w, "%s\n", sendString);
57 pajs 1.2 fflush(f_w);
58     reply=fpgetline(f_r);
59 pajs 1.1 /* Returns pointer to static buffer */
60     return reply;
61     }
62    
63     int ihost_configure(ihost_state_t *ihost_state){
64     struct sockaddr_in addr;
65     struct in_addr haddr;
66 pajs 1.16 struct sockaddr ip;
67     int ip_len;
68 pajs 1.1 int sd;
69 pajs 1.2 FILE *fm_fd_r, *fm_fd_w;
70 pajs 1.1 char *reply;
71 pajs 1.2 char *reply_ptr;
72 pajs 1.1
73 pajs 1.5 /* Check to see if anything needs to be free'd */
74     if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
75     if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
76     if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
77     if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
78    
79 pajs 1.1 if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
80     errf("Can't create AF_INET socket (%m)");
81     return -1;
82     }
83    
84     if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
85     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
86     return -1;
87     }
88    
89 pajs 1.9 memset(&addr, 0, sizeof addr);
90 pajs 1.1 addr.sin_family = AF_INET;
91 pajs 1.9 memcpy(&addr.sin_addr, &haddr, sizeof haddr);
92 pajs 1.1 addr.sin_port = htons(ihost_state->fm_port);
93    
94     if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
95     errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
96     return -1;
97     }
98    
99 pajs 1.2 /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
100     if ((fm_fd_r=fdopen(sd,"r")) == NULL){
101 pajs 1.8 errf("Failed to open read stream (%m)");
102 pajs 1.2 return -1;
103     }
104    
105     if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
106 pajs 1.8 errf("Failed to open write stream (%m)");
107 pajs 1.1 return -1;
108     }
109 pajs 1.16 ip_len=sizeof ip;
110     memset(&ip, 0, ip_len);
111     if((getsockname(sd, &ip, &ip_len)) != 0){
112     errf("Failed to get IP address (%m)");
113     return -1;
114     }
115     if (ip.sa_family!=AF_INET){
116     errf("sa family is wrong type");
117     return -1;
118     }
119    
120     if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){
121     errf("Failed to get IP (%m)");
122     return -1;
123     }
124 pajs 1.2
125 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
126 pajs 1.1 if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
127     errf("Server error");
128     return -1;
129     }
130    
131 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
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 pajs 1.7 if((ihost_state->last_modified=strdup(reply)) == NULL){
137     errf("strdup failed (%m)");
138 pajs 1.4 return -1;
139     }
140 pajs 1.1
141 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
142 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
143 pajs 1.1 errf("Server error (%m)");
144     return -1;
145     }
146 pajs 1.7 if((ihost_state->files_list=strdup(reply)) == NULL){
147     errf("strdup failed (%m)");
148 pajs 1.6 return -1;
149     }
150 pajs 1.1
151 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
152 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
153 pajs 1.1 errf("Server error (%m)");
154     return -1;
155     }
156     if((ihost_state->my_fqdn=strdup(reply)) == NULL){
157     errf("strdup failed (%m)");
158     return -1;
159     }
160    
161 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
162 pajs 1.1 if(reply== NULL){
163     errf("Server error (%m)");
164     return -1;
165     }
166     if (strncasecmp(reply, "ERROR", 5) != 0){
167     ihost_state->udp_update_time=atoi(reply);
168     }
169    
170 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
171 pajs 1.1 if(reply== NULL){
172     errf("Server error (%m)");
173     return -1;
174     }
175     if (strncasecmp(reply, "ERROR", 5) != 0){
176     ihost_state->tcp_update_time=atoi(reply);
177     }
178    
179 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
180 pajs 1.1 if(reply== NULL){
181     errf("Server error (%m)");
182     return -1;
183     }
184    
185 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
186 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
187 pajs 1.9 errf("Server error FILTER failed (%m)");
188 pajs 1.2 return -1;
189     }
190     reply_ptr=strchr(reply,';');
191     if (reply_ptr==NULL){
192     errf("Incorrect data returned");
193     return -1;
194     }
195     *reply_ptr='\0';
196     if((ihost_state->server_fqdn=strdup(reply)) == NULL){
197     errf("strdup failed (%m)");
198     return -1;
199     }
200 pajs 1.8 reply=reply_ptr + 1;
201 pajs 1.2 reply_ptr=strchr(reply,';');
202     if (reply_ptr==NULL){
203     errf("Incorrect data returned 2");
204     return -1;
205     }
206     *reply_ptr='\0';
207     ihost_state->server_udp_port=atoi(reply);
208 pajs 1.8 reply=reply_ptr+1;
209 pajs 1.2 ihost_state->server_tcp_port=atoi(reply);
210     if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
211     errf("Incorrect data returned 3 ");
212     return -1;
213     }
214    
215 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "END");
216 pajs 1.2 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
217     errf("Server error (%m)");
218     return -1;
219     }
220 pajs 1.1
221 pajs 1.2 if(fclose(fm_fd_r) !=0){
222 pajs 1.8 errf("Failed to close read FD (%m)");
223 pajs 1.2 return -1;
224     }
225     if(fclose(fm_fd_w) !=0){
226 pajs 1.8 errf("Failed to close write FD (%m)");
227 pajs 1.2 return -1;
228     }
229 pajs 1.1
230     return 0;
231     }
232 pajs 1.4
233     int heartbeat(ihost_state_t *ihost_state){
234     struct sockaddr_in addr;
235     struct in_addr haddr;
236     int sd;
237     FILE *fm_fd_r, *fm_fd_w;
238     char *reply;
239     int exitcode=0;
240    
241     if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
242     errf("Can't create AF_INET socket (%m)");
243     return -1;
244     }
245    
246     if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
247     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
248     return -1;
249     }
250    
251 pajs 1.9 memset(&addr, 0, sizeof addr);
252 pajs 1.4 addr.sin_family = AF_INET;
253 pajs 1.9 memcpy(&addr.sin_addr, &haddr, sizeof haddr);
254 pajs 1.4 addr.sin_port = htons(ihost_state->server_tcp_port);
255    
256     if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
257     errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
258     return -1;
259     }
260    
261     /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
262     if ((fm_fd_r=fdopen(sd,"r")) == NULL){
263     errf("Failed to open stream (%m)");
264     return -1;
265     }
266    
267     if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
268     errf("Failed to open stream (%m)");
269     return -1;
270     }
271    
272 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
273 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
274 pajs 1.4 errf("Server error");
275     return -1;
276     }
277 pajs 1.9
278 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
279 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
280 pajs 1.4 errf("Server error");
281     return -1;
282     }
283    
284     reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
285     if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
286     errf("Server error");
287     return -1;
288     }
289    
290     reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
291     if (reply==NULL) {
292     errf("Server error");
293     return -1;
294     }
295 pajs 1.6 if (strncasecmp(reply, "ERROR", 5) == 0){
296 pajs 1.4 /* Means the config has changed */
297     exitcode=RECONFIGURE_RETURN_CODE;
298     }
299 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
300 pajs 1.6 if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
301 pajs 1.4 errf("Server error");
302     return -1;
303     }
304 pajs 1.5 if (ihost_state->key!=NULL) free(ihost_state->key);
305    
306 pajs 1.7 if((ihost_state->key=strdup(reply)) == NULL){
307     errf("strdup failed (%m)");
308 pajs 1.4 return -1;
309     }
310    
311 pajs 1.7 reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
312 pajs 1.4 if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
313     errf("Server error (%m)");
314     return -1;
315     }
316    
317 pajs 1.10 fflush(fm_fd_r);
318     fflush(fm_fd_w);
319    
320 pajs 1.6 if(fclose(fm_fd_r) !=0){
321 pajs 1.8 errf("Failed to close read FD (%m)");
322 pajs 1.6 return -1;
323     }
324     if(fclose(fm_fd_w) !=0){
325 pajs 1.8 errf("Failed to close write FD (%m)");
326 pajs 1.6 return -1;
327     }
328    
329 pajs 1.4 return exitcode;
330     }
331    
332 pajs 1.9 char *stat_grab(ihost_state_t *ihost_state, int counter){
333     #define NUM_STATS 9
334     char *stats[NUM_STATS];
335     char *xml_data=NULL;
336     char *xml_data_p;
337     int x=0;
338    
339     stats[0]=get_cpu_stats();
340     stats[1]=get_disk_stats();
341     stats[2]=get_load_stats();
342     stats[3]=get_memory_stats();
343     stats[4]=get_os_info();
344     stats[5]=get_page_stats();
345     stats[6]=get_process_stats();
346     stats[7]=get_swap_stats();
347     stats[8]=get_user_stats();
348    
349     for(;x<NUM_STATS;x++){
350     if(stats[x]==NULL){
351     return NULL;
352     }
353     if(xml_data==NULL){
354     if((xml_data=strf("%s", stats[x])) == NULL){
355     errf("str failed (%m)");
356     return NULL;
357     }
358     }else{
359     xml_data_p=xml_data;
360     if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){
361     errf("str failed (%m)");
362     return NULL;
363     }
364     free(xml_data_p);
365     }
366     free(stats[x]);
367     }
368     xml_data_p=xml_data;
369 pajs 1.16 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), ihost_state->my_ip, ihost_state->key, xml_data);
370 pajs 1.9 free(xml_data_p);
371    
372     return xml_data;
373     }
374    
375     int send_stats(ihost_state_t *ihost_state, char *data_stream){
376     struct sockaddr_in addr;
377     struct in_addr haddr;
378    
379     int sd;
380     size_t len;
381    
382     len=strlen(data_stream);
383     if(len>UDP_MAX_PACKET_SIZE){
384     errf("Too big to send to server. Please reconfigure client and server and recompile");
385     exit(1);
386     }
387    
388     if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
389     errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
390     return -1;
391     }
392    
393     if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
394     errf("failed to create UDP socket (%m)");
395     return -1;
396     }
397    
398     memset(&addr, 0, sizeof(addr));
399     addr.sin_family=AF_INET;
400     memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
401     addr.sin_port = htons(ihost_state->server_udp_port);
402    
403     if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){
404     errf("Send the wrong number of bytes (%m)");
405     return -1;
406     }
407    
408 pajs 1.10 close(sd);
409    
410 pajs 1.9 return 0;
411     }
412 pajs 1.1
413 pajs 1.3 int main(int argc, char **argv){
414 pajs 1.2 ihost_state_t ihost_state;
415 pajs 1.6 int heartbeat_exit;
416     int counter=0;
417 pajs 1.10 long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0;
418 pajs 1.9 int sleep_delay=0;
419     char *xml_stats;
420 pajs 1.5
421     /* NULL'ify so i can tell if i need to free it or not */
422     ihost_state.fm_host=NULL;
423     ihost_state.my_fqdn=NULL;
424     ihost_state.server_fqdn=NULL;
425     ihost_state.last_modified=NULL;
426     ihost_state.files_list=NULL;
427     ihost_state.key=NULL;
428    
429 pajs 1.3 errf_set_progname(argv[0]);
430     if(argc!=3){
431     errf_usage("<host> <port>");
432     exit(1);
433     }
434 pajs 1.2
435 pajs 1.3 ihost_state.fm_host=argv[1];
436     ihost_state.fm_port=atoi(argv[2]);
437 pajs 1.2
438     if(ihost_configure(&ihost_state)!=0){
439     errf("configure failed");
440 pajs 1.6 /* Ok, ideally we prob should have 2 copies of the structure and carry on if this
441     happens.. But we dont :) (at the moment) */
442     exit(1);
443 pajs 1.2 }
444 pajs 1.5
445 pajs 1.9 for(;;){
446     cur_time=time(NULL);
447     if(cur_time>=tcp_time){
448 pajs 1.10 /*printf("sending TCP\n");*/
449 pajs 1.9 heartbeat_exit=heartbeat(&ihost_state);
450     if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
451 pajs 1.10 /*errf("heartbeat needs to be reconfigured");*/
452 pajs 1.9 ihost_configure(&ihost_state);
453     /* So udp doesn't wait til next sending before updating */
454     udp_time=0;
455     }
456     if(heartbeat_exit==-1){
457     errf("ah crap");
458     exit(1);
459     }
460     tcp_time=time(NULL)+ihost_state.tcp_update_time;
461     }
462 pajs 1.10
463 pajs 1.9 if(cur_time>=udp_time){
464 pajs 1.10 /*printf("sending UDP\n");*/
465     stat_grab_time=time(NULL);
466 pajs 1.13 if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){
467 pajs 1.9 errf("Failed to get stats (%m)");
468     exit(1);
469     }
470 pajs 1.10 stat_grab_time=time(NULL)-stat_grab_time;
471     send_stats(&ihost_state, xml_stats);
472     free(xml_stats);
473     udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time;
474 pajs 1.9 }
475 pajs 1.10
476     if(tcp_time<udp_time){
477     sleep_delay=tcp_time-time(NULL);
478 pajs 1.9 }else{
479 pajs 1.10 sleep_delay=udp_time-time(NULL);
480 pajs 1.6 }
481 pajs 1.10
482     /*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/
483     if(sleep_delay>0) sleep(sleep_delay);
484 pajs 1.6 }
485 pajs 1.1 return 0;
486     }
487