librenms/poller-service.py

322 lines
13 KiB
Python
Raw Normal View History

2015-07-06 14:52:32 +00:00
#! /usr/bin/env python
2015-07-06 14:47:18 +00:00
"""
poller-service A service to wrap SNMP polling. It will poll up to $threads devices at a time, and will not re-poll
2015-07-07 18:33:33 +00:00
devices that have been polled within the last $poll_frequency seconds. It will prioritize devices based
on the last time polled. If resources are sufficient, this service should poll every device every
2015-07-07 17:02:13 +00:00
$poll_frequency seconds, but should gracefully degrade if resources are inefficient, polling devices as
2015-07-16 16:11:45 +00:00
frequently as possible. This service is based on Job Snijders' poller-wrapper.py.
2015-07-06 14:47:18 +00:00
Author: Clint Armstrong <clint@clintarmstrong.net>
Date: July 2015
2015-07-16 16:11:45 +00:00
License: BSD 2-Clause
2015-08-21 11:44:05 +00:00
Copyright (c) 2015, Clint Armstrong
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2015-07-06 14:47:18 +00:00
"""
import json
import os
import subprocess
import sys
import threading
import time
import MySQLdb
2015-07-07 15:20:09 +00:00
import logging
import logging.handlers
2015-07-06 16:00:21 +00:00
from datetime import datetime, timedelta
2015-09-02 15:54:27 +00:00
from collections import namedtuple
2015-07-06 14:47:18 +00:00
2015-07-07 15:20:09 +00:00
log = logging.getLogger('poller-service')
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('poller-service: %(message)s')
2015-07-07 18:33:33 +00:00
handler = logging.handlers.SysLogHandler(address='/dev/log')
2015-07-07 15:20:09 +00:00
handler.setFormatter(formatter)
log.addHandler(handler)
2015-07-06 14:47:18 +00:00
install_dir = os.path.dirname(os.path.realpath(__file__))
2015-07-06 14:52:59 +00:00
config_file = install_dir + '/config.php'
2015-07-06 14:47:18 +00:00
2015-07-07 15:40:29 +00:00
log.info('INFO: Starting poller-service')
2015-07-06 19:10:48 +00:00
2015-07-07 18:33:33 +00:00
class DB:
conn = None
def connect(self):
while True:
try:
if db_port == 0:
self.conn = MySQLdb.connect(host=db_server, user=db_username, passwd=db_password, db=db_dbname)
else:
self.conn = MySQLdb.connect(host=db_server, port=db_port, user=db_username, passwd=db_password, db=db_dbname)
break
except (AttributeError, MySQLdb.OperationalError):
2015-09-09 13:20:03 +00:00
log.warning('WARNING: MySQL Error, reconnecting.')
time.sleep(.5)
self.conn.autocommit(True)
2015-09-09 12:25:00 +00:00
self.conn.ping(True)
def query(self, sql):
2015-09-09 13:20:03 +00:00
while True:
try:
cursor = self.conn.cursor()
cursor.execute(sql)
ret = cursor.fetchall()
cursor.close()
return ret
except (AttributeError, MySQLdb.OperationalError):
log.warning('WARNING: MySQL Operational Error during query, reconnecting.')
self.connect()
except (AttributeError, MySQLdb.ProgrammingError):
log.warning('WARNING: MySQL Programming Error during query, attempting query again.')
2015-09-09 13:26:00 +00:00
cursor.close()
2015-07-06 14:47:18 +00:00
def get_config_data():
2015-07-06 14:52:59 +00:00
config_cmd = ['/usr/bin/env', 'php', '%s/config_to_json.php' % install_dir]
2015-07-06 14:47:18 +00:00
try:
proc = subprocess.Popen(config_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
except:
2015-07-07 15:20:09 +00:00
log.critical("ERROR: Could not execute: %s" % config_cmd)
2015-07-06 14:47:18 +00:00
sys.exit(2)
2015-07-19 01:09:49 +00:00
return proc.communicate()[0].decode()
2015-07-06 14:47:18 +00:00
try:
with open(config_file) as f:
pass
except IOError as e:
2015-07-07 15:20:09 +00:00
log.critical("ERROR: Oh dear... %s does not seem readable" % config_file)
2015-07-06 14:47:18 +00:00
sys.exit(2)
try:
config = json.loads(get_config_data())
except:
2015-07-07 15:20:09 +00:00
log.critical("ERROR: Could not load or parse configuration, are PATHs correct?")
2015-07-06 14:47:18 +00:00
sys.exit(2)
2015-07-07 15:29:04 +00:00
try:
2015-07-19 01:38:27 +00:00
loglevel = int(config['poller_service_loglevel'])
2015-07-07 15:29:04 +00:00
except KeyError:
2015-07-19 01:31:01 +00:00
loglevel = 20
2015-07-19 01:38:27 +00:00
except ValueError:
loglevel = logging.getLevelName(config['poller_service_loglevel'])
try:
log.setLevel(loglevel)
except ValueError:
2015-07-19 01:31:01 +00:00
log.warning('ERROR: {0} is not a valid log level. If using python 3.4.0-3.4.1 you must specify loglevel by number'.format(str(loglevel)))
2015-07-19 01:38:27 +00:00
log.setLevel(20)
2015-07-07 15:29:04 +00:00
2015-07-06 14:47:18 +00:00
poller_path = config['install_dir'] + '/poller.php'
2015-07-07 17:02:13 +00:00
discover_path = config['install_dir'] + '/discovery.php'
2015-07-06 14:47:18 +00:00
db_username = config['db_user']
db_password = config['db_pass']
if config['db_host'][:5].lower() == 'unix:':
2015-07-06 19:10:48 +00:00
db_server = config['db_host']
db_port = 0
2015-07-06 14:47:18 +00:00
elif ':' in config['db_host']:
2015-07-06 19:10:48 +00:00
db_server = config['db_host'].rsplit(':')[0]
db_port = int(config['db_host'].rsplit(':')[1])
2015-07-06 14:47:18 +00:00
else:
2015-07-06 19:10:48 +00:00
db_server = config['db_host']
db_port = 0
2015-07-06 14:47:18 +00:00
db_dbname = config['db_name']
2015-07-06 16:00:21 +00:00
2015-07-06 14:47:18 +00:00
try:
2015-07-07 15:40:29 +00:00
amount_of_workers = int(config['poller_service_workers'])
2015-07-06 14:47:18 +00:00
if amount_of_workers == 0:
2015-07-16 15:55:04 +00:00
amount_of_workers = 16
2015-07-07 15:40:29 +00:00
except KeyError:
2015-07-06 14:47:18 +00:00
amount_of_workers = 16
try:
2015-07-07 17:02:13 +00:00
poll_frequency = int(config['poller_service_poll_frequency'])
if poll_frequency == 0:
2015-07-16 15:55:04 +00:00
poll_frequency = 300
2015-07-07 15:40:29 +00:00
except KeyError:
2015-07-07 17:02:13 +00:00
poll_frequency = 300
try:
discover_frequency = int(config['poller_service_discover_frequency'])
2015-07-07 17:10:36 +00:00
if discover_frequency == 0:
2015-07-16 15:55:04 +00:00
discover_frequency = 21600
2015-07-07 17:02:13 +00:00
except KeyError:
2015-07-07 17:10:36 +00:00
discover_frequency = 21600
2015-07-06 14:47:18 +00:00
2015-07-06 18:51:03 +00:00
try:
2015-07-07 15:40:29 +00:00
down_retry = int(config['poller_service_down_retry'])
2015-07-06 18:51:03 +00:00
if down_retry == 0:
2015-07-16 15:55:04 +00:00
down_retry = 60
2015-07-07 15:40:29 +00:00
except KeyError:
2015-07-07 17:33:16 +00:00
down_retry = 60
2015-07-06 18:51:03 +00:00
db = DB()
2015-07-06 19:10:48 +00:00
def lockFree(lock, db=db):
2015-07-19 00:04:44 +00:00
query = "SELECT IS_FREE_LOCK('{0}')".format(lock)
2015-09-09 13:22:31 +00:00
return db.query(query)[0][0] == 1
2015-07-06 16:00:21 +00:00
2015-07-06 19:10:48 +00:00
def getLock(lock, db=db):
2015-07-19 00:04:44 +00:00
query = "SELECT GET_LOCK('{0}', 0)".format(lock)
2015-09-09 13:22:31 +00:00
return db.query(query)[0][0] == 1
2015-07-06 18:51:03 +00:00
2015-07-06 19:10:48 +00:00
def releaseLock(lock, db=db):
2015-07-19 00:04:44 +00:00
query = "SELECT RELEASE_LOCK('{0}')".format(lock)
cursor = db.query(query)
2015-09-09 13:22:31 +00:00
return db.query(query)[0][0] == 1
2015-07-06 18:51:03 +00:00
2015-07-07 18:33:33 +00:00
def sleep_until(timestamp):
now = datetime.now()
if timestamp > now:
sleeptime = (timestamp - now).seconds
else:
sleeptime = 0
time.sleep(sleeptime)
2015-07-19 00:04:44 +00:00
poller_group = ('and poller_group IN({0}) '
2015-07-07 18:33:33 +00:00
.format(str(config['distributed_poller_group'])) if 'distributed_poller_group' in config else '')
# Add last_polled and last_polled_timetaken so we can sort by the time the last poll started, with the goal
# of having each device complete a poll within the given time range.
2015-07-13 18:32:00 +00:00
dev_query = ('SELECT device_id, status, '
'CAST( '
' DATE_ADD( '
' DATE_SUB( '
' last_polled, '
' INTERVAL last_polled_timetaken SECOND '
' ), '
' INTERVAL {0} SECOND) '
' AS DATETIME '
') AS next_poll, '
'CAST( '
' DATE_ADD( '
' DATE_SUB( '
' last_discovered, '
' INTERVAL last_discovered_timetaken SECOND '
' ), '
' INTERVAL {1} SECOND) '
' AS DATETIME '
') as next_discovery '
2015-07-13 14:15:06 +00:00
'FROM devices WHERE '
'disabled = 0 '
2015-09-02 16:56:59 +00:00
'AND IS_FREE_LOCK(CONCAT("poll.", device_id)) '
'AND IS_FREE_LOCK(CONCAT("discovery.", device_id)) '
2015-09-02 18:22:42 +00:00
'AND IS_FREE_LOCK(CONCAT("queue.", device_id)) '
2015-07-13 14:15:06 +00:00
'AND ( last_poll_attempted < DATE_SUB(NOW(), INTERVAL {2} SECOND ) '
' OR last_poll_attempted IS NULL ) '
'{3} '
2015-07-17 17:33:24 +00:00
'ORDER BY next_poll asc '
2015-09-09 18:47:32 +00:00
'LIMIT 1 ').format(poll_frequency,
2015-07-13 14:15:06 +00:00
discover_frequency,
down_retry,
poller_group)
2015-07-13 14:59:26 +00:00
next_update = datetime.now() + timedelta(minutes=1)
2015-07-07 15:29:04 +00:00
devices_scanned = 0
2015-07-06 18:51:03 +00:00
2015-07-07 15:20:09 +00:00
2015-09-09 18:58:39 +00:00
def poll_worker(thread_id):
2015-09-09 18:47:32 +00:00
global dev_query
global devices_scanned
db = DB()
while True:
2015-09-09 19:01:27 +00:00
dev_row = db.query(dev_query)
if len(dev_row) < 1:
# Sleep 1 second after getting an empty query, don't hammer the sql server for no reason.
time.sleep(1)
2015-09-09 19:01:27 +00:00
continue
device_id, status, next_poll, next_discovery = dev_row[0]
2015-07-13 15:10:14 +00:00
2015-09-09 18:47:32 +00:00
if not getLock('queue.{0}'.format(device_id), db):
2015-09-09 19:09:19 +00:00
releaseLock('queue.{0}'.format(device_id), db)
2015-09-02 18:22:42 +00:00
continue
2015-07-07 17:10:36 +00:00
if next_poll and next_poll > datetime.now():
2015-09-09 18:58:39 +00:00
log.debug('DEBUG: Thread {0} Sleeping until {1} before polling {2}'.format(thread_id, next_poll, device_id))
sleep_until(next_poll)
2015-07-07 17:02:13 +00:00
action = 'poll'
2015-07-13 18:32:00 +00:00
if (not next_discovery or next_discovery < datetime.now()) and status == 1:
2015-07-07 17:02:13 +00:00
action = 'discovery'
2015-09-09 18:58:39 +00:00
log.debug('DEBUG: Thread {0} Starting {1} of device {2}'.format(thread_id, action, device_id))
2015-07-07 15:20:09 +00:00
devices_scanned += 1
2015-09-09 13:22:31 +00:00
db.query('UPDATE devices SET last_poll_attempted = NOW() WHERE device_id = {0}'.format(device_id))
2015-09-09 18:47:32 +00:00
if not getLock('{0}.{1}'.format(action, device_id), db):
releaseLock('{0}.{1}'.format(action, device_id), db)
2015-09-09 19:09:19 +00:00
releaseLock('queue.{0}'.format(device_id), db)
2015-09-02 18:22:42 +00:00
continue
2015-09-09 19:09:19 +00:00
releaseLock('queue.{0}'.format(device_id), db)
2015-09-09 18:47:32 +00:00
try:
start_time = time.time()
path = poller_path
if action == 'discovery':
path = discover_path
command = "/usr/bin/env php %s -h %s >> /dev/null 2>&1" % (path, device_id)
subprocess.check_call(command, shell=True)
elapsed_time = int(time.time() - start_time)
if elapsed_time < 300:
2015-09-09 18:58:39 +00:00
log.debug("DEBUG: Thread {0} finished {1} of device {2} in {3} seconds".format(thread_id, action, device_id, elapsed_time))
2015-09-09 18:47:32 +00:00
else:
2015-09-09 18:58:39 +00:00
log.warning("WARNING: Thread {0} finished {1} of device {2} in {3} seconds".format(thread_id, action, device_id, elapsed_time))
2015-09-09 18:47:32 +00:00
except (KeyboardInterrupt, SystemExit):
raise
except:
pass
finally:
releaseLock('{0}.{1}'.format(action, device_id), db)
2015-07-06 18:51:03 +00:00
2015-09-09 18:47:32 +00:00
t = []
for i in range(0, amount_of_workers):
2015-09-09 18:58:39 +00:00
t.append(threading.Thread(target=poll_worker, args=[i]))
2015-09-09 19:07:55 +00:00
t[i].daemon = True
2015-09-09 18:47:32 +00:00
t[i].start()
2015-07-13 13:48:18 +00:00
2015-07-13 14:06:57 +00:00
2015-09-09 18:47:32 +00:00
while True:
sleep_until(next_update)
seconds_taken = (datetime.now() - (next_update - timedelta(minutes=1))).seconds
update_query = ('INSERT INTO pollers(poller_name, '
' last_polled, '
' devices, '
' time_taken) '
' values("{0}", NOW(), "{1}", "{2}") '
'ON DUPLICATE KEY UPDATE '
' last_polled=values(last_polled), '
' devices=values(devices), '
' time_taken=values(time_taken) ').format(config['distributed_poller_name'].strip(),
devices_scanned,
seconds_taken)
try:
db.query(update_query)
except:
log.critical('ERROR: MySQL query error. Is your schema up to date?')
sys.exit(2)
log.info('INFO: {0} devices scanned in the last minute'.format(devices_scanned))
devices_scanned = 0
next_update = datetime.now() + timedelta(minutes=1)