From 9111feee38443b918a0e51dc7f4f096e20947b27 Mon Sep 17 00:00:00 2001 From: Dave Pedu Date: Fri, 4 Dec 2015 22:41:57 -0800 Subject: [PATCH] initial commit - data collection phase --- .gitignore | 2 + examples/config.test.json | 12 ++++ pymonitor/__init__.py | 0 pymonitor/daemon.py | 118 ++++++++++++++++++++++++++++++++ pymonitor/monitors/__init__.py | 0 pymonitor/monitors/diskspace.py | 0 pymonitor/monitors/uptime.py | 6 ++ requirements.txt | 9 +++ 8 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 examples/config.test.json create mode 100644 pymonitor/__init__.py create mode 100755 pymonitor/daemon.py create mode 100644 pymonitor/monitors/__init__.py create mode 100644 pymonitor/monitors/diskspace.py create mode 100644 pymonitor/monitors/uptime.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a295864 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +__pycache__ diff --git a/examples/config.test.json b/examples/config.test.json new file mode 100644 index 0000000..9438606 --- /dev/null +++ b/examples/config.test.json @@ -0,0 +1,12 @@ +{ + "backend": { + "url": "http://192.168.1.210:8297/" + }, + "monitors": [ + { + "type":"uptime", + "freq":"5", + "args":{} + } + ] +} \ No newline at end of file diff --git a/pymonitor/__init__.py b/pymonitor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pymonitor/daemon.py b/pymonitor/daemon.py new file mode 100755 index 0000000..f6a9c32 --- /dev/null +++ b/pymonitor/daemon.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python3 + +from threading import Thread +from time import time,sleep +import logging +import json +import sys +import os + +class MonitorDaemon(Thread): + def __init__(self, config): + Thread.__init__(self) + self.config = config + self.threads = [] + + def run(self): + """ + Start all monitoring threads and block until they exit + """ + logger = logging.getLogger("monitordaemon") + + checkerPath = os.path.dirname(os.path.realpath(__file__))+"/monitors/" + sys.path.append(checkerPath) + logger.info("path %s" % checkerPath) + + # Create/start all monitoring threads + logger.info("starting monitor threads") + for instance in self.config["monitors"]: + monitor_thread = MonitorThread(instance) + monitor_thread.start() + self.threads.append(monitor_thread) + + # Tear down all threads + logger.info("joining monitor threads") + for monitor_thread in self.threads: + monitor_thread.join() + + logger.info("joined monitor threads") + + def shutdown(self): + """ + Signal all monitoring threads to stop + """ + for monitor_thread in self.threads: + monitor_thread.shutdown() + + +class MonitorThread(Thread): + def __init__(self, config): + """ + Load checker function and prepare scheduler + """ + Thread.__init__(self) + self.config = config + self.logger = logging.getLogger("monitordaemon.monitorthread.%s"%self.config["type"]) + self.logger.info("initing worker thread with config %s" % self.config) + + self.logger.info("importing %s" % self.config["type"]) + self.checker_func = getattr(__import__(self.config["type"]), self.config["type"]) + self.logger.info("checker func %s" % self.checker_func) + + self.alive = True + self.delay = int(self.config["freq"]) + self.lastRun = 0 + + def run(self): + """ + Call execute method every x seconds forever + """ + self.logger.info("starting scheduler") + while self.alive: + if time() - self.lastRun > self.delay: + self.lastRun = time() + self.execute(self.config["args"]) + self.logger.info("scheduler exited") + + def execute(self, args): + """ + Run the loaded checker function + """ + result = self.checker_func(**args) + self.logger.info("result: %s" % (result,)) + + def shutdown(self): + """ + Tell thread to exit + """ + self.logger.info("cancelling scheduler") + self.alive=False + +if __name__ == '__main__': + from optparse import OptionParser + logging.basicConfig(level=logging.DEBUG, format="%(asctime)-15s %(levelname)-8s %(name)s@%(filename)s:%(lineno)d %(message)s") + logger = logging.getLogger("init") + + parser = OptionParser() + parser.add_option("-c", "--config", action="store", type="string", dest="config", help="Path to config file") + + (options, args) = parser.parse_args() + logger.debug("options: %s" % options) + + if options.config == None: + parser.print_help() + sys.exit() + + with open(options.config, "r") as c: + conf = json.load(c) + + logger.info("starting daemon with conf: %s" % conf) + + daemon = MonitorDaemon(conf) + try: + daemon.start() + daemon.join() + except KeyboardInterrupt: + print("") + daemon.shutdown() + diff --git a/pymonitor/monitors/__init__.py b/pymonitor/monitors/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pymonitor/monitors/diskspace.py b/pymonitor/monitors/diskspace.py new file mode 100644 index 0000000..e69de29 diff --git a/pymonitor/monitors/uptime.py b/pymonitor/monitors/uptime.py new file mode 100644 index 0000000..e6e360c --- /dev/null +++ b/pymonitor/monitors/uptime.py @@ -0,0 +1,6 @@ +def uptime(): + with open("/proc/uptime", "r") as f: + return {"uptime":int(float(f.read().split(" ")[0]))} + +if __name__ == '__main__': + print(uptime()["uptime"]) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..21165dc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +chardet==2.2.1 +colorama==0.2.5 +elasticsearch==2.1.0 +html5lib==0.999 +requests==2.2.1 +six==1.5.2 +urllib3==1.12 +virtualenv==13.1.2 +wheel==0.24.0