#! /usr/bin/env python3 """ poller-wrapper A small tool which wraps around the poller and tries to guide the polling process with a more modern approach with a Queue and workers Authors: Job Snijders Orsiris de Jong Date: Oct 2019 Usage: This program accepts one command line argument: the number of threads that should run simultaneously. If no argument is given it will assume a default of 16 threads. Ubuntu Linux: apt-get install python-mysqldb FreeBSD: cd /usr/ports/*/py-MySQLdb && make install clean RHEL 7: yum install MySQL-python RHEL 8: dnf install mariadb-connector-c-devel gcc && python -m pip install mysqlclient Tested on: Python 3.6.8 / PHP 7.2.11 / CentOS 8.0 License: To the extent possible under law, Job Snijders has waived all copyright and related or neighboring rights to this script. This script has been put into the Public Domain. This work is published from: The Netherlands. """ import LibreNMS.library as LNMS try: import json import os import queue import subprocess import sys import threading import time from optparse import OptionParser except ImportError as exc: print('ERROR: missing one or more of the following python modules:') print('threading, queue, sys, subprocess, time, os, json') print('ERROR: %s' % exc) sys.exit(2) APP_NAME = "poller_wrapper" LOG_FILE = "logs/" + APP_NAME + ".log" _DEBUG = False distpoll = False real_duration = 0 polled_devices = 0 """ Threading helper functions """ # (c) 2015, GPLv3, Daniel Preussker << << <<> /dev/null" command = "/usr/bin/env php %s -h %s %s 2>&1" % (poller_path, device_id, output) # TODO: replace with command_runner subprocess.check_call(command, shell=True) elapsed_time = int(time.time() - start_time) print_queue.put([threading.current_thread().name, device_id, elapsed_time]) except (KeyboardInterrupt, SystemExit): raise except: pass poll_queue.task_done() if __name__ == '__main__': logger = LNMS.logger_get_logger(LOG_FILE, debug=_DEBUG) install_dir = os.path.dirname(os.path.realpath(__file__)) LNMS.check_for_file(install_dir + '/config.php') config = json.loads(LNMS.get_config_data(install_dir)) poller_path = config['install_dir'] + '/poller.php' log_dir = config['log_dir'] if 'rrd' in config and 'step' in config['rrd']: step = config['rrd']['step'] else: step = 300 # (c) 2015, GPLv3, Daniel Preussker << << << << 0: try: time.sleep(1) nodes = memc.get(nodes_tag) except: pass print("Clearing Locks for %s" % time_tag) x = minlocks while x <= maxlocks: res = memc.delete('poller.device.%s.%s' % (x, time_tag)) x += 1 print("%s Locks Cleared" % x) print("Clearing Nodes") memc.delete(master_tag) memc.delete(nodes_tag) else: memc.decr(nodes_tag) print("Finished %.3fs after interval start." % (time.time() - int(time_tag))) # EOC6 show_stopper = False db = LNMS.db_open(config['db_socket'], config['db_host'], config['db_port'], config['db_user'], config['db_pass'], config['db_name']) cursor = db.cursor() query = "update pollers set last_polled=NOW(), devices='%d', time_taken='%d' where poller_name='%s'" % ( polled_devices, total_time, config['distributed_poller_name']) response = cursor.execute(query) if response == 1: db.commit() else: query = "insert into pollers set poller_name='%s', last_polled=NOW(), devices='%d', time_taken='%d'" % ( config['distributed_poller_name'], polled_devices, total_time) cursor.execute(query) db.commit() db.close() if total_time > step: print( "WARNING: the process took more than %s seconds to finish, you need faster hardware or more threads" % step) 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] > step: 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 %s seconds, the script cannot recommend you what to do." % step) else: recommend = int(total_time / step * amount_of_workers + 1) print( "WARNING: Consider setting a minimum of %d threads. (This does not constitute professional advice!)" % recommend) sys.exit(2)