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