Skip to content

Commit

Permalink
Add checks for openvswitch and helper function to enable the checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew Freiberger committed Apr 22, 2021
1 parent 4b8c496 commit 169e5e5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
32 changes: 32 additions & 0 deletions charmhelpers/contrib/charmsupport/nrpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import yaml

from charmhelpers.core.hookenv import (
DEBUG,
config,
hook_name,
local_unit,
Expand Down Expand Up @@ -509,6 +510,37 @@ def add_haproxy_checks(nrpe, unit_name):
check_cmd='check_haproxy_queue_depth.sh')


def add_openvswitch_checks(nrpe, unit_name):
"""
Add checks for openvswitch
:param NRPE nrpe: NRPE object to add check to
:param str unit_name: Unit name to use in check description
"""
enable_sudo_for_openvswitch_checks()
nrpe.add_check(
shortname='openvswitch',
description='Check Open vSwitch {%s}' % unit_name,
check_cmd='check_openvswitch.py')


def enable_sudo_for_openvswitch_checks():
sudoers_dir = "/etc/sudoers.d"
sudoers_mode = 0o100440
ovs_sudoers_file = "99-check_openvswitch"
ovs_sudoers_entry = "nagios ALL=(root) NOPASSWD: /usr/bin/ovs-vsctl show"
dest = os.path.join(sudoers_dir, ovs_sudoers_file)
try:
with open(dest, "w") as sudoer_file:
sudoer_file.write(ovs_sudoers_entry)
os.chmod(dest, sudoers_mode)
os.chown(dest, uid=0, gid=0)
except (OSError, IOError) as e:
log("Failed to setup sudoers file for check_openvswitch: {}".format(e))
else:
log("Sudoers file for check_openvswitch installed: {}".format(dest), DEBUG)


def remove_deprecated_check(nrpe, deprecated_services):
"""
Remove checks fro deprecated services in list
Expand Down
62 changes: 62 additions & 0 deletions charmhelpers/contrib/openstack/files/check_openvswitch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
# -*- coding: us-ascii -*-
"""Check for issues with NVME hardware devices."""

import argparse
import re
import subprocess
import sys

from nagios_plugin3 import CriticalError, UnknownError, try_check


def parse_ovs_status():
"""Check for errors in 'ovs-vsctl show' output."""
try:
cmd = ["/usr/bin/sudo", "/usr/bin/ovs-vsctl", "show"]
ovs_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise UnknownError(
"UNKNOWN: Failed to query ovs: {}".format(
e.output.decode(errors="ignore").rstrip()
)
)

ovs_vsctl_show_errors = []
ovs_error_re = re.compile(r"^.*error: (?P<message>.+)$", re.I)
for line in ovs_output.decode(errors="ignore").splitlines():
m = ovs_error_re.match(line)
if m:
ovs_vsctl_show_errors.append(m.group("message"))

if ovs_vsctl_show_errors:
numerrs = len(ovs_vsctl_show_errors)
raise CriticalError(
"CRITICAL: Found {} error(s) in ovs-vsctl show: "
"{}".format(numerrs, ", ".join(ovs_vsctl_show_errors))
)

print("OK: no errors found in openvswitch")


def parse_args(argv=None):
"""Process CLI arguments."""
parser = argparse.ArgumentParser(
prog="check_openvswitch",
description=(
"this program checks openvswitch status and outputs an "
"appropriate Nagios status line"
),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
return parser.parse_args(argv)


def main(argv):
"""Define main subroutine."""
parse_args(argv)
try_check(parse_ovs_status)


if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit 169e5e5

Please sign in to comment.