Skip to content

Commit

Permalink
fixing spacing, clean up, adding verify_config
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Sterling committed Oct 3, 2014
1 parent 965dc3e commit 37aeecd
Showing 1 changed file with 43 additions and 91 deletions.
134 changes: 43 additions & 91 deletions graphios.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,65 +36,38 @@
from ConfigParser import SafeConfigParser
from optparse import OptionParser
import copy
import graphios_backends as backends
import logging
import logging.handlers
import os
import os.path
import re
import sys
import time
import graphios_backends as backends


############################################################
# #### You will likely need to change some of the below #####
##### Do not edit this file, edit the graphios.cfg #####

# nagios spool directory
spool_directory = '/var/spool/nagios/graphios'
#
# # graphios log info

# graphios log info
log_file = ''
log_max_size = 25165824 # 24 MB
# # log_level = logging.INFO
# log_level = logging.DEBUG # DEBUG is quite verbose
#
# # How long to sleep between processing the spool directory
# sleep_time = 15
#
# # when we can't connect to carbon, the sleeptime is doubled until we hit max
# sleep_max = 480
#
# # keep a replayable archive log of processed metrics
# metric_archive = '/usr/local/nagios/var/graphios_metric_archive.log'
#
# # test mode makes it so we print what we would add to carbon, and not delete
# # any files from the spool directory. log_level must be DEBUG as well.
# test_mode = False
#
# # Character to use as replacement for invalid characters in metric names
# replacement_character = '_'
#
# # use service description as part of your carbon metric
# # $GRAPHIOSPREFIX.$HOSTNAME.$SERVICEDESC.$GRAPHIOSPOSTFIX.$PERFDATA
# use_service_desc = False
#
# config file stuff

#-------------------------------------------------

# by default we will check the current path for graphios.cfg, if config_file
# is set, we will use that instead.

# is passed as a command line argument we will use that instead.
config_file = ''

# change me to False before releasing
debug = True

# config dictionary
cfg = {}
be = ""
# default config values (these will be over-ridden with the config

# #### You should stop changing things unless you know what you are doing #####
##############################################################################
# backend global
be = ""

# options parsing
parser = OptionParser("""usage: %prog [options]
Expand All @@ -116,14 +89,6 @@

log = logging.getLogger('log')

# # import the backend plug-ins
# bfiles = [fname[:-3] for fname in os.listdir(bdir) if fname.endswith(".py")]

# if bdir not in sys.path:
# sys.path.insert(1, bdir)

# backends = [__import__(fname) for fname in bfiles]


class GraphiosMetric(object):
def __init__(self):
Expand All @@ -146,6 +111,12 @@ def __init__(self):
self.VALID = False # if this metric is valid

def validate(self):
# because we eliminated all whitespace, there shouldn't be any quotes
# this happens more with windows nagios plugins
re.sub("'", "", self.LABEL)
re.sub('"', "", self.LABEL)
re.sub("'", "", self.VALUE)
re.sub('"', "", self.VALUE)
if (
self.TIMET is not '' and
self.PERFDATA is not '' and
Expand All @@ -170,6 +141,9 @@ def chk_bool(value):


def read_config(config_file):
"""
reads the config file
"""
if config_file == '':
config_file = "%s/graphios.cfg" % sys.path[0]
config = SafeConfigParser()
Expand Down Expand Up @@ -197,7 +171,7 @@ def read_config(config_file):

def verify_config(config_dict):
"""
will verify the needed variables are found
verifies the required config variables are found
"""
global spool_directory
ensure_list = ['replacement_character', 'log_file', 'log_max_size',
Expand All @@ -223,34 +197,32 @@ def print_debug(msg):
print msg


def verify_opts():
def verify_options(opts):
"""
verify needed config options are there
verify the passed command line options, puts into global cfg
"""
# refactoring configure
pass
global cfg
global spool_directory
# because these have defaults in the parser section we know they will be
# set. So we don't have to do a bunch of ifs.
if cfg["log_file"] == "''":
cfg["log_file"] = "%s/graphios.log" % sys.path[0]
else:
cfg["log_file"] = opts.log_file
cfg["log_file"] = opts.log_file
cfg["log_max_size"] = 25165824 # 24 MB
if opts.verbose:
cfg["debug"] = True
cfg["spool_directory"] = opts.spool_directory
spool_directory = opts.spool_directory
cfg["backend"] = opts.backend


def configure(opts=''):
def configure():
"""
sets up graphios config
"""
global cfg
global debug
global spool_directory
if opts != '':
cfg["log_file"] = opts.log_file
cfg["log_max_size"] = 25165824 # 24 MB
if opts.verbose:
cfg["debug"] = True
if opts.spool_directory:
cfg["spool_directory"] = opts.spool_directory
spool_directory = opts.spool_directory
cfg["backend"] = opts.backend

if cfg["log_file"] == "''":
cfg["log_file"] = "%s/graphios.log" % sys.path[0]

log_handler = logging.handlers.RotatingFileHandler(
cfg["log_file"], maxBytes=cfg["log_max_size"], backupCount=4,
#encoding='bz2')
Expand All @@ -276,18 +248,15 @@ def process_log(file_name):
by '::' it looks like:
DATATYPE::HOSTPERFDATA TIMET::1399738074 etc..
"""

processed_objects = [] # the final list of metric objects we'll return
graphite_lines = 0 # count the number of valid lines we process

try:
host_data_file = open(file_name, "r")
file_array = host_data_file.readlines()
host_data_file.close()
except OSError as ex:
except (IOError, OSError) as ex:
log.critical("Can't open file:%s error: %s" % (file_name, ex))
sys.exit(2)

# parse each line into a metric object
for line in file_array:
if not re.search("^DATATYPE::", line):
Expand All @@ -311,7 +280,6 @@ def process_log(file_name):
except:
log.critical("failed to parse metric: %s" % nobj.PERFDATA)
continue

return processed_objects


Expand All @@ -331,7 +299,6 @@ def get_mobj(nag_array):
else:
value = re.sub("\s", "", value)
setattr(mobj, var_name, value)

mobj.validate()
if mobj.VALID is True:
return mobj
Expand Down Expand Up @@ -367,25 +334,20 @@ def process_spool_dir(directory):
processed_dict = {}
all_done = True
file_dir = os.path.join(directory, perfdata_file)

if check_skip_file(perfdata_file, file_dir):
continue

num_files += 1
mobjs = process_log(file_dir)
mobjs_len = len(mobjs)
processed_dict = send_backends(mobjs)

#process the output from the backends and decide the fate of the file
for backend in be["essential_backends"]:
if processed_dict[backend] < mobjs_len:
log.critical("keeping %s, insufficent metrics sent from %s" %
(file_dir, backend))
all_done = False

if all_done is True:
handle_file(file_dir, len(mobjs))

log.info("Processed %s files (%s metrics) in %s" % (num_files,
mobjs_len, directory))

Expand All @@ -406,7 +368,6 @@ def check_skip_file(file_name, file_dir):
# file was 0 bytes
handle_file(file_dir, 0)
return True

return False


Expand All @@ -424,18 +385,15 @@ def init_backends():
be = {} # a top-level global for important backend-related stuff
be["enabled_backends"] = {} # a dict of instantiated backend objects
be["essential_backends"] = [] # a list of backends we actually care about

#PLUGIN WRITERS! register your new backends by adding their obj name here
avail_backends = ("carbon",
"statsd",
"librato",
"stdout",
)

#populate the controller dict from avail + config. this assumes you named
#your backend the same as the config option that enables your backend (eg.
#carbon and enable_carbon)

for backend in avail_backends:
cfg_option = "enable_%s" % (backend)
if cfg_option in cfg and cfg[cfg_option] is True:
Expand All @@ -448,7 +406,6 @@ def init_backends():
else:
be["essential_backends"].append(backend)
# not proud of that slovenly conditional ^^

log.info("Enabled backends: %s" % be["enabled_backends"].keys())


Expand All @@ -457,19 +414,15 @@ def send_backends(metrics):
use the enabled_backends dict to call into the backend send functions
"""
global be

if len(be["enabled_backends"]) < 1:
log.critical("At least one Back-end must be enabled in graphios.cfg")
sys.exit(1)

ret = {} # return a dict of who processed what
processed_lines = 0

for backend in be["enabled_backends"]:
processed_lines = be["enabled_backends"][backend].send(metrics)
#log.debug('%s processed %s metrics' % backend, processed_lines)
ret[backend] = processed_lines

return ret


Expand All @@ -485,18 +438,17 @@ def main():


if __name__ == '__main__':
# global cfg
global cfg
if len(sys.argv) > 1:
(options, args) = parser.parse_args()
if options.config:
cfg = read_config(options.config)
verify_config(cfg)
else:
configure(options)
cfg = verify_options(options)
else:
cfg = read_config(config_file)
verify_config(cfg)
configure()
print cfg
verify_config(cfg)
configure()
print cfg # fix me, comment me out before release
init_backends()
main()

0 comments on commit 37aeecd

Please sign in to comment.