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> |
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 |
|
|
112 |
< |
memset((char *)&addr, 0, sizeof addr); |
112 |
> |
memset(&addr, 0, sizeof addr); |
113 |
|
addr.sin_family = AF_INET; |
114 |
< |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
114 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
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 |
< |
|
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=strdup(inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr)))==NULL){ |
144 |
> |
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
145 |
> |
return -1; |
146 |
> |
} |
147 |
> |
|
148 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
149 |
|
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
150 |
< |
errf("Server error"); |
150 |
> |
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
151 |
|
return -1; |
152 |
|
} |
153 |
|
|
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 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
165 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
166 |
< |
errf("Server error (%m)"); |
166 |
> |
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
167 |
|
return -1; |
168 |
|
} |
169 |
|
if((ihost_state->files_list=strdup(reply)) == NULL){ |
170 |
< |
errf("strdup failed (%m)"); |
170 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
171 |
|
return -1; |
172 |
|
} |
173 |
|
|
174 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
175 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
176 |
< |
errf("Server error (%m)"); |
176 |
> |
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
177 |
|
return -1; |
178 |
|
} |
179 |
|
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
180 |
< |
errf("strdup failed (%m)"); |
180 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
181 |
|
return -1; |
182 |
|
} |
183 |
|
|
184 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
185 |
|
if(reply== NULL){ |
186 |
< |
errf("Server error (%m)"); |
186 |
> |
logmessage(LOG_ERR, "Server error (%m)"); |
187 |
|
return -1; |
188 |
|
} |
189 |
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
190 |
|
ihost_state->udp_update_time=atoi(reply); |
191 |
|
} |
192 |
< |
|
192 |
> |
|
193 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
194 |
|
if(reply== NULL){ |
195 |
< |
errf("Server error (%m)"); |
195 |
> |
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
196 |
|
return -1; |
197 |
|
} |
198 |
|
if (strncasecmp(reply, "ERROR", 5) != 0){ |
201 |
|
|
202 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
203 |
|
if(reply== NULL){ |
204 |
< |
errf("Server error (%m)"); |
204 |
> |
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
205 |
|
return -1; |
206 |
|
} |
207 |
|
|
208 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
209 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
210 |
< |
errf("Server error (%m)"); |
210 |
> |
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
211 |
|
return -1; |
212 |
|
} |
213 |
|
reply_ptr=strchr(reply,';'); |
214 |
|
if (reply_ptr==NULL){ |
215 |
< |
errf("Incorrect data returned"); |
215 |
> |
logmessage(LOG_ERR, "Incorrect data returned"); |
216 |
|
return -1; |
217 |
|
} |
218 |
|
*reply_ptr='\0'; |
219 |
|
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
220 |
< |
errf("strdup failed (%m)"); |
220 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
221 |
|
return -1; |
222 |
|
} |
223 |
|
reply=reply_ptr + 1; |
224 |
|
reply_ptr=strchr(reply,';'); |
225 |
|
if (reply_ptr==NULL){ |
226 |
< |
errf("Incorrect data returned 2"); |
226 |
> |
logmessage(LOG_ERR, "Incorrect data returned 2"); |
227 |
|
return -1; |
228 |
|
} |
229 |
|
*reply_ptr='\0'; |
231 |
|
reply=reply_ptr+1; |
232 |
|
ihost_state->server_tcp_port=atoi(reply); |
233 |
|
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
234 |
< |
errf("Incorrect data returned 3 "); |
234 |
> |
logmessage(LOG_ERR, "Incorrect data returned 3 "); |
235 |
|
return -1; |
236 |
|
} |
237 |
|
|
238 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
239 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
240 |
< |
errf("Server error (%m)"); |
240 |
> |
logmessage(LOG_ERR, "Server error on END (%m)"); |
241 |
|
return -1; |
242 |
|
} |
243 |
|
|
244 |
|
if(fclose(fm_fd_r) !=0){ |
245 |
< |
errf("Failed to close read FD (%m)"); |
245 |
> |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
246 |
|
return -1; |
247 |
|
} |
248 |
|
if(fclose(fm_fd_w) !=0){ |
249 |
< |
errf("Failed to close write FD (%m)"); |
249 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
250 |
|
return -1; |
251 |
|
} |
252 |
|
|
261 |
|
char *reply; |
262 |
|
int exitcode=0; |
263 |
|
|
264 |
+ |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
265 |
|
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
266 |
< |
errf("Can't create AF_INET socket (%m)"); |
266 |
> |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
267 |
|
return -1; |
268 |
|
} |
269 |
|
|
270 |
|
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
271 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
271 |
> |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
272 |
|
return -1; |
273 |
|
} |
274 |
|
|
275 |
< |
memset((char *)&addr, 0, sizeof addr); |
275 |
> |
memset(&addr, 0, sizeof addr); |
276 |
|
addr.sin_family = AF_INET; |
277 |
< |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
277 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
278 |
|
addr.sin_port = htons(ihost_state->server_tcp_port); |
279 |
|
|
280 |
|
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
281 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
281 |
> |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
282 |
|
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 |
< |
errf("Failed to open stream (%m)"); |
287 |
> |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
288 |
|
return -1; |
289 |
|
} |
290 |
|
|
291 |
|
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
292 |
< |
errf("Failed to open stream (%m)"); |
292 |
> |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
293 |
|
return -1; |
294 |
|
} |
295 |
|
|
296 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
297 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
298 |
< |
errf("Server error"); |
298 |
> |
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
299 |
|
return -1; |
300 |
|
} |
301 |
< |
if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); |
301 |
> |
|
302 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
303 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
304 |
< |
errf("Server error"); |
304 |
> |
logmessage(LOG_ERR, "Server error on CONFIG"); |
305 |
|
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 |
< |
errf("Server error"); |
310 |
> |
logmessage(LOG_ERR, "Server error on fileslist"); |
311 |
|
return -1; |
312 |
|
} |
313 |
|
|
314 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
315 |
|
if (reply==NULL) { |
316 |
< |
errf("Server error"); |
316 |
> |
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
317 |
|
return -1; |
318 |
|
} |
319 |
|
if (strncasecmp(reply, "ERROR", 5) == 0){ |
320 |
|
/* Means the config has changed */ |
321 |
+ |
logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); |
322 |
|
exitcode=RECONFIGURE_RETURN_CODE; |
323 |
|
} |
324 |
+ |
|
325 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
326 |
|
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
327 |
< |
errf("Server error"); |
327 |
> |
logmessage(LOG_ERR, "Server error on KEY"); |
328 |
|
return -1; |
329 |
|
} |
330 |
|
if (ihost_state->key!=NULL) free(ihost_state->key); |
331 |
|
|
332 |
|
if((ihost_state->key=strdup(reply)) == NULL){ |
333 |
< |
errf("strdup failed (%m)"); |
333 |
> |
logmessage(LOG_ERR, "strdup failed (%m)"); |
334 |
|
return -1; |
335 |
|
} |
336 |
|
|
337 |
|
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
338 |
|
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
339 |
< |
errf("Server error (%m)"); |
339 |
> |
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
340 |
|
return -1; |
341 |
|
} |
342 |
|
|
343 |
+ |
fflush(fm_fd_r); |
344 |
+ |
fflush(fm_fd_w); |
345 |
+ |
|
346 |
|
if(fclose(fm_fd_r) !=0){ |
347 |
< |
errf("Failed to close read FD (%m)"); |
347 |
> |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
348 |
|
return -1; |
349 |
|
} |
350 |
|
if(fclose(fm_fd_w) !=0){ |
351 |
< |
errf("Failed to close write FD (%m)"); |
351 |
> |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
352 |
|
return -1; |
353 |
|
} |
354 |
|
|
355 |
|
return exitcode; |
356 |
|
} |
357 |
|
|
358 |
+ |
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 |
+ |
int xml_size=0; |
364 |
+ |
int x=0; |
365 |
|
|
366 |
+ |
logmessage(LOG_DEBUG,"get_cpu_stats"); |
367 |
+ |
stats[0]=get_cpu_stats(); |
368 |
+ |
logmessage(LOG_DEBUG,"get_disk_stats"); |
369 |
+ |
stats[1]=get_disk_stats(); |
370 |
+ |
logmessage(LOG_DEBUG,"get_load_stats"); |
371 |
+ |
stats[2]=get_load_stats(); |
372 |
+ |
logmessage(LOG_DEBUG,"get_memory_stats"); |
373 |
+ |
stats[3]=get_memory_stats(); |
374 |
+ |
logmessage(LOG_DEBUG,"get_os_info"); |
375 |
+ |
stats[4]=get_os_info(); |
376 |
+ |
logmessage(LOG_DEBUG,"get_page_stats"); |
377 |
+ |
stats[5]=get_page_stats(); |
378 |
+ |
logmessage(LOG_DEBUG,"get_process_stats"); |
379 |
+ |
stats[6]=get_process_stats(); |
380 |
+ |
logmessage(LOG_DEBUG,"get_swap_stats"); |
381 |
+ |
stats[7]=get_swap_stats(); |
382 |
+ |
logmessage(LOG_DEBUG,"get_user_stats"); |
383 |
+ |
stats[8]=get_user_stats(); |
384 |
+ |
|
385 |
+ |
|
386 |
+ |
for(x=0;x<NUM_STATS;x++){ |
387 |
+ |
if(stats[x]==NULL){ |
388 |
+ |
logmessage(LOG_ERR,"Function returned NULL"); |
389 |
+ |
return NULL; |
390 |
+ |
} |
391 |
+ |
xml_size+=strlen(stats[x]); |
392 |
+ |
} |
393 |
+ |
|
394 |
+ |
xml_data=malloc(xml_size+1); |
395 |
+ |
xml_data=strcpy(xml_data, stats[0]); |
396 |
+ |
free(stats[0]); |
397 |
+ |
for(x=1;x<NUM_STATS;x++){ |
398 |
+ |
strcat(xml_data, stats[x]); |
399 |
+ |
free(stats[x]); |
400 |
+ |
} |
401 |
+ |
|
402 |
+ |
xml_data_p=xml_data; |
403 |
+ |
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); |
404 |
+ |
free(xml_data_p); |
405 |
+ |
|
406 |
+ |
logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
407 |
+ |
return xml_data; |
408 |
+ |
} |
409 |
+ |
|
410 |
+ |
int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
411 |
+ |
struct sockaddr_in addr; |
412 |
+ |
struct in_addr haddr; |
413 |
+ |
|
414 |
+ |
int sd; |
415 |
+ |
size_t len; |
416 |
+ |
|
417 |
+ |
len=strlen(data_stream); |
418 |
+ |
if(len>UDP_MAX_PACKET_SIZE){ |
419 |
+ |
logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
420 |
+ |
exit(1); |
421 |
+ |
} |
422 |
+ |
logmessage(LOG_DEBUG,"Resolving IP of server"); |
423 |
+ |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
424 |
+ |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
425 |
+ |
return -1; |
426 |
+ |
} |
427 |
+ |
logmessage(LOG_DEBUG,"Creating UDP socket to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
428 |
+ |
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
429 |
+ |
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
430 |
+ |
return -1; |
431 |
+ |
} |
432 |
+ |
|
433 |
+ |
memset(&addr, 0, sizeof(addr)); |
434 |
+ |
addr.sin_family=AF_INET; |
435 |
+ |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
436 |
+ |
addr.sin_port = htons(ihost_state->server_udp_port); |
437 |
+ |
|
438 |
+ |
logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
439 |
+ |
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
440 |
+ |
logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
441 |
+ |
return -1; |
442 |
+ |
} |
443 |
+ |
|
444 |
+ |
close(sd); |
445 |
+ |
|
446 |
+ |
return 0; |
447 |
+ |
} |
448 |
+ |
|
449 |
+ |
void usage(char *progname){ |
450 |
+ |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
451 |
+ |
fprintf(stderr, "Options\n"); |
452 |
+ |
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
453 |
+ |
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
454 |
+ |
fprintf(stderr, " -s Send errors to syslog\n"); |
455 |
+ |
fprintf(stderr, " -V Print version number\n"); |
456 |
+ |
fprintf(stderr, " -h Prints this help page\n"); |
457 |
+ |
exit(1); |
458 |
+ |
} |
459 |
+ |
|
460 |
|
int main(int argc, char **argv){ |
461 |
|
ihost_state_t ihost_state; |
462 |
|
int heartbeat_exit; |
463 |
|
int counter=0; |
464 |
+ |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
465 |
+ |
int sleep_delay=0; |
466 |
+ |
char *xml_stats; |
467 |
+ |
pid_t pid; |
468 |
+ |
int cmdopt; |
469 |
+ |
extern int optind; |
470 |
+ |
int verbose=0, daemon=0; |
471 |
+ |
extern int syslog_logging; |
472 |
+ |
extern int log_level; |
473 |
+ |
extern int cur_level; |
474 |
+ |
FILE *f; |
475 |
|
|
476 |
+ |
log_level=1; |
477 |
+ |
cur_level=1; |
478 |
+ |
syslog_logging=0; |
479 |
|
|
480 |
+ |
errf_set_ofunc(log_errors); |
481 |
|
/* NULL'ify so i can tell if i need to free it or not */ |
482 |
|
ihost_state.fm_host=NULL; |
483 |
|
ihost_state.my_fqdn=NULL; |
487 |
|
ihost_state.key=NULL; |
488 |
|
|
489 |
|
errf_set_progname(argv[0]); |
490 |
< |
if(argc!=3){ |
491 |
< |
errf_usage("<host> <port>"); |
490 |
> |
|
491 |
> |
|
492 |
> |
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
493 |
> |
switch(cmdopt){ |
494 |
> |
case 'v': |
495 |
> |
verbose++; |
496 |
> |
break; |
497 |
> |
|
498 |
> |
case 'd': |
499 |
> |
/* Force syslog logging since stderr will be closed in this case */ |
500 |
> |
syslog_logging=1; |
501 |
> |
daemon=1; |
502 |
> |
break; |
503 |
> |
|
504 |
> |
case 's': |
505 |
> |
syslog_logging=1; |
506 |
> |
break; |
507 |
> |
|
508 |
> |
case 'h': |
509 |
> |
usage(argv[0]); |
510 |
> |
break; |
511 |
> |
|
512 |
> |
case 'V': |
513 |
> |
errf("%s version %s",argv[0], VERSION_NO); |
514 |
> |
break; |
515 |
> |
|
516 |
> |
default: |
517 |
> |
usage(argv[0]); |
518 |
> |
exit(1); |
519 |
> |
} |
520 |
> |
} |
521 |
> |
|
522 |
> |
if(argc!=optind+2){ |
523 |
> |
usage(argv[0]); |
524 |
|
exit(1); |
525 |
|
} |
526 |
+ |
ihost_state.fm_host=argv[optind]; |
527 |
+ |
ihost_state.fm_port=atoi(argv[optind+1]); |
528 |
+ |
if(ihost_state.fm_port==0){ |
529 |
+ |
errf("Invalid port number"); |
530 |
+ |
usage(argv[0]); |
531 |
+ |
} |
532 |
|
|
533 |
< |
ihost_state.fm_host=argv[1]; |
534 |
< |
ihost_state.fm_port=atoi(argv[2]); |
533 |
> |
if(daemon==1){ |
534 |
> |
pid=fork(); |
535 |
> |
if(pid==-1){ |
536 |
> |
errf("Fork failed, can't background. Exiting"); |
537 |
> |
exit(1); |
538 |
> |
}else if(pid!=0){ |
539 |
> |
/* Parent process */ |
540 |
> |
return 0; |
541 |
> |
} |
542 |
> |
/* We should now be in the background*/ |
543 |
> |
if(setsid()==-1){ |
544 |
> |
errf("setsid failed (%m)"); |
545 |
> |
exit(1); |
546 |
> |
} |
547 |
> |
fclose(stdin); |
548 |
> |
fclose(stdout); |
549 |
> |
fclose(stderr); |
550 |
> |
} |
551 |
|
|
552 |
+ |
if(syslog_logging==1){ |
553 |
+ |
openlog(errf_get_progname(),0,LOG_ERR); |
554 |
+ |
setlogmask(LOG_UPTO(LOG_DEBUG)); |
555 |
+ |
} |
556 |
+ |
|
557 |
+ |
switch(verbose){ |
558 |
+ |
case 0: |
559 |
+ |
/* Critical errors + */ |
560 |
+ |
log_level=LOG_ERR; |
561 |
+ |
break; |
562 |
+ |
case 1: |
563 |
+ |
/* Recoverable errors */ |
564 |
+ |
log_level=LOG_WARNING; |
565 |
+ |
break; |
566 |
+ |
case 2: |
567 |
+ |
/* Print stuff like the XML packets */ |
568 |
+ |
log_level=LOG_INFO; |
569 |
+ |
break; |
570 |
+ |
default: |
571 |
+ |
/* Must have lots of v's */ |
572 |
+ |
/* Print out everything its doing */ |
573 |
+ |
log_level=LOG_DEBUG; |
574 |
+ |
break; |
575 |
+ |
} |
576 |
+ |
|
577 |
+ |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
578 |
+ |
pid=getpid(); |
579 |
+ |
if((f=fopen(PID_FILE,"w")) == NULL){ |
580 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file"); |
581 |
+ |
}else{ |
582 |
+ |
if((fprintf(f,"%d",(int)pid)) > 0 ){ |
583 |
+ |
logmessage(LOG_WARNING, "Failed to write PID file"); |
584 |
+ |
} |
585 |
+ |
if((fclose(f))!=0){ |
586 |
+ |
logmessage(LOG_ERR, "failed to close PID file"); |
587 |
+ |
exit(1); |
588 |
+ |
} |
589 |
+ |
} |
590 |
+ |
|
591 |
|
if(ihost_configure(&ihost_state)!=0){ |
592 |
< |
errf("configure failed"); |
592 |
> |
logmessage(LOG_ERR,"configure failed"); |
593 |
|
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
594 |
|
happens.. But we dont :) (at the moment) */ |
595 |
|
exit(1); |
596 |
|
} |
597 |
|
|
598 |
< |
while(TRUE){ |
598 |
> |
for(;;){ |
599 |
> |
cur_time=time(NULL); |
600 |
> |
if(cur_time>=tcp_time){ |
601 |
> |
logmessage(LOG_DEBUG,"Sending heartbeat"); |
602 |
> |
heartbeat_exit=heartbeat(&ihost_state); |
603 |
> |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
604 |
> |
logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
605 |
> |
ihost_configure(&ihost_state); |
606 |
> |
udp_time=0; |
607 |
> |
} |
608 |
> |
if(heartbeat_exit==-1){ |
609 |
> |
logmessage(LOG_ERR,"Heartbeat failed"); |
610 |
> |
exit(1); |
611 |
> |
} |
612 |
> |
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
613 |
> |
logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
614 |
> |
} |
615 |
|
|
616 |
< |
heartbeat_exit=heartbeat(&ihost_state); |
617 |
< |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
618 |
< |
errf("heartbeat needs to be reconfigured"); |
619 |
< |
ihost_configure(&ihost_state); |
616 |
> |
if(cur_time>=udp_time){ |
617 |
> |
logmessage(LOG_DEBUG,"Sending udp data"); |
618 |
> |
/* Work out how long it takes to get the stats for next time round |
619 |
> |
so the sleep time can be adjusted accordingly */ |
620 |
> |
stat_grab_time=time(NULL); |
621 |
> |
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
622 |
> |
logmessage(LOG_ERR,"Failed to get stats (%m)"); |
623 |
> |
exit(1); |
624 |
> |
} |
625 |
> |
stat_grab_time=time(NULL)-stat_grab_time; |
626 |
> |
send_stats(&ihost_state, xml_stats); |
627 |
> |
free(xml_stats); |
628 |
> |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
629 |
> |
logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
630 |
|
} |
631 |
< |
if(heartbeat_exit==-1){ |
632 |
< |
errf("ah crap"); |
633 |
< |
exit(1); |
634 |
< |
} |
635 |
< |
printf("Count : %d\n",counter++); |
636 |
< |
printf("waiting %d\n",ihost_state.tcp_update_time); |
637 |
< |
sleep(ihost_state.tcp_update_time); |
631 |
> |
|
632 |
> |
if(tcp_time<udp_time){ |
633 |
> |
sleep_delay=tcp_time-time(NULL); |
634 |
> |
}else{ |
635 |
> |
sleep_delay=udp_time-time(NULL); |
636 |
> |
} |
637 |
> |
logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
638 |
> |
if(sleep_delay>0) sleep(sleep_delay); |
639 |
|
} |
640 |
|
return 0; |
641 |
|
} |