def manage(): status_set('maintenance', 'starting') config = hookenv.config() manager = ServiceManager([ { 'service': 'nrpe-install', 'data_ready': [ nrpe_utils.install_packages, nrpe_utils.install_charm_files, ], 'start': [], 'stop': [], }, { 'service': 'nrpe-config', 'required_data': [ config, nrpe_helpers.MonitorsRelation(), nrpe_helpers.PrincipalRelation(), nrpe_helpers.NagiosInfo(), ], 'data_ready': [ nrpe_utils.update_nrpe_external_master_relation, nrpe_utils.update_monitor_relation, nrpe_utils.create_host_export_fragment, nrpe_utils.render_nrped_files, helpers.render_template( source='nrpe.tmpl', target='/etc/nagios/nrpe.cfg' ), ], 'provided_data': [nrpe_helpers.PrincipalRelation()], 'ports': [hookenv.config('server_port'), 'ICMP'], 'start': [nrpe_utils.maybe_open_ports, nrpe_utils.restart_nrpe], 'stop': [], }, { 'service': 'nrpe-rsync', 'required_data': [ config, nrpe_helpers.PrincipalRelation(), nrpe_helpers.RsyncEnabled(), nrpe_helpers.NagiosInfo(), ], 'data_ready': [ nrpe_utils.remove_host_export_fragments, helpers.render_template( source='rsync-juju.d.tmpl', target='/etc/rsync-juju.d/010-nrpe-external-master.conf' ), nrpe_utils.create_host_export_fragment, ], 'start': [nrpe_utils.restart_rsync], 'stop': [], }, ]) manager.manage() status_set('active', 'ready')
def __call__(self, manager, service_name, event_name): nag_hostname = nrpe_helpers.PrincipalRelation().nagios_hostname() target = '/var/lib/nagios/export/host__{}.cfg'.format(nag_hostname) renderer = helpers.render_template( source='export_host.cfg.tmpl', target=target, perms=0o644, ) renderer(manager, service_name, event_name)
def update_nrpe_external_master_relation(service_name): """ Send updated nagios_hostname to charms attached to nrpe_external_master relation. """ principal_relation = nrpe_helpers.PrincipalRelation() for rid in hookenv.relation_ids('nrpe-external-master'): hookenv.relation_set( relation_id=rid, relation_settings=principal_relation.provide_data())
class Nrpe(CharmBase): """Nrpe.""" monitors_relation = nrpe_helpers.MonitorsRelation() principal_relation = nrpe_helpers.PrincipalRelation() nagios_info = nrpe_helpers.NagiosInfo() def __init__(self, *args): """Initialize class level attributes and observe events.""" super().__init__(*args) self._general_info = GeneralInfo(self, 'general-info') self._local_monitors = LocalMonitors(self, 'local-monitors') self._monitors = Monitors(self, 'monitors') self._nrpe_external_master = NrpeExternalMaster(self, 'nrpe-external-master') self.framework.observe( self.on.install, self._on_install ) self.framework.observe( self.on.config_changed, self._on_config_changed ) self.framework.observe( self._general_info.on.available, self._on_check_status_and_configure_event ) self.framework.observe( self._monitors.on.available, self._on_check_status_and_configure_event ) self.framework.observe( self._local_monitors.on.available, self._on_check_status_and_configure_event ) self.framework.observe( self._nrpe_external_master.on.available, self._on_check_status_and_configure_event ) def _on_install(self, event): """Install packages and charm files.""" logger.debug("IN _ON_INSTALL") logger.debug(monitors_relation.__dict__) logger.debug(principal_relation.__dict__) logger.debug(nagios_info.__dict__) nrpe_utils.install_packages() nrpe_utils.install_charm_files() def _on_config_changed(self, event): logger.debug("IN _ON_CONFIG_CHANGED") logger.debug(monitors_relation.__dict__) logger.debug(principal_relation.__dict__) logger.debug(nagios_info.__dict__) self._check_status_and_configure_event(event) def _check_status_and_configure_event(event): logger.debug("IN _CHECK_STATUS_AND_CONFIGUR") logger.debug(monitors_relation.__dict__) logger.debug(principal_relation.__dict__) logger.debug(nagios_info.__dict__) if not self._check_status_and_configure(): event.defer() def _check_status_and_configure(self): if not self._check_status(): return False self._configure() return True def _check_status(self): if not (self.monitors_relation and self.principal_relation and self.nagios_info): return False return True def _configure(self): nrpe_utils.update_nrpe_external_master_relation() nrpe_utils.update_monitor_relation() nrpe_utils.create_host_export_fragment nrpe_utils.render_nrped_files() helpers.render_template( source="nrpe.tmpl", target="/etc/nagios/nrpe.cfg" ) nrpe_utils.maybe_open_ports nrpe_utils.restart_nrpe()
def manage(): """Manage nrpe service.""" status_set("maintenance", "starting") config = hookenv.config() manager = ServiceManager([ { "service": "nrpe-install", "data_ready": [ nrpe_utils.install_packages, nrpe_utils.install_charm_files, ], "start": [], "stop": [], }, { "service": "nrpe-config", "required_data": [ config, nrpe_helpers.MonitorsRelation(), nrpe_helpers.PrincipalRelation(), nrpe_helpers.NagiosInfo(), ], "data_ready": [ nrpe_utils.update_nrpe_external_master_relation, nrpe_utils.update_monitor_relation, nrpe_utils.create_host_export_fragment, nrpe_utils.render_nrped_files, helpers.render_template(source="nrpe.tmpl", target="/etc/nagios/nrpe.cfg"), ], "provided_data": [nrpe_helpers.PrincipalRelation()], "ports": [hookenv.config("server_port"), "ICMP"], "start": [nrpe_utils.maybe_open_ports, nrpe_utils.restart_nrpe], "stop": [], }, { "service": "nrpe-rsync", "required_data": [ config, nrpe_helpers.PrincipalRelation(), nrpe_helpers.RsyncEnabled(), nrpe_helpers.NagiosInfo(), ], "data_ready": [ nrpe_utils.remove_host_export_fragments, helpers.render_template( source="rsync-juju.d.tmpl", target="/etc/rsync-juju.d/010-nrpe-external-master.conf", ), nrpe_utils.create_host_export_fragment, ], "start": [nrpe_utils.restart_rsync], "stop": [], }, ]) manager.manage() if nrpe_utils.has_consumer(): status_set("active", "ready") else: status_set("blocked", "Nagios server not configured or related")