示例#1
0
def update_monitor_relation(service_name):
    """ Send updated monitor yaml to charms attached to monitor relation """
    monitor_relation = nrpe_helpers.MonitorsRelation()
    for rid in hookenv.relation_ids('monitors'):
        hookenv.relation_set(
            relation_id=rid,
            relation_settings=monitor_relation.provide_data()
        )
示例#2
0
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 process_local_monitors():
    """Get all the monitor dicts and write out and local checks."""
    monitor_dicts = nrpe_helpers.MonitorsRelation().get_monitor_dicts()
    for monitor_src in monitor_dicts.keys():
        monitor_dict = monitor_dicts[monitor_src]
        if not (monitor_dict and "local" in monitor_dict["monitors"]):
            continue
        monitors = monitor_dict["monitors"]["local"]
        for checktype in monitors:
            for check in monitors[checktype]:
                render_nrpe_check_config(
                    nrpe_helpers.NRPECheckCtxt(
                        checktype,
                        monitors[checktype][check],
                        monitor_src,
                    ))
示例#4
0
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()
示例#5
0
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")