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.2 by pajs, Fri May 10 20:05:05 2002 UTC vs.
Revision 1.19 by pajs, Tue May 21 14:23: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 <sys/types.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>
28 > #include <string.h>
29 > #include "statgrab.h"
30 > #include <time.h>
31 > #include <sys/socket.h>
32 > #include <netinet/in.h>
33 > #include <arpa/inet.h>
34 > #include <syslog.h>
35  
36 + #define versionNo 0.8
37 + #define RECONFIGURE_RETURN_CODE 2
38 + #define UDP_MAX_PACKET_SIZE 8192
39 + #define PID_FILE "/var/tmp/.ihost.pid"
40 +
41 + #define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0)
42 +
43   typedef struct{
44          int fm_port;
45          char *fm_host;
46 <        
46 >
47 >        char *my_ip;    
48          char *my_fqdn;
49          char *server_fqdn;
50          int server_udp_port;
51          int server_tcp_port;
52 <        long last_modified;
52 >        char *last_modified;
53          char *files_list;
54          char *key;
55          int udp_update_time;
# Line 24 | Line 57 | typedef struct{
57  
58   }ihost_state_t;
59  
60 < char* sock_comm(FILE *f_r, FILE *f_w, char* sendString){
60 > static int log_level;
61 > static int cur_level;
62 > static int syslog_logging;
63 >
64 > void log_errors(const char *message){
65 >        if(log_level>=cur_level){
66 >                if (syslog_logging==1){
67 >                        syslog(cur_level, "%s\n", message);
68 >                }else{
69 >                        fprintf(stderr, "%s\n", message);
70 >                }
71 >        }      
72 > }
73 >
74 > char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){
75          char *reply;
76 <        fprintf(f_w, "%s", sendString);
76 >        fprintf(f_w, "%s\n", sendString);
77          fflush(f_w);
78          reply=fpgetline(f_r);
79          /* Returns pointer to static buffer */
# Line 36 | Line 83 | char* sock_comm(FILE *f_r, FILE *f_w, char* sendString
83   int ihost_configure(ihost_state_t *ihost_state){
84          struct sockaddr_in addr;
85          struct in_addr haddr;
86 +        struct sockaddr ip;
87 +        int ip_len;
88          int sd;
89          FILE *fm_fd_r, *fm_fd_w;
90          char *reply;
91          char *reply_ptr;
92  
93 +        /* Check to see if anything needs to be free'd */
94 +        if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
95 +        if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
96 +        if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
97 +        if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
98 +
99 +        logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->fm_host, ihost_state->fm_port);
100          if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
101 <                errf("Can't create AF_INET socket (%m)");
101 >                logmessage(LOG_ERR, "Can't create AF_INET socket (%m)");
102                  return -1;
103          }
104  
105          if (get_host_addr(ihost_state->fm_host, &haddr) != 0){
106 <                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
106 >                logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host);
107                  return -1;
108          }
109  
110 <        memset((char *)&addr, 0, sizeof addr);
110 >        memset(&addr, 0, sizeof addr);
111          addr.sin_family = AF_INET;
112 <        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
112 >        memcpy(&addr.sin_addr, &haddr, sizeof haddr);
113          addr.sin_port =  htons(ihost_state->fm_port);
114  
115          if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
116 <                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
116 >                logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
117                  return -1;
118          }
119  
120          /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
121          if ((fm_fd_r=fdopen(sd,"r")) == NULL){
122 <                errf("Failed to open stream (%m)");
122 >                logmessage(LOG_ERR, "Failed to open read stream (%m)");
123                  return -1;
124          }
125  
126          if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
127 <                errf("Failed to open stream (%m)");
127 >                logmessage(LOG_ERR, "Failed to open write stream (%m)");
128                  return -1;
129          }
130 <
131 <        reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG\n");
130 >        ip_len=sizeof ip;
131 >        memset(&ip, 0, ip_len);
132 >        if((getsockname(sd, &ip, &ip_len)) != 0){
133 >                logmessage(LOG_ERR, "Failed to get IP address (%m)");
134 >                return -1;
135 >        }
136 >        if (ip.sa_family!=AF_INET){
137 >                logmessage(LOG_ERR, "sa family is wrong type");
138 >                return -1;
139 >        }
140 >        
141 >        if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){
142 >                logmessage(LOG_ERR, "Failed to get IP (%m)");
143 >                return -1;
144 >        }      
145 >        
146 >        logmessage(LOG_DEBUG, "Sending STARTCONIG");
147 >        reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG");
148          if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
149 <                errf("Server error");  
149 >                logmessage(LOG_ERR, "Server error on STARTCONFIG");    
150                  return -1;
151          }
152  
153 <        reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED\n");
153 >        logmessage(LOG_DEBUG, "Sending LASTMODIFIED");
154 >        reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED");
155          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
156 <                errf("Server error (%m)");
156 >                logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)");
157                  return -1;
158          }
159 <        ihost_state->last_modified=atol(reply);
160 <        
161 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
159 >        if((ihost_state->last_modified=strdup(reply)) == NULL){
160 >                logmessage(LOG_ERR, "strdup failed (%m)");
161 >                return -1;
162 >        }
163 >
164 >        logmessage(LOG_DEBUG, "Sending FILELIST");
165 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST");
166          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
167 <                errf("Server error (%m)");
167 >                logmessage(LOG_ERR, "Server error on FILELIST (%m)");
168                  return -1;
169          }
170 <        if((ihost_state->files_list=strdup(reply)) == NULL){
171 <                errf("strdup failed (%m)");
172 <                return -1;
173 <        }
170 >        if((ihost_state->files_list=strdup(reply)) == NULL){
171 >                logmessage(LOG_ERR, "strdup failed (%m)");
172 >                return -1;
173 >        }
174  
175 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n");
175 >        logmessage(LOG_DEBUG, "Sending FQDN");
176 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN");
177          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
178 <                errf("Server error (%m)");
178 >                logmessage(LOG_ERR, "Server error on FQDN (%m)");
179                  return -1;
180          }
181          if((ihost_state->my_fqdn=strdup(reply)) == NULL){
182 <                errf("strdup failed (%m)");
182 >                logmessage(LOG_ERR, "strdup failed (%m)");
183                  return -1;
184          }
185  
186 <        reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime\n");
186 >        logmessage(LOG_DEBUG, "Sending FQDN");
187 >        reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime");
188          if(reply== NULL){
189 <                errf("Server error (%m)");
189 >                logmessage(LOG_ERR, "Server error (%m)");
190                  return -1;
191          }
192          if (strncasecmp(reply, "ERROR", 5) != 0){
193                  ihost_state->udp_update_time=atoi(reply);
194          }
195 <
196 <        reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime\n");
195 >        
196 >        logmessage(LOG_DEBUG, "Sending TCPUpdateTime");
197 >        reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime");
198          if(reply== NULL){
199 <                errf("Server error (%m)");
199 >                logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)");
200                  return -1;
201          }
202          if (strncasecmp(reply, "ERROR", 5) != 0){
203                  ihost_state->tcp_update_time=atoi(reply);
204          }
205  
206 <        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG\n");
206 >        logmessage(LOG_DEBUG, "Sending ENDCONFIG");
207 >        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG");
208          if(reply== NULL){
209 <                errf("Server error (%m)");
209 >                logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)");
210                  return -1;
211          }
212  
213 <        reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER\n");
213 >        logmessage(LOG_DEBUG, "Sending FILTER");
214 >        reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER");
215          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
216 <                errf("Server error (%m)");
216 >                logmessage(LOG_ERR, "Server error FILTER failed (%m)");
217                  return -1;
218          }
219          reply_ptr=strchr(reply,';');
220          if (reply_ptr==NULL){
221 <                errf("Incorrect data returned");
221 >                logmessage(LOG_ERR, "Incorrect data returned");
222                  return -1;
223          }
224          *reply_ptr='\0';
225          if((ihost_state->server_fqdn=strdup(reply)) == NULL){
226 <                errf("strdup failed (%m)");
226 >                logmessage(LOG_ERR, "strdup failed (%m)");
227                  return -1;
228          }
229 <        reply=++reply_ptr;
229 >        reply=reply_ptr + 1;
230          reply_ptr=strchr(reply,';');
231          if (reply_ptr==NULL){
232 <                errf("Incorrect data returned 2");
232 >                logmessage(LOG_ERR, "Incorrect data returned 2");
233                  return -1;
234          }
235          *reply_ptr='\0';
236          ihost_state->server_udp_port=atoi(reply);
237 <        reply=++reply_ptr;
237 >        reply=reply_ptr+1;
238          ihost_state->server_tcp_port=atoi(reply);
239          if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){
240 <                errf("Incorrect data returned 3 ");
240 >                logmessage(LOG_ERR, "Incorrect data returned 3 ");
241                  return -1;
242          }
243  
244 <        reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
244 >        logmessage(LOG_DEBUG, "Sending END");
245 >        reply=sock_comm(fm_fd_r, fm_fd_w, "END");
246          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
247 <                errf("Server error (%m)");
247 >                logmessage(LOG_ERR, "Server error on END (%m)");
248                  return -1;
249          }
250  
251          if(fclose(fm_fd_r) !=0){
252 <                errf("Failed to close FD (%m)");
252 >                logmessage(LOG_ERR, "Failed to close read FD (%m)");
253                  return -1;
254          }
255          if(fclose(fm_fd_w) !=0){
256 <                errf("Failed to close FD (%m)");
256 >                logmessage(LOG_ERR, "Failed to close write FD (%m)");
257                  return -1;
258          }
259  
260          return 0;
261 + }
262  
263 + int heartbeat(ihost_state_t *ihost_state){
264 +        struct sockaddr_in addr;
265 +        struct in_addr haddr;
266 +        int sd;
267 +        FILE *fm_fd_r, *fm_fd_w;
268 +        char *reply;
269 +        int exitcode=0;
270 +
271 +        logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port);
272 +        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
273 +                logmessage(LOG_ERR, "Can't create AF_INET socket (%m)");
274 +                return -1;
275 +        }
276 +
277 +        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
278 +                logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn);
279 +                return -1;
280 +        }
281 +
282 +        memset(&addr, 0, sizeof addr);
283 +        addr.sin_family = AF_INET;
284 +        memcpy(&addr.sin_addr, &haddr, sizeof haddr);
285 +        addr.sin_port =  htons(ihost_state->server_tcp_port);
286 +
287 +        if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
288 +                logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port);
289 +                return -1;
290 +        }
291 +
292 +        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
293 +        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
294 +                logmessage(LOG_ERR, "Failed to open stream (%m)");
295 +                return -1;
296 +        }
297 +
298 +        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
299 +                logmessage(LOG_ERR, "Failed to open stream (%m)");
300 +                return -1;
301 +        }
302 +
303 +        logmessage(LOG_DEBUG, "Sending HEARTBEAT");
304 +        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT");
305 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
306 +                logmessage(LOG_ERR, "Server error on HEARTBEAT");
307 +                return -1;
308 +        }
309 +
310 +        logmessage(LOG_DEBUG, "Sending CONFIG");
311 +        reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG");
312 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
313 +                logmessage(LOG_ERR, "Server error on CONFIG");
314 +                return -1;
315 +        }
316 +
317 +        logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list);
318 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
319 +        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
320 +                logmessage(LOG_ERR, "Server error on fileslist");
321 +                return -1;
322 +        }
323 +
324 +        logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified);
325 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
326 +        if (reply==NULL) {
327 +                logmessage(LOG_ERR, "Server error NULL recieved on lastmodified");
328 +                return -1;
329 +        }
330 +        if (strncasecmp(reply, "ERROR", 5) == 0){
331 +        /* Means the config has changed */
332 +                logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required");
333 +                exitcode=RECONFIGURE_RETURN_CODE;
334 +        }
335 +
336 +        logmessage(LOG_DEBUG,"Sending KEY");
337 +        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY");
338 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
339 +                logmessage(LOG_ERR, "Server error on KEY");
340 +                return -1;
341 +        }
342 +        if (ihost_state->key!=NULL) free(ihost_state->key);
343 +
344 +        if((ihost_state->key=strdup(reply)) == NULL){
345 +                logmessage(LOG_ERR, "strdup failed (%m)");
346 +                return -1;
347 +        }
348 +
349 +        logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT");
350 +        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT");
351 +        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
352 +                logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)");
353 +                return -1;
354 +        }
355 +
356 +        fflush(fm_fd_r);
357 +        fflush(fm_fd_w);
358 +
359 +        if(fclose(fm_fd_r) !=0){
360 +                logmessage(LOG_ERR, "Failed to close read FD (%m)");
361 +                return -1;
362 +        }
363 +        if(fclose(fm_fd_w) !=0){
364 +                logmessage(LOG_ERR, "Failed to close write FD (%m)");
365 +                return -1;
366 +        }
367 +
368 +        return exitcode;                
369   }
370  
371 < int main(){
371 > char *stat_grab(ihost_state_t *ihost_state, int counter){
372 > #define NUM_STATS 9
373 >        char *stats[NUM_STATS];
374 >        char *xml_data=NULL;
375 >        char *xml_data_p;
376 >        int x=0;
377 >
378 >        logmessage(LOG_DEBUG,"get_cpu_stats");  
379 >        stats[0]=get_cpu_stats();
380 >        logmessage(LOG_DEBUG,"get_disk_stats");
381 >        stats[1]=get_disk_stats();
382 >        logmessage(LOG_DEBUG,"get_load_stats");
383 >        stats[2]=get_load_stats();      
384 >        logmessage(LOG_DEBUG,"get_memory_stats");              
385 >        stats[3]=get_memory_stats();
386 >        logmessage(LOG_DEBUG,"get_os_info");    
387 >        stats[4]=get_os_info();
388 >        logmessage(LOG_DEBUG,"get_page_stats");
389 >        stats[5]=get_page_stats();
390 >        logmessage(LOG_DEBUG,"get_process_stats");      
391 >        stats[6]=get_process_stats();
392 >        logmessage(LOG_DEBUG,"get_swap_stats");
393 >        stats[7]=get_swap_stats();
394 >        logmessage(LOG_DEBUG,"get_user_stats");
395 >        stats[8]=get_user_stats();
396 >
397 >        for(;x<NUM_STATS;x++){
398 >                if(stats[x]==NULL){
399 >                        logmessage(LOG_ERR,"Function returned NULL");
400 >                        return NULL;
401 >                }
402 >                if(xml_data==NULL){
403 >                        if((xml_data=strf("%s", stats[x])) == NULL){
404 >                                logmessage(LOG_ERR, "str failed (%m)");
405 >                                return NULL;
406 >                        }
407 >                }else{
408 >                        xml_data_p=xml_data;
409 >                        if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){
410 >                                logmessage(LOG_ERR, "str failed (%m)");
411 >                                return NULL;
412 >                        }
413 >                        free(xml_data_p);
414 >                }
415 >                free(stats[x]);
416 >        }
417 >        xml_data_p=xml_data;
418 >        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);
419 >        free(xml_data_p);
420 >
421 >        logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data);  
422 >        return xml_data;
423 > }
424 >
425 > int send_stats(ihost_state_t *ihost_state, char *data_stream){
426 >        struct sockaddr_in addr;
427 >        struct in_addr haddr;
428 >
429 >        int sd;
430 >        size_t len;
431 >
432 >        len=strlen(data_stream);
433 >        if(len>UDP_MAX_PACKET_SIZE){
434 >                logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile");
435 >                exit(1);
436 >        }
437 >        logmessage(LOG_DEBUG,"Resolving IP of server");
438 >        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
439 >                logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn);
440 >                return -1;
441 >        }
442 >        logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port);
443 >        if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
444 >                logmessage(LOG_ERR, "failed to create UDP socket (%m)");
445 >                return -1;
446 >        }
447 >
448 >        memset(&addr, 0, sizeof(addr));
449 >        addr.sin_family=AF_INET;
450 >        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
451 >        addr.sin_port =  htons(ihost_state->server_udp_port);
452 >
453 >        logmessage(LOG_INFO,"Sending packet : %s", data_stream);
454 >        if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){
455 >                logmessage(LOG_ERR, "Send the wrong number of bytes (%m)");
456 >                return -1;
457 >        }
458 >
459 >        close(sd);
460 >
461 >        return 0;      
462 > }
463 >
464 > void usage(char *progname){
465 >        fprintf(stderr, "Usage %s [options] server port\n", progname);
466 >        fprintf(stderr, "Options\n");
467 >        fprintf(stderr, "  -v           Verbose, the more v flags the more verbose, eg -vv\n");
468 >        fprintf(stderr, "  -d           Daemon mode, self backgrounding\n");
469 >        fprintf(stderr, "  -s           Send errors to syslog\n");
470 >        fprintf(stderr, "  -V           Print version number\n");
471 >        fprintf(stderr, "  -h           Prints this help page\n");      
472 >        exit(1);        
473 > }
474 >
475 > int main(int argc, char **argv){
476          ihost_state_t ihost_state;
477 +        int heartbeat_exit;
478 +        int counter=0;
479 +        long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0;
480 +        int sleep_delay=0;
481 +        char *xml_stats;
482 +        pid_t pid;
483 +        int cmdopt;
484 +        extern int optind;
485 +        int verbose=0, daemon=0;
486 +        extern int syslog_logging;
487 +        extern int log_level;
488 +        extern int cur_level;
489 +        FILE *f;
490  
491 <        ihost_state.fm_host=strdup("kernow.ukc.ac.uk");
492 <        ihost_state.fm_port=4567;
491 >        log_level=1;
492 >        cur_level=1;
493 >        syslog_logging=0;
494  
495 +        errf_set_ofunc(log_errors);
496 +        /* NULL'ify so i can tell if i need to free it or not */
497 +        ihost_state.fm_host=NULL;
498 +        ihost_state.my_fqdn=NULL;
499 +        ihost_state.server_fqdn=NULL;
500 +        ihost_state.last_modified=NULL;
501 +        ihost_state.files_list=NULL;
502 +        ihost_state.key=NULL;
503 +
504 +        errf_set_progname(argv[0]);
505 +        
506 +
507 +        while((cmdopt=getopt(argc, argv, "vdshV")) != -1){
508 +                switch(cmdopt){
509 +                        case 'v':
510 +                                verbose++;
511 +                                break;
512 +                        
513 +                        case 'd':
514 +                                /* Force syslog logging since stderr will be closed in this case */
515 +                                syslog_logging=1;
516 +                                daemon=1;
517 +                                break;
518 +                        
519 +                        case 's':
520 +                                syslog_logging=1;
521 +                                break;
522 +        
523 +                        case 'h':
524 +                                usage(argv[0]);
525 +                                break;
526 +
527 +                        case 'V':
528 +                                errf("%s version %f",argv[0], versionNo);
529 +                                break;
530 +
531 +                        default:
532 +                                usage(argv[0]);
533 +                                exit(1);
534 +                }
535 +        }
536 +
537 +        if(argc!=optind+2){
538 +                usage(argv[0]);
539 +                exit(1);
540 +        }
541 +        ihost_state.fm_host=argv[optind];
542 +        ihost_state.fm_port=atoi(argv[optind+1]);
543 +        if(ihost_state.fm_port==0){
544 +                errf("Invalid port number");
545 +                usage(argv[0]);
546 +        }
547 +
548 +        if(daemon==1){
549 +                pid=fork();
550 +                if(pid==-1){
551 +                        errf("Fork failed, can't background. Exiting");
552 +                        exit(1);
553 +                }else if(pid!=0){
554 +                        /* Parent process */
555 +                        return 0;
556 +                }
557 +                /* We should now be in the background*/
558 +                if(setsid()==-1){
559 +                        errf("setsid failed (%m)");
560 +                        exit(1);
561 +                }
562 +                fclose(stdin);
563 +                fclose(stdout);
564 +                fclose(stderr);
565 +        }
566 +
567 +        if(syslog_logging==1){
568 +                   openlog(errf_get_progname(),0,LOG_ERR);
569 +                   setlogmask(LOG_UPTO(LOG_DEBUG));
570 +        }
571 +                
572 +        switch(verbose){
573 +                case 0:
574 +                        /* Critical errors + */
575 +                        log_level=LOG_ERR;
576 +                        break;
577 +                case 1:
578 +                        /* Recoverable errors */
579 +                        log_level=LOG_WARNING;
580 +                        break;
581 +                case 2:
582 +                        /* Print stuff like the XML packets */
583 +                        log_level=LOG_INFO;
584 +                        break;
585 +                default:
586 +                        /* Must have lots of v's */
587 +                        /* Print out everything its doing */
588 +                        log_level=LOG_DEBUG;
589 +                        break;
590 +        }
591 +
592 +        logmessage(LOG_DEBUG,"Writing PID FILE");
593 +        pid=getpid();
594 +        if((f=fopen(PID_FILE,"w")) == NULL){
595 +                logmessage(LOG_WARNING, "Failed to write PID file");
596 +        }else{
597 +                if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){
598 +                        logmessage(LOG_WARNING, "Failed to write PID file");
599 +                }
600 +                if((fclose(f))!=0){
601 +                        logmessage(LOG_ERR, "failed to close PID file");
602 +                        exit(1);
603 +                }
604 +        }
605 +
606          if(ihost_configure(&ihost_state)!=0){
607 <                errf("configure failed");
607 >                logmessage(LOG_ERR,"configure failed");
608 >                /* Ok, ideally we prob should have 2 copies of the structure and carry on if this
609 >                happens.. But we dont :) (at the moment) */
610 >                exit(1);
611          }
612  
613 +        for(;;){
614 +                cur_time=time(NULL);
615 +                if(cur_time>=tcp_time){
616 +                        logmessage(LOG_DEBUG,"Sending heartbeat");
617 +                        heartbeat_exit=heartbeat(&ihost_state);
618 +                        if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
619 +                                logmessage(LOG_INFO,"heartbeat needs to be reconfigured");
620 +                                ihost_configure(&ihost_state);
621 +                                udp_time=0;
622 +                        }
623 +                        if(heartbeat_exit==-1){
624 +                                logmessage(LOG_ERR,"Heartbeat failed");
625 +                                exit(1);
626 +                        }
627 +                        tcp_time=time(NULL)+ihost_state.tcp_update_time;
628 +                        logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time);
629 +                }
630 +
631 +                if(cur_time>=udp_time){
632 +                        logmessage(LOG_DEBUG,"Sending udp data");
633 +
634 +                        stat_grab_time=time(NULL);
635 +                        if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){
636 +                                logmessage(LOG_ERR,"Failed to get stats (%m)");
637 +                                exit(1);
638 +                        }
639 +                        stat_grab_time=time(NULL)-stat_grab_time;
640 +                        send_stats(&ihost_state, xml_stats);
641 +                        free(xml_stats);
642 +                        udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time;
643 +                        logmessage(LOG_DEBUG,"next udp time should be %d", udp_time);
644 +                }
645 +
646 +                if(tcp_time<udp_time){
647 +                        sleep_delay=tcp_time-time(NULL);
648 +                }else{
649 +                        sleep_delay=udp_time-time(NULL);
650 +                }
651 +                logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay);
652 +                if(sleep_delay>0) sleep(sleep_delay);
653 +        }
654          return 0;
655   }
656  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines