From 9e20a388c0f8b116a4d175245778aeb355fd11d9 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh <567675+ajkavanagh@users.noreply.github.com> Date: Thu, 16 Jul 2020 16:15:33 +0100 Subject: [PATCH] Fix directory /etc/nagios/nrpe.d/ issue (#492) * Fix directory /etc/nagios/nrpe.d/ issue Under certain deployment conditions, the charm can attempt to write to the /etc/nagios/nrpe.d/ directory before it exists. This directory is created by the nrpe charm, but if the hacluster gets installed first, then it can be triggered to attempt to set up the nrpe entries before the directory can be created by nrpe. This change (and the associated charm-helpers change) ensures that the charm will delay the nrpe config until the directory is available (and thus, the nrpe charm is fully installed) Related Bug: LP: #1882557 * Simplify the check so it doesn't do the write. Co-authored-by: David Ames --- charmhelpers/contrib/charmsupport/nrpe.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/charmhelpers/contrib/charmsupport/nrpe.py b/charmhelpers/contrib/charmsupport/nrpe.py index d775861b0..eb6f8c080 100644 --- a/charmhelpers/contrib/charmsupport/nrpe.py +++ b/charmhelpers/contrib/charmsupport/nrpe.py @@ -18,14 +18,14 @@ # Authors: # Matthew Wedgwood -import subprocess -import pwd +import glob import grp import os -import glob -import shutil +import pwd import re import shlex +import shutil +import subprocess import yaml from charmhelpers.core.hookenv import ( @@ -265,6 +265,11 @@ def __init__(self, hostname=None, primary=True): relation_set(relation_id=rid, relation_settings={'primary': self.primary}) self.remove_check_queue = set() + @classmethod + def does_nrpe_conf_dir_exist(cls): + """Return True if th nrpe_confdif directory exists.""" + return os.path.isdir(cls.nrpe_confdir) + def add_check(self, *args, **kwargs): shortname = None if kwargs.get('shortname') is None: @@ -310,6 +315,12 @@ def write(self): nrpe_monitors = {} monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}} + + # check that the charm can write to the conf dir. If not, then nagios + # probably isn't installed, and we can defer. + if not self.does_nrpe_conf_dir_exist(): + return + for nrpecheck in self.checks: nrpecheck.write(self.nagios_context, self.hostname, self.nagios_servicegroups)