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