1 |
tdb |
1.11 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.23 |
* http://www.i-scream.org.uk |
4 |
tdb |
1.11 |
* Copyright (C) 2000-2002 i-scream |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
tdb |
1.26 |
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include "config.h" |
23 |
|
|
#endif |
24 |
|
|
|
25 |
pajs |
1.1 |
#include <stdio.h> |
26 |
|
|
#include <stdlib.h> |
27 |
pajs |
1.28 |
#include <unistd.h> |
28 |
|
|
#include <string.h> |
29 |
pajs |
1.18 |
#include <sys/types.h> |
30 |
pajs |
1.28 |
#include <sys/socket.h> |
31 |
|
|
#include <stdarg.h> |
32 |
|
|
#include <errno.h> |
33 |
pajs |
1.1 |
#include <netdb.h> |
34 |
pajs |
1.28 |
|
35 |
tdb |
1.29 |
#include "ukcprog.h" |
36 |
pajs |
1.9 |
#include "statgrab.h" |
37 |
pajs |
1.1 |
|
38 |
pajs |
1.28 |
#define LOG_CRIT 0 |
39 |
|
|
#define LOG_ERR 1 |
40 |
|
|
#define LOG_INFO 2 |
41 |
|
|
#define LOG_DEBUG 3 |
42 |
|
|
|
43 |
pajs |
1.1 |
typedef struct{ |
44 |
pajs |
1.28 |
int filtermanager_port; |
45 |
|
|
char *filtermanager_host; |
46 |
|
|
|
47 |
|
|
char *host_ip; |
48 |
|
|
char *host_fqdn; |
49 |
pajs |
1.16 |
|
50 |
pajs |
1.1 |
char *server_fqdn; |
51 |
|
|
int server_udp_port; |
52 |
pajs |
1.28 |
|
53 |
|
|
/* Weird stuff iscream wants sent to it */ |
54 |
pajs |
1.4 |
char *last_modified; |
55 |
pajs |
1.28 |
char *file_list; |
56 |
|
|
|
57 |
pajs |
1.1 |
int udp_update_time; |
58 |
pajs |
1.28 |
// int config_ttl; |
59 |
|
|
|
60 |
|
|
time_t config_ttl; |
61 |
pajs |
1.1 |
|
62 |
|
|
}ihost_state_t; |
63 |
|
|
|
64 |
pajs |
1.28 |
typedef struct{ |
65 |
|
|
int verbose; |
66 |
|
|
int daemon; |
67 |
|
|
|
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 |
|
|
void log_msg(int level, char *format, ...){ |
79 |
|
|
extern int errno; |
80 |
|
|
int cur_errno; |
81 |
|
|
va_list ap; |
82 |
|
|
|
83 |
|
|
cur_errno=errno; |
84 |
|
|
|
85 |
|
|
if(level<=ihost_config.verbose){ |
86 |
|
|
va_start(ap, format); |
87 |
|
|
vfprintf(ihost_config.log, format, ap); |
88 |
|
|
va_end(ap); |
89 |
|
|
if(level==LOG_CRIT){ |
90 |
|
|
fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno)); |
91 |
pajs |
1.18 |
}else{ |
92 |
pajs |
1.28 |
fprintf(ihost_config.log, "\n"); |
93 |
pajs |
1.18 |
} |
94 |
pajs |
1.28 |
} |
95 |
pajs |
1.18 |
} |
96 |
|
|
|
97 |
pajs |
1.28 |
/* Takes many pointers, checks if they are NULL or not, and then free's them */ |
98 |
|
|
/* Deprciated - and i only wrote it today! :) |
99 |
|
|
void m_free(int num_pointers, ...){ |
100 |
|
|
int x=0; |
101 |
|
|
va_list ap; |
102 |
|
|
void *p; |
103 |
pajs |
1.1 |
|
104 |
pajs |
1.28 |
va_start(ap, num_pointers); |
105 |
|
|
for(;x<num_pointers;x++){ |
106 |
|
|
p=va_arg(ap, void*); |
107 |
|
|
if(p!=NULL){ |
108 |
|
|
free(p); |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
va_end(ap); |
112 |
|
|
} |
113 |
|
|
*/ |
114 |
|
|
|
115 |
|
|
int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ |
116 |
|
|
|
117 |
|
|
struct in_addr haddr; |
118 |
|
|
|
119 |
|
|
log_msg(LOG_DEBUG, "Resolving name for udp connection"); |
120 |
|
|
if(get_host_addr(hostname, &haddr) != 0){ |
121 |
|
|
log_msg(LOG_CRIT, "Failed to lookup name"); |
122 |
|
|
return 1; |
123 |
|
|
} |
124 |
pajs |
1.1 |
|
125 |
pajs |
1.28 |
if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
126 |
|
|
log_msg(LOG_CRIT, "Failed to create UDP socket"); |
127 |
|
|
return 1; |
128 |
pajs |
1.1 |
} |
129 |
|
|
|
130 |
pajs |
1.28 |
memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in)); |
131 |
|
|
udp_sockinfo->addr.sin_family=AF_INET; |
132 |
|
|
memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr)); |
133 |
|
|
udp_sockinfo->addr.sin_port = htons(port); |
134 |
pajs |
1.1 |
|
135 |
pajs |
1.28 |
log_msg(LOG_DEBUG, "Socket created"); |
136 |
|
|
return 0; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
FILE *create_tcp_connection(char *hostname, int port){ |
140 |
|
|
int sock; |
141 |
|
|
struct sockaddr_in addr; |
142 |
|
|
struct in_addr haddr; |
143 |
|
|
FILE *f; |
144 |
|
|
|
145 |
|
|
log_msg(LOG_DEBUG, "Creating tcp socket"); |
146 |
|
|
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ |
147 |
|
|
log_msg(LOG_CRIT, "Failed to make TCP Socket"); |
148 |
|
|
return NULL; |
149 |
pajs |
1.1 |
} |
150 |
|
|
|
151 |
pajs |
1.28 |
if((get_host_addr(hostname, &haddr))!=0){ |
152 |
|
|
log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
153 |
|
|
return NULL; |
154 |
pajs |
1.2 |
} |
155 |
|
|
|
156 |
pajs |
1.28 |
memset(&addr, 0, sizeof(addr)); |
157 |
|
|
addr.sin_family = AF_INET; |
158 |
|
|
memcpy(&addr.sin_addr, &haddr, sizeof(haddr)); |
159 |
|
|
addr.sin_port = htons(port); |
160 |
|
|
|
161 |
|
|
log_msg(LOG_DEBUG, "Creating a tcp connection"); |
162 |
|
|
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
163 |
|
|
log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
164 |
|
|
return NULL; |
165 |
pajs |
1.1 |
} |
166 |
pajs |
1.28 |
|
167 |
|
|
if((f=fdopen(sock, "r+"))==NULL){ |
168 |
|
|
log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection"); |
169 |
|
|
close(sock); |
170 |
|
|
return NULL; |
171 |
pajs |
1.16 |
} |
172 |
pajs |
1.28 |
|
173 |
|
|
return f; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
int tcp_comm(FILE *f, char *send, char **response, char *expected){ |
177 |
|
|
|
178 |
|
|
log_msg(LOG_DEBUG, "Sending %s", send); |
179 |
|
|
fprintf(f, "%s\n", send); |
180 |
|
|
fflush(f); |
181 |
|
|
*response=fpgetline(f); |
182 |
|
|
fseek(f, 0, SEEK_CUR); |
183 |
|
|
|
184 |
|
|
if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response); |
185 |
|
|
|
186 |
|
|
if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; |
187 |
|
|
|
188 |
|
|
if(expected==NULL) return 0; |
189 |
|
|
|
190 |
|
|
if((strcmp(expected, *response))==0) return 0; |
191 |
|
|
|
192 |
|
|
log_msg(LOG_DEBUG, "Did not get expected response"); |
193 |
|
|
return -1; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){ |
197 |
|
|
if((tcp_comm(f, send, response, expected))!=0){ |
198 |
pajs |
1.16 |
return -1; |
199 |
|
|
} |
200 |
pajs |
1.25 |
|
201 |
pajs |
1.28 |
*response=strdup(*response); |
202 |
|
|
if (response==NULL) return -1; |
203 |
|
|
|
204 |
|
|
return 0; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
int ihost_getconfig(ihost_state_t *ihost_state){ |
208 |
|
|
|
209 |
|
|
FILE *tcp_con; |
210 |
|
|
char *response; |
211 |
|
|
char *response_ptr; |
212 |
|
|
|
213 |
|
|
/* Keep these in case of a failure and so it can keep running on the old config */ |
214 |
|
|
char *file_list=NULL; |
215 |
|
|
char *last_modified=NULL; |
216 |
|
|
char *host_fqdn=NULL; |
217 |
|
|
char *host_ip=NULL; |
218 |
|
|
int udp_update_time=0; |
219 |
|
|
char *server_fqdn=NULL; |
220 |
|
|
int server_udp_port=0; |
221 |
|
|
time_t config_ttl=0; |
222 |
|
|
|
223 |
pajs |
1.30 |
if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ |
224 |
|
|
goto error; |
225 |
|
|
} |
226 |
pajs |
1.28 |
|
227 |
|
|
if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
228 |
|
|
if(tcp_con==NULL){ |
229 |
|
|
goto error; |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ |
233 |
|
|
goto error; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ |
237 |
|
|
goto error; |
238 |
|
|
} |
239 |
pajs |
1.18 |
|
240 |
pajs |
1.28 |
if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ |
241 |
|
|
if((tcp_comm(tcp_con, "END", &response, "OK"))==0){ |
242 |
|
|
goto error; |
243 |
|
|
} |
244 |
|
|
fclose(tcp_con); |
245 |
|
|
return 0; |
246 |
|
|
}else{ |
247 |
|
|
if((strcmp(response, "EXPIRED"))!=0){ |
248 |
|
|
goto error; |
249 |
|
|
} |
250 |
|
|
} |
251 |
pajs |
1.1 |
} |
252 |
|
|
|
253 |
pajs |
1.28 |
/* If we got to here, the config must of expired */ |
254 |
pajs |
1.18 |
|
255 |
pajs |
1.28 |
if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){ |
256 |
|
|
goto error; |
257 |
pajs |
1.1 |
} |
258 |
|
|
|
259 |
pajs |
1.28 |
if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){ |
260 |
|
|
goto error; |
261 |
pajs |
1.1 |
} |
262 |
pajs |
1.28 |
last_modified=response; |
263 |
|
|
|
264 |
|
|
if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ |
265 |
|
|
goto error; |
266 |
pajs |
1.1 |
} |
267 |
pajs |
1.28 |
file_list=response; |
268 |
pajs |
1.1 |
|
269 |
pajs |
1.28 |
if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){ |
270 |
|
|
goto error; |
271 |
pajs |
1.1 |
} |
272 |
pajs |
1.28 |
host_fqdn=response; |
273 |
|
|
|
274 |
|
|
if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){ |
275 |
|
|
goto error; |
276 |
pajs |
1.1 |
} |
277 |
pajs |
1.28 |
host_ip=response; |
278 |
|
|
|
279 |
|
|
if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){ |
280 |
|
|
goto error; |
281 |
pajs |
1.1 |
} |
282 |
pajs |
1.28 |
udp_update_time=atoi(response); |
283 |
|
|
|
284 |
|
|
if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){ |
285 |
|
|
goto error; |
286 |
pajs |
1.1 |
} |
287 |
pajs |
1.28 |
config_ttl=atoi(response); |
288 |
pajs |
1.1 |
|
289 |
pajs |
1.28 |
if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){ |
290 |
|
|
goto error; |
291 |
pajs |
1.1 |
} |
292 |
|
|
|
293 |
pajs |
1.28 |
if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){ |
294 |
|
|
goto error; |
295 |
|
|
}else{ |
296 |
|
|
response_ptr=strchr(response,';'); |
297 |
|
|
if(response_ptr==NULL){ |
298 |
|
|
log_msg(LOG_ERR, "Incorrect data sent by server"); |
299 |
|
|
goto error; |
300 |
|
|
} |
301 |
|
|
*response_ptr='\0'; |
302 |
|
|
server_fqdn=strdup(response); |
303 |
|
|
if(server_fqdn==NULL){ |
304 |
|
|
goto error; |
305 |
|
|
} |
306 |
|
|
response_ptr++; |
307 |
|
|
if(response_ptr==NULL){ |
308 |
|
|
log_msg(LOG_ERR, "Incorrect data sent by server"); |
309 |
|
|
goto error; |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
printf("string : %s\n", response_ptr); |
313 |
|
|
server_udp_port=atoi(response_ptr); |
314 |
|
|
|
315 |
|
|
if (server_udp_port==0){ |
316 |
|
|
log_msg(LOG_ERR, "Incorrect data sent by server"); |
317 |
|
|
goto error; |
318 |
|
|
} |
319 |
pajs |
1.2 |
} |
320 |
pajs |
1.28 |
|
321 |
|
|
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
322 |
|
|
goto error; |
323 |
pajs |
1.2 |
} |
324 |
pajs |
1.28 |
|
325 |
|
|
fclose(tcp_con); |
326 |
|
|
|
327 |
|
|
/* We have the data we need, and its all been read correctly */ |
328 |
|
|
|
329 |
|
|
/* Free the old data before pointing them to the new data. m_free copes should |
330 |
|
|
* this already be NULL */ |
331 |
|
|
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
332 |
|
|
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
333 |
|
|
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
334 |
|
|
if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
335 |
|
|
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
336 |
|
|
|
337 |
|
|
ihost_state->file_list=file_list; |
338 |
|
|
ihost_state->last_modified=last_modified; |
339 |
|
|
ihost_state->host_fqdn=host_fqdn; |
340 |
|
|
ihost_state->host_ip=host_ip; |
341 |
|
|
ihost_state->server_fqdn=server_fqdn; |
342 |
|
|
ihost_state->server_udp_port=server_udp_port; |
343 |
|
|
ihost_state->udp_update_time=udp_update_time; |
344 |
|
|
ihost_state->config_ttl=config_ttl; |
345 |
|
|
|
346 |
|
|
log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time); |
347 |
|
|
log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl); |
348 |
|
|
|
349 |
|
|
return 0; |
350 |
|
|
|
351 |
|
|
error: |
352 |
|
|
|
353 |
|
|
if(file_list!=NULL) free(file_list); |
354 |
|
|
if(last_modified!=NULL) free(last_modified); |
355 |
|
|
if(host_fqdn!=NULL) free(host_fqdn); |
356 |
|
|
if(server_fqdn!=NULL) free(server_fqdn); |
357 |
|
|
if(host_ip!=NULL) free(host_ip); |
358 |
|
|
fclose(tcp_con); |
359 |
|
|
|
360 |
|
|
return -1; |
361 |
|
|
} |
362 |
|
|
|
363 |
|
|
int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ |
364 |
|
|
char tmp[size]; |
365 |
|
|
cpu_percent_t *cpu_percent; |
366 |
|
|
mem_stat_t *mem_stats; |
367 |
|
|
load_stat_t *load_stats; |
368 |
|
|
user_stat_t *user_stats; |
369 |
|
|
swap_stat_t *swap_stats; |
370 |
|
|
general_stat_t *general_stats; |
371 |
|
|
disk_stat_t *disk_stats; |
372 |
|
|
diskio_stat_t *diskio_stats; |
373 |
|
|
process_stat_t *process_stats; |
374 |
|
|
network_stat_t *network_stats; |
375 |
|
|
page_stat_t *page_stats; |
376 |
|
|
int disk_entries=0; |
377 |
|
|
int diskio_entries=0; |
378 |
|
|
int network_entries=0; |
379 |
|
|
|
380 |
|
|
int counter; |
381 |
|
|
long long x; |
382 |
|
|
long long y; |
383 |
|
|
|
384 |
|
|
/* Print start of the packet we want */ |
385 |
|
|
snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \ |
386 |
|
|
seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); |
387 |
|
|
|
388 |
|
|
/* Get cpu stats, check it is correct, then fill in its entry for the xml */ |
389 |
|
|
if((cpu_percent=cpu_percent_usage())==NULL){ |
390 |
|
|
log_msg(LOG_CRIT, "Failed to get cpu statistics"); |
391 |
|
|
}else{ |
392 |
|
|
snprintf(tmp, size, \ |
393 |
|
|
"<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \ |
394 |
|
|
cpu_percent->user, \ |
395 |
|
|
cpu_percent->kernel, \ |
396 |
|
|
cpu_percent->idle, \ |
397 |
|
|
cpu_percent->iowait, \ |
398 |
|
|
cpu_percent->swap); |
399 |
|
|
|
400 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
401 |
pajs |
1.2 |
} |
402 |
pajs |
1.28 |
|
403 |
|
|
|
404 |
|
|
/*Get mem stats, and fill in xml */ |
405 |
|
|
if((mem_stats=get_memory_stats())==NULL){ |
406 |
|
|
log_msg(LOG_CRIT, "Failed to get memory statistics"); |
407 |
|
|
}else{ |
408 |
|
|
snprintf(tmp, size, \ |
409 |
|
|
"<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \ |
410 |
|
|
mem_stats->total, \ |
411 |
|
|
mem_stats->free, \ |
412 |
|
|
mem_stats->used, \ |
413 |
|
|
mem_stats->cache); |
414 |
|
|
|
415 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
416 |
pajs |
1.2 |
} |
417 |
pajs |
1.28 |
|
418 |
|
|
|
419 |
|
|
/* Get load stats */ |
420 |
|
|
if((load_stats=get_load_stats())==NULL){ |
421 |
|
|
log_msg(LOG_CRIT, "Failed to get load statistics"); |
422 |
|
|
}else{ |
423 |
|
|
snprintf(tmp, size, \ |
424 |
|
|
"<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \ |
425 |
|
|
load_stats->min1, \ |
426 |
|
|
load_stats->min5, \ |
427 |
|
|
load_stats->min15); |
428 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
429 |
pajs |
1.2 |
} |
430 |
|
|
|
431 |
pajs |
1.28 |
|
432 |
|
|
/* get user stats */ |
433 |
|
|
|
434 |
|
|
if((user_stats=get_user_stats())==NULL){ |
435 |
|
|
log_msg(LOG_CRIT, "Failed to get user statistics"); |
436 |
|
|
}else{ |
437 |
|
|
|
438 |
|
|
snprintf(tmp, size, \ |
439 |
|
|
"<users><list>%s</list><count>%d</count></users>", \ |
440 |
|
|
user_stats->name_list, \ |
441 |
|
|
user_stats->num_entries); |
442 |
|
|
|
443 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
444 |
pajs |
1.2 |
} |
445 |
pajs |
1.1 |
|
446 |
pajs |
1.28 |
|
447 |
|
|
/* swap stats */ |
448 |
|
|
if((swap_stats=get_swap_stats())==NULL){ |
449 |
|
|
log_msg(LOG_CRIT, "Failed to get swap statistics"); |
450 |
|
|
}else{ |
451 |
|
|
snprintf(tmp, size, \ |
452 |
|
|
"<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\ |
453 |
|
|
swap_stats->total, \ |
454 |
|
|
swap_stats->used, \ |
455 |
|
|
swap_stats->free); |
456 |
|
|
|
457 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
458 |
pajs |
1.2 |
} |
459 |
pajs |
1.28 |
|
460 |
|
|
|
461 |
|
|
/* general stats */ |
462 |
|
|
|
463 |
|
|
if((general_stats=get_general_stats())==NULL){ |
464 |
|
|
log_msg(LOG_CRIT, "Failed to get general statistics"); |
465 |
|
|
}else{ |
466 |
|
|
snprintf(tmp, size, \ |
467 |
|
|
"<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \ |
468 |
|
|
general_stats->os_name, \ |
469 |
|
|
general_stats->os_release, \ |
470 |
|
|
general_stats->os_version, \ |
471 |
|
|
general_stats->hostname, \ |
472 |
|
|
general_stats->platform, \ |
473 |
|
|
(long)general_stats->uptime); |
474 |
|
|
|
475 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
476 |
|
|
|
477 |
pajs |
1.2 |
} |
478 |
pajs |
1.1 |
|
479 |
pajs |
1.28 |
|
480 |
|
|
/* process stats */ |
481 |
|
|
if((process_stats=get_process_stats())==NULL){ |
482 |
|
|
log_msg(LOG_CRIT, "Failed to get general statistics"); |
483 |
|
|
}else{ |
484 |
|
|
snprintf(tmp, size, \ |
485 |
|
|
"<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\ |
486 |
|
|
process_stats->sleeping, \ |
487 |
|
|
process_stats->running, \ |
488 |
|
|
process_stats->zombie, \ |
489 |
|
|
process_stats->stopped, \ |
490 |
|
|
process_stats->total); |
491 |
|
|
|
492 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
493 |
pajs |
1.4 |
|
494 |
pajs |
1.28 |
} |
495 |
pajs |
1.4 |
|
496 |
|
|
|
497 |
pajs |
1.28 |
/* Get paging stats */ |
498 |
|
|
if((page_stats=get_page_stats_diff())==NULL){ |
499 |
|
|
log_msg(LOG_CRIT, "Failed to get paging statistics"); |
500 |
|
|
}else{ |
501 |
|
|
if(page_stats->systime!=0){ |
502 |
|
|
x=page_stats->pages_pagein / page_stats->systime; |
503 |
|
|
y=page_stats->pages_pageout / page_stats->systime; |
504 |
|
|
}else{ |
505 |
|
|
x=page_stats->pages_pagein; |
506 |
|
|
y=page_stats->pages_pageout; |
507 |
|
|
} |
508 |
|
|
snprintf(tmp, size, \ |
509 |
|
|
"<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \ |
510 |
|
|
x, \ |
511 |
|
|
y); |
512 |
|
|
|
513 |
|
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
514 |
|
|
} |
515 |
pajs |
1.4 |
|
516 |
|
|
|
517 |
pajs |
1.28 |
/* get diskio stats */ |
518 |
|
|
|
519 |
|
|
if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){ |
520 |
pajs |
1.31 |
log_msg(LOG_CRIT, "Failed to get diskio statistics"); |
521 |
pajs |
1.28 |
}else{ |
522 |
|
|
strlcat(xml, "<diskio>", size); |
523 |
|
|
for(counter=0;counter<diskio_entries;counter++){ |
524 |
pajs |
1.4 |
|
525 |
pajs |
1.28 |
if(diskio_stats->systime!=0){ |
526 |
|
|
x=diskio_stats->read_bytes / diskio_stats->systime; |
527 |
|
|
y=diskio_stats->write_bytes / diskio_stats->systime; |
528 |
|
|
}else{ |
529 |
|
|
x=diskio_stats->read_bytes; |
530 |
|
|
y=diskio_stats->write_bytes; |
531 |
|
|
} |
532 |
|
|
|
533 |
|
|
snprintf(tmp, size, \ |
534 |
|
|
"<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \ |
535 |
|
|
counter, \ |
536 |
|
|
diskio_stats->disk_name, \ |
537 |
|
|
x, \ |
538 |
|
|
y, \ |
539 |
|
|
counter); |
540 |
pajs |
1.9 |
|
541 |
pajs |
1.28 |
strlcat(xml, tmp, size); |
542 |
|
|
diskio_stats++; |
543 |
|
|
} |
544 |
pajs |
1.4 |
|
545 |
pajs |
1.28 |
if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error; |
546 |
pajs |
1.4 |
|
547 |
|
|
} |
548 |
pajs |
1.18 |
|
549 |
pajs |
1.28 |
|
550 |
|
|
/* get networks stats */ |
551 |
|
|
|
552 |
|
|
if((network_stats=get_network_stats_diff(&network_entries))==NULL){ |
553 |
|
|
log_msg(LOG_CRIT, "Failed to get network statistics"); |
554 |
|
|
}else{ |
555 |
|
|
strlcat(xml, "<net>", size); |
556 |
|
|
for(counter=0;counter<network_entries;counter++){ |
557 |
|
|
if(network_stats->systime!=0){ |
558 |
|
|
x=network_stats->rx / network_stats->systime; |
559 |
|
|
y=network_stats->tx / network_stats->systime; |
560 |
|
|
}else{ |
561 |
|
|
x=network_stats->rx; |
562 |
|
|
y=network_stats->tx; |
563 |
|
|
} |
564 |
pajs |
1.4 |
|
565 |
pajs |
1.28 |
snprintf(tmp, size, \ |
566 |
|
|
"<p%d name=\"%s\" rx=\"%lld\" tx=\"%lld\"></p%d>", \ |
567 |
|
|
counter, \ |
568 |
|
|
network_stats->interface_name, \ |
569 |
|
|
x, \ |
570 |
|
|
y, \ |
571 |
|
|
counter); |
572 |
pajs |
1.4 |
|
573 |
pajs |
1.28 |
strlcat(xml, tmp, size); |
574 |
|
|
network_stats++; |
575 |
|
|
} |
576 |
pajs |
1.10 |
|
577 |
pajs |
1.28 |
if(strlcat(xml, "</net>", size) >= size) goto too_big_error; |
578 |
pajs |
1.6 |
|
579 |
pajs |
1.28 |
} |
580 |
pajs |
1.4 |
|
581 |
pajs |
1.18 |
|
582 |
pajs |
1.28 |
/* get disk stats */ |
583 |
|
|
|
584 |
|
|
if((disk_stats=get_disk_stats(&disk_entries))==NULL){ |
585 |
|
|
log_msg(LOG_CRIT, "Failed to get disk statistics"); |
586 |
|
|
}else{ |
587 |
|
|
strlcat(xml, "<disk>", size); |
588 |
|
|
for(counter=0;counter<disk_entries;counter++){ |
589 |
|
|
snprintf(tmp, size, \ |
590 |
|
|
"<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\"%lld\" used=\"%lld\" avail=\"%lld\" totalinodes=\"%lld\" usedinodes=\"%lld\" freeinodes=\"%lld\"></p%d>", \ |
591 |
|
|
counter, \ |
592 |
|
|
disk_stats->device_name, \ |
593 |
|
|
disk_stats->mnt_point, \ |
594 |
|
|
disk_stats->fs_type, \ |
595 |
|
|
disk_stats->size, \ |
596 |
|
|
disk_stats->used, \ |
597 |
|
|
disk_stats->avail, \ |
598 |
|
|
disk_stats->total_inodes, \ |
599 |
|
|
disk_stats->used_inodes, \ |
600 |
|
|
disk_stats->free_inodes, \ |
601 |
|
|
counter); |
602 |
pajs |
1.18 |
|
603 |
pajs |
1.28 |
strlcat(xml, tmp, size); |
604 |
pajs |
1.9 |
|
605 |
pajs |
1.28 |
disk_stats++; |
606 |
|
|
} |
607 |
pajs |
1.9 |
|
608 |
pajs |
1.28 |
if(strlcat(xml, "</disk>", size) >= size) goto too_big_error; |
609 |
pajs |
1.9 |
|
610 |
|
|
} |
611 |
pajs |
1.28 |
|
612 |
pajs |
1.9 |
|
613 |
pajs |
1.28 |
if(strlcat(xml, "</packet>", size) >= size) goto too_big_error; |
614 |
pajs |
1.9 |
|
615 |
pajs |
1.28 |
/*If we got to here, it should of all been filled in nicely now */ |
616 |
|
|
return 0; |
617 |
pajs |
1.10 |
|
618 |
pajs |
1.28 |
too_big_error: |
619 |
|
|
log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet"); |
620 |
|
|
return -1; |
621 |
pajs |
1.9 |
} |
622 |
pajs |
1.1 |
|
623 |
pajs |
1.28 |
|
624 |
|
|
|
625 |
pajs |
1.17 |
void usage(char *progname){ |
626 |
pajs |
1.28 |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
627 |
|
|
fprintf(stderr, "Options\n"); |
628 |
|
|
fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); |
629 |
|
|
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
630 |
|
|
fprintf(stderr, " -V Print version number\n"); |
631 |
|
|
fprintf(stderr, " -h Prints this help page\n"); |
632 |
|
|
exit(1); |
633 |
pajs |
1.17 |
} |
634 |
|
|
|
635 |
pajs |
1.3 |
int main(int argc, char **argv){ |
636 |
pajs |
1.28 |
|
637 |
pajs |
1.2 |
ihost_state_t ihost_state; |
638 |
pajs |
1.28 |
udp_sockinfo_t udp_sockinfo; |
639 |
|
|
|
640 |
pajs |
1.17 |
int cmdopt; |
641 |
|
|
extern int optind; |
642 |
pajs |
1.28 |
pid_t pid; |
643 |
pajs |
1.19 |
FILE *f; |
644 |
pajs |
1.28 |
int packet_num=0; |
645 |
|
|
int len; |
646 |
pajs |
1.18 |
|
647 |
pajs |
1.28 |
char packet[MAX_UDP_PACKET_SIZE]; |
648 |
pajs |
1.18 |
|
649 |
pajs |
1.28 |
time_t cur_time, sleep_delay, udp_time=0, config_time=0; |
650 |
pajs |
1.18 |
|
651 |
pajs |
1.28 |
/* Set default settings */ |
652 |
|
|
ihost_config.verbose=1; |
653 |
|
|
ihost_config.daemon=1; |
654 |
|
|
/* Set all errors to go down stderr until told otherwise */ |
655 |
|
|
ihost_config.log=stderr; |
656 |
|
|
|
657 |
|
|
/* Blank ihost_state to default settings */ |
658 |
|
|
ihost_state.filtermanager_host=NULL; |
659 |
|
|
ihost_state.host_fqdn=NULL; |
660 |
|
|
ihost_state.host_ip=NULL; |
661 |
|
|
ihost_state.server_fqdn=NULL; |
662 |
|
|
ihost_state.file_list=NULL; |
663 |
|
|
ihost_state.last_modified=NULL; |
664 |
|
|
|
665 |
|
|
while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ |
666 |
|
|
switch(cmdopt){ |
667 |
|
|
case 'v': |
668 |
|
|
ihost_config.verbose++; |
669 |
|
|
break; |
670 |
|
|
|
671 |
|
|
case 'f': |
672 |
|
|
/* Force syslog logging since stderr will be closed in this case */ |
673 |
|
|
ihost_config.daemon=0; |
674 |
|
|
break; |
675 |
|
|
|
676 |
|
|
case 'h': |
677 |
|
|
usage(argv[0]); |
678 |
|
|
break; |
679 |
|
|
|
680 |
|
|
case 'V': |
681 |
tdb |
1.29 |
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
682 |
pajs |
1.28 |
break; |
683 |
|
|
|
684 |
|
|
default: |
685 |
|
|
usage(argv[0]); |
686 |
|
|
exit(1); |
687 |
|
|
} |
688 |
|
|
} |
689 |
|
|
|
690 |
|
|
if(argc!=optind+2){ |
691 |
|
|
usage(argv[0]); |
692 |
|
|
exit(1); |
693 |
|
|
} |
694 |
pajs |
1.17 |
|
695 |
pajs |
1.28 |
ihost_state.filtermanager_host=strdup(argv[optind]); |
696 |
|
|
ihost_state.filtermanager_port=atoi(argv[optind+1]); |
697 |
|
|
|
698 |
|
|
if(gethostbyname(ihost_state.filtermanager_host)==NULL){ |
699 |
|
|
log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); |
700 |
pajs |
1.3 |
exit(1); |
701 |
|
|
} |
702 |
pajs |
1.28 |
if(ihost_state.filtermanager_port==0){ |
703 |
|
|
log_msg(LOG_ERR, "Invalid port number"); |
704 |
|
|
exit(1); |
705 |
pajs |
1.17 |
} |
706 |
pajs |
1.18 |
|
707 |
pajs |
1.28 |
if(ihost_config.daemon){ |
708 |
pajs |
1.18 |
pid=fork(); |
709 |
|
|
if(pid==-1){ |
710 |
pajs |
1.28 |
log_msg(LOG_CRIT, "Failed to background exiting"); |
711 |
|
|
exit(1); |
712 |
pajs |
1.18 |
}else if(pid!=0){ |
713 |
pajs |
1.28 |
/* Parent process */ |
714 |
|
|
return 0; |
715 |
|
|
} |
716 |
|
|
/* We should now be in the background*/ |
717 |
|
|
if(setsid()==-1){ |
718 |
|
|
log_msg(LOG_CRIT, "setsid failed"); |
719 |
|
|
exit(1); |
720 |
|
|
} |
721 |
|
|
|
722 |
tdb |
1.29 |
if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
723 |
pajs |
1.28 |
ihost_config.log=stderr; |
724 |
tdb |
1.29 |
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
725 |
pajs |
1.18 |
exit(1); |
726 |
|
|
} |
727 |
pajs |
1.28 |
|
728 |
|
|
fclose(stdin); |
729 |
|
|
fclose(stdout); |
730 |
|
|
fclose(stderr); |
731 |
|
|
|
732 |
|
|
} |
733 |
|
|
|
734 |
|
|
log_msg(LOG_INFO, "Starting ihost"); |
735 |
|
|
|
736 |
|
|
log_msg(LOG_DEBUG,"Writing PID FILE"); |
737 |
|
|
|
738 |
|
|
pid=getpid(); |
739 |
|
|
|
740 |
|
|
if((f=fopen(PID_FILE,"w")) == NULL){ |
741 |
|
|
log_msg(LOG_CRIT, "Failed to write PID file"); |
742 |
|
|
}else{ |
743 |
|
|
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
744 |
|
|
log_msg(LOG_CRIT, "Failed to write PID file"); |
745 |
|
|
} |
746 |
|
|
if((fclose(f))!=0){ |
747 |
|
|
log_msg(LOG_CRIT, "failed to close PID file"); |
748 |
|
|
} |
749 |
|
|
} |
750 |
|
|
|
751 |
|
|
/* Get the initial config from the filter manager. Should this fail, |
752 |
|
|
* wait, and then try again. */ |
753 |
|
|
|
754 |
|
|
get_diskio_stats_diff(&packet_num); |
755 |
|
|
packet_num=0; |
756 |
|
|
|
757 |
|
|
while(ihost_getconfig(&ihost_state)!=0){ |
758 |
|
|
log_msg(LOG_ERR, "Failed to get ihost config"); |
759 |
|
|
sleep(10); |
760 |
pajs |
1.18 |
} |
761 |
|
|
|
762 |
pajs |
1.28 |
printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); |
763 |
|
|
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
764 |
|
|
log_msg(LOG_ERR, "Failed to create udp socket"); |
765 |
|
|
sleep(10); |
766 |
pajs |
1.18 |
} |
767 |
pajs |
1.2 |
|
768 |
pajs |
1.28 |
config_time=time(NULL)+ihost_state.config_ttl; |
769 |
pajs |
1.5 |
|
770 |
pajs |
1.28 |
/* Now have config.. collect data and send as often as required */ |
771 |
pajs |
1.9 |
for(;;){ |
772 |
|
|
cur_time=time(NULL); |
773 |
pajs |
1.28 |
|
774 |
|
|
if(cur_time>=udp_time){ |
775 |
|
|
if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){ |
776 |
|
|
log_msg(LOG_ERR, "Failed to get system stats"); |
777 |
|
|
} |
778 |
|
|
|
779 |
|
|
len=strlen(packet); |
780 |
|
|
log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); |
781 |
|
|
|
782 |
|
|
if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ |
783 |
|
|
log_msg(LOG_CRIT, "Failed to send packet"); |
784 |
pajs |
1.9 |
} |
785 |
pajs |
1.28 |
udp_time=cur_time+ihost_state.udp_update_time; |
786 |
|
|
log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); |
787 |
pajs |
1.9 |
} |
788 |
pajs |
1.28 |
|
789 |
|
|
if(cur_time>=config_time){ |
790 |
|
|
if(ihost_getconfig(&ihost_state)!=0){ |
791 |
|
|
/* If we can't get the config, try again 5 minutes time */ |
792 |
|
|
log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time"); |
793 |
|
|
config_time=time(NULL)+300; |
794 |
|
|
}else{ |
795 |
|
|
close(udp_sockinfo.sock); |
796 |
|
|
|
797 |
|
|
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
798 |
|
|
log_msg(LOG_CRIT, "Failed to create udp socket"); |
799 |
|
|
sleep(10); |
800 |
|
|
} |
801 |
|
|
|
802 |
|
|
config_time=time(NULL)+ihost_state.config_ttl; |
803 |
pajs |
1.10 |
|
804 |
pajs |
1.28 |
log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl); |
805 |
pajs |
1.9 |
} |
806 |
|
|
} |
807 |
pajs |
1.10 |
|
808 |
pajs |
1.28 |
sleep_delay=udp_time-time(NULL); |
809 |
|
|
log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); |
810 |
pajs |
1.10 |
if(sleep_delay>0) sleep(sleep_delay); |
811 |
pajs |
1.28 |
} |
812 |
|
|
|
813 |
|
|
return(0); |
814 |
pajs |
1.1 |
} |