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 |
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> |
21 |
– |
#include <sys/socket.h> |
26 |
|
#include <stdlib.h> |
27 |
|
#include <unistd.h> |
28 |
< |
#include <syslog.h> |
29 |
< |
#include <netinet/in.h> |
30 |
< |
#include "ukcprog.h" |
28 |
> |
#include <string.h> |
29 |
> |
#include <sys/types.h> |
30 |
> |
#include <sys/socket.h> |
31 |
> |
#include <stdarg.h> |
32 |
> |
#include <errno.h> |
33 |
|
#include <netdb.h> |
34 |
< |
#include <strings.h> |
34 |
> |
|
35 |
> |
#include "ukcprog.h" |
36 |
|
#include "statgrab.h" |
37 |
|
|
38 |
< |
#define RECONFIGURE_RETURN_CODE 2 |
39 |
< |
#define UDP_MAX_PACKET_SIZE 8192 |
38 |
> |
#define LOG_CRIT 0 |
39 |
> |
#define LOG_ERR 1 |
40 |
> |
#define LOG_INFO 2 |
41 |
> |
#define LOG_DEBUG 3 |
42 |
|
|
43 |
|
typedef struct{ |
44 |
< |
int fm_port; |
45 |
< |
char *fm_host; |
46 |
< |
|
47 |
< |
char *my_fqdn; |
44 |
> |
int filtermanager_port; |
45 |
> |
char *filtermanager_host; |
46 |
> |
|
47 |
> |
char *host_ip; |
48 |
> |
char *host_fqdn; |
49 |
> |
|
50 |
|
char *server_fqdn; |
51 |
|
int server_udp_port; |
52 |
< |
int server_tcp_port; |
52 |
> |
|
53 |
> |
/* Weird stuff iscream wants sent to it */ |
54 |
|
char *last_modified; |
55 |
< |
char *files_list; |
56 |
< |
char *key; |
55 |
> |
char *file_list; |
56 |
> |
|
57 |
|
int udp_update_time; |
58 |
< |
int tcp_update_time; |
58 |
> |
// int config_ttl; |
59 |
|
|
60 |
+ |
time_t config_ttl; |
61 |
+ |
|
62 |
|
}ihost_state_t; |
63 |
|
|
64 |
< |
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
65 |
< |
char *reply; |
66 |
< |
fprintf(f_w, "%s\n", sendString); |
53 |
< |
fflush(f_w); |
54 |
< |
reply=fpgetline(f_r); |
55 |
< |
/* Returns pointer to static buffer */ |
56 |
< |
return reply; |
57 |
< |
} |
64 |
> |
typedef struct{ |
65 |
> |
int verbose; |
66 |
> |
int daemon; |
67 |
|
|
68 |
< |
int ihost_configure(ihost_state_t *ihost_state){ |
68 |
> |
FILE *log; |
69 |
> |
}ihost_config_t; |
70 |
> |
|
71 |
> |
typedef struct{ |
72 |
|
struct sockaddr_in addr; |
73 |
< |
struct in_addr haddr; |
74 |
< |
int sd; |
63 |
< |
FILE *fm_fd_r, *fm_fd_w; |
64 |
< |
char *reply; |
65 |
< |
char *reply_ptr; |
73 |
> |
int sock; |
74 |
> |
}udp_sockinfo_t; |
75 |
|
|
76 |
< |
/* Check to see if anything needs to be free'd */ |
68 |
< |
if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn); |
69 |
< |
if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
70 |
< |
if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
71 |
< |
if (ihost_state->files_list!=NULL) free(ihost_state->files_list); |
76 |
> |
ihost_config_t ihost_config; |
77 |
|
|
78 |
< |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
74 |
< |
errf("Can't create AF_INET socket (%m)"); |
75 |
< |
return -1; |
76 |
< |
} |
78 |
> |
extern int errno; |
79 |
|
|
80 |
< |
if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
81 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
82 |
< |
return -1; |
80 |
> |
/* Taken from the OpenSSH code. Its licence included in function.*/ |
81 |
> |
#ifndef HAVE_STRLCAT |
82 |
> |
|
83 |
> |
/* |
84 |
> |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
85 |
> |
* All rights reserved. |
86 |
> |
* |
87 |
> |
* Redistribution and use in source and binary forms, with or without |
88 |
> |
* modification, are permitted provided that the following conditions |
89 |
> |
* are met: |
90 |
> |
* 1. Redistributions of source code must retain the above copyright |
91 |
> |
* notice, this list of conditions and the following disclaimer. |
92 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
93 |
> |
* notice, this list of conditions and the following disclaimer in the |
94 |
> |
* documentation and/or other materials provided with the distribution. |
95 |
> |
* 3. The name of the author may not be used to endorse or promote products |
96 |
> |
* derived from this software without specific prior written permission. |
97 |
> |
* |
98 |
> |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
99 |
> |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
100 |
> |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
101 |
> |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
102 |
> |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
103 |
> |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
104 |
> |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
105 |
> |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
106 |
> |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
107 |
> |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
108 |
> |
*/ |
109 |
> |
|
110 |
> |
/* |
111 |
> |
* Appends src to string dst of size siz (unlike strncat, siz is the |
112 |
> |
* full size of dst, not space left). At most siz-1 characters |
113 |
> |
* will be copied. Always NUL terminates (unless siz <= strlen(dst)). |
114 |
> |
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
115 |
> |
* If retval >= siz, truncation occurred. |
116 |
> |
*/ |
117 |
> |
size_t |
118 |
> |
strlcat(dst, src, siz) |
119 |
> |
char *dst; |
120 |
> |
const char *src; |
121 |
> |
size_t siz; |
122 |
> |
{ |
123 |
> |
register char *d = dst; |
124 |
> |
register const char *s = src; |
125 |
> |
register size_t n = siz; |
126 |
> |
size_t dlen; |
127 |
> |
|
128 |
> |
/* Find the end of dst and adjust bytes left but don't go past end */ |
129 |
> |
while (n-- != 0 && *d != '\0') |
130 |
> |
d++; |
131 |
> |
dlen = d - dst; |
132 |
> |
n = siz - dlen; |
133 |
> |
|
134 |
> |
if (n == 0) |
135 |
> |
return(dlen + strlen(s)); |
136 |
> |
while (*s != '\0') { |
137 |
> |
if (n != 1) { |
138 |
> |
*d++ = *s; |
139 |
> |
n--; |
140 |
> |
} |
141 |
> |
s++; |
142 |
> |
} |
143 |
> |
*d = '\0'; |
144 |
> |
|
145 |
> |
return(dlen + (s - src)); /* count does not include NUL */ |
146 |
> |
} |
147 |
> |
|
148 |
> |
#endif |
149 |
> |
/* End strlcat function taken from OpenSSH */ |
150 |
> |
|
151 |
> |
void log_msg(int level, char *format, ...){ |
152 |
> |
int cur_errno; |
153 |
> |
va_list ap; |
154 |
> |
|
155 |
> |
cur_errno=errno; |
156 |
> |
|
157 |
> |
if(level<=ihost_config.verbose){ |
158 |
> |
va_start(ap, format); |
159 |
> |
vfprintf(ihost_config.log, format, ap); |
160 |
> |
va_end(ap); |
161 |
> |
if(level==LOG_CRIT){ |
162 |
> |
fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno)); |
163 |
> |
}else{ |
164 |
> |
fprintf(ihost_config.log, "\n"); |
165 |
> |
} |
166 |
|
} |
167 |
+ |
} |
168 |
|
|
169 |
< |
memset(&addr, 0, sizeof addr); |
170 |
< |
addr.sin_family = AF_INET; |
171 |
< |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
172 |
< |
addr.sin_port = htons(ihost_state->fm_port); |
169 |
> |
/* Takes many pointers, checks if they are NULL or not, and then free's them */ |
170 |
> |
/* Deprciated - and i only wrote it today! :) |
171 |
> |
void m_free(int num_pointers, ...){ |
172 |
> |
int x=0; |
173 |
> |
va_list ap; |
174 |
> |
void *p; |
175 |
|
|
176 |
< |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
177 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
178 |
< |
return -1; |
176 |
> |
va_start(ap, num_pointers); |
177 |
> |
for(;x<num_pointers;x++){ |
178 |
> |
p=va_arg(ap, void*); |
179 |
> |
if(p!=NULL){ |
180 |
> |
free(p); |
181 |
> |
} |
182 |
|
} |
183 |
+ |
va_end(ap); |
184 |
+ |
} |
185 |
+ |
*/ |
186 |
|
|
187 |
< |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
188 |
< |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
189 |
< |
errf("Failed to open read stream (%m)"); |
190 |
< |
return -1; |
187 |
> |
int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ |
188 |
> |
|
189 |
> |
struct in_addr haddr; |
190 |
> |
|
191 |
> |
log_msg(LOG_DEBUG, "Resolving name for udp connection"); |
192 |
> |
if(get_host_addr(hostname, &haddr) != 0){ |
193 |
> |
log_msg(LOG_CRIT, "Failed to lookup name"); |
194 |
> |
return 1; |
195 |
|
} |
196 |
|
|
197 |
< |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
198 |
< |
errf("Failed to open write stream (%m)"); |
199 |
< |
return -1; |
197 |
> |
if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
198 |
> |
log_msg(LOG_CRIT, "Failed to create UDP socket"); |
199 |
> |
return 1; |
200 |
|
} |
201 |
|
|
202 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
203 |
< |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
204 |
< |
errf("Server error"); |
205 |
< |
return -1; |
202 |
> |
memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in)); |
203 |
> |
udp_sockinfo->addr.sin_family=AF_INET; |
204 |
> |
memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr)); |
205 |
> |
udp_sockinfo->addr.sin_port = htons(port); |
206 |
> |
|
207 |
> |
log_msg(LOG_DEBUG, "Socket created"); |
208 |
> |
return 0; |
209 |
> |
} |
210 |
> |
|
211 |
> |
FILE *create_tcp_connection(char *hostname, int port){ |
212 |
> |
int sock; |
213 |
> |
struct sockaddr_in addr; |
214 |
> |
struct in_addr haddr; |
215 |
> |
FILE *f; |
216 |
> |
|
217 |
> |
log_msg(LOG_DEBUG, "Creating tcp socket"); |
218 |
> |
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ |
219 |
> |
log_msg(LOG_CRIT, "Failed to make TCP Socket"); |
220 |
> |
return NULL; |
221 |
|
} |
222 |
|
|
223 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
224 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
225 |
< |
errf("Server error (%m)"); |
113 |
< |
return -1; |
223 |
> |
if((get_host_addr(hostname, &haddr))!=0){ |
224 |
> |
log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
225 |
> |
return NULL; |
226 |
|
} |
227 |
< |
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
228 |
< |
errf("strdup failed (%m)"); |
229 |
< |
return -1; |
230 |
< |
} |
231 |
< |
|
232 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
233 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
234 |
< |
errf("Server error (%m)"); |
235 |
< |
return -1; |
227 |
> |
|
228 |
> |
memset(&addr, 0, sizeof(addr)); |
229 |
> |
addr.sin_family = AF_INET; |
230 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof(haddr)); |
231 |
> |
addr.sin_port = htons(port); |
232 |
> |
|
233 |
> |
log_msg(LOG_DEBUG, "Creating a tcp connection"); |
234 |
> |
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
235 |
> |
log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
236 |
> |
return NULL; |
237 |
|
} |
125 |
– |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
126 |
– |
errf("strdup failed (%m)"); |
127 |
– |
return -1; |
128 |
– |
} |
238 |
|
|
239 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
240 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
241 |
< |
errf("Server error (%m)"); |
242 |
< |
return -1; |
239 |
> |
if((f=fdopen(sock, "r+"))==NULL){ |
240 |
> |
log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection"); |
241 |
> |
close(sock); |
242 |
> |
return NULL; |
243 |
|
} |
244 |
< |
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
245 |
< |
errf("strdup failed (%m)"); |
244 |
> |
|
245 |
> |
return f; |
246 |
> |
} |
247 |
> |
|
248 |
> |
int tcp_comm(FILE *f, char *send, char **response, char *expected){ |
249 |
> |
|
250 |
> |
log_msg(LOG_DEBUG, "Sending %s", send); |
251 |
> |
fprintf(f, "%s\n", send); |
252 |
> |
fflush(f); |
253 |
> |
*response=fpgetline(f); |
254 |
> |
fseek(f, 0, SEEK_CUR); |
255 |
> |
|
256 |
> |
if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response); |
257 |
> |
|
258 |
> |
if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; |
259 |
> |
|
260 |
> |
if(expected==NULL) return 0; |
261 |
> |
|
262 |
> |
if((strcmp(expected, *response))==0) return 0; |
263 |
> |
|
264 |
> |
log_msg(LOG_DEBUG, "Did not get expected response"); |
265 |
> |
return -1; |
266 |
> |
} |
267 |
> |
|
268 |
> |
int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){ |
269 |
> |
if((tcp_comm(f, send, response, expected))!=0){ |
270 |
|
return -1; |
271 |
|
} |
272 |
|
|
273 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
274 |
< |
if(reply== NULL){ |
275 |
< |
errf("Server error (%m)"); |
276 |
< |
return -1; |
273 |
> |
*response=strdup(*response); |
274 |
> |
if (response==NULL) return -1; |
275 |
> |
|
276 |
> |
return 0; |
277 |
> |
} |
278 |
> |
|
279 |
> |
int ihost_getconfig(ihost_state_t *ihost_state){ |
280 |
> |
|
281 |
> |
FILE *tcp_con; |
282 |
> |
char *response; |
283 |
> |
char *response_ptr; |
284 |
> |
|
285 |
> |
/* Keep these in case of a failure and so it can keep running on the old config */ |
286 |
> |
char *file_list=NULL; |
287 |
> |
char *last_modified=NULL; |
288 |
> |
char *host_fqdn=NULL; |
289 |
> |
char *host_ip=NULL; |
290 |
> |
int udp_update_time=0; |
291 |
> |
char *server_fqdn=NULL; |
292 |
> |
int server_udp_port=0; |
293 |
> |
time_t config_ttl=0; |
294 |
> |
|
295 |
> |
if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ |
296 |
> |
goto error; |
297 |
|
} |
145 |
– |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
146 |
– |
ihost_state->udp_update_time=atoi(reply); |
147 |
– |
} |
298 |
|
|
299 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
300 |
< |
if(reply== NULL){ |
301 |
< |
errf("Server error (%m)"); |
302 |
< |
return -1; |
299 |
> |
if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
300 |
> |
if(tcp_con==NULL){ |
301 |
> |
goto error; |
302 |
> |
} |
303 |
> |
|
304 |
> |
if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ |
305 |
> |
goto error; |
306 |
> |
} |
307 |
> |
|
308 |
> |
if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ |
309 |
> |
goto error; |
310 |
> |
} |
311 |
> |
|
312 |
> |
if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ |
313 |
> |
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
314 |
> |
goto error; |
315 |
> |
} |
316 |
> |
fclose(tcp_con); |
317 |
> |
return 0; |
318 |
> |
}else{ |
319 |
> |
if((strcmp(response, "EXPIRED"))!=0){ |
320 |
> |
goto error; |
321 |
> |
} |
322 |
> |
} |
323 |
|
} |
154 |
– |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
155 |
– |
ihost_state->tcp_update_time=atoi(reply); |
156 |
– |
} |
324 |
|
|
325 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
326 |
< |
if(reply== NULL){ |
327 |
< |
errf("Server error (%m)"); |
328 |
< |
return -1; |
325 |
> |
/* If we got to here, the config must of expired */ |
326 |
> |
|
327 |
> |
if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){ |
328 |
> |
goto error; |
329 |
|
} |
330 |
|
|
331 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
332 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
166 |
< |
errf("Server error FILTER failed (%m)"); |
167 |
< |
return -1; |
331 |
> |
if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){ |
332 |
> |
goto error; |
333 |
|
} |
334 |
< |
reply_ptr=strchr(reply,';'); |
335 |
< |
if (reply_ptr==NULL){ |
336 |
< |
errf("Incorrect data returned"); |
337 |
< |
return -1; |
334 |
> |
last_modified=response; |
335 |
> |
|
336 |
> |
if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ |
337 |
> |
goto error; |
338 |
|
} |
339 |
< |
*reply_ptr='\0'; |
340 |
< |
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
341 |
< |
errf("strdup failed (%m)"); |
342 |
< |
return -1; |
339 |
> |
file_list=response; |
340 |
> |
|
341 |
> |
if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){ |
342 |
> |
goto error; |
343 |
|
} |
344 |
< |
reply=reply_ptr + 1; |
345 |
< |
reply_ptr=strchr(reply,';'); |
346 |
< |
if (reply_ptr==NULL){ |
347 |
< |
errf("Incorrect data returned 2"); |
183 |
< |
return -1; |
344 |
> |
host_fqdn=response; |
345 |
> |
|
346 |
> |
if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){ |
347 |
> |
goto error; |
348 |
|
} |
349 |
< |
*reply_ptr='\0'; |
350 |
< |
ihost_state->server_udp_port=atoi(reply); |
351 |
< |
reply=reply_ptr+1; |
352 |
< |
ihost_state->server_tcp_port=atoi(reply); |
189 |
< |
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
190 |
< |
errf("Incorrect data returned 3 "); |
191 |
< |
return -1; |
349 |
> |
host_ip=response; |
350 |
> |
|
351 |
> |
if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){ |
352 |
> |
goto error; |
353 |
|
} |
354 |
+ |
udp_update_time=atoi(response); |
355 |
|
|
356 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
357 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
196 |
< |
errf("Server error (%m)"); |
197 |
< |
return -1; |
356 |
> |
if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){ |
357 |
> |
goto error; |
358 |
|
} |
359 |
+ |
config_ttl=atoi(response); |
360 |
|
|
361 |
< |
if(fclose(fm_fd_r) !=0){ |
362 |
< |
errf("Failed to close read FD (%m)"); |
202 |
< |
return -1; |
361 |
> |
if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){ |
362 |
> |
goto error; |
363 |
|
} |
364 |
< |
if(fclose(fm_fd_w) !=0){ |
365 |
< |
errf("Failed to close write FD (%m)"); |
366 |
< |
return -1; |
364 |
> |
|
365 |
> |
if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){ |
366 |
> |
goto error; |
367 |
> |
}else{ |
368 |
> |
response_ptr=strchr(response,';'); |
369 |
> |
if(response_ptr==NULL){ |
370 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
371 |
> |
goto error; |
372 |
> |
} |
373 |
> |
*response_ptr='\0'; |
374 |
> |
server_fqdn=strdup(response); |
375 |
> |
if(server_fqdn==NULL){ |
376 |
> |
goto error; |
377 |
> |
} |
378 |
> |
response_ptr++; |
379 |
> |
if(response_ptr==NULL){ |
380 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
381 |
> |
goto error; |
382 |
> |
} |
383 |
> |
|
384 |
> |
printf("string : %s\n", response_ptr); |
385 |
> |
server_udp_port=atoi(response_ptr); |
386 |
> |
|
387 |
> |
if (server_udp_port==0){ |
388 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
389 |
> |
goto error; |
390 |
> |
} |
391 |
|
} |
392 |
+ |
|
393 |
+ |
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
394 |
+ |
goto error; |
395 |
+ |
} |
396 |
|
|
397 |
+ |
fclose(tcp_con); |
398 |
+ |
|
399 |
+ |
/* We have the data we need, and its all been read correctly */ |
400 |
+ |
|
401 |
+ |
/* Free the old data before pointing them to the new data. m_free copes should |
402 |
+ |
* this already be NULL */ |
403 |
+ |
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
404 |
+ |
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
405 |
+ |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
406 |
+ |
if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
407 |
+ |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
408 |
+ |
|
409 |
+ |
ihost_state->file_list=file_list; |
410 |
+ |
ihost_state->last_modified=last_modified; |
411 |
+ |
ihost_state->host_fqdn=host_fqdn; |
412 |
+ |
ihost_state->host_ip=host_ip; |
413 |
+ |
ihost_state->server_fqdn=server_fqdn; |
414 |
+ |
ihost_state->server_udp_port=server_udp_port; |
415 |
+ |
ihost_state->udp_update_time=udp_update_time; |
416 |
+ |
ihost_state->config_ttl=config_ttl; |
417 |
+ |
|
418 |
+ |
log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time); |
419 |
+ |
log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl); |
420 |
+ |
|
421 |
|
return 0; |
422 |
+ |
|
423 |
+ |
error: |
424 |
+ |
|
425 |
+ |
if(file_list!=NULL) free(file_list); |
426 |
+ |
if(last_modified!=NULL) free(last_modified); |
427 |
+ |
if(host_fqdn!=NULL) free(host_fqdn); |
428 |
+ |
if(server_fqdn!=NULL) free(server_fqdn); |
429 |
+ |
if(host_ip!=NULL) free(host_ip); |
430 |
+ |
fclose(tcp_con); |
431 |
+ |
|
432 |
+ |
return -1; |
433 |
|
} |
434 |
|
|
435 |
< |
int heartbeat(ihost_state_t *ihost_state){ |
436 |
< |
struct sockaddr_in addr; |
437 |
< |
struct in_addr haddr; |
438 |
< |
int sd; |
439 |
< |
FILE *fm_fd_r, *fm_fd_w; |
440 |
< |
char *reply; |
441 |
< |
int exitcode=0; |
435 |
> |
int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ |
436 |
> |
char tmp[size]; |
437 |
> |
cpu_percent_t *cpu_percent; |
438 |
> |
mem_stat_t *mem_stats; |
439 |
> |
load_stat_t *load_stats; |
440 |
> |
user_stat_t *user_stats; |
441 |
> |
swap_stat_t *swap_stats; |
442 |
> |
general_stat_t *general_stats; |
443 |
> |
disk_stat_t *disk_stats; |
444 |
> |
diskio_stat_t *diskio_stats; |
445 |
> |
process_stat_t *process_stats; |
446 |
> |
network_stat_t *network_stats; |
447 |
> |
page_stat_t *page_stats; |
448 |
> |
int disk_entries=0; |
449 |
> |
int diskio_entries=0; |
450 |
> |
int network_entries=0; |
451 |
|
|
452 |
< |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
453 |
< |
errf("Can't create AF_INET socket (%m)"); |
454 |
< |
return -1; |
223 |
< |
} |
452 |
> |
int counter; |
453 |
> |
long long x; |
454 |
> |
long long y; |
455 |
|
|
456 |
< |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
457 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
458 |
< |
return -1; |
228 |
< |
} |
456 |
> |
/* Print start of the packet we want */ |
457 |
> |
snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \ |
458 |
> |
seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); |
459 |
|
|
460 |
< |
memset(&addr, 0, sizeof addr); |
461 |
< |
addr.sin_family = AF_INET; |
462 |
< |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
463 |
< |
addr.sin_port = htons(ihost_state->server_tcp_port); |
460 |
> |
/* Get cpu stats, check it is correct, then fill in its entry for the xml */ |
461 |
> |
if((cpu_percent=cpu_percent_usage())==NULL){ |
462 |
> |
log_msg(LOG_CRIT, "Failed to get cpu statistics"); |
463 |
> |
}else{ |
464 |
> |
snprintf(tmp, size, \ |
465 |
> |
"<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \ |
466 |
> |
cpu_percent->user, \ |
467 |
> |
cpu_percent->kernel, \ |
468 |
> |
cpu_percent->idle, \ |
469 |
> |
cpu_percent->iowait, \ |
470 |
> |
cpu_percent->swap); |
471 |
|
|
472 |
< |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
473 |
< |
errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
237 |
< |
return -1; |
238 |
< |
} |
472 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
473 |
> |
} |
474 |
|
|
475 |
< |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
476 |
< |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
477 |
< |
errf("Failed to open stream (%m)"); |
478 |
< |
return -1; |
479 |
< |
} |
475 |
> |
|
476 |
> |
/*Get mem stats, and fill in xml */ |
477 |
> |
if((mem_stats=get_memory_stats())==NULL){ |
478 |
> |
log_msg(LOG_CRIT, "Failed to get memory statistics"); |
479 |
> |
}else{ |
480 |
> |
snprintf(tmp, size, \ |
481 |
> |
"<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \ |
482 |
> |
mem_stats->total, \ |
483 |
> |
mem_stats->free, \ |
484 |
> |
mem_stats->used, \ |
485 |
> |
mem_stats->cache); |
486 |
> |
|
487 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
488 |
> |
} |
489 |
|
|
246 |
– |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
247 |
– |
errf("Failed to open stream (%m)"); |
248 |
– |
return -1; |
249 |
– |
} |
490 |
|
|
491 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
492 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
493 |
< |
errf("Server error"); |
494 |
< |
return -1; |
495 |
< |
} |
491 |
> |
/* Get load stats */ |
492 |
> |
if((load_stats=get_load_stats())==NULL){ |
493 |
> |
log_msg(LOG_CRIT, "Failed to get load statistics"); |
494 |
> |
}else{ |
495 |
> |
snprintf(tmp, size, \ |
496 |
> |
"<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \ |
497 |
> |
load_stats->min1, \ |
498 |
> |
load_stats->min5, \ |
499 |
> |
load_stats->min15); |
500 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
501 |
> |
} |
502 |
|
|
257 |
– |
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
258 |
– |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
259 |
– |
errf("Server error"); |
260 |
– |
return -1; |
261 |
– |
} |
503 |
|
|
504 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
505 |
< |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
506 |
< |
errf("Server error"); |
507 |
< |
return -1; |
508 |
< |
} |
504 |
> |
/* get user stats */ |
505 |
> |
|
506 |
> |
if((user_stats=get_user_stats())==NULL){ |
507 |
> |
log_msg(LOG_CRIT, "Failed to get user statistics"); |
508 |
> |
}else{ |
509 |
|
|
510 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
511 |
< |
if (reply==NULL) { |
512 |
< |
errf("Server error"); |
513 |
< |
return -1; |
514 |
< |
} |
515 |
< |
if (strncasecmp(reply, "ERROR", 5) == 0){ |
275 |
< |
/* Means the config has changed */ |
276 |
< |
exitcode=RECONFIGURE_RETURN_CODE; |
510 |
> |
snprintf(tmp, size, \ |
511 |
> |
"<users><list>%s</list><count>%d</count></users>", \ |
512 |
> |
user_stats->name_list, \ |
513 |
> |
user_stats->num_entries); |
514 |
> |
|
515 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
516 |
|
} |
278 |
– |
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
279 |
– |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
280 |
– |
errf("Server error"); |
281 |
– |
return -1; |
282 |
– |
} |
283 |
– |
if (ihost_state->key!=NULL) free(ihost_state->key); |
284 |
– |
|
285 |
– |
if((ihost_state->key=strdup(reply)) == NULL){ |
286 |
– |
errf("strdup failed (%m)"); |
287 |
– |
return -1; |
288 |
– |
} |
517 |
|
|
290 |
– |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
291 |
– |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
292 |
– |
errf("Server error (%m)"); |
293 |
– |
return -1; |
294 |
– |
} |
518 |
|
|
519 |
< |
fflush(fm_fd_r); |
520 |
< |
fflush(fm_fd_w); |
519 |
> |
/* swap stats */ |
520 |
> |
if((swap_stats=get_swap_stats())==NULL){ |
521 |
> |
log_msg(LOG_CRIT, "Failed to get swap statistics"); |
522 |
> |
}else{ |
523 |
> |
snprintf(tmp, size, \ |
524 |
> |
"<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\ |
525 |
> |
swap_stats->total, \ |
526 |
> |
swap_stats->used, \ |
527 |
> |
swap_stats->free); |
528 |
> |
|
529 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
530 |
> |
} |
531 |
|
|
299 |
– |
if(fclose(fm_fd_r) !=0){ |
300 |
– |
errf("Failed to close read FD (%m)"); |
301 |
– |
return -1; |
302 |
– |
} |
303 |
– |
if(fclose(fm_fd_w) !=0){ |
304 |
– |
errf("Failed to close write FD (%m)"); |
305 |
– |
return -1; |
306 |
– |
} |
532 |
|
|
533 |
< |
return exitcode; |
534 |
< |
} |
533 |
> |
/* general stats */ |
534 |
> |
|
535 |
> |
if((general_stats=get_general_stats())==NULL){ |
536 |
> |
log_msg(LOG_CRIT, "Failed to get general statistics"); |
537 |
> |
}else{ |
538 |
> |
snprintf(tmp, size, \ |
539 |
> |
"<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \ |
540 |
> |
general_stats->os_name, \ |
541 |
> |
general_stats->os_release, \ |
542 |
> |
general_stats->os_version, \ |
543 |
> |
general_stats->hostname, \ |
544 |
> |
general_stats->platform, \ |
545 |
> |
(long)general_stats->uptime); |
546 |
> |
|
547 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
548 |
> |
|
549 |
> |
} |
550 |
|
|
311 |
– |
char *stat_grab(ihost_state_t *ihost_state, int counter){ |
312 |
– |
#define NUM_STATS 9 |
313 |
– |
char *stats[NUM_STATS]; |
314 |
– |
char *xml_data=NULL; |
315 |
– |
char *xml_data_p; |
316 |
– |
int x=0; |
551 |
|
|
552 |
< |
stats[0]=get_cpu_stats(); |
553 |
< |
stats[1]=get_disk_stats(); |
554 |
< |
stats[2]=get_load_stats(); |
555 |
< |
stats[3]=get_memory_stats(); |
556 |
< |
stats[4]=get_os_info(); |
557 |
< |
stats[5]=get_page_stats(); |
558 |
< |
stats[6]=get_process_stats(); |
559 |
< |
stats[7]=get_swap_stats(); |
560 |
< |
stats[8]=get_user_stats(); |
552 |
> |
/* process stats */ |
553 |
> |
if((process_stats=get_process_stats())==NULL){ |
554 |
> |
log_msg(LOG_CRIT, "Failed to get general statistics"); |
555 |
> |
}else{ |
556 |
> |
snprintf(tmp, size, \ |
557 |
> |
"<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\ |
558 |
> |
process_stats->sleeping, \ |
559 |
> |
process_stats->running, \ |
560 |
> |
process_stats->zombie, \ |
561 |
> |
process_stats->stopped, \ |
562 |
> |
process_stats->total); |
563 |
|
|
564 |
< |
for(;x<NUM_STATS;x++){ |
565 |
< |
if(stats[x]==NULL){ |
566 |
< |
return NULL; |
567 |
< |
} |
568 |
< |
if(xml_data==NULL){ |
569 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
570 |
< |
errf("str failed (%m)"); |
571 |
< |
return NULL; |
572 |
< |
} |
564 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
565 |
> |
|
566 |
> |
} |
567 |
> |
|
568 |
> |
|
569 |
> |
/* Get paging stats */ |
570 |
> |
if((page_stats=get_page_stats_diff())==NULL){ |
571 |
> |
log_msg(LOG_CRIT, "Failed to get paging statistics"); |
572 |
> |
}else{ |
573 |
> |
if(page_stats->systime!=0){ |
574 |
> |
x=page_stats->pages_pagein / page_stats->systime; |
575 |
> |
y=page_stats->pages_pageout / page_stats->systime; |
576 |
|
}else{ |
577 |
< |
xml_data_p=xml_data; |
578 |
< |
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); |
577 |
> |
x=page_stats->pages_pagein; |
578 |
> |
y=page_stats->pages_pageout; |
579 |
|
} |
580 |
< |
free(stats[x]); |
580 |
> |
snprintf(tmp, size, \ |
581 |
> |
"<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \ |
582 |
> |
x, \ |
583 |
> |
y); |
584 |
> |
|
585 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
586 |
|
} |
587 |
< |
xml_data_p=xml_data; |
588 |
< |
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); |
589 |
< |
free(xml_data_p); |
587 |
> |
|
588 |
> |
|
589 |
> |
/* get diskio stats */ |
590 |
|
|
591 |
< |
return xml_data; |
592 |
< |
} |
591 |
> |
if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){ |
592 |
> |
log_msg(LOG_CRIT, "Failed to get diskio statistics"); |
593 |
> |
}else{ |
594 |
> |
strlcat(xml, "<diskio>", size); |
595 |
> |
for(counter=0;counter<diskio_entries;counter++){ |
596 |
|
|
597 |
< |
int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
598 |
< |
struct sockaddr_in addr; |
599 |
< |
struct in_addr haddr; |
597 |
> |
if(diskio_stats->systime!=0){ |
598 |
> |
x=diskio_stats->read_bytes / diskio_stats->systime; |
599 |
> |
y=diskio_stats->write_bytes / diskio_stats->systime; |
600 |
> |
}else{ |
601 |
> |
x=diskio_stats->read_bytes; |
602 |
> |
y=diskio_stats->write_bytes; |
603 |
> |
} |
604 |
> |
|
605 |
> |
snprintf(tmp, size, \ |
606 |
> |
"<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \ |
607 |
> |
counter, \ |
608 |
> |
diskio_stats->disk_name, \ |
609 |
> |
x, \ |
610 |
> |
y, \ |
611 |
> |
counter); |
612 |
|
|
613 |
< |
int sd; |
614 |
< |
size_t len; |
613 |
> |
strlcat(xml, tmp, size); |
614 |
> |
diskio_stats++; |
615 |
> |
} |
616 |
|
|
617 |
< |
len=strlen(data_stream); |
618 |
< |
if(len>UDP_MAX_PACKET_SIZE){ |
363 |
< |
errf("Too big to send to server. Please reconfigure client and server and recompile"); |
364 |
< |
exit(1); |
617 |
> |
if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error; |
618 |
> |
|
619 |
|
} |
620 |
+ |
|
621 |
|
|
622 |
< |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
623 |
< |
errf("Failed to resolve address %s (%m)", ihost_state->fm_host); |
624 |
< |
return -1; |
625 |
< |
} |
622 |
> |
/* get networks stats */ |
623 |
> |
|
624 |
> |
if((network_stats=get_network_stats_diff(&network_entries))==NULL){ |
625 |
> |
log_msg(LOG_CRIT, "Failed to get network statistics"); |
626 |
> |
}else{ |
627 |
> |
strlcat(xml, "<net>", size); |
628 |
> |
for(counter=0;counter<network_entries;counter++){ |
629 |
> |
if(network_stats->systime!=0){ |
630 |
> |
x=network_stats->rx / network_stats->systime; |
631 |
> |
y=network_stats->tx / network_stats->systime; |
632 |
> |
}else{ |
633 |
> |
x=network_stats->rx; |
634 |
> |
y=network_stats->tx; |
635 |
> |
} |
636 |
|
|
637 |
< |
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
638 |
< |
errf("failed to create UDP socket (%m)"); |
639 |
< |
return -1; |
637 |
> |
snprintf(tmp, size, \ |
638 |
> |
"<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \ |
639 |
> |
counter, \ |
640 |
> |
network_stats->interface_name, \ |
641 |
> |
x, \ |
642 |
> |
y, \ |
643 |
> |
counter); |
644 |
> |
|
645 |
> |
strlcat(xml, tmp, size); |
646 |
> |
network_stats++; |
647 |
> |
} |
648 |
> |
|
649 |
> |
if(strlcat(xml, "</net>", size) >= size) goto too_big_error; |
650 |
> |
|
651 |
|
} |
652 |
|
|
377 |
– |
memset(&addr, 0, sizeof(addr)); |
378 |
– |
addr.sin_family=AF_INET; |
379 |
– |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
380 |
– |
addr.sin_port = htons(ihost_state->server_udp_port); |
653 |
|
|
654 |
< |
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
655 |
< |
errf("Send the wrong number of bytes (%m)"); |
656 |
< |
return -1; |
654 |
> |
/* get disk stats */ |
655 |
> |
|
656 |
> |
if((disk_stats=get_disk_stats(&disk_entries))==NULL){ |
657 |
> |
log_msg(LOG_CRIT, "Failed to get disk statistics"); |
658 |
> |
}else{ |
659 |
> |
strlcat(xml, "<disk>", size); |
660 |
> |
for(counter=0;counter<disk_entries;counter++){ |
661 |
> |
snprintf(tmp, size, \ |
662 |
> |
"<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \ |
663 |
> |
counter, \ |
664 |
> |
disk_stats->device_name, \ |
665 |
> |
disk_stats->mnt_point, \ |
666 |
> |
disk_stats->fs_type, \ |
667 |
> |
disk_stats->size, \ |
668 |
> |
disk_stats->used, \ |
669 |
> |
disk_stats->avail, \ |
670 |
> |
disk_stats->total_inodes, \ |
671 |
> |
disk_stats->used_inodes, \ |
672 |
> |
disk_stats->free_inodes, \ |
673 |
> |
counter); |
674 |
> |
|
675 |
> |
strlcat(xml, tmp, size); |
676 |
> |
|
677 |
> |
disk_stats++; |
678 |
> |
} |
679 |
> |
|
680 |
> |
if(strlcat(xml, "</disk>", size) >= size) goto too_big_error; |
681 |
> |
|
682 |
|
} |
683 |
+ |
|
684 |
|
|
685 |
< |
close(sd); |
685 |
> |
if(strlcat(xml, "</packet>", size) >= size) goto too_big_error; |
686 |
|
|
687 |
< |
return 0; |
687 |
> |
/*If we got to here, it should of all been filled in nicely now */ |
688 |
> |
return 0; |
689 |
> |
|
690 |
> |
too_big_error: |
691 |
> |
log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet"); |
692 |
> |
return -1; |
693 |
|
} |
694 |
|
|
695 |
+ |
|
696 |
+ |
|
697 |
+ |
void usage(char *progname){ |
698 |
+ |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
699 |
+ |
fprintf(stderr, "Options\n"); |
700 |
+ |
fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); |
701 |
+ |
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
702 |
+ |
fprintf(stderr, " -V Print version number\n"); |
703 |
+ |
fprintf(stderr, " -h Prints this help page\n"); |
704 |
+ |
exit(1); |
705 |
+ |
} |
706 |
+ |
|
707 |
|
int main(int argc, char **argv){ |
708 |
+ |
|
709 |
|
ihost_state_t ihost_state; |
710 |
< |
int heartbeat_exit; |
711 |
< |
int counter=0; |
712 |
< |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
713 |
< |
int sleep_delay=0; |
714 |
< |
char *xml_stats; |
710 |
> |
udp_sockinfo_t udp_sockinfo; |
711 |
> |
|
712 |
> |
int cmdopt; |
713 |
> |
extern int optind; |
714 |
> |
pid_t pid; |
715 |
> |
FILE *f; |
716 |
> |
int packet_num=0; |
717 |
> |
int len; |
718 |
|
|
719 |
< |
/* NULL'ify so i can tell if i need to free it or not */ |
401 |
< |
ihost_state.fm_host=NULL; |
402 |
< |
ihost_state.my_fqdn=NULL; |
403 |
< |
ihost_state.server_fqdn=NULL; |
404 |
< |
ihost_state.last_modified=NULL; |
405 |
< |
ihost_state.files_list=NULL; |
406 |
< |
ihost_state.key=NULL; |
719 |
> |
char packet[MAX_UDP_PACKET_SIZE]; |
720 |
|
|
721 |
< |
errf_set_progname(argv[0]); |
722 |
< |
if(argc!=3){ |
723 |
< |
errf_usage("<host> <port>"); |
721 |
> |
time_t cur_time, sleep_delay, udp_time=0, config_time=0; |
722 |
> |
|
723 |
> |
/* Set default settings */ |
724 |
> |
ihost_config.verbose=1; |
725 |
> |
ihost_config.daemon=1; |
726 |
> |
/* Set all errors to go down stderr until told otherwise */ |
727 |
> |
ihost_config.log=stderr; |
728 |
> |
|
729 |
> |
/* Blank ihost_state to default settings */ |
730 |
> |
ihost_state.filtermanager_host=NULL; |
731 |
> |
ihost_state.host_fqdn=NULL; |
732 |
> |
ihost_state.host_ip=NULL; |
733 |
> |
ihost_state.server_fqdn=NULL; |
734 |
> |
ihost_state.file_list=NULL; |
735 |
> |
ihost_state.last_modified=NULL; |
736 |
> |
|
737 |
> |
while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ |
738 |
> |
switch(cmdopt){ |
739 |
> |
case 'v': |
740 |
> |
ihost_config.verbose++; |
741 |
> |
break; |
742 |
> |
|
743 |
> |
case 'f': |
744 |
> |
/* Force syslog logging since stderr will be closed in this case */ |
745 |
> |
ihost_config.daemon=0; |
746 |
> |
break; |
747 |
> |
|
748 |
> |
case 'h': |
749 |
> |
usage(argv[0]); |
750 |
> |
break; |
751 |
> |
|
752 |
> |
case 'V': |
753 |
> |
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
754 |
> |
break; |
755 |
> |
|
756 |
> |
default: |
757 |
> |
usage(argv[0]); |
758 |
> |
exit(1); |
759 |
> |
} |
760 |
> |
} |
761 |
> |
|
762 |
> |
if(argc!=optind+2){ |
763 |
> |
usage(argv[0]); |
764 |
> |
exit(1); |
765 |
> |
} |
766 |
> |
|
767 |
> |
ihost_state.filtermanager_host=strdup(argv[optind]); |
768 |
> |
ihost_state.filtermanager_port=atoi(argv[optind+1]); |
769 |
> |
|
770 |
> |
if(gethostbyname(ihost_state.filtermanager_host)==NULL){ |
771 |
> |
log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); |
772 |
|
exit(1); |
773 |
|
} |
774 |
+ |
if(ihost_state.filtermanager_port==0){ |
775 |
+ |
log_msg(LOG_ERR, "Invalid port number"); |
776 |
+ |
exit(1); |
777 |
+ |
} |
778 |
|
|
779 |
< |
ihost_state.fm_host=argv[1]; |
780 |
< |
ihost_state.fm_port=atoi(argv[2]); |
779 |
> |
if(ihost_config.daemon){ |
780 |
> |
pid=fork(); |
781 |
> |
if(pid==-1){ |
782 |
> |
log_msg(LOG_CRIT, "Failed to background exiting"); |
783 |
> |
exit(1); |
784 |
> |
}else if(pid!=0){ |
785 |
> |
/* Parent process */ |
786 |
> |
return 0; |
787 |
> |
} |
788 |
> |
/* We should now be in the background*/ |
789 |
> |
if(setsid()==-1){ |
790 |
> |
log_msg(LOG_CRIT, "setsid failed"); |
791 |
> |
exit(1); |
792 |
> |
} |
793 |
> |
|
794 |
> |
if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
795 |
> |
ihost_config.log=stderr; |
796 |
> |
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
797 |
> |
exit(1); |
798 |
> |
} |
799 |
|
|
800 |
< |
if(ihost_configure(&ihost_state)!=0){ |
801 |
< |
errf("configure failed"); |
802 |
< |
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
803 |
< |
happens.. But we dont :) (at the moment) */ |
804 |
< |
exit(1); |
800 |
> |
fclose(stdin); |
801 |
> |
fclose(stdout); |
802 |
> |
fclose(stderr); |
803 |
> |
|
804 |
> |
} |
805 |
> |
|
806 |
> |
log_msg(LOG_INFO, "Starting ihost"); |
807 |
> |
|
808 |
> |
log_msg(LOG_DEBUG,"Writing PID FILE"); |
809 |
> |
|
810 |
> |
pid=getpid(); |
811 |
> |
|
812 |
> |
if((f=fopen(PID_FILE,"w")) == NULL){ |
813 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
814 |
> |
}else{ |
815 |
> |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
816 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
817 |
> |
} |
818 |
> |
if((fclose(f))!=0){ |
819 |
> |
log_msg(LOG_CRIT, "failed to close PID file"); |
820 |
> |
} |
821 |
> |
} |
822 |
> |
|
823 |
> |
/* Get the initial config from the filter manager. Should this fail, |
824 |
> |
* wait, and then try again. */ |
825 |
> |
|
826 |
> |
get_diskio_stats_diff(&packet_num); |
827 |
> |
packet_num=0; |
828 |
> |
|
829 |
> |
while(ihost_getconfig(&ihost_state)!=0){ |
830 |
> |
log_msg(LOG_ERR, "Failed to get ihost config"); |
831 |
> |
sleep(10); |
832 |
|
} |
833 |
|
|
834 |
+ |
printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); |
835 |
+ |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
836 |
+ |
log_msg(LOG_ERR, "Failed to create udp socket"); |
837 |
+ |
sleep(10); |
838 |
+ |
} |
839 |
+ |
|
840 |
+ |
config_time=time(NULL)+ihost_state.config_ttl; |
841 |
+ |
|
842 |
+ |
/* Now have config.. collect data and send as often as required */ |
843 |
|
for(;;){ |
844 |
|
cur_time=time(NULL); |
426 |
– |
if(cur_time>=tcp_time){ |
427 |
– |
/*printf("sending TCP\n");*/ |
428 |
– |
heartbeat_exit=heartbeat(&ihost_state); |
429 |
– |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
430 |
– |
/*errf("heartbeat needs to be reconfigured");*/ |
431 |
– |
ihost_configure(&ihost_state); |
432 |
– |
/* So udp doesn't wait til next sending before updating */ |
433 |
– |
udp_time=0; |
434 |
– |
} |
435 |
– |
if(heartbeat_exit==-1){ |
436 |
– |
errf("ah crap"); |
437 |
– |
exit(1); |
438 |
– |
} |
439 |
– |
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
440 |
– |
} |
845 |
|
|
846 |
|
if(cur_time>=udp_time){ |
847 |
< |
/*printf("sending UDP\n");*/ |
848 |
< |
stat_grab_time=time(NULL); |
445 |
< |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
446 |
< |
errf("Failed to get stats (%m)"); |
447 |
< |
exit(1); |
847 |
> |
if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){ |
848 |
> |
log_msg(LOG_ERR, "Failed to get system stats"); |
849 |
|
} |
850 |
< |
stat_grab_time=time(NULL)-stat_grab_time; |
851 |
< |
send_stats(&ihost_state, xml_stats); |
852 |
< |
free(xml_stats); |
853 |
< |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
850 |
> |
|
851 |
> |
len=strlen(packet); |
852 |
> |
log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); |
853 |
> |
|
854 |
> |
if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ |
855 |
> |
log_msg(LOG_CRIT, "Failed to send packet"); |
856 |
> |
} |
857 |
> |
udp_time=cur_time+ihost_state.udp_update_time; |
858 |
> |
log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); |
859 |
|
} |
860 |
+ |
|
861 |
+ |
if(cur_time>=config_time){ |
862 |
+ |
if(ihost_getconfig(&ihost_state)!=0){ |
863 |
+ |
/* If we can't get the config, try again 5 minutes time */ |
864 |
+ |
log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time"); |
865 |
+ |
config_time=time(NULL)+300; |
866 |
+ |
}else{ |
867 |
+ |
close(udp_sockinfo.sock); |
868 |
|
|
869 |
< |
if(tcp_time<udp_time){ |
870 |
< |
sleep_delay=tcp_time-time(NULL); |
871 |
< |
}else{ |
872 |
< |
sleep_delay=udp_time-time(NULL); |
869 |
> |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
870 |
> |
log_msg(LOG_CRIT, "Failed to create udp socket"); |
871 |
> |
sleep(10); |
872 |
> |
} |
873 |
> |
|
874 |
> |
config_time=time(NULL)+ihost_state.config_ttl; |
875 |
> |
|
876 |
> |
log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl); |
877 |
> |
} |
878 |
|
} |
879 |
|
|
880 |
< |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
880 |
> |
sleep_delay=udp_time-time(NULL); |
881 |
> |
log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); |
882 |
|
if(sleep_delay>0) sleep(sleep_delay); |
883 |
< |
} |
884 |
< |
return 0; |
883 |
> |
} |
884 |
> |
|
885 |
> |
return(0); |
886 |
|
} |
466 |
– |
|