| 1 |
|
/* |
| 2 |
|
* i-scream central monitoring system |
| 3 |
< |
* http://www.i-scream.org.uk |
| 4 |
< |
* Copyright (C) 2000-2002 i-scream |
| 3 |
> |
* http://www.i-scream.org |
| 4 |
> |
* Copyright (C) 2000-2004 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 |
| 31 |
|
#include <stdarg.h> |
| 32 |
|
#include <errno.h> |
| 33 |
|
#include <netdb.h> |
| 34 |
+ |
#include <netinet/in.h> |
| 35 |
+ |
#include <time.h> |
| 36 |
|
|
| 37 |
< |
#include "ukcprog.h" |
| 38 |
< |
#include "statgrab.h" |
| 37 |
> |
#include <ukcprog.h> |
| 38 |
> |
#include <statgrab.h> |
| 39 |
|
|
| 40 |
|
#define LOG_CRIT 0 |
| 41 |
|
#define LOG_ERR 1 |
| 42 |
< |
#define LOG_INFO 2 |
| 42 |
> |
#define LOG_INFO 2 |
| 43 |
|
#define LOG_DEBUG 3 |
| 44 |
|
|
| 45 |
|
typedef struct{ |
| 48 |
|
|
| 49 |
|
char *host_ip; |
| 50 |
|
char *host_fqdn; |
| 51 |
+ |
int preset_fqdn; |
| 52 |
+ |
int preset_ip; |
| 53 |
|
|
| 54 |
|
char *server_fqdn; |
| 55 |
|
int server_udp_port; |
| 56 |
< |
|
| 56 |
> |
|
| 57 |
|
/* Weird stuff iscream wants sent to it */ |
| 58 |
|
char *last_modified; |
| 59 |
|
char *file_list; |
| 60 |
|
|
| 61 |
|
int udp_update_time; |
| 58 |
– |
// int config_ttl; |
| 62 |
|
|
| 63 |
|
time_t config_ttl; |
| 64 |
|
|
| 65 |
|
}ihost_state_t; |
| 66 |
|
|
| 67 |
|
typedef struct{ |
| 68 |
< |
int verbose; |
| 69 |
< |
int daemon; |
| 68 |
> |
int verbose; |
| 69 |
> |
int daemon; |
| 70 |
|
|
| 71 |
|
FILE *log; |
| 72 |
< |
}ihost_config_t; |
| 72 |
> |
}ihost_config_t; |
| 73 |
|
|
| 74 |
|
typedef struct{ |
| 75 |
|
struct sockaddr_in addr; |
| 78 |
|
|
| 79 |
|
ihost_config_t ihost_config; |
| 80 |
|
|
| 81 |
+ |
extern int errno; |
| 82 |
+ |
|
| 83 |
+ |
/* Taken from the OpenSSH code. Its licence included in function.*/ |
| 84 |
+ |
#ifndef HAVE_STRLCAT |
| 85 |
+ |
|
| 86 |
+ |
/* |
| 87 |
+ |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
| 88 |
+ |
* All rights reserved. |
| 89 |
+ |
* |
| 90 |
+ |
* Redistribution and use in source and binary forms, with or without |
| 91 |
+ |
* modification, are permitted provided that the following conditions |
| 92 |
+ |
* are met: |
| 93 |
+ |
* 1. Redistributions of source code must retain the above copyright |
| 94 |
+ |
* notice, this list of conditions and the following disclaimer. |
| 95 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
| 96 |
+ |
* notice, this list of conditions and the following disclaimer in the |
| 97 |
+ |
* documentation and/or other materials provided with the distribution. |
| 98 |
+ |
* 3. The name of the author may not be used to endorse or promote products |
| 99 |
+ |
* derived from this software without specific prior written permission. |
| 100 |
+ |
* |
| 101 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 102 |
+ |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 103 |
+ |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 104 |
+ |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 105 |
+ |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 106 |
+ |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 107 |
+ |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 108 |
+ |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 109 |
+ |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 110 |
+ |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 111 |
+ |
*/ |
| 112 |
+ |
|
| 113 |
+ |
/* |
| 114 |
+ |
* Appends src to string dst of size siz (unlike strncat, siz is the |
| 115 |
+ |
* full size of dst, not space left). At most siz-1 characters |
| 116 |
+ |
* will be copied. Always NUL terminates (unless siz <= strlen(dst)). |
| 117 |
+ |
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
| 118 |
+ |
* If retval >= siz, truncation occurred. |
| 119 |
+ |
*/ |
| 120 |
+ |
size_t |
| 121 |
+ |
strlcat(dst, src, siz) |
| 122 |
+ |
char *dst; |
| 123 |
+ |
const char *src; |
| 124 |
+ |
size_t siz; |
| 125 |
+ |
{ |
| 126 |
+ |
register char *d = dst; |
| 127 |
+ |
register const char *s = src; |
| 128 |
+ |
register size_t n = siz; |
| 129 |
+ |
size_t dlen; |
| 130 |
+ |
|
| 131 |
+ |
/* Find the end of dst and adjust bytes left but don't go past end */ |
| 132 |
+ |
while (n-- != 0 && *d != '\0') |
| 133 |
+ |
d++; |
| 134 |
+ |
dlen = d - dst; |
| 135 |
+ |
n = siz - dlen; |
| 136 |
+ |
|
| 137 |
+ |
if (n == 0) |
| 138 |
+ |
return(dlen + strlen(s)); |
| 139 |
+ |
while (*s != '\0') { |
| 140 |
+ |
if (n != 1) { |
| 141 |
+ |
*d++ = *s; |
| 142 |
+ |
n--; |
| 143 |
+ |
} |
| 144 |
+ |
s++; |
| 145 |
+ |
} |
| 146 |
+ |
*d = '\0'; |
| 147 |
+ |
|
| 148 |
+ |
return(dlen + (s - src)); /* count does not include NUL */ |
| 149 |
+ |
} |
| 150 |
+ |
|
| 151 |
+ |
#endif |
| 152 |
+ |
/* End strlcat function taken from OpenSSH */ |
| 153 |
+ |
|
| 154 |
|
void log_msg(int level, char *format, ...){ |
| 79 |
– |
extern int errno; |
| 155 |
|
int cur_errno; |
| 156 |
|
va_list ap; |
| 157 |
|
|
| 166 |
|
}else{ |
| 167 |
|
fprintf(ihost_config.log, "\n"); |
| 168 |
|
} |
| 169 |
+ |
fflush(ihost_config.log); |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
|
| 97 |
– |
/* 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 |
– |
|
| 104 |
– |
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 |
– |
|
| 173 |
|
int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ |
| 174 |
|
|
| 175 |
|
struct in_addr haddr; |
| 200 |
|
struct in_addr haddr; |
| 201 |
|
FILE *f; |
| 202 |
|
|
| 203 |
< |
log_msg(LOG_DEBUG, "Creating tcp socket"); |
| 203 |
> |
log_msg(LOG_DEBUG, "Creating tcp socket"); |
| 204 |
|
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ |
| 205 |
|
log_msg(LOG_CRIT, "Failed to make TCP Socket"); |
| 206 |
|
return NULL; |
| 208 |
|
|
| 209 |
|
if((get_host_addr(hostname, &haddr))!=0){ |
| 210 |
|
log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
| 211 |
+ |
close(sock); |
| 212 |
|
return NULL; |
| 213 |
|
} |
| 214 |
|
|
| 220 |
|
log_msg(LOG_DEBUG, "Creating a tcp connection"); |
| 221 |
|
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
| 222 |
|
log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
| 223 |
+ |
close(sock); |
| 224 |
|
return NULL; |
| 225 |
|
} |
| 226 |
|
|
| 241 |
|
*response=fpgetline(f); |
| 242 |
|
fseek(f, 0, SEEK_CUR); |
| 243 |
|
|
| 244 |
< |
if(*response!=NULL) log_msg(LOG_DEBUG, "Recieved %s", *response); |
| 244 |
> |
if(*response!=NULL) log_msg(LOG_DEBUG, "Received %s", *response); |
| 245 |
|
|
| 246 |
|
if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; |
| 247 |
< |
|
| 247 |
> |
|
| 248 |
|
if(expected==NULL) return 0; |
| 249 |
|
|
| 250 |
|
if((strcmp(expected, *response))==0) return 0; |
| 254 |
|
} |
| 255 |
|
|
| 256 |
|
int tcp_comm_strdup(FILE *f, char *send, char **response, char *expected){ |
| 257 |
< |
if((tcp_comm(f, send, response, expected))!=0){ |
| 257 |
> |
if((tcp_comm(f, send, response, expected))!=0){ |
| 258 |
|
return -1; |
| 259 |
|
} |
| 260 |
|
|
| 263 |
|
|
| 264 |
|
return 0; |
| 265 |
|
} |
| 266 |
< |
|
| 266 |
> |
|
| 267 |
|
int ihost_getconfig(ihost_state_t *ihost_state){ |
| 268 |
|
|
| 269 |
|
FILE *tcp_con; |
| 271 |
|
char *response_ptr; |
| 272 |
|
|
| 273 |
|
/* Keep these in case of a failure and so it can keep running on the old config */ |
| 274 |
< |
char *file_list=NULL; |
| 274 |
> |
char *file_list=NULL; |
| 275 |
|
char *last_modified=NULL; |
| 276 |
|
char *host_fqdn=NULL; |
| 277 |
|
char *host_ip=NULL; |
| 281 |
|
time_t config_ttl=0; |
| 282 |
|
|
| 283 |
|
if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port))==NULL){ |
| 284 |
< |
goto error; |
| 284 |
> |
return -1; |
| 285 |
|
} |
| 286 |
|
|
| 287 |
|
if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
| 288 |
|
if(tcp_con==NULL){ |
| 289 |
|
goto error; |
| 290 |
< |
} |
| 291 |
< |
|
| 290 |
> |
} |
| 291 |
> |
|
| 292 |
|
if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ |
| 293 |
|
goto error; |
| 294 |
|
} |
| 295 |
< |
|
| 295 |
> |
|
| 296 |
|
if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ |
| 297 |
|
goto error; |
| 298 |
|
} |
| 299 |
< |
|
| 299 |
> |
|
| 300 |
|
if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ |
| 301 |
|
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
| 302 |
|
goto error; |
| 320 |
|
goto error; |
| 321 |
|
} |
| 322 |
|
last_modified=response; |
| 323 |
< |
|
| 323 |
> |
|
| 324 |
|
if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ |
| 325 |
|
goto error; |
| 326 |
|
} |
| 365 |
|
} |
| 366 |
|
response_ptr++; |
| 367 |
|
if(response_ptr==NULL){ |
| 368 |
< |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
| 369 |
< |
goto error; |
| 370 |
< |
} |
| 368 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
| 369 |
> |
goto error; |
| 370 |
> |
} |
| 371 |
|
|
| 372 |
< |
printf("string : %s\n", response_ptr); |
| 313 |
< |
server_udp_port=atoi(response_ptr); |
| 372 |
> |
server_udp_port=atoi(response_ptr); |
| 373 |
|
|
| 374 |
|
if (server_udp_port==0){ |
| 375 |
|
log_msg(LOG_ERR, "Incorrect data sent by server"); |
| 376 |
|
goto error; |
| 377 |
|
} |
| 378 |
|
} |
| 379 |
< |
|
| 379 |
> |
|
| 380 |
|
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
| 381 |
|
goto error; |
| 382 |
|
} |
| 387 |
|
|
| 388 |
|
/* Free the old data before pointing them to the new data. m_free copes should |
| 389 |
|
* this already be NULL */ |
| 390 |
< |
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
| 391 |
< |
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
| 392 |
< |
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); |
| 390 |
> |
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
| 391 |
> |
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
| 392 |
> |
if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
| 393 |
|
|
| 394 |
+ |
if(ihost_state->preset_fqdn){ |
| 395 |
+ |
if(host_fqdn != NULL) free(host_fqdn); |
| 396 |
+ |
}else{ |
| 397 |
+ |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
| 398 |
+ |
ihost_state->host_fqdn=host_fqdn; |
| 399 |
+ |
} |
| 400 |
+ |
|
| 401 |
+ |
if(ihost_state->preset_ip){ |
| 402 |
+ |
if(host_ip != NULL) free(host_ip); |
| 403 |
+ |
}else{ |
| 404 |
+ |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
| 405 |
+ |
ihost_state->host_ip=host_ip; |
| 406 |
+ |
} |
| 407 |
+ |
|
| 408 |
+ |
|
| 409 |
|
ihost_state->file_list=file_list; |
| 410 |
|
ihost_state->last_modified=last_modified; |
| 339 |
– |
ihost_state->host_fqdn=host_fqdn; |
| 340 |
– |
ihost_state->host_ip=host_ip; |
| 411 |
|
ihost_state->server_fqdn=server_fqdn; |
| 412 |
|
ihost_state->server_udp_port=server_udp_port; |
| 413 |
|
ihost_state->udp_update_time=udp_update_time; |
| 432 |
|
|
| 433 |
|
int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ |
| 434 |
|
char tmp[size]; |
| 435 |
< |
cpu_percent_t *cpu_percent; |
| 436 |
< |
mem_stat_t *mem_stats; |
| 437 |
< |
load_stat_t *load_stats; |
| 438 |
< |
user_stat_t *user_stats; |
| 439 |
< |
swap_stat_t *swap_stats; |
| 440 |
< |
general_stat_t *general_stats; |
| 441 |
< |
disk_stat_t *disk_stats; |
| 442 |
< |
diskio_stat_t *diskio_stats; |
| 443 |
< |
process_stat_t *process_stats; |
| 444 |
< |
network_stat_t *network_stats; |
| 445 |
< |
page_stat_t *page_stats; |
| 435 |
> |
sg_cpu_percents *cpu_percent; |
| 436 |
> |
sg_mem_stats *mem_stats; |
| 437 |
> |
sg_load_stats *load_stats; |
| 438 |
> |
sg_user_stats *user_stats; |
| 439 |
> |
sg_swap_stats *swap_stats; |
| 440 |
> |
sg_host_info *general_stats; |
| 441 |
> |
sg_fs_stats *disk_stats; |
| 442 |
> |
sg_disk_io_stats *diskio_stats; |
| 443 |
> |
sg_process_count *process_stats; |
| 444 |
> |
sg_network_io_stats *network_stats; |
| 445 |
> |
sg_page_stats *page_stats; |
| 446 |
|
int disk_entries=0; |
| 447 |
|
int diskio_entries=0; |
| 448 |
|
int network_entries=0; |
| 453 |
|
|
| 454 |
|
/* Print start of the packet we want */ |
| 455 |
|
snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \ |
| 456 |
< |
seq_no, ihost_state->host_fqdn, time(NULL), ihost_state->host_ip); |
| 456 |
> |
seq_no, ihost_state->host_fqdn, (long) time(NULL), ihost_state->host_ip); |
| 457 |
|
|
| 458 |
|
/* Get cpu stats, check it is correct, then fill in its entry for the xml */ |
| 459 |
< |
if((cpu_percent=cpu_percent_usage())==NULL){ |
| 460 |
< |
log_msg(LOG_CRIT, "Failed to get cpu statistics"); |
| 459 |
> |
if((cpu_percent=sg_get_cpu_percents())==NULL){ |
| 460 |
> |
log_msg(LOG_CRIT, "Failed to get cpu statistics: %s (%s)", |
| 461 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 462 |
|
}else{ |
| 463 |
|
snprintf(tmp, size, \ |
| 464 |
|
"<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \ |
| 471 |
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
| 472 |
|
} |
| 473 |
|
|
| 474 |
< |
|
| 475 |
< |
/*Get mem stats, and fill in xml */ |
| 476 |
< |
if((mem_stats=get_memory_stats())==NULL){ |
| 477 |
< |
log_msg(LOG_CRIT, "Failed to get memory statistics"); |
| 474 |
> |
|
| 475 |
> |
/*Get mem stats, and fill in xml */ |
| 476 |
> |
if((mem_stats=sg_get_mem_stats())==NULL){ |
| 477 |
> |
log_msg(LOG_CRIT, "Failed to get memory statistics: %s (%s)", |
| 478 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 479 |
|
}else{ |
| 480 |
|
snprintf(tmp, size, \ |
| 481 |
|
"<memory><total>%lld</total><free>%lld</free><used>%lld</used><cache>%lld</cache></memory>", \ |
| 483 |
|
mem_stats->free, \ |
| 484 |
|
mem_stats->used, \ |
| 485 |
|
mem_stats->cache); |
| 486 |
< |
|
| 486 |
> |
|
| 487 |
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
| 488 |
|
} |
| 489 |
|
|
| 490 |
|
|
| 491 |
< |
/* Get load stats */ |
| 492 |
< |
if((load_stats=get_load_stats())==NULL){ |
| 493 |
< |
log_msg(LOG_CRIT, "Failed to get load statistics"); |
| 491 |
> |
/* Get load stats */ |
| 492 |
> |
if((load_stats=sg_get_load_stats())==NULL){ |
| 493 |
> |
log_msg(LOG_CRIT, "Failed to get load statistics: %s (%s)", |
| 494 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 495 |
|
}else{ |
| 496 |
|
snprintf(tmp, size, \ |
| 497 |
|
"<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \ |
| 503 |
|
|
| 504 |
|
|
| 505 |
|
/* get user stats */ |
| 506 |
< |
|
| 507 |
< |
if((user_stats=get_user_stats())==NULL){ |
| 508 |
< |
log_msg(LOG_CRIT, "Failed to get user statistics"); |
| 506 |
> |
|
| 507 |
> |
if((user_stats=sg_get_user_stats())==NULL){ |
| 508 |
> |
log_msg(LOG_CRIT, "Failed to get user statistics: %s (%s)", |
| 509 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 510 |
|
}else{ |
| 511 |
|
|
| 512 |
|
snprintf(tmp, size, \ |
| 513 |
|
"<users><list>%s</list><count>%d</count></users>", \ |
| 514 |
|
user_stats->name_list, \ |
| 515 |
|
user_stats->num_entries); |
| 516 |
< |
|
| 516 |
> |
|
| 517 |
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
| 518 |
|
} |
| 519 |
|
|
| 520 |
|
|
| 521 |
|
/* swap stats */ |
| 522 |
< |
if((swap_stats=get_swap_stats())==NULL){ |
| 523 |
< |
log_msg(LOG_CRIT, "Failed to get swap statistics"); |
| 522 |
> |
if((swap_stats=sg_get_swap_stats())==NULL){ |
| 523 |
> |
log_msg(LOG_CRIT, "Failed to get swap statistics: %s (%s)", |
| 524 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 525 |
|
}else{ |
| 526 |
|
snprintf(tmp, size, \ |
| 527 |
|
"<swap><total>%lld</total><used>%lld</used><free>%lld</free></swap>",\ |
| 528 |
|
swap_stats->total, \ |
| 529 |
|
swap_stats->used, \ |
| 530 |
|
swap_stats->free); |
| 531 |
< |
|
| 531 |
> |
|
| 532 |
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
| 533 |
|
} |
| 534 |
|
|
| 535 |
|
|
| 536 |
|
/* general stats */ |
| 537 |
< |
|
| 538 |
< |
if((general_stats=get_general_stats())==NULL){ |
| 539 |
< |
log_msg(LOG_CRIT, "Failed to get general statistics"); |
| 537 |
> |
|
| 538 |
> |
if((general_stats=sg_get_host_info())==NULL){ |
| 539 |
> |
log_msg(LOG_CRIT, "Failed to get host info statistics: %s (%s)", |
| 540 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 541 |
|
}else{ |
| 542 |
|
snprintf(tmp, size, \ |
| 543 |
|
"<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \ |
| 547 |
|
general_stats->hostname, \ |
| 548 |
|
general_stats->platform, \ |
| 549 |
|
(long)general_stats->uptime); |
| 550 |
< |
|
| 550 |
> |
|
| 551 |
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
| 552 |
< |
|
| 552 |
> |
|
| 553 |
|
} |
| 554 |
|
|
| 555 |
< |
|
| 555 |
> |
|
| 556 |
|
/* process stats */ |
| 557 |
< |
if((process_stats=get_process_stats())==NULL){ |
| 558 |
< |
log_msg(LOG_CRIT, "Failed to get general statistics"); |
| 557 |
> |
if((process_stats=sg_get_process_count())==NULL){ |
| 558 |
> |
log_msg(LOG_CRIT, "Failed to get process statistics: %s (%s)", |
| 559 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 560 |
|
}else{ |
| 561 |
|
snprintf(tmp, size, \ |
| 562 |
|
"<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\ |
| 572 |
|
|
| 573 |
|
|
| 574 |
|
/* Get paging stats */ |
| 575 |
< |
if((page_stats=get_page_stats_diff())==NULL){ |
| 576 |
< |
log_msg(LOG_CRIT, "Failed to get paging statistics"); |
| 575 |
> |
if((page_stats=sg_get_page_stats_diff())==NULL){ |
| 576 |
> |
log_msg(LOG_CRIT, "Failed to get paging statistics: %s (%s)", |
| 577 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 578 |
|
}else{ |
| 579 |
|
if(page_stats->systime!=0){ |
| 580 |
|
x=page_stats->pages_pagein / page_stats->systime; |
| 584 |
|
y=page_stats->pages_pageout; |
| 585 |
|
} |
| 586 |
|
snprintf(tmp, size, \ |
| 587 |
< |
"<pages><swapins>%lld</swapins><swapouts>%lld</swapouts></pages>", \ |
| 587 |
> |
"<pages><pageins>%lld</pageins><pageouts>%lld</pageouts></pages>", \ |
| 588 |
|
x, \ |
| 589 |
|
y); |
| 590 |
< |
|
| 590 |
> |
|
| 591 |
|
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
| 592 |
|
} |
| 593 |
|
|
| 594 |
|
|
| 595 |
|
/* get diskio stats */ |
| 596 |
< |
|
| 597 |
< |
if((diskio_stats=get_diskio_stats_diff(&diskio_entries))==NULL){ |
| 598 |
< |
log_msg(LOG_CRIT, "Failed to get diskio statistics"); |
| 596 |
> |
|
| 597 |
> |
if((diskio_stats=sg_get_disk_io_stats_diff(&diskio_entries))==NULL){ |
| 598 |
> |
log_msg(LOG_CRIT, "Failed to get disk io statistics: %s (%s)", |
| 599 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 600 |
|
}else{ |
| 601 |
|
strlcat(xml, "<diskio>", size); |
| 602 |
|
for(counter=0;counter<diskio_entries;counter++){ |
| 606 |
|
y=diskio_stats->write_bytes / diskio_stats->systime; |
| 607 |
|
}else{ |
| 608 |
|
x=diskio_stats->read_bytes; |
| 609 |
< |
y=diskio_stats->write_bytes; |
| 609 |
> |
y=diskio_stats->write_bytes; |
| 610 |
|
} |
| 611 |
< |
|
| 611 |
> |
|
| 612 |
|
snprintf(tmp, size, \ |
| 613 |
|
"<p%d name=\"%s\" rbytes=\"%lld\" wbytes=\"%lld\"></p%d>", \ |
| 614 |
|
counter, \ |
| 625 |
|
|
| 626 |
|
} |
| 627 |
|
|
| 628 |
< |
|
| 628 |
> |
|
| 629 |
|
/* get networks stats */ |
| 630 |
< |
|
| 631 |
< |
if((network_stats=get_network_stats_diff(&network_entries))==NULL){ |
| 632 |
< |
log_msg(LOG_CRIT, "Failed to get network statistics"); |
| 630 |
> |
|
| 631 |
> |
if((network_stats=sg_get_network_io_stats_diff(&network_entries))==NULL){ |
| 632 |
> |
log_msg(LOG_CRIT, "Failed to get network io statistics: %s (%s)", |
| 633 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 634 |
|
}else{ |
| 635 |
|
strlcat(xml, "<net>", size); |
| 636 |
|
for(counter=0;counter<network_entries;counter++){ |
| 660 |
|
|
| 661 |
|
|
| 662 |
|
/* get disk stats */ |
| 663 |
< |
|
| 664 |
< |
if((disk_stats=get_disk_stats(&disk_entries))==NULL){ |
| 665 |
< |
log_msg(LOG_CRIT, "Failed to get disk statistics"); |
| 663 |
> |
|
| 664 |
> |
if((disk_stats=sg_get_fs_stats(&disk_entries))==NULL){ |
| 665 |
> |
log_msg(LOG_CRIT, "Failed to get fs statistics: %s (%s)", |
| 666 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 667 |
|
}else{ |
| 668 |
|
strlcat(xml, "<disk>", size); |
| 669 |
|
for(counter=0;counter<disk_entries;counter++){ |
| 689 |
|
if(strlcat(xml, "</disk>", size) >= size) goto too_big_error; |
| 690 |
|
|
| 691 |
|
} |
| 611 |
– |
|
| 692 |
|
|
| 693 |
+ |
|
| 694 |
|
if(strlcat(xml, "</packet>", size) >= size) goto too_big_error; |
| 695 |
|
|
| 696 |
|
/*If we got to here, it should of all been filled in nicely now */ |
| 702 |
|
} |
| 703 |
|
|
| 704 |
|
|
| 705 |
< |
|
| 705 |
> |
|
| 706 |
|
void usage(char *progname){ |
| 707 |
< |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
| 708 |
< |
fprintf(stderr, "Options\n"); |
| 709 |
< |
fprintf(stderr, " -v Verbose mode,-vv would make even more verbose\n"); |
| 710 |
< |
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
| 711 |
< |
fprintf(stderr, " -V Print version number\n"); |
| 712 |
< |
fprintf(stderr, " -h Prints this help page\n"); |
| 713 |
< |
exit(1); |
| 707 |
> |
fprintf(stderr, "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n", progname); |
| 708 |
> |
fprintf(stderr, " -v Verbose mode, -vv would make even more verbose\n"); |
| 709 |
> |
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
| 710 |
> |
fprintf(stderr, " -n Set the machine name to be reported as\n"); |
| 711 |
> |
fprintf(stderr, " -i Set the IP to be reported as\n"); |
| 712 |
> |
fprintf(stderr, " -s Specifies the i-scream server to connect to\n"); |
| 713 |
> |
fprintf(stderr, " default: %s\n", DEF_SERVER_NAME); |
| 714 |
> |
fprintf(stderr, " -p Specifies the i-scream server port\n"); |
| 715 |
> |
fprintf(stderr, " default: %d\n", DEF_SERVER_PORT); |
| 716 |
> |
fprintf(stderr, " -V Print version number\n"); |
| 717 |
> |
fprintf(stderr, " -h Prints this help page\n"); |
| 718 |
> |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
| 719 |
> |
exit(1); |
| 720 |
|
} |
| 721 |
|
|
| 722 |
|
int main(int argc, char **argv){ |
| 723 |
|
|
| 724 |
|
ihost_state_t ihost_state; |
| 725 |
|
udp_sockinfo_t udp_sockinfo; |
| 726 |
< |
|
| 726 |
> |
|
| 727 |
|
int cmdopt; |
| 728 |
|
extern int optind; |
| 729 |
|
pid_t pid; |
| 735 |
|
|
| 736 |
|
time_t cur_time, sleep_delay, udp_time=0, config_time=0; |
| 737 |
|
|
| 738 |
< |
/* Set default settings */ |
| 738 |
> |
/* Set default settings */ |
| 739 |
|
ihost_config.verbose=1; |
| 740 |
|
ihost_config.daemon=1; |
| 741 |
|
/* Set all errors to go down stderr until told otherwise */ |
| 742 |
|
ihost_config.log=stderr; |
| 743 |
|
|
| 744 |
|
/* Blank ihost_state to default settings */ |
| 745 |
< |
ihost_state.filtermanager_host=NULL; |
| 745 |
> |
ihost_state.filtermanager_host=DEF_SERVER_NAME; |
| 746 |
> |
ihost_state.filtermanager_port=DEF_SERVER_PORT; |
| 747 |
|
ihost_state.host_fqdn=NULL; |
| 748 |
|
ihost_state.host_ip=NULL; |
| 749 |
+ |
ihost_state.preset_fqdn = 0; |
| 750 |
+ |
ihost_state.preset_ip = 0; |
| 751 |
|
ihost_state.server_fqdn=NULL; |
| 752 |
|
ihost_state.file_list=NULL; |
| 753 |
|
ihost_state.last_modified=NULL; |
| 754 |
|
|
| 755 |
< |
while((cmdopt=getopt(argc, argv, "vfhV")) != -1){ |
| 756 |
< |
switch(cmdopt){ |
| 757 |
< |
case 'v': |
| 758 |
< |
ihost_config.verbose++; |
| 759 |
< |
break; |
| 755 |
> |
while((cmdopt=getopt(argc, argv, "vfVn:i:s:p:h")) != -1){ |
| 756 |
> |
switch(cmdopt){ |
| 757 |
> |
case 'v': |
| 758 |
> |
ihost_config.verbose++; |
| 759 |
> |
break; |
| 760 |
|
|
| 761 |
< |
case 'f': |
| 762 |
< |
/* Force syslog logging since stderr will be closed in this case */ |
| 763 |
< |
ihost_config.daemon=0; |
| 764 |
< |
break; |
| 761 |
> |
case 'f': |
| 762 |
> |
/* Force syslog logging since stderr will be closed in this case */ |
| 763 |
> |
ihost_config.daemon=0; |
| 764 |
> |
break; |
| 765 |
|
|
| 766 |
< |
case 'h': |
| 767 |
< |
usage(argv[0]); |
| 768 |
< |
break; |
| 766 |
> |
case 'V': |
| 767 |
> |
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
| 768 |
> |
break; |
| 769 |
> |
case 'n': |
| 770 |
> |
ihost_state.preset_fqdn = 1; |
| 771 |
> |
ihost_state.host_fqdn = strdup(optarg); |
| 772 |
> |
if(ihost_state.host_fqdn == NULL){ |
| 773 |
> |
fprintf(stderr, "Missing hostname\n"); |
| 774 |
> |
usage(argv[0]); |
| 775 |
> |
} |
| 776 |
> |
break; |
| 777 |
> |
case 'i': |
| 778 |
> |
/* Hmm.. Someone could set any string to be the IP, and it will let it */ |
| 779 |
> |
ihost_state.preset_ip = 1; |
| 780 |
> |
ihost_state.host_ip = strdup(optarg); |
| 781 |
> |
if(ihost_state.host_ip == NULL){ |
| 782 |
> |
fprintf(stderr, "Missing ip\n"); |
| 783 |
> |
usage(argv[0]); |
| 784 |
> |
} |
| 785 |
> |
break; |
| 786 |
|
|
| 787 |
< |
case 'V': |
| 788 |
< |
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
| 789 |
< |
break; |
| 787 |
> |
case 's': |
| 788 |
> |
ihost_state.filtermanager_host=strdup(optarg); |
| 789 |
> |
break; |
| 790 |
|
|
| 791 |
< |
default: |
| 792 |
< |
usage(argv[0]); |
| 793 |
< |
exit(1); |
| 687 |
< |
} |
| 688 |
< |
} |
| 791 |
> |
case 'p': |
| 792 |
> |
ihost_state.filtermanager_port=atoi(optarg); |
| 793 |
> |
break; |
| 794 |
|
|
| 795 |
< |
if(argc!=optind+2){ |
| 796 |
< |
usage(argv[0]); |
| 797 |
< |
exit(1); |
| 798 |
< |
} |
| 795 |
> |
case 'h': |
| 796 |
> |
default: |
| 797 |
> |
usage(argv[0]); |
| 798 |
> |
exit(1); |
| 799 |
> |
} |
| 800 |
> |
} |
| 801 |
|
|
| 695 |
– |
ihost_state.filtermanager_host=strdup(argv[optind]); |
| 696 |
– |
ihost_state.filtermanager_port=atoi(argv[optind+1]); |
| 697 |
– |
|
| 802 |
|
if(gethostbyname(ihost_state.filtermanager_host)==NULL){ |
| 803 |
|
log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); |
| 804 |
|
exit(1); |
| 812 |
|
pid=fork(); |
| 813 |
|
if(pid==-1){ |
| 814 |
|
log_msg(LOG_CRIT, "Failed to background exiting"); |
| 815 |
< |
exit(1); |
| 815 |
> |
exit(1); |
| 816 |
|
}else if(pid!=0){ |
| 817 |
< |
/* Parent process */ |
| 818 |
< |
return 0; |
| 819 |
< |
} |
| 820 |
< |
/* We should now be in the background*/ |
| 821 |
< |
if(setsid()==-1){ |
| 822 |
< |
log_msg(LOG_CRIT, "setsid failed"); |
| 823 |
< |
exit(1); |
| 824 |
< |
} |
| 825 |
< |
|
| 817 |
> |
/* Parent process */ |
| 818 |
> |
return 0; |
| 819 |
> |
} |
| 820 |
> |
/* We should now be in the background*/ |
| 821 |
> |
if(setsid()==-1){ |
| 822 |
> |
log_msg(LOG_CRIT, "setsid failed"); |
| 823 |
> |
exit(1); |
| 824 |
> |
} |
| 825 |
> |
|
| 826 |
|
if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
| 827 |
|
ihost_config.log=stderr; |
| 828 |
|
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
| 829 |
|
exit(1); |
| 830 |
|
} |
| 831 |
|
|
| 832 |
< |
fclose(stdin); |
| 833 |
< |
fclose(stdout); |
| 834 |
< |
fclose(stderr); |
| 832 |
> |
fclose(stdin); |
| 833 |
> |
fclose(stdout); |
| 834 |
> |
fclose(stderr); |
| 835 |
|
|
| 836 |
< |
} |
| 836 |
> |
} |
| 837 |
|
|
| 838 |
< |
log_msg(LOG_INFO, "Starting ihost"); |
| 735 |
< |
|
| 736 |
< |
log_msg(LOG_DEBUG,"Writing PID FILE"); |
| 838 |
> |
log_msg(LOG_INFO, "Starting ihost..."); |
| 839 |
|
|
| 840 |
< |
pid=getpid(); |
| 840 |
> |
log_msg(LOG_DEBUG, "Running sg_init()"); |
| 841 |
> |
if(sg_init()){ |
| 842 |
> |
log_msg(LOG_CRIT, "sg_init failed: %s (%s)", |
| 843 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
| 844 |
> |
exit(1); |
| 845 |
> |
} |
| 846 |
|
|
| 847 |
< |
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 |
< |
} |
| 847 |
> |
log_msg(LOG_DEBUG,"Writing PID FILE"); |
| 848 |
|
|
| 849 |
< |
/* Get the initial config from the filter manager. Should this fail, |
| 849 |
> |
pid=getpid(); |
| 850 |
> |
|
| 851 |
> |
if((f=fopen(PID_FILE,"w")) == NULL){ |
| 852 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
| 853 |
> |
}else{ |
| 854 |
> |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
| 855 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
| 856 |
> |
} |
| 857 |
> |
if((fclose(f))!=0){ |
| 858 |
> |
log_msg(LOG_CRIT, "failed to close PID file"); |
| 859 |
> |
} |
| 860 |
> |
} |
| 861 |
> |
|
| 862 |
> |
/* Get the initial config from the filter manager. Should this fail, |
| 863 |
|
* wait, and then try again. */ |
| 864 |
|
|
| 865 |
< |
get_diskio_stats_diff(&packet_num); |
| 865 |
> |
sg_get_disk_io_stats_diff(&packet_num); |
| 866 |
|
packet_num=0; |
| 867 |
|
|
| 868 |
|
while(ihost_getconfig(&ihost_state)!=0){ |
| 870 |
|
sleep(10); |
| 871 |
|
} |
| 872 |
|
|
| 762 |
– |
printf("%s\n%d\n", ihost_state.server_fqdn, ihost_state.server_udp_port); |
| 873 |
|
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
| 874 |
< |
log_msg(LOG_ERR, "Failed to create udp socket"); |
| 874 |
> |
log_msg(LOG_ERR, "Failed to create udp socket"); |
| 875 |
|
sleep(10); |
| 876 |
|
} |
| 877 |
|
|
| 888 |
|
|
| 889 |
|
len=strlen(packet); |
| 890 |
|
log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); |
| 891 |
< |
|
| 891 |
> |
|
| 892 |
|
if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ |
| 893 |
|
log_msg(LOG_CRIT, "Failed to send packet"); |
| 894 |
|
} |
| 895 |
|
udp_time=cur_time+ihost_state.udp_update_time; |
| 896 |
|
log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); |
| 897 |
|
} |
| 898 |
< |
|
| 898 |
> |
|
| 899 |
|
if(cur_time>=config_time){ |
| 900 |
|
if(ihost_getconfig(&ihost_state)!=0){ |
| 901 |
|
/* If we can't get the config, try again 5 minutes time */ |
| 904 |
|
}else{ |
| 905 |
|
close(udp_sockinfo.sock); |
| 906 |
|
|
| 907 |
< |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
| 908 |
< |
log_msg(LOG_CRIT, "Failed to create udp socket"); |
| 909 |
< |
sleep(10); |
| 907 |
> |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
| 908 |
> |
log_msg(LOG_CRIT, "Failed to create udp socket"); |
| 909 |
> |
sleep(10); |
| 910 |
|
} |
| 911 |
|
|
| 912 |
|
config_time=time(NULL)+ihost_state.config_ttl; |
| 918 |
|
sleep_delay=udp_time-time(NULL); |
| 919 |
|
log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); |
| 920 |
|
if(sleep_delay>0) sleep(sleep_delay); |
| 921 |
< |
} |
| 922 |
< |
|
| 921 |
> |
} |
| 922 |
> |
|
| 923 |
|
return(0); |
| 924 |
|
} |