1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
/********************************************
* NetMRG Integrator
*
* netmrg.cpp
* NetMRG Gatherer
*
* see doc/LICENSE for copyright information
********************************************/
/*
NetMRG Monitoring Procedure
Copyright 2001-2006 Brady Alleman. All Rights Reserved.
MySQL examples from http://mysql.turbolift.com/mysql/chapter4.php3
pthreads examples from http://www.math.arizona.edu/swig/pthreads/threads.html
net-snmp examples from http://net-snmp.sf.net/
Thanks to Patrick Haller (http://haller.ws) for helping to debug threading in the original gatherer.
*/
#include "common.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <errno.h>
#include <list>
#include <iostream>
#include <termios.h>
#include <signal.h>
#include <time.h>
#include <cstring>
using namespace std;
int active_threads = 0;
bool netmrg_terminated = false;
// Include the NetMRG Headers
#include "types.h"
#include "utils.h"
#include "locks.h"
#include "settings.h"
#include "snmp.h"
#include "db.h"
#include "rrd.h"
#include "mappings.h"
#include "devices.h"
ScheduleType schedule = schOnce;
#ifdef OLD_MYSQL
#define MYSQL_THREAD_INIT
#define MYSQL_THREAD_END
#else
#define MYSQL_THREAD_INIT mysql_thread_init()
#define MYSQL_THREAD_END mysql_thread_end()
#endif
// child - the thread spawned to process each device
void *child(void * arg)
{
int device_id = *(int *) arg;
MYSQL_THREAD_INIT;
process_device(device_id);
netmrg_mutex_lock(lkActiveThreads);
active_threads--;
netmrg_cond_signal(cActiveThreads);
netmrg_mutex_unlock(lkActiveThreads);
MYSQL_THREAD_END;
pthread_exit(0);
} // end child
// remove lock file
void remove_lockfile()
{
unlink(get_setting(setPathLockFile).c_str());
}
// SIGTERM signal handler
void handle_sigterm(int signum)
{
netmrg_terminated = true;
}
// Say we're going away due to SIGTERM
void saydie()
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, "Caught signal, shutting down.");
}
// set things up, and spawn the threads for data gathering
void run_netmrg()
{
init_logging();
debuglogger(DEBUG_GLOBAL, LEVEL_NOTICE, NULL, "NetMRG starting.");
pid_t mypid = getpid();
FILE *lockfile;
int lockfile_res;
// check for existing lockfile
if (file_exists(get_setting(setPathLockFile)))
{
lockfile = fopen(get_setting(setPathLockFile).c_str(), "r");
pid_t otherpid;
lockfile_res = fscanf(lockfile, "%d", &otherpid);
fclose(lockfile);
// if we could have sent the signal, or if the problem wasn't finding the PID, die.
if ( (kill(otherpid, 0) != -1) || (errno != ESRCH) )
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL,
"Critical: Another instance of NetMRG appears to be running. (PID " + inttostr(otherpid) + ")");
exit(254);
}
else
{
debuglogger(DEBUG_GLOBAL, LEVEL_NOTICE, NULL, "Removing stale lock file.");
remove_lockfile();
}
}
// create lockfile
debuglogger(DEBUG_GLOBAL, LEVEL_INFO, NULL, "Creating Lockfile.");
if ((lockfile = fopen(get_setting(setPathLockFile).c_str(),"w+")) != NULL)
{
fprintf(lockfile, "%d", mypid);
fclose(lockfile);
atexit(remove_lockfile);
}
else
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, string("Critical: Lockfile creation failure. (") + strerror(errno) + ")");
exit(2);
}
// SNMP library initialization
snmp_init();
atexit(snmp_cleanup);
// RRDTOOL command pipe setup
rrd_init();
atexit(rrd_cleanup);
// Setup SIGTERM/SIGINT catching
struct sigaction term_sigaction;
term_sigaction.sa_handler = &handle_sigterm;
if (sigaction(SIGTERM, &term_sigaction, NULL))
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, "Failed to add signal handler.");
exit(10);
}
if (sigaction(SIGINT, &term_sigaction, NULL))
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, "Failed to add signal handler.");
exit(10);
}
do
{
time_t start_time = time(NULL);
// open mysql connection for initial queries
MYSQL mysql;
MYSQL_RES *mysql_res;
MYSQL_ROW mysql_row;
if (!db_connect(&mysql))
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, "Critical: Master database connection failed.");
exit(3);
}
// verify the database version matches the gatherer version
mysql_res = db_query(&mysql, NULL, "SELECT version FROM versioninfo WHERE module = 'Main'");
mysql_row = mysql_fetch_row(mysql_res);
if (string(mysql_row[0]) != string(NETMRG_VERSION))
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, string("Critical: Database version (") + mysql_row[0] + ") and gatherer version (" + NETMRG_VERSION + ") do not match.");
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, "Log into the web interface to perform the database upgrade.");
exit(4);
}
mysql_free_result(mysql_res);
// request list of devices to process
mysql_res = db_query(&mysql, NULL, "SELECT id FROM devices WHERE disabled=0 ORDER BY id");
long int num_rows = mysql_num_rows(mysql_res);
pthread_t* threads = new pthread_t[num_rows];
int* ids = new int[num_rows];
// reading settings isn't necessarily efficient. storing them locally.
int THREAD_COUNT = get_setting_int(setThreadCount);
int dev_counter = 0;
// deploy more threads as needed
int last_active_threads = 0;
netmrg_mutex_lock(lkActiveThreads);
while (dev_counter < num_rows)
{
debuglogger(DEBUG_THREAD, LEVEL_INFO, NULL, "[ACTIVE] Last: " +
inttostr(last_active_threads) + ", Now: " +
inttostr(active_threads));
last_active_threads = active_threads;
while ((active_threads < THREAD_COUNT) && (dev_counter < num_rows))
{
mysql_row = mysql_fetch_row(mysql_res);
int dev_id = strtoint(string(mysql_row[0]));
ids[dev_counter] = dev_id;
pthread_create(&threads[dev_counter], NULL, child, &ids[dev_counter]);
pthread_detach(threads[dev_counter]);
dev_counter++;
active_threads++;
}
netmrg_cond_wait(cActiveThreads, lkActiveThreads);
if (netmrg_terminated)
{
saydie();
break;
}
}
// wait until all threads exit
while (active_threads != 0)
{
netmrg_cond_wait(cActiveThreads, lkActiveThreads);
debuglogger(DEBUG_THREAD, LEVEL_INFO, NULL, "[PASSIVE] Last: " +
inttostr(last_active_threads) + ", Now: " +
inttostr(active_threads));
last_active_threads = active_threads;
}
netmrg_mutex_unlock(lkActiveThreads);
// free active devices results
mysql_free_result(mysql_res);
delete [] threads;
delete [] ids;
// clean up mysql
mysql_close(&mysql);
debuglogger(DEBUG_GLOBAL, LEVEL_INFO, NULL, "Closed MySQL connection.");
// determine runtime and store it
long int run_time = time( NULL ) - start_time;
debuglogger(DEBUG_GLOBAL, LEVEL_INFO, NULL, "Runtime: " + inttostr(run_time));
FILE *runtime;
if ((runtime = fopen(get_setting(setPathRuntimeFile).c_str(),"w+")))
{
fprintf(runtime, "%ld", run_time);
fclose(runtime);
}
else
{
debuglogger(DEBUG_GLOBAL, LEVEL_ERROR, NULL, "Failed to open runtime file for writing.");
}
if ((schedule == schWait) && (!netmrg_terminated))
{
if ( time(NULL) > (start_time + get_setting_int(setPollInterval)))
{
debuglogger(DEBUG_GLOBAL, LEVEL_ERROR, NULL, "We're running behind!");
}
else
{
timespec tosleep, unslept;
tosleep.tv_sec = start_time + get_setting_int(setPollInterval) - time(NULL);
tosleep.tv_nsec = 0;
while (nanosleep(&tosleep, &unslept))
{
if ((errno == EINTR) && (netmrg_terminated))
{
saydie();
break;
}
tosleep.tv_sec = unslept.tv_sec;
}
}
}
}
while ((schedule != schOnce) && (!netmrg_terminated));
}
void show_version()
{
printf("\nNetMRG Data Gatherer\n");
printf("Version %s\n\n", NETMRG_VERSION);
}
void show_usage()
{
show_version();
printf("General:\n");
printf("-v Display Version\n");
printf("-h Show usage (you are here)\n");
printf("-C <file> Use alternate configuration file <file>\n");
printf("-t <num> Limits number of simultaneous threads to <num>\n");
printf("-X Become a daemon.\n");
printf("-M <mode> Scheduling mode, <mode> is:\n");
printf(" once = return after one gather cycle (default).\n");
printf(" wait = gather once every interval, waiting between intervals.\n");
printf("\nMode of Operation:\n");
printf("-i <devid> Recache the interfaces of device <devid>\n");
printf("-d <devid> Recache the disks of device <devid>\n");
printf("-P <devid> Recache the properties of device <devid>\n");
printf("-K <file> Parse config file <file>, or default if omitted (for syntax checking)\n");
printf("If no mode is specified, the default is to gather data for all enabled devices.\n");
printf("\nLogging:\n");
printf("-S Syslog; output logs to syslog instead of stdout.\n");
printf("-b Bland; disable color output.\n");
printf("-a All; display all debug messages.\n");
printf("-m Most; display more than the default.\n");
printf("-q Quiet; display no debug messages.\n");
printf("-c <cm> Use debug component mask <cm>.\n");
printf("-l <lm> Use debug level mask <lm>.\n");
printf("-s Safe; omit potentially sensitive information.\n");
printf("\nDatabase Settings:\n");
printf("-H <host> Use database server on <host>\n");
printf("-D <db> Use database named <db>\n");
printf("-u <user> Use database user name <user>\n");
printf("-p <pass> Use database password <pass>, will prompt for password if <pass> is omitted\n");
printf("\n");
}
void external_snmp_recache(int device_id, int type)
{
MYSQL mysql;
MYSQL_RES *mysql_res;
MYSQL_ROW mysql_row;
DeviceInfo info;
if (!db_connect(&mysql))
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, NULL, "Critical: Master database connection failed.");
exit(3);
}
info.device_id = device_id;
mysql_res = db_query(&mysql, &info, string("SELECT ip, snmp_read_community, snmp_version, snmp_port, ") +
"snmp_timeout, snmp_retries, dev_type FROM devices WHERE id=" + inttostr(device_id));
mysql_row = mysql_fetch_row(mysql_res);
if (mysql_row == NULL)
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, &info, "Device does not exist.");
exit(1);
}
info.snmp_version = strtoint(mysql_row[2]);
if (info.snmp_version == 0)
{
debuglogger(DEBUG_GLOBAL, LEVEL_CRITICAL, &info, "Can't recache a device without SNMP.");
exit(1);
}
info.ip = mysql_row[0];
info.snmp_read_community = mysql_row[1];
info.snmp_port = strtoint(mysql_row[3]);
info.snmp_timeout = strtoint(mysql_row[4]);
info.snmp_retries = strtoint(mysql_row[5]);
info.device_type = strtoint(mysql_row[6]);
info.parameters.push_front(ValuePair("ip", info.ip));
info.parameters.push_front(ValuePair("snmp_read_community", info.snmp_read_community));
mysql_free_result(mysql_res);
snmp_init();
snmp_session_init(info);
switch (type)
{
case 1: do_snmp_interface_recache(&info, &mysql); break;
case 2: do_snmp_disk_recache(&info, &mysql); break;
case 3: do_properties_recache(info, &mysql); break;
}
snmp_session_cleanup(info);
snmp_cleanup();
mysql_close(&mysql);
}
// daemonize - make us lurk around the system
void daemonize()
{
pid_t pid;
int chdir_res;
pid = fork();
if (pid < 0)
{
// failed to fork, keep going with this process
fprintf(stderr, "Failed to fork; unable to daemonize.\n");
fprintf(stderr, "Continuing in the foreground.\n");
return;
}
else if (pid != 0)
{
// fork successful, and we're the parent, time to die.
exit(0);
}
else
{
// we're the child, keep going
setsid();
chdir_res = chdir("/");
umask(0);
return;
}
}
// main - the body of the program
int main(int argc, char **argv)
{
int option_char;
load_settings_default();
load_settings_file(DEF_CONFIG_FILE);
string temppass;
if (vt100_compatible())
{
set_log_method(LOG_METHOD_VT100);
}
while ((option_char = getopt(argc, argv, "hvXSqasmbM:i:d:P:c:l:H:D:u:p::t:C:K::")) != EOF)
switch (option_char)
{
case 'h': show_usage();
exit(0);
break;
case 'v': show_version();
exit(0);
break;
case 'M': if (strcmp(optarg, "once") == 0)
schedule = schOnce;
else if (strcmp(optarg, "wait") == 0)
schedule = schWait;
else
fprintf(stderr, "I don't know what schedule '%s' is. Using default.\n", optarg);
break;
case 'i': external_snmp_recache(strtoint(optarg), 1);
exit(0);
break;
case 'X': daemonize();
break;
case 'S': set_log_method(LOG_METHOD_SYSLOG);
break;
case 'b': set_log_method(LOG_METHOD_STDOUT);
break;
case 'd': external_snmp_recache(strtoint(optarg), 2);
exit(0);
break;
case 'P': external_snmp_recache(strtoint(optarg), 3);
exit(0);
break;
case 'c': set_debug_components(strtoint(optarg));
break;
case 'l': set_debug_level(strtoint(optarg));
break;
case 'q': set_debug_level(0);
break;
case 'a': set_debug_level(LEVEL_ALL);
set_debug_components(DEBUG_ALL);
break;
case 'm': set_debug_level(LEVEL_MOST);
set_debug_components(DEBUG_MOST);
break;
case 's': set_debug_safety(true);
break;
case 'H': set_setting(setDBHost, optarg);
break;
case 'D': set_setting(setDBDB, optarg);
break;
case 'u': set_setting(setDBUser, optarg);
break;
case 'p': if (optarg != NULL)
{
// if password specified, use it
temppass = string(optarg);
// obscure password from process listing
while (*optarg) *optarg++= 'x';
}
else
{
/* Make sure stdin is a terminal */
if (!isatty(STDIN_FILENO))
{
fprintf(stderr, "Not bound to a terminal. Using empty string for password.\n");
temppass = "";
}
else
{
// Save terminal settings
struct termios saved_tattr;
tcgetattr (STDIN_FILENO, &saved_tattr);
// don't echo input
struct termios tattr;
tcgetattr (STDIN_FILENO, &tattr);
tattr.c_lflag &= (ICANON|ECHONL|ISIG);
tattr.c_lflag &= -ECHO;
tcsetattr (STDIN_FILENO, TCSANOW, &tattr);
// if password not specified, prompt for it
cout << "Password: ";
cin >> temppass;
// Restore terminal settings
tcsetattr (STDIN_FILENO, TCSANOW, &saved_tattr);
}
}
set_setting(setDBPass, temppass);
break;
case 't': set_setting(setThreadCount, optarg);
break;
case 'C': load_settings_file(optarg);
break;
case 'K': set_debug_level(LEVEL_DEBUG);
set_debug_components(DEBUG_GLOBAL);
if (optarg != NULL)
{
load_settings_file(optarg);
}
print_settings();
exit(0);
break;
}
run_netmrg();
}
|