1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
#include <stdio.h> |
2 |
– |
#include <sys/socket.h> |
22 |
|
#include <stdlib.h> |
23 |
+ |
#include <sys/types.h> |
24 |
|
#include <unistd.h> |
25 |
|
#include <syslog.h> |
26 |
|
#include <netinet/in.h> |
27 |
|
#include "ukcprog.h" |
28 |
|
#include <netdb.h> |
29 |
< |
#include <strings.h> |
29 |
> |
#include <string.h> |
30 |
|
#include "statgrab.h" |
31 |
+ |
#include <time.h> |
32 |
+ |
#include <sys/socket.h> |
33 |
+ |
#include <arpa/inet.h> |
34 |
+ |
#include <syslog.h> |
35 |
|
|
36 |
+ |
#define VERSION_NO "1.0rc1" |
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; |
57 |
|
|
58 |
|
}ihost_state_t; |
59 |
|
|
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 |
+ |
logmessage(LOG_DEBUG, "Sending %s",sendString); |
77 |
|
fprintf(f_w, "%s\n", sendString); |
78 |
|
fflush(f_w); |
79 |
|
reply=fpgetline(f_r); |
80 |
+ |
if (reply!=NULL) logmessage(LOG_DEBUG, "Received %s", reply); |
81 |
|
/* Returns pointer to static buffer */ |
82 |
|
return reply; |
83 |
|
} |
85 |
|
int ihost_configure(ihost_state_t *ihost_state){ |
86 |
|
struct sockaddr_in addr; |
87 |
|
struct in_addr haddr; |
88 |
+ |
struct sockaddr ip; |
89 |
+ |
int ip_len; |
90 |
|
int sd; |
91 |
|
FILE *fm_fd_r, *fm_fd_w; |
92 |
|
char *reply; |
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 |
+ |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->fm_host, ihost_state->fm_port); |
102 |
|
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
103 |
< |
errf("Can't create AF_INET socket (%m)"); |
103 |
> |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
104 |
|
return -1; |
105 |
|
} |
106 |
|
|
107 |
|
if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
108 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
108 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host); |
109 |
|
return -1; |
110 |
|
} |
111 |
|
|
115 |
|
addr.sin_port = htons(ihost_state->fm_port); |
116 |
|
|
117 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
118 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
118 |
> |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
119 |
|
return -1; |
120 |
|
} |
121 |
|
|
122 |
|
/* 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 |
< |
errf("Failed to open read stream (%m)"); |
124 |
> |
logmessage(LOG_ERR, "Failed to open read stream (%m)"); |
125 |
|
return -1; |
126 |
|
} |
127 |
|
|
128 |
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
129 |
< |
errf("Failed to open write stream (%m)"); |
129 |
> |
logmessage(LOG_ERR, "Failed to open write stream (%m)"); |
130 |
|
return -1; |
131 |
|
} |
132 |
+ |
ip_len=sizeof ip; |
133 |
+ |
memset(&ip, 0, ip_len); |
134 |
+ |
if((getsockname(sd, &ip, &ip_len)) != 0){ |
135 |
+ |
logmessage(LOG_ERR, "Failed to get IP address (%m)"); |
136 |
+ |
return -1; |
137 |
+ |
} |
138 |
+ |
if (ip.sa_family!=AF_INET){ |
139 |
+ |
logmessage(LOG_ERR, "sa family is wrong type"); |
140 |
+ |
return -1; |
141 |
+ |
} |
142 |
|
|
143 |
+ |
if(ihost_state->my_ip!=NULL) free(ihost_state->my_ip); |
144 |
+ |
if((ihost_state->my_ip=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){ |
145 |
+ |
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
146 |
+ |
return -1; |
147 |
+ |
} |
148 |
+ |
|
149 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
150 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
151 |
< |
errf("Server error"); |
151 |
> |
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
152 |
|
return -1; |
153 |
|
} |
154 |
|
|
155 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
156 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
157 |
< |
errf("Server error (%m)"); |
157 |
> |
logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
158 |
|
return -1; |
159 |
|
} |
160 |
|
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
161 |
< |
errf("strdup failed (%m)"); |
161 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
162 |
|
return -1; |
163 |
|
} |
164 |
< |
|
164 |
> |
|
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)"); |
171 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
172 |
|
return -1; |
173 |
|
} |
174 |
|
|
175 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
176 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
177 |
< |
errf("Server error (%m)"); |
177 |
> |
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
178 |
|
return -1; |
179 |
|
} |
180 |
|
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
181 |
< |
errf("strdup failed (%m)"); |
181 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
182 |
|
return -1; |
183 |
|
} |
184 |
|
|
185 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
186 |
|
if(reply== NULL){ |
187 |
< |
errf("Server error (%m)"); |
187 |
> |
logmessage(LOG_ERR, "Server error (%m)"); |
188 |
|
return -1; |
189 |
|
} |
190 |
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
191 |
|
ihost_state->udp_update_time=atoi(reply); |
192 |
|
} |
193 |
< |
|
193 |
> |
|
194 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
195 |
|
if(reply== NULL){ |
196 |
< |
errf("Server error (%m)"); |
196 |
> |
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
197 |
|
return -1; |
198 |
|
} |
199 |
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
202 |
|
|
203 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
204 |
|
if(reply== NULL){ |
205 |
< |
errf("Server error (%m)"); |
205 |
> |
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
206 |
|
return -1; |
207 |
|
} |
208 |
|
|
209 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
210 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
211 |
< |
errf("Server error FILTER failed (%m)"); |
211 |
> |
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
212 |
|
return -1; |
213 |
|
} |
214 |
|
reply_ptr=strchr(reply,';'); |
215 |
|
if (reply_ptr==NULL){ |
216 |
< |
errf("Incorrect data returned"); |
216 |
> |
logmessage(LOG_ERR, "Incorrect data returned"); |
217 |
|
return -1; |
218 |
|
} |
219 |
|
*reply_ptr='\0'; |
220 |
|
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
221 |
< |
errf("strdup failed (%m)"); |
221 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
222 |
|
return -1; |
223 |
|
} |
224 |
|
reply=reply_ptr + 1; |
225 |
|
reply_ptr=strchr(reply,';'); |
226 |
|
if (reply_ptr==NULL){ |
227 |
< |
errf("Incorrect data returned 2"); |
227 |
> |
logmessage(LOG_ERR, "Incorrect data returned 2"); |
228 |
|
return -1; |
229 |
|
} |
230 |
|
*reply_ptr='\0'; |
232 |
|
reply=reply_ptr+1; |
233 |
|
ihost_state->server_tcp_port=atoi(reply); |
234 |
|
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
235 |
< |
errf("Incorrect data returned 3 "); |
235 |
> |
logmessage(LOG_ERR, "Incorrect data returned 3 "); |
236 |
|
return -1; |
237 |
|
} |
238 |
|
|
239 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
240 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
241 |
< |
errf("Server error (%m)"); |
241 |
> |
logmessage(LOG_ERR, "Server error on END (%m)"); |
242 |
|
return -1; |
243 |
|
} |
244 |
|
|
245 |
|
if(fclose(fm_fd_r) !=0){ |
246 |
< |
errf("Failed to close read FD (%m)"); |
246 |
> |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
247 |
|
return -1; |
248 |
|
} |
249 |
|
if(fclose(fm_fd_w) !=0){ |
250 |
< |
errf("Failed to close write FD (%m)"); |
250 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
251 |
|
return -1; |
252 |
|
} |
253 |
|
|
262 |
|
char *reply; |
263 |
|
int exitcode=0; |
264 |
|
|
265 |
+ |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
266 |
|
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
267 |
< |
errf("Can't create AF_INET socket (%m)"); |
267 |
> |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
268 |
|
return -1; |
269 |
|
} |
270 |
|
|
271 |
|
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
272 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
272 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
273 |
|
return -1; |
274 |
|
} |
275 |
|
|
279 |
|
addr.sin_port = htons(ihost_state->server_tcp_port); |
280 |
|
|
281 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
282 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
282 |
> |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
283 |
|
return -1; |
284 |
|
} |
285 |
|
|
286 |
|
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
287 |
|
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
288 |
< |
errf("Failed to open stream (%m)"); |
288 |
> |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
289 |
|
return -1; |
290 |
|
} |
291 |
|
|
292 |
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
293 |
< |
errf("Failed to open stream (%m)"); |
293 |
> |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
294 |
|
return -1; |
295 |
|
} |
296 |
|
|
297 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
298 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
299 |
< |
errf("Server error"); |
299 |
> |
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
300 |
|
return -1; |
301 |
|
} |
302 |
|
|
303 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
304 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
305 |
< |
errf("Server error"); |
305 |
> |
logmessage(LOG_ERR, "Server error on CONFIG"); |
306 |
|
return -1; |
307 |
|
} |
308 |
|
|
309 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
310 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
311 |
< |
errf("Server error"); |
311 |
> |
logmessage(LOG_ERR, "Server error on fileslist"); |
312 |
|
return -1; |
313 |
|
} |
314 |
|
|
315 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
316 |
|
if (reply==NULL) { |
317 |
< |
errf("Server error"); |
317 |
> |
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
318 |
|
return -1; |
319 |
|
} |
320 |
|
if (strncasecmp(reply, "ERROR", 5) == 0){ |
321 |
|
/* Means the config has changed */ |
322 |
+ |
logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); |
323 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
324 |
|
} |
325 |
+ |
|
326 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
327 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
328 |
< |
errf("Server error"); |
328 |
> |
logmessage(LOG_ERR, "Server error on KEY"); |
329 |
|
return -1; |
330 |
|
} |
331 |
|
if (ihost_state->key!=NULL) free(ihost_state->key); |
332 |
|
|
333 |
|
if((ihost_state->key=strdup(reply)) == NULL){ |
334 |
< |
errf("strdup failed (%m)"); |
334 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
335 |
|
return -1; |
336 |
|
} |
337 |
|
|
338 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
339 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
340 |
< |
errf("Server error (%m)"); |
340 |
> |
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
341 |
|
return -1; |
342 |
|
} |
343 |
|
|
345 |
|
fflush(fm_fd_w); |
346 |
|
|
347 |
|
if(fclose(fm_fd_r) !=0){ |
348 |
< |
errf("Failed to close read FD (%m)"); |
348 |
> |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
349 |
|
return -1; |
350 |
|
} |
351 |
|
if(fclose(fm_fd_w) !=0){ |
352 |
< |
errf("Failed to close write FD (%m)"); |
352 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
353 |
|
return -1; |
354 |
|
} |
355 |
|
|
361 |
|
char *stats[NUM_STATS]; |
362 |
|
char *xml_data=NULL; |
363 |
|
char *xml_data_p; |
364 |
+ |
int xml_size=0; |
365 |
|
int x=0; |
366 |
< |
|
366 |
> |
|
367 |
> |
logmessage(LOG_DEBUG,"get_cpu_stats"); |
368 |
|
stats[0]=get_cpu_stats(); |
369 |
+ |
logmessage(LOG_DEBUG,"get_disk_stats"); |
370 |
|
stats[1]=get_disk_stats(); |
371 |
+ |
logmessage(LOG_DEBUG,"get_load_stats"); |
372 |
|
stats[2]=get_load_stats(); |
373 |
+ |
logmessage(LOG_DEBUG,"get_memory_stats"); |
374 |
|
stats[3]=get_memory_stats(); |
375 |
+ |
logmessage(LOG_DEBUG,"get_os_info"); |
376 |
|
stats[4]=get_os_info(); |
377 |
+ |
logmessage(LOG_DEBUG,"get_page_stats"); |
378 |
|
stats[5]=get_page_stats(); |
379 |
+ |
logmessage(LOG_DEBUG,"get_process_stats"); |
380 |
|
stats[6]=get_process_stats(); |
381 |
+ |
logmessage(LOG_DEBUG,"get_swap_stats"); |
382 |
|
stats[7]=get_swap_stats(); |
383 |
+ |
logmessage(LOG_DEBUG,"get_user_stats"); |
384 |
|
stats[8]=get_user_stats(); |
385 |
+ |
|
386 |
|
|
387 |
< |
for(;x<NUM_STATS;x++){ |
387 |
> |
for(x=0;x<NUM_STATS;x++){ |
388 |
|
if(stats[x]==NULL){ |
389 |
+ |
logmessage(LOG_ERR,"Function returned NULL"); |
390 |
|
return NULL; |
391 |
|
} |
392 |
< |
if(xml_data==NULL){ |
393 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
394 |
< |
errf("str failed (%m)"); |
395 |
< |
return NULL; |
396 |
< |
} |
397 |
< |
}else{ |
398 |
< |
xml_data_p=xml_data; |
399 |
< |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
321 |
< |
errf("str failed (%m)"); |
322 |
< |
return NULL; |
323 |
< |
} |
324 |
< |
free(xml_data_p); |
325 |
< |
} |
392 |
> |
xml_size+=strlen(stats[x]); |
393 |
> |
} |
394 |
> |
|
395 |
> |
xml_data=malloc(xml_size+1); |
396 |
> |
xml_data=strcpy(xml_data, stats[0]); |
397 |
> |
free(stats[0]); |
398 |
> |
for(x=1;x<NUM_STATS;x++){ |
399 |
> |
strcat(xml_data, stats[x]); |
400 |
|
free(stats[x]); |
401 |
|
} |
402 |
+ |
|
403 |
|
xml_data_p=xml_data; |
404 |
< |
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); |
404 |
> |
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); |
405 |
|
free(xml_data_p); |
406 |
< |
|
406 |
> |
|
407 |
> |
logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
408 |
|
return xml_data; |
409 |
|
} |
410 |
|
|
417 |
|
|
418 |
|
len=strlen(data_stream); |
419 |
|
if(len>UDP_MAX_PACKET_SIZE){ |
420 |
< |
errf("Too big to send to server. Please reconfigure client and server and recompile"); |
420 |
> |
logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
421 |
|
exit(1); |
422 |
|
} |
423 |
< |
|
423 |
> |
logmessage(LOG_DEBUG,"Resolving IP of server"); |
424 |
|
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
425 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
425 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
426 |
|
return -1; |
427 |
|
} |
428 |
< |
|
428 |
> |
logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
429 |
|
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
430 |
< |
errf("failed to create UDP socket (%m)"); |
430 |
> |
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
431 |
|
return -1; |
432 |
|
} |
433 |
|
|
436 |
|
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
437 |
|
addr.sin_port = htons(ihost_state->server_udp_port); |
438 |
|
|
439 |
+ |
logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
440 |
|
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
441 |
< |
errf("Send the wrong number of bytes (%m)"); |
441 |
> |
logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
442 |
|
return -1; |
443 |
|
} |
444 |
|
|
447 |
|
return 0; |
448 |
|
} |
449 |
|
|
450 |
+ |
void usage(char *progname){ |
451 |
+ |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
452 |
+ |
fprintf(stderr, "Options\n"); |
453 |
+ |
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
454 |
+ |
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
455 |
+ |
fprintf(stderr, " -s Send errors to syslog\n"); |
456 |
+ |
fprintf(stderr, " -V Print version number\n"); |
457 |
+ |
fprintf(stderr, " -h Prints this help page\n"); |
458 |
+ |
exit(1); |
459 |
+ |
} |
460 |
+ |
|
461 |
|
int main(int argc, char **argv){ |
462 |
|
ihost_state_t ihost_state; |
463 |
|
int heartbeat_exit; |
465 |
|
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
466 |
|
int sleep_delay=0; |
467 |
|
char *xml_stats; |
468 |
+ |
pid_t pid; |
469 |
+ |
int cmdopt; |
470 |
+ |
extern int optind; |
471 |
+ |
int verbose=0, daemon=0; |
472 |
+ |
extern int syslog_logging; |
473 |
+ |
extern int log_level; |
474 |
+ |
extern int cur_level; |
475 |
+ |
FILE *f; |
476 |
|
|
477 |
+ |
log_level=1; |
478 |
+ |
cur_level=1; |
479 |
+ |
syslog_logging=0; |
480 |
+ |
|
481 |
+ |
errf_set_ofunc(log_errors); |
482 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
483 |
|
ihost_state.fm_host=NULL; |
484 |
|
ihost_state.my_fqdn=NULL; |
486 |
|
ihost_state.last_modified=NULL; |
487 |
|
ihost_state.files_list=NULL; |
488 |
|
ihost_state.key=NULL; |
489 |
+ |
ihost_state.my_ip=NULL; |
490 |
|
|
491 |
|
errf_set_progname(argv[0]); |
492 |
< |
if(argc!=3){ |
493 |
< |
errf_usage("<host> <port>"); |
492 |
> |
|
493 |
> |
|
494 |
> |
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
495 |
> |
switch(cmdopt){ |
496 |
> |
case 'v': |
497 |
> |
verbose++; |
498 |
> |
break; |
499 |
> |
|
500 |
> |
case 'd': |
501 |
> |
/* Force syslog logging since stderr will be closed in this case */ |
502 |
> |
syslog_logging=1; |
503 |
> |
daemon=1; |
504 |
> |
break; |
505 |
> |
|
506 |
> |
case 's': |
507 |
> |
syslog_logging=1; |
508 |
> |
break; |
509 |
> |
|
510 |
> |
case 'h': |
511 |
> |
usage(argv[0]); |
512 |
> |
break; |
513 |
> |
|
514 |
> |
case 'V': |
515 |
> |
errf("%s version %s",argv[0], VERSION_NO); |
516 |
> |
break; |
517 |
> |
|
518 |
> |
default: |
519 |
> |
usage(argv[0]); |
520 |
> |
exit(1); |
521 |
> |
} |
522 |
> |
} |
523 |
> |
|
524 |
> |
if(argc!=optind+2){ |
525 |
> |
usage(argv[0]); |
526 |
|
exit(1); |
527 |
|
} |
528 |
+ |
ihost_state.fm_host=argv[optind]; |
529 |
+ |
ihost_state.fm_port=atoi(argv[optind+1]); |
530 |
+ |
if(ihost_state.fm_port==0){ |
531 |
+ |
errf("Invalid port number"); |
532 |
+ |
usage(argv[0]); |
533 |
+ |
} |
534 |
|
|
535 |
< |
ihost_state.fm_host=argv[1]; |
536 |
< |
ihost_state.fm_port=atoi(argv[2]); |
535 |
> |
if(daemon==1){ |
536 |
> |
pid=fork(); |
537 |
> |
if(pid==-1){ |
538 |
> |
errf("Fork failed, can't background. Exiting"); |
539 |
> |
exit(1); |
540 |
> |
}else if(pid!=0){ |
541 |
> |
/* Parent process */ |
542 |
> |
return 0; |
543 |
> |
} |
544 |
> |
/* We should now be in the background*/ |
545 |
> |
if(setsid()==-1){ |
546 |
> |
errf("setsid failed (%m)"); |
547 |
> |
exit(1); |
548 |
> |
} |
549 |
> |
fclose(stdin); |
550 |
> |
fclose(stdout); |
551 |
> |
fclose(stderr); |
552 |
> |
} |
553 |
|
|
554 |
+ |
if(syslog_logging==1){ |
555 |
+ |
openlog(errf_get_progname(),0,LOG_ERR); |
556 |
+ |
setlogmask(LOG_UPTO(LOG_DEBUG)); |
557 |
+ |
} |
558 |
+ |
|
559 |
+ |
switch(verbose){ |
560 |
+ |
case 0: |
561 |
+ |
/* Critical errors + */ |
562 |
+ |
log_level=LOG_ERR; |
563 |
+ |
break; |
564 |
+ |
case 1: |
565 |
+ |
/* Recoverable errors */ |
566 |
+ |
log_level=LOG_WARNING; |
567 |
+ |
break; |
568 |
+ |
case 2: |
569 |
+ |
/* Print stuff like the XML packets */ |
570 |
+ |
log_level=LOG_INFO; |
571 |
+ |
break; |
572 |
+ |
default: |
573 |
+ |
/* Must have lots of v's */ |
574 |
+ |
/* Print out everything its doing */ |
575 |
+ |
log_level=LOG_DEBUG; |
576 |
+ |
break; |
577 |
+ |
} |
578 |
+ |
|
579 |
+ |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
580 |
+ |
pid=getpid(); |
581 |
+ |
if((f=fopen(PID_FILE,"w")) == NULL){ |
582 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
583 |
+ |
}else{ |
584 |
+ |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
585 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file (%m)"); |
586 |
+ |
} |
587 |
+ |
if((fclose(f))!=0){ |
588 |
+ |
logmessage(LOG_ERR, "failed to close PID file"); |
589 |
+ |
exit(1); |
590 |
+ |
} |
591 |
+ |
} |
592 |
+ |
|
593 |
|
if(ihost_configure(&ihost_state)!=0){ |
594 |
< |
errf("configure failed"); |
594 |
> |
logmessage(LOG_ERR,"configure failed"); |
595 |
|
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
596 |
|
happens.. But we dont :) (at the moment) */ |
597 |
|
exit(1); |
600 |
|
for(;;){ |
601 |
|
cur_time=time(NULL); |
602 |
|
if(cur_time>=tcp_time){ |
603 |
< |
/*printf("sending TCP\n");*/ |
603 |
> |
logmessage(LOG_DEBUG,"Sending heartbeat"); |
604 |
|
heartbeat_exit=heartbeat(&ihost_state); |
605 |
|
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
606 |
< |
/*errf("heartbeat needs to be reconfigured");*/ |
606 |
> |
logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
607 |
|
ihost_configure(&ihost_state); |
413 |
– |
/* So udp doesn't wait til next sending before updating */ |
608 |
|
udp_time=0; |
609 |
|
} |
610 |
|
if(heartbeat_exit==-1){ |
611 |
< |
errf("ah crap"); |
611 |
> |
logmessage(LOG_ERR,"Heartbeat failed"); |
612 |
|
exit(1); |
613 |
|
} |
614 |
|
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
615 |
+ |
logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
616 |
|
} |
617 |
|
|
618 |
|
if(cur_time>=udp_time){ |
619 |
< |
/*printf("sending UDP\n");*/ |
619 |
> |
logmessage(LOG_DEBUG,"Sending udp data"); |
620 |
> |
/* Work out how long it takes to get the stats for next time round |
621 |
> |
so the sleep time can be adjusted accordingly */ |
622 |
|
stat_grab_time=time(NULL); |
623 |
< |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
624 |
< |
errf("Failed to get stats (%m)"); |
623 |
> |
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
624 |
> |
logmessage(LOG_ERR,"Failed to get stats (%m)"); |
625 |
|
exit(1); |
626 |
|
} |
627 |
|
stat_grab_time=time(NULL)-stat_grab_time; |
628 |
|
send_stats(&ihost_state, xml_stats); |
629 |
|
free(xml_stats); |
630 |
|
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
631 |
+ |
logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
632 |
|
} |
633 |
|
|
634 |
|
if(tcp_time<udp_time){ |
636 |
|
}else{ |
637 |
|
sleep_delay=udp_time-time(NULL); |
638 |
|
} |
639 |
< |
|
442 |
< |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
639 |
> |
logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
640 |
|
if(sleep_delay>0) sleep(sleep_delay); |
641 |
|
} |
642 |
|
return 0; |