ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
Revision: 1.21
Committed: Tue May 21 15:44:34 2002 UTC (22 years, 7 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.20: +2 -15 lines
Log Message:
Brain failure :) , now says what its sending sanely, and also says what its recieved.

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