simplify things a bit. single threaded, handle one host, no config file.

This commit is contained in:
Jeffrey Forman 2013-08-22 21:30:39 -04:00
parent 00b34264b9
commit c599753e87
3 changed files with 20 additions and 94 deletions

View File

@ -3,7 +3,6 @@
import argparse
import logging
import multiprocessing
import pickle
import re
import socket
@ -14,18 +13,21 @@ import time
LOGGING_FORMAT = "%(asctime)s:%(levelname)s:%(message)s"
RE_LEFTRIGHT = re.compile(r"^(?P<left>\S+)\s+(?P<right>\S+)$")
## TODO: Catch keyboard interrupt properly and die when requested
class Munin():
"""Munin host object with querying getter functions."""
def __init__(self, hostname="localhost", port=4949, args=None):
def __init__(self, hostname, port=4949, args=None):
self.hostname = hostname
self.port = port
self.args = args
def go(self):
"""Bootstrap method to start processing hosts's Munin stats."""
self.connect()
self.process_host_stats()
while True:
self.connect()
self.process_host_stats()
time.sleep(self.args.interval)
def connect(self):
"""Initial connection to Munin host."""
@ -166,40 +168,34 @@ def parse_args():
parser.add_argument("--carbon",
action="store",
help="Carbon host and Pickle port (ex: localhost:2004).")
parser.add_argument("--muninhosts",
parser.add_argument("--host",
action="store",
help="Comma separated list of hosts running Munin to query for stats.")
default="localhost",
help="Comma separated list of hosts running Munin to query for stats. Default: %(default)s")
parser.add_argument("--interval",
type=int,
default=60,
help="Interval (seconds) between polling Munin host for statistics. Default: %(default)s")
parser.add_argument("--noop",
action="store_true",
help="Don't actually send Munin data to Carbon.")
parser.add_argument("--poolsize", type=int, default=1,
help="Pool size of simultaneous connections when polling multiple hosts.")
help="Don't actually send Munin data to Carbon. Default: %(default)s")
parser.add_argument("--prefix",
action="store",
default="servers",
help="Prefix used on graphite target's name (default='servers')")
help="Prefix used on graphite target's name. Default: %(default)s")
parser.add_argument("--noprefix",
action="store_true",
default=False,
help="Do not use a prefix on graphite target's name")
parser.add_argument("--verbose",
help="Do not use a prefix on graphite target's name. Default: %(default)s")
parser.add_argument("--verbose","-v",
choices=[1, 2, 3],
default=2,
type=int,
help="Verbosity level. 1:ERROR, 2:INFO/Default, 3:DEBUG.")
help="Verbosity level. 1:ERROR, 2:INFO, 3:DEBUG. Default: %(default)d")
args = parser.parse_args()
return args
def worker_bootstrap(host, args):
"""Handles pool process allocation."""
munin_obj = Munin(hostname=host, args=args)
return munin_obj.go()
def worker_return(retval):
"""Outputs any return values from each pool iteration."""
logging.debug("Iteration Return Value: %s", retval)
def main():
args = parse_args()
if args.verbose == 1:
@ -210,12 +206,8 @@ def main():
LOGGING_LEVEL = logging.INFO
logging.basicConfig(format=LOGGING_FORMAT, level=LOGGING_LEVEL)
pool = multiprocessing.Pool(args.poolsize)
muninhosts = args.muninhosts.split(",")
for muninhost in muninhosts:
pool.apply_async(worker_bootstrap, args = (muninhost, args,), callback = worker_return)
pool.close()
pool.join()
munin = Munin(hostname=args.host, args=args)
munin.go()
if __name__ == '__main__':
main()

View File

@ -1,13 +0,0 @@
[host1.domain1.local]
address: host1.domain1.local
[mta.domain1.local]
graphitename: smtp
[www.domain1.local]
address: server1.domain1.local
[mta2.domain1.local]
graphitename: smtp2
[jabber.domain1.local]

View File

@ -1,53 +0,0 @@
#!/usr/bin/env python
import ConfigParser
import sys
class M2GConfigParser():
def __init__(self, config_path):
self._config_path = config_path
self._config_dict = {} # The entire file parsed into a dictionary.
self._graphite_config = {} # The data we'll use for Munin queries.
self._parsed_config = ConfigParser.SafeConfigParser()
self._parsed_config.read(self._config_path)
def parse_config(self):
"""Parse out all the config's data."""
self.set_config_dict()
self.set_host_list()
def set_config_dict(self):
"""Populate the object's dictionary from the config file."""
sections = self._parsed_config.sections()
for current_section in sections:
options = self._parsed_config.options(current_section)
self._config_dict[current_section] = {}
for current_option in options:
self._config_dict[current_section][current_option] = self._parsed_config.get(current_section, current_option)
def get_config_dict(self):
return self._config_dict
def set_host_list(self):
"""From a parsed config, list the computed Graphite host list."""
for section in self._config_dict:
self._graphite_config[section] = {}
# Set Hostname to query first
if "address" in self._config_dict[section]:
self._graphite_config[section]["address"] = self._config_dict[section]["address"]
else:
self._graphite_config[section]["address"] = section
# Set graphitename to display in Graphite
if "graphitename" in self._config_dict[section]:
self._graphite_config[section]["graphitename"] = self._config_dict[section]["graphitename"]
else:
self._graphite_config[section]["graphitename"] = section.split(".")[0]
def get_graphite_config(self):
return self._graphite_config
if __name__ == "__main__":
myconfig = M2GConfigParser(sys.argv[1])
myconfig.parse_config()
print "Graphite Config: %r" % myconfig.get_graphite_config()