#! /usr/bin/env python2 """ 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 Author: Job Snijders Date: Jan 2013 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 Tested on: Python 2.7.3 / PHP 5.3.10-1ubuntu3.4 / Ubuntu 12.04 LTS 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. """ try: import json import os import Queue import subprocess import sys import threading import time except: print "ERROR: missing one or more of the following python modules:" print "threading, Queue, sys, subprocess, time, os, json" sys.exit(2) try: import MySQLdb except: print "ERROR: missing the mysql python module:" print "On ubuntu: apt-get install python-mysqldb" print "On FreeBSD: cd /usr/ports/*/py-MySQLdb && make install clean" sys.exit(2) """ Fetch configuration details from the config_to_json.php script """ ob_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'] db_port = int(config['db_port']) if config['db_socket']: db_server = config['db_host'] db_socket = config['db_socket'] else: db_server = config['db_host'] db_socket = None db_dbname = config['db_name'] def db_open(): try: if db_socket: db = MySQLdb.connect(host=db_server, unix_socket=db_socket, user=db_username, passwd=db_password, db=db_dbname) else: db = MySQLdb.connect(host=db_server, port=db_port, user=db_username, passwd=db_password, db=db_dbname) return db except: print "ERROR: Could not connect to MySQL database!" sys.exit(2) # (c) 2015, GPLv3, Daniel Preussker << << << << << << 0 and nodes is not None: 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 = db_open() 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)