From 7f262fd2cc3fcbd1b8daca8440d19fef493bab35 Mon Sep 17 00:00:00 2001 From: Clint Armstrong Date: Mon, 6 Jul 2015 10:47:18 -0400 Subject: [PATCH] limit sql query to amount_of_workers --- poller-service.py | 202 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 poller-service.py diff --git a/poller-service.py b/poller-service.py new file mode 100644 index 0000000000..291e95534a --- /dev/null +++ b/poller-service.py @@ -0,0 +1,202 @@ +""" + poller-service A service to wrap SNMP polling. It will poll up to $threads devices at a time, and will not re-poll + devices that have been polled within the last $frequency seconds. It will prioritize devices based on + the last time polled. If resources are sufficient, this service should poll every device every + $frequency seconds, but should gracefully degrade if resources are inefficient, polling devices as + frequently as possible. This service is based on poller-wrapper.py. + + Author: Clint Armstrong + Date: July 2015 + + Usage: poller-service [threads] [frequency] + Default is 16 threads and 300 seconds. +""" + +import json +import os +import Queue +import subprocess +import sys +import threading +import time +import MySQLdb + +install_dir = os.path.dirname(os.path.realpath(__file__)) +config_file = ob_install_dir + '/config.php' + +def get_config_data(): + config_cmd = ['/usr/bin/env', 'php', '%s/config_to_json.php' % ob_install_dir] + try: + proc = subprocess.Popen(config_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) + except: + print "ERROR: Could not execute: %s" % config_cmd + sys.exit(2) + return proc.communicate()[0] + +try: + with open(config_file) as f: + pass +except IOError as e: + print "ERROR: Oh dear... %s does not seem readable" % config_file + sys.exit(2) + +try: + config = json.loads(get_config_data()) +except: + print "ERROR: Could not load or parse configuration, are PATHs correct?" + sys.exit(2) + +poller_path = config['install_dir'] + '/poller.php' +db_username = config['db_user'] +db_password = config['db_pass'] + +if config['db_host'][:5].lower() == 'unix:': + db_server = config['db_host'] + db_port = 0 +elif ':' in config['db_host']: + db_server = config['db_host'].rsplit(':')[0] + db_port = int(config['db_host'].rsplit(':')[1]) +else: + db_server = config['db_host'] + db_port =0 + +db_dbname = config['db_name'] + +# (c) 2015, GPLv3, Daniel Preussker << 300: + print "WARNING: the process took more than 5 minutes to finish, you need faster hardware or more threads" + print "INFO: in sequential style polling the elapsed time would have been: %s seconds" % real_duration + for device in per_device_duration: + if per_device_duration[device] > 300: + print "WARNING: device %s is taking too long: %s seconds" % (device, per_device_duration[device]) + show_stopper = True + if show_stopper: + print "ERROR: Some devices are taking more than 300 seconds, the script cannot recommend you what to do." + else: + recommend = int(total_time / 300.0 * amount_of_workers + 1) + print "WARNING: Consider setting a minimum of %d threads. (This does not constitute professional advice!)" % recommend + + sys.exit(2)