18 |
|
*/ |
19 |
|
|
20 |
|
#include <stdio.h> |
21 |
– |
#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" |
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; |
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 |
|
fprintf(f_w, "%s\n", 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; |
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 |
|
|
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 read 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 write stream (%m)"); |
127 |
> |
logmessage(LOG_ERR, "Failed to open write stream (%m)"); |
128 |
|
return -1; |
129 |
|
} |
130 |
< |
|
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 |
+ |
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 |
|
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
160 |
< |
errf("strdup failed (%m)"); |
160 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
161 |
|
return -1; |
162 |
|
} |
163 |
< |
|
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)"); |
171 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
172 |
|
return -1; |
173 |
|
} |
174 |
|
|
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 |
+ |
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 |
< |
|
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 |
+ |
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 |
+ |
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 FILTER failed (%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 + 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'; |
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 |
+ |
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 read 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 write FD (%m)"); |
256 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
257 |
|
return -1; |
258 |
|
} |
259 |
|
|
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 |
< |
errf("Can't create AF_INET socket (%m)"); |
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 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
278 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
279 |
|
return -1; |
280 |
|
} |
281 |
|
|
285 |
|
addr.sin_port = htons(ihost_state->server_tcp_port); |
286 |
|
|
287 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
288 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
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 |
< |
errf("Failed to open stream (%m)"); |
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 |
< |
errf("Failed to open stream (%m)"); |
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 |
< |
errf("Server error"); |
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 |
< |
errf("Server error"); |
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 |
< |
errf("Server error"); |
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 |
< |
errf("Server error"); |
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 |
< |
errf("Server error"); |
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 |
< |
errf("strdup failed (%m)"); |
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 |
< |
errf("Server error (%m)"); |
352 |
> |
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
353 |
|
return -1; |
354 |
|
} |
355 |
|
|
357 |
|
fflush(fm_fd_w); |
358 |
|
|
359 |
|
if(fclose(fm_fd_r) !=0){ |
360 |
< |
errf("Failed to close read FD (%m)"); |
360 |
> |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
361 |
|
return -1; |
362 |
|
} |
363 |
|
if(fclose(fm_fd_w) !=0){ |
364 |
< |
errf("Failed to close write FD (%m)"); |
364 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
365 |
|
return -1; |
366 |
|
} |
367 |
|
|
373 |
|
char *stats[NUM_STATS]; |
374 |
|
char *xml_data=NULL; |
375 |
|
char *xml_data_p; |
376 |
+ |
int xml_size=0; |
377 |
|
int x=0; |
378 |
< |
|
378 |
> |
|
379 |
> |
logmessage(LOG_DEBUG,"get_cpu_stats"); |
380 |
|
stats[0]=get_cpu_stats(); |
381 |
+ |
logmessage(LOG_DEBUG,"get_disk_stats"); |
382 |
|
stats[1]=get_disk_stats(); |
383 |
+ |
logmessage(LOG_DEBUG,"get_load_stats"); |
384 |
|
stats[2]=get_load_stats(); |
385 |
+ |
logmessage(LOG_DEBUG,"get_memory_stats"); |
386 |
|
stats[3]=get_memory_stats(); |
387 |
+ |
logmessage(LOG_DEBUG,"get_os_info"); |
388 |
|
stats[4]=get_os_info(); |
389 |
+ |
logmessage(LOG_DEBUG,"get_page_stats"); |
390 |
|
stats[5]=get_page_stats(); |
391 |
+ |
logmessage(LOG_DEBUG,"get_process_stats"); |
392 |
|
stats[6]=get_process_stats(); |
393 |
+ |
logmessage(LOG_DEBUG,"get_swap_stats"); |
394 |
|
stats[7]=get_swap_stats(); |
395 |
+ |
logmessage(LOG_DEBUG,"get_user_stats"); |
396 |
|
stats[8]=get_user_stats(); |
397 |
+ |
|
398 |
|
|
399 |
< |
for(;x<NUM_STATS;x++){ |
399 |
> |
for(x=0;x<NUM_STATS;x++){ |
400 |
|
if(stats[x]==NULL){ |
401 |
+ |
logmessage(LOG_ERR,"Function returned NULL"); |
402 |
|
return NULL; |
403 |
|
} |
404 |
< |
if(xml_data==NULL){ |
333 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
334 |
< |
errf("str failed (%m)"); |
335 |
< |
return NULL; |
336 |
< |
} |
337 |
< |
}else{ |
338 |
< |
xml_data_p=xml_data; |
339 |
< |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
340 |
< |
errf("str failed (%m)"); |
341 |
< |
return NULL; |
342 |
< |
} |
343 |
< |
free(xml_data_p); |
344 |
< |
} |
345 |
< |
free(stats[x]); |
404 |
> |
xml_size+=strlen(stats[x]); |
405 |
|
} |
406 |
+ |
|
407 |
+ |
xml_data=malloc(xml_size); |
408 |
+ |
xml_data=strcpy(xml_data, stats[0]); |
409 |
+ |
|
410 |
+ |
for(x=1;x<NUM_STATS;x++){ |
411 |
+ |
strcat(xml_data, stats[x]); |
412 |
+ |
} |
413 |
+ |
|
414 |
|
xml_data_p=xml_data; |
415 |
< |
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); |
415 |
> |
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); |
416 |
|
free(xml_data_p); |
417 |
< |
|
417 |
> |
|
418 |
> |
logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
419 |
|
return xml_data; |
420 |
|
} |
421 |
|
|
428 |
|
|
429 |
|
len=strlen(data_stream); |
430 |
|
if(len>UDP_MAX_PACKET_SIZE){ |
431 |
< |
errf("Too big to send to server. Please reconfigure client and server and recompile"); |
431 |
> |
logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
432 |
|
exit(1); |
433 |
|
} |
434 |
< |
|
434 |
> |
logmessage(LOG_DEBUG,"Resolving IP of server"); |
435 |
|
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
436 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
436 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
437 |
|
return -1; |
438 |
|
} |
439 |
< |
|
439 |
> |
logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
440 |
|
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
441 |
< |
errf("failed to create UDP socket (%m)"); |
441 |
> |
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
442 |
|
return -1; |
443 |
|
} |
444 |
|
|
447 |
|
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
448 |
|
addr.sin_port = htons(ihost_state->server_udp_port); |
449 |
|
|
450 |
+ |
logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
451 |
|
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
452 |
< |
errf("Send the wrong number of bytes (%m)"); |
452 |
> |
logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
453 |
|
return -1; |
454 |
|
} |
455 |
|
|
458 |
|
return 0; |
459 |
|
} |
460 |
|
|
461 |
+ |
void usage(char *progname){ |
462 |
+ |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
463 |
+ |
fprintf(stderr, "Options\n"); |
464 |
+ |
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
465 |
+ |
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
466 |
+ |
fprintf(stderr, " -s Send errors to syslog\n"); |
467 |
+ |
fprintf(stderr, " -V Print version number\n"); |
468 |
+ |
fprintf(stderr, " -h Prints this help page\n"); |
469 |
+ |
exit(1); |
470 |
+ |
} |
471 |
+ |
|
472 |
|
int main(int argc, char **argv){ |
473 |
|
ihost_state_t ihost_state; |
474 |
|
int heartbeat_exit; |
476 |
|
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
477 |
|
int sleep_delay=0; |
478 |
|
char *xml_stats; |
479 |
+ |
pid_t pid; |
480 |
+ |
int cmdopt; |
481 |
+ |
extern int optind; |
482 |
+ |
int verbose=0, daemon=0; |
483 |
+ |
extern int syslog_logging; |
484 |
+ |
extern int log_level; |
485 |
+ |
extern int cur_level; |
486 |
+ |
FILE *f; |
487 |
|
|
488 |
+ |
log_level=1; |
489 |
+ |
cur_level=1; |
490 |
+ |
syslog_logging=0; |
491 |
+ |
|
492 |
+ |
errf_set_ofunc(log_errors); |
493 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
494 |
|
ihost_state.fm_host=NULL; |
495 |
|
ihost_state.my_fqdn=NULL; |
499 |
|
ihost_state.key=NULL; |
500 |
|
|
501 |
|
errf_set_progname(argv[0]); |
502 |
< |
if(argc!=3){ |
503 |
< |
errf_usage("<host> <port>"); |
502 |
> |
|
503 |
> |
|
504 |
> |
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
505 |
> |
switch(cmdopt){ |
506 |
> |
case 'v': |
507 |
> |
verbose++; |
508 |
> |
break; |
509 |
> |
|
510 |
> |
case 'd': |
511 |
> |
/* Force syslog logging since stderr will be closed in this case */ |
512 |
> |
syslog_logging=1; |
513 |
> |
daemon=1; |
514 |
> |
break; |
515 |
> |
|
516 |
> |
case 's': |
517 |
> |
syslog_logging=1; |
518 |
> |
break; |
519 |
> |
|
520 |
> |
case 'h': |
521 |
> |
usage(argv[0]); |
522 |
> |
break; |
523 |
> |
|
524 |
> |
case 'V': |
525 |
> |
errf("%s version %f",argv[0], versionNo); |
526 |
> |
break; |
527 |
> |
|
528 |
> |
default: |
529 |
> |
usage(argv[0]); |
530 |
> |
exit(1); |
531 |
> |
} |
532 |
> |
} |
533 |
> |
|
534 |
> |
if(argc!=optind+2){ |
535 |
> |
usage(argv[0]); |
536 |
|
exit(1); |
537 |
|
} |
538 |
+ |
ihost_state.fm_host=argv[optind]; |
539 |
+ |
ihost_state.fm_port=atoi(argv[optind+1]); |
540 |
+ |
if(ihost_state.fm_port==0){ |
541 |
+ |
errf("Invalid port number"); |
542 |
+ |
usage(argv[0]); |
543 |
+ |
} |
544 |
|
|
545 |
< |
ihost_state.fm_host=argv[1]; |
546 |
< |
ihost_state.fm_port=atoi(argv[2]); |
545 |
> |
if(daemon==1){ |
546 |
> |
pid=fork(); |
547 |
> |
if(pid==-1){ |
548 |
> |
errf("Fork failed, can't background. Exiting"); |
549 |
> |
exit(1); |
550 |
> |
}else if(pid!=0){ |
551 |
> |
/* Parent process */ |
552 |
> |
return 0; |
553 |
> |
} |
554 |
> |
/* We should now be in the background*/ |
555 |
> |
if(setsid()==-1){ |
556 |
> |
errf("setsid failed (%m)"); |
557 |
> |
exit(1); |
558 |
> |
} |
559 |
> |
fclose(stdin); |
560 |
> |
fclose(stdout); |
561 |
> |
fclose(stderr); |
562 |
> |
} |
563 |
|
|
564 |
+ |
if(syslog_logging==1){ |
565 |
+ |
openlog(errf_get_progname(),0,LOG_ERR); |
566 |
+ |
setlogmask(LOG_UPTO(LOG_DEBUG)); |
567 |
+ |
} |
568 |
+ |
|
569 |
+ |
switch(verbose){ |
570 |
+ |
case 0: |
571 |
+ |
/* Critical errors + */ |
572 |
+ |
log_level=LOG_ERR; |
573 |
+ |
break; |
574 |
+ |
case 1: |
575 |
+ |
/* Recoverable errors */ |
576 |
+ |
log_level=LOG_WARNING; |
577 |
+ |
break; |
578 |
+ |
case 2: |
579 |
+ |
/* Print stuff like the XML packets */ |
580 |
+ |
log_level=LOG_INFO; |
581 |
+ |
break; |
582 |
+ |
default: |
583 |
+ |
/* Must have lots of v's */ |
584 |
+ |
/* Print out everything its doing */ |
585 |
+ |
log_level=LOG_DEBUG; |
586 |
+ |
break; |
587 |
+ |
} |
588 |
+ |
|
589 |
+ |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
590 |
+ |
pid=getpid(); |
591 |
+ |
if((f=fopen(PID_FILE,"w")) == NULL){ |
592 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file"); |
593 |
+ |
}else{ |
594 |
+ |
if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){ |
595 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file"); |
596 |
+ |
} |
597 |
+ |
if((fclose(f))!=0){ |
598 |
+ |
logmessage(LOG_ERR, "failed to close PID file"); |
599 |
+ |
exit(1); |
600 |
+ |
} |
601 |
+ |
} |
602 |
+ |
|
603 |
|
if(ihost_configure(&ihost_state)!=0){ |
604 |
< |
errf("configure failed"); |
604 |
> |
logmessage(LOG_ERR,"configure failed"); |
605 |
|
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
606 |
|
happens.. But we dont :) (at the moment) */ |
607 |
|
exit(1); |
610 |
|
for(;;){ |
611 |
|
cur_time=time(NULL); |
612 |
|
if(cur_time>=tcp_time){ |
613 |
< |
/*printf("sending TCP\n");*/ |
613 |
> |
logmessage(LOG_DEBUG,"Sending heartbeat"); |
614 |
|
heartbeat_exit=heartbeat(&ihost_state); |
615 |
|
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
616 |
< |
/*errf("heartbeat needs to be reconfigured");*/ |
616 |
> |
logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
617 |
|
ihost_configure(&ihost_state); |
432 |
– |
/* So udp doesn't wait til next sending before updating */ |
618 |
|
udp_time=0; |
619 |
|
} |
620 |
|
if(heartbeat_exit==-1){ |
621 |
< |
errf("ah crap"); |
621 |
> |
logmessage(LOG_ERR,"Heartbeat failed"); |
622 |
|
exit(1); |
623 |
|
} |
624 |
|
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
625 |
+ |
logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
626 |
|
} |
627 |
|
|
628 |
|
if(cur_time>=udp_time){ |
629 |
< |
/*printf("sending UDP\n");*/ |
629 |
> |
logmessage(LOG_DEBUG,"Sending udp data"); |
630 |
> |
|
631 |
|
stat_grab_time=time(NULL); |
632 |
< |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
633 |
< |
errf("Failed to get stats (%m)"); |
632 |
> |
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
633 |
> |
logmessage(LOG_ERR,"Failed to get stats (%m)"); |
634 |
|
exit(1); |
635 |
|
} |
636 |
|
stat_grab_time=time(NULL)-stat_grab_time; |
637 |
|
send_stats(&ihost_state, xml_stats); |
638 |
|
free(xml_stats); |
639 |
|
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
640 |
+ |
logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
641 |
|
} |
642 |
|
|
643 |
|
if(tcp_time<udp_time){ |
645 |
|
}else{ |
646 |
|
sleep_delay=udp_time-time(NULL); |
647 |
|
} |
648 |
< |
|
461 |
< |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
648 |
> |
logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
649 |
|
if(sleep_delay>0) sleep(sleep_delay); |
650 |
|
} |
651 |
|
return 0; |