Skip to content

Commit

Permalink
make nagios_performance default to true;
Browse files Browse the repository at this point in the history
be more careful handling the input value;
allow Check instances to report configuration errors
(#12)
  • Loading branch information
freddrake committed Apr 30, 2015
1 parent ccc99c2 commit 4c3bfb2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ To do
Changes
*******

- Parse metrics from Nagios plugin output by default. If thresholds are
specified and metric parsing is disabled, an error is generated.

0.5.3 (2015-03-10)
==================

Expand Down
40 changes: 32 additions & 8 deletions src/zc/cimaa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,22 @@ def __init__(self, config):
try:
cparser = zc.cimaa.parser.parse_file(path)
except zc.cimaa.parser.Error:
checks.append(BadCheck(prefix, path))
checks.append(BadCheck(prefix, path,
'error parsing %s' % path))
else:
for section in cparser:
config = dict(cparser[section])
if not section.startswith('//'):
section = prefix + section
checks.append(Check(section, config))
if section.startswith('//'):
fullname = section
else:
fullname = prefix + section
try:
checks.append(Check(fullname, config))
except zc.cimaa.parser.Error as e:
checks.append(BadCheck(
prefix, section,
'error loading check %s [%s]: %s'
% (path, section, e)))

signal.signal(signal.SIGTERM, self.shutdown)

Expand Down Expand Up @@ -224,8 +233,22 @@ def __init__(self, name, config):
if 'thresholds' in config:
self.thresholds = zc.cimaa.threshold.Thresholds(
config['thresholds'])
self.parse_nagios = (
config.get('nagios_performance', '').lower() == 'true')
parse = True
if 'nagios_performance' in config:
parse = config['nagios_performance'].lower()
if parse == 'true':
pass
elif parse == 'false':
if 'thresholds' in config:
raise zc.cimaa.parser.Error(
'nagios_performance can\'t be false'
' when thresholds are specified')
parse = False
else:
raise zc.cimaa.parser.Error(
'bad value for nagios_performance: %r'
% config['nagios_performance'])
self.parse_nagios = parse

def should_run(self, minute):
interval = self.interval
Expand Down Expand Up @@ -391,17 +414,18 @@ class BadCheck(Check):
retry = 0
chances = 1

def __init__(self, name, path):
def __init__(self, name, path, message):
self.name = name
self.path = path
self.message = message

def should_run(self, minute):
return True

def perform(self):
return dict(faults=[
dict(
message="error parsing %s" % self.path,
message=self.message,
severity=logging.CRITICAL,
updated=time.time(),
),
Expand Down
101 changes: 87 additions & 14 deletions src/zc/cimaa/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,24 @@ Nagios plugins can include performance data in their output::
>>> with open('foo.txt', 'w') as f:
... f.write(src)
Normally, performance data is ignored:
Performance data is parsed by default:

>>> agent.perform(0)
2014-12-13T16:14:47.820000 //test.example.com/test/foo.txt#/ 2643.0 MB
2014-12-13T16:14:47.820000 //test.example.com/test/foo.txt#/boot 68.0 MB
2014-12-13T16:14:47.820000 //test.example.com/test/foo.txt#/home 69357.0 MB
2014-12-13T16:14:47.820000 //test.example.com/test/foo.txt#/var/log 818.0 MB
2014-12-13T16:14:47.820000 //test.example.com/test/foo.txt#speed 0.0
2014-12-13T16:14:47.820000 //test.example.com/test/foo.txt#loudness 0.0

>>> print agent.db
{'test.example.com': [{'message': 'Missing metric (2 of 4)',
'name': '//test.example.com/test/foo.txt#speed',
'severity': 40,
'since': 1418487287.82,
'updated': 1418487287.82},
{'message': 'Missing metric (2 of 4)',
'name': '//test.example.com/test/foo.txt#loudness',
'severity': 40,
'since': 1418487287.82,
'updated': 1418487287.82}]}
{'test.example.com': []}

>>> agent.clear()

If we want parsing of performance data, we need to use the
``nagios_performance`` option in the check definition:
::
The configuration setting ``nagios_performance`` controls whether
metrics are parsed from Nagios-style output, and can be specified
explicitly::

[foo.txt]
command = PY filecheck.py foo.txt
Expand Down Expand Up @@ -200,6 +198,81 @@ If we want parsing of performance data, we need to use the

>>> agent.clear()

If thresholds are specified and ``nagios_performance`` is set to false,
a fault is generated when the configuration is loaded::

[foo.txt]
command = PY filecheck.py foo.txt
nagios_performance = FALSE
thresholds =
speed warning > 50 error > 70 critical > 110 clear < 60
loudness warning > 11 error > 20
free ? warning <= 20 error <= 10 critical <= 3

.. -> src
>>> with open(os.path.join('agent.d', 'test.cfg'), 'w') as f:
... f.write(src.replace('PY', sys.executable))
>>> agent = zc.cimaa.agent.Agent('agent.cfg')
>>> agent.perform(0)
OutputAlerter trigger //test.example.com/test/
error loading check agent.d/test.cfg [foo.txt]:
nagios_performance can't be false when thresholds are specified
>>> print agent.db
{'test.example.com':
[{'message': "error loading check agent.d/test.cfg [foo.txt]:
nagios_performance can't be false when thresholds are specified",
'name': '//test.example.com/test/',
'severity': 50,
'since': 1418487287.82,
'triggered': 'y',
'updated': 1418487287.82}]}
Without thresholds, disable metrics parsing is allowed, and can be used
to avoid problems with output from some Nagios-style plugins::

[foo.txt]
command = PY filecheck.py foo.txt
nagios_performance = FALSE

.. -> src
>>> with open(os.path.join('agent.d', 'test.cfg'), 'w') as f:
... f.write(src.replace('PY', sys.executable))
>>> agent = zc.cimaa.agent.Agent('agent.cfg')
>>> agent.perform(0)
>>> print agent.db
{'test.example.com': []}
>>> agent.clear()
Specifying an insane value for ``nagios_performance`` will also generate
a fault::

[foo.txt]
command = PY filecheck.py foo.txt
nagios_performance = yada yada

.. -> src
>>> with open(os.path.join('agent.d', 'test.cfg'), 'w') as f:
... f.write(src.replace('PY', sys.executable))
>>> agent = zc.cimaa.agent.Agent('agent.cfg')
>>> agent.perform(0)
OutputAlerter trigger //test.example.com/test/
error loading check agent.d/test.cfg [foo.txt]:
bad value for nagios_performance: 'yada yada'
>>> print agent.db
{'test.example.com':
[{'message': "error loading check agent.d/test.cfg [foo.txt]:
bad value for nagios_performance: 'yada yada'",
'name': '//test.example.com/test/',
'severity': 50,
'since': 1418487287.82,
'triggered': 'y',
'updated': 1418487287.82}]}
Logging metrics handler
========================

Expand Down

0 comments on commit 4c3bfb2

Please sign in to comment.