Skip to content

Commit

Permalink
Rewrite using pynag.Plugins.simple
Browse files Browse the repository at this point in the history
  • Loading branch information
melmorabity committed Jan 26, 2018
1 parent 86e32b1 commit 63c9502
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Mohamed El Morabity <melmorabity -(at)- fedoraproject.org>

## Usage

check_chrony.py --hostname HOSTNAME --warning WARNING --critical CRITICAL [--port PORT]
check_chrony.py [--hostname HOSTNAME] --warning WARNING --critical CRITICAL [--port PORT]

### Options

Expand All @@ -35,4 +35,4 @@ Offset to result in critical status (in seconds)
## Examples

$ ./check_chrony -H localhost -w 0.5 -c 1
OK - Offset -4.2285e-05 seconds | 'offset'=-0.000042285s;;;;
OK: Offset 0.000590362 seconds | 'offset'=0.000590362s;0.5;1;;
82 changes: 34 additions & 48 deletions check_chrony.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2016, 2017 Mohamed El Morabity <melmorabity@fedoraproject.com>
# Copyright (C) 2016-2018 Mohamed El Morabity <melmorabity@fedoraproject.com>
#
# This module is free software: you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation, either version 3 of the
Expand All @@ -17,74 +17,60 @@

import re
import subprocess
import pynag.Plugins

from pynag import Plugins
from pynag.Plugins import simple as Plugin


DEFAULT_CHRONYD_PORT = 323

helper = pynag.Plugins.PluginHelper()
plugin = Plugin()

# Specify arguments to the plugin
helper.parser.description = 'This plugin checks the clock offset of chronyd.'
helper.parser.add_option('-H', '--hostname', help='Host name or IP address')
helper.parser.add_option('-p', '--port', type='int', default=DEFAULT_CHRONYD_PORT,
help='Chrony UDP port (default: %i)' % DEFAULT_CHRONYD_PORT)
helper.parser.add_option('-w', '--warning', type='float',
help='Offset to result in warning status (in seconds)')
helper.parser.add_option('-c', '--critical', type='float',
help='Offset to result in critical status (in seconds)')
helper.parse_arguments()

if not helper.options.hostname:
helper.parser.error('Hostname is mandatory')
if not helper.options.warning:
helper.parser.error('Warning level is mandatory')
if not helper.options.critical:
helper.parser.error('Critical level is mandatory')
if helper.options.critical <= helper.options.warning:
helper.parser.error('Critical level cannot be lesser than or equal to warning')
plugin.add_arg('p', 'port', 'Chrony UDP port', required=None)

plugin.activate()

if plugin['critical'] <= plugin['warning']:
plugin.parser.error('Critical level cannot be lesser than or equal to warning')

if plugin['host'] is None:
plugin['host'] = 'localhost'

if plugin['port'] is None:
plugin['port'] = DEFAULT_CHRONYD_PORT
else:
try:
plugin['port'] = float(plugin['port'])
if plugin['port'] <= 0:
raise ValueError
except ValueError as ex:
plugin.parser.error('Invalid port number')

# Run chrony tracking
command = ['chronyc', '-h', helper.options.hostname, '-p', str(helper.options.port), 'tracking']
if helper.options.show_debug:
command.append('-d')
command = ['chronyc', '-h', plugin['host'], '-p', str(plugin['port']), 'tracking']

try:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None)
output = process.communicate()[0]
except OSError as e:
helper.status(pynag.Plugins.unknown)
helper.add_summary(str(e))
helper.exit()
except OSError as ex:
plugin.nagios_exit(Plugins.UNKNOWN, str(ex))

if process.returncode != 0:
helper.status(pynag.Plugins.critical)
helper.add_summary(output)
helper.exit()
plugin.nagios_exit(Plugins.CRITICAL, output.rstrip())

matcher = re.search(r'^Leap status\s*:\s*(.*)$', output, flags=re.MULTILINE)
leap_status = matcher.group(1)
if leap_status == 'Not synchronised':
helper.status(pynag.Plugins.critical)
helper.add_summary('Server is not synchronised')
helper.exit()
plugin.nagios_exit(Plugins.CRITICAL, 'Server is not synchronised')
if leap_status == 'Unknown':
helper.status(pynag.Plugins.unknown)
helper.add_summary('Server status is unknown')
helper.exit()
plugin.nagios_exit(Plugins.UNKNOWN, 'Server status is unknown')

matcher = re.search(r'^System time\s*:\s*([0-9]+\.[0-9]*) seconds (slow|fast)', output,
flags=re.MULTILINE)
offset = float(matcher.group(1))
if matcher.group(2) == 'fast':
offset = -offset
if abs(offset) >= helper.options.critical:
helper.status(pynag.Plugins.critical)
elif abs(offset) >= helper.options.warning:
helper.status(pynag.Plugins.warning)
else:
helper.status(pynag.Plugins.ok)

helper.add_metric(label='offset', value='%.9f' % offset, uom='s')
helper.add_summary('Offset %g seconds' % offset)
helper.exit()
status = Plugins.check_threshold(abs(offset), warning=plugin['warning'],
critical=plugin['critical'])
plugin.add_perfdata('offset', offset, uom='s', warn=plugin['warning'], crit=plugin['critical'])
plugin.nagios_exit(status, 'Offset {} seconds'.format(offset))

0 comments on commit 63c9502

Please sign in to comment.