示例#1
0
def principal_unit_cache():
    """ Cache the principal unit that a beat is related to.

    This info will not change over the lifetime of the deployment, so we
    only need to set it once.
    """
    cache = kv()
    if not cache.get('principal_name'):
        principal_name = principal_unit()
        if principal_name:
            cache.set('principal_name', principal_name)
def principal_unit_cache():
    """ Cache the principal unit that a beat is related to.

    This info will not change over the lifetime of the deployment, so we
    only need to set it once.
    """
    cache = kv()
    if not cache.get('principal_name'):
        principal_name = principal_unit()
        if principal_name:
            cache.set('principal_name', principal_name)
示例#3
0
 def nagios_hostname(self):
     """ Return the string that nagios will use to identify this host """
     host_context = hookenv.config('nagios_host_context')
     if host_context:
         host_context += '-'
     hostname_type = hookenv.config('nagios_hostname_type')
     if hostname_type == 'host' or not self.is_ready():
         nagios_hostname = "{}{}".format(host_context, socket.gethostname())
         return nagios_hostname
     else:
         principal_unitname = hookenv.principal_unit()
         # Fallback to using "primary" if it exists.
         if not principal_unitname:
             for relunit in self[self.name]:
                 if relunit.get('primary', 'False').lower() == 'true':
                     principal_unitname = relunit['__unit__']
                     break
         nagios_hostname = "{}{}".format(host_context, principal_unitname)
         nagios_hostname = nagios_hostname.replace('/', '-')
         return nagios_hostname
 def nagios_hostname(self):
     """Return the string that nagios will use to identify this host."""
     host_context = hookenv.config("nagios_host_context")
     if host_context:
         host_context += "-"
     hostname_type = hookenv.config("nagios_hostname_type")
     if hostname_type == "host" or not self.is_ready():
         nagios_hostname = "{}{}".format(host_context, socket.gethostname())
         return nagios_hostname
     else:
         principal_unitname = hookenv.principal_unit()
         # Fallback to using "primary" if it exists.
         if not principal_unitname:
             for relunit in self[self.name]:
                 if relunit.get("primary", "False").lower() == "true":
                     principal_unitname = relunit["__unit__"]
                     break
         nagios_hostname = "{}{}".format(host_context, principal_unitname)
         nagios_hostname = nagios_hostname.replace("/", "-")
         return nagios_hostname
示例#5
0
    def __init__(self):
        procs = self.proc_count()

        if hookenv.config('procs') == "auto":
            proc_thresholds = "-k -w {} -c {}".format(25 * procs + 100,
                                                      50 * procs + 100)
        else:
            proc_thresholds = hookenv.config('procs')

        if hookenv.config('load') == 'auto':
            # Give 1min load alerts higher thresholds than 15 min load alerts
            warn_multipliers = (4, 2, 1)
            crit_multipliers = (8, 4, 2)
            load_thresholds = ('-w %s -c %s') \
                % (','.join([str(m * procs) for m in warn_multipliers]),
                   ','.join([str(m * procs) for m in crit_multipliers]))
        else:
            load_thresholds = hookenv.config('load')

        if hookenv.config('disk_root'):
            disk_root_thresholds = hookenv.config('disk_root') + " -p / "
        else:
            disk_root_thresholds = ''

        pkg_plugin_dir = '/usr/lib/nagios/plugins/'
        local_plugin_dir = '/usr/local/lib/nagios/plugins/'
        checks = [
            {
                'description': 'Root disk',
                'cmd_name': 'check_disk_root',
                'cmd_exec': pkg_plugin_dir + 'check_disk',
                'cmd_params': disk_root_thresholds,
            },
            {
                'description': 'Number of Zombie processes',
                'cmd_name': 'check_zombie_procs',
                'cmd_exec': pkg_plugin_dir + 'check_procs',
                'cmd_params': hookenv.config('zombies'),
            },
            {
                'description': 'Number of processes',
                'cmd_name': 'check_total_procs',
                'cmd_exec': pkg_plugin_dir + 'check_procs',
                'cmd_params': proc_thresholds,
            },
            {
                'description': 'System Load',
                'cmd_name': 'check_load',
                'cmd_exec': pkg_plugin_dir + 'check_load',
                'cmd_params': load_thresholds,
            },
            {
                'description': 'Number of Users',
                'cmd_name': 'check_users',
                'cmd_exec': pkg_plugin_dir + 'check_users',
                'cmd_params': hookenv.config('users'),
            },
            {
                'description': 'Swap',
                'cmd_name': 'check_swap',
                'cmd_exec': pkg_plugin_dir + 'check_swap',
                'cmd_params': hookenv.config('swap'),
            },
            {
                'description': 'Swap Activity',
                'cmd_name': 'check_swap_activity',
                'cmd_exec': local_plugin_dir + 'check_swap_activity',
                'cmd_params': hookenv.config('swap_activity'),
            },
            {
                'description': 'Memory',
                'cmd_name': 'check_mem',
                'cmd_exec': local_plugin_dir + 'check_mem.pl',
                'cmd_params': hookenv.config('mem'),
            },
            # {
            #     'description': 'Connnection tracking table',
            #     'cmd_name': 'check_conntrack',
            #     'cmd_exec': local_plugin_dir + 'check_conntrack.sh',
            #     'cmd_params': hookenv.config('conntrack'),
            # },
        ]
        self['checks'] = []
        sub_postfix = str(hookenv.config("sub_postfix"))
        # Automatically use _sub for checks shipped on a unit with the nagios
        # charm. Mostly for backwards compatibility.
        principal_unit = hookenv.principal_unit()
        if sub_postfix == '' and principal_unit:
            md = hookenv._metadata_unit(principal_unit)
            if md and md.pop('name', None) == 'nagios':
                sub_postfix = '_sub'
        for check in checks:
            # This can be used to clean up old files before rendering the new ones
            check['matching_files'] = glob.glob(
                '/etc/nagios/nrpe.d/{}*.cfg'.format(check['cmd_name']))
            check['description'] += " (sub)"
            check['cmd_name'] += sub_postfix
            self['checks'].append(check)
def get_minion_id():
    config = hookenv.config()
    if config['unit-as-id']:
        return hookenv.principal_unit().replace('/', '-')
    else:
        return socket.getfqdn()
    def __init__(self):
        """Set dict values."""
        procs = self.proc_count()

        if hookenv.config("procs") == "auto":
            proc_thresholds = "-k -w {} -c {}".format(25 * procs + 100,
                                                      50 * procs + 100)
        else:
            proc_thresholds = hookenv.config("procs")

        if hookenv.config("load") == "auto":
            # Give 1min load alerts higher thresholds than 15 min load alerts
            warn_multipliers = (4, 2, 1)
            crit_multipliers = (8, 4, 2)
            load_thresholds = ("-w %s -c %s") % (
                ",".join([str(m * procs) for m in warn_multipliers]),
                ",".join([str(m * procs) for m in crit_multipliers]),
            )
        else:
            load_thresholds = hookenv.config("load")

        if hookenv.config("disk_root"):
            disk_root_thresholds = hookenv.config("disk_root") + " -p / "
        else:
            disk_root_thresholds = ""

        pkg_plugin_dir = "/usr/lib/nagios/plugins/"
        local_plugin_dir = "/usr/local/lib/nagios/plugins/"
        checks = [
            {
                "description": "Root disk",
                "cmd_name": "check_disk_root",
                "cmd_exec": pkg_plugin_dir + "check_disk",
                "cmd_params": disk_root_thresholds,
            },
            {
                "description": "Number of Zombie processes",
                "cmd_name": "check_zombie_procs",
                "cmd_exec": pkg_plugin_dir + "check_procs",
                "cmd_params": hookenv.config("zombies"),
            },
            {
                "description": "Number of processes",
                "cmd_name": "check_total_procs",
                "cmd_exec": pkg_plugin_dir + "check_procs",
                "cmd_params": proc_thresholds,
            },
            {
                "description": "System Load",
                "cmd_name": "check_load",
                "cmd_exec": pkg_plugin_dir + "check_load",
                "cmd_params": load_thresholds,
            },
            {
                "description": "Number of Users",
                "cmd_name": "check_users",
                "cmd_exec": pkg_plugin_dir + "check_users",
                "cmd_params": hookenv.config("users"),
            },
            {
                "description": "Swap",
                "cmd_name": "check_swap",
                "cmd_exec": pkg_plugin_dir + "check_swap",
                "cmd_params": hookenv.config("swap"),
            },
            {
                "description": "Swap Activity",
                "cmd_name": "check_swap_activity",
                "cmd_exec": local_plugin_dir + "check_swap_activity",
                "cmd_params": hookenv.config("swap_activity"),
            },
            {
                "description": "Memory",
                "cmd_name": "check_mem",
                "cmd_exec": local_plugin_dir + "check_mem.pl",
                "cmd_params": hookenv.config("mem"),
            },
            {
                "description": "Connnection tracking table",
                "cmd_name": "check_conntrack",
                "cmd_exec": local_plugin_dir + "check_conntrack.sh",
                "cmd_params": hookenv.config("conntrack"),
            },
            {
                "description": "XFS Errors",
                "cmd_name": "check_xfs_errors",
                "cmd_exec": local_plugin_dir + "check_xfs_errors.py",
                "cmd_params": hookenv.config("xfs_errors"),
            },
        ]

        if not is_container():
            arp_check = {
                "description": "ARP cache entries",
                "cmd_name": "check_arp_cache",
                "cmd_exec": os.path.join(local_plugin_dir,
                                         "check_arp_cache.py"),
                # Specify params here to enable the check, not required otherwise.
                "cmd_params": "-w 60 -c 80",
            }
            checks.append(arp_check)
            ro_filesystem_excludes = hookenv.config("ro_filesystem_excludes")
            if ro_filesystem_excludes == "":
                # specify cmd_params = '' to disable/remove the check from nrpe
                check_ro_filesystem = {
                    "description":
                    "Readonly filesystems",
                    "cmd_name":
                    "check_ro_filesystem",
                    "cmd_exec":
                    os.path.join(local_plugin_dir, "check_ro_filesystem.py"),
                    "cmd_params":
                    "",
                }
            else:
                check_ro_filesystem = {
                    "description":
                    "Readonly filesystems",
                    "cmd_name":
                    "check_ro_filesystem",
                    "cmd_exec":
                    os.path.join(local_plugin_dir, "check_ro_filesystem.py"),
                    "cmd_params":
                    "-e {}".format(hookenv.config("ro_filesystem_excludes")),
                }
            checks.append(check_ro_filesystem)

        if hookenv.config("lacp_bonds").strip():
            for bond_iface in hookenv.config("lacp_bonds").strip().split():
                if os.path.exists("/sys/class/net/{}".format(bond_iface)):
                    description = "LACP Check {}".format(bond_iface)
                    cmd_name = "check_lacp_{}".format(bond_iface)
                    cmd_exec = local_plugin_dir + "check_lacp_bond.py"
                    cmd_params = "-i {}".format(bond_iface)
                    lacp_check = {
                        "description": description,
                        "cmd_name": cmd_name,
                        "cmd_exec": cmd_exec,
                        "cmd_params": cmd_params,
                    }
                    checks.append(lacp_check)

        if hookenv.config("netlinks"):
            ifaces = yaml.safe_load(hookenv.config("netlinks"))
            d_ifaces = self.parse_netlinks(ifaces)
            for iface in d_ifaces:
                description = "Netlinks status ({})".format(iface)
                cmd_name = "check_netlinks_{}".format(iface)
                cmd_exec = local_plugin_dir + "check_netlinks.py"
                cmd_params = d_ifaces[iface]
                netlink_check = {
                    "description": description,
                    "cmd_name": cmd_name,
                    "cmd_exec": cmd_exec,
                    "cmd_params": cmd_params,
                }
                checks.append(netlink_check)

        self["checks"] = []
        sub_postfix = str(hookenv.config("sub_postfix"))
        # Automatically use _sub for checks shipped on a unit with the nagios
        # charm. Mostly for backwards compatibility.
        principal_unit = hookenv.principal_unit()
        if sub_postfix == "" and principal_unit:
            md = hookenv._metadata_unit(principal_unit)
            if md and md.pop("name", None) == "nagios":
                sub_postfix = "_sub"
        nrpe_config_sub_tmpl = "/etc/nagios/nrpe.d/{}_*.cfg"
        nrpe_config_tmpl = "/etc/nagios/nrpe.d/{}.cfg"
        for check in checks:
            # This can be used to clean up old files before rendering the new
            # ones
            nrpe_configfiles_sub = nrpe_config_sub_tmpl.format(
                check["cmd_name"])
            nrpe_configfiles = nrpe_config_tmpl.format(check["cmd_name"])
            check["matching_files"] = glob.glob(nrpe_configfiles_sub)
            check["matching_files"].extend(glob.glob(nrpe_configfiles))
            check["description"] += " (sub)"
            check["cmd_name"] += sub_postfix
            self["checks"].append(check)
示例#8
0
    def __init__(self):
        procs = self.proc_count()

        if hookenv.config('procs') == "auto":
            proc_thresholds = "-k -w {} -c {}".format(25 * procs + 100,
                                                      50 * procs + 100)
        else:
            proc_thresholds = hookenv.config('procs')

        if hookenv.config('load') == 'auto':
            # Give 1min load alerts higher thresholds than 15 min load alerts
            warn_multipliers = (4, 2, 1)
            crit_multipliers = (8, 4, 2)
            load_thresholds = ('-w %s -c %s') \
                % (','.join([str(m * procs) for m in warn_multipliers]),
                   ','.join([str(m * procs) for m in crit_multipliers]))
        else:
            load_thresholds = hookenv.config('load')

        if hookenv.config('disk_root'):
            disk_root_thresholds = hookenv.config('disk_root') + " -p / "
        else:
            disk_root_thresholds = ''

        pkg_plugin_dir = '/usr/lib/nagios/plugins/'
        local_plugin_dir = '/usr/local/lib/nagios/plugins/'
        checks = [
            {
                'description': 'Root disk',
                'cmd_name': 'check_disk_root',
                'cmd_exec': pkg_plugin_dir + 'check_disk',
                'cmd_params': disk_root_thresholds,
            },
            {
                'description': 'Number of Zombie processes',
                'cmd_name': 'check_zombie_procs',
                'cmd_exec': pkg_plugin_dir + 'check_procs',
                'cmd_params': hookenv.config('zombies'),
            },
            {
                'description': 'Number of processes',
                'cmd_name': 'check_total_procs',
                'cmd_exec': pkg_plugin_dir + 'check_procs',
                'cmd_params': proc_thresholds,
            },
            {
                'description': 'System Load',
                'cmd_name': 'check_load',
                'cmd_exec': pkg_plugin_dir + 'check_load',
                'cmd_params': load_thresholds,
            },
            {
                'description': 'Number of Users',
                'cmd_name': 'check_users',
                'cmd_exec': pkg_plugin_dir + 'check_users',
                'cmd_params': hookenv.config('users'),
            },
            {
                'description': 'Swap',
                'cmd_name': 'check_swap',
                'cmd_exec': pkg_plugin_dir + 'check_swap',
                'cmd_params': hookenv.config('swap'),
            },
            {
                'description': 'Swap Activity',
                'cmd_name': 'check_swap_activity',
                'cmd_exec': local_plugin_dir + 'check_swap_activity',
                'cmd_params': hookenv.config('swap_activity'),
            },
            {
                'description': 'Memory',
                'cmd_name': 'check_mem',
                'cmd_exec': local_plugin_dir + 'check_mem.pl',
                'cmd_params': hookenv.config('mem'),
            },
            {
                'description': 'Connnection tracking table',
                'cmd_name': 'check_conntrack',
                'cmd_exec': local_plugin_dir + 'check_conntrack.sh',
                'cmd_params': hookenv.config('conntrack'),
            },
            {
                'description': 'XFS Errors',
                'cmd_name': 'check_xfs_errors',
                'cmd_exec': local_plugin_dir + 'check_xfs_errors.py',
                'cmd_params': hookenv.config('xfs_errors'),
            },
        ]

        if not is_container():
            arp_check = {
                'description': 'ARP cache entries',
                'cmd_name': 'check_arp_cache',
                'cmd_exec': os.path.join(local_plugin_dir,
                                         'check_arp_cache.py'),
                'cmd_params':
                '-w 60 -c 80',  # Specify params here to enable the check, not required otherwise.
            }
            checks.append(arp_check)
            ro_filesystem_excludes = hookenv.config('ro_filesystem_excludes')
            if ro_filesystem_excludes == '':
                # specify cmd_params = '' to disable/remove the check from nrpe
                check_ro_filesystem = {
                    'description':
                    'Readonly filesystems',
                    'cmd_name':
                    'check_ro_filesystem',
                    'cmd_exec':
                    os.path.join(local_plugin_dir, 'check_ro_filesystem.py'),
                    'cmd_params':
                    '',
                }
            else:
                check_ro_filesystem = {
                    'description':
                    'Readonly filesystems',
                    'cmd_name':
                    'check_ro_filesystem',
                    'cmd_exec':
                    os.path.join(local_plugin_dir, 'check_ro_filesystem.py'),
                    'cmd_params':
                    '-e {}'.format(hookenv.config('ro_filesystem_excludes')),
                }
            checks.append(check_ro_filesystem)

        if hookenv.config('lacp_bonds').strip():
            for bond_iface in hookenv.config('lacp_bonds').strip().split():
                if os.path.exists('/sys/class/net/{}'.format(bond_iface)):
                    description = 'LACP Check {}'.format(bond_iface)
                    cmd_name = 'check_lacp_{}'.format(bond_iface)
                    cmd_exec = local_plugin_dir + 'check_lacp_bond.py'
                    cmd_params = '-i {}'.format(bond_iface)
                    lacp_check = {
                        'description': description,
                        'cmd_name': cmd_name,
                        'cmd_exec': cmd_exec,
                        'cmd_params': cmd_params
                    }
                    checks.append(lacp_check)

        if hookenv.config('netlinks'):
            ifaces = yaml.safe_load(hookenv.config('netlinks'))
            d_ifaces = self.parse_netlinks(ifaces)
            for iface in d_ifaces:
                description = 'Netlinks status ({})'.format(iface)
                cmd_name = 'check_netlinks_{}'.format(iface)
                cmd_exec = local_plugin_dir + 'check_netlinks.py'
                cmd_params = d_ifaces[iface]
                netlink_check = {
                    'description': description,
                    'cmd_name': cmd_name,
                    'cmd_exec': cmd_exec,
                    'cmd_params': cmd_params
                }
                checks.append(netlink_check)

        self['checks'] = []
        sub_postfix = str(hookenv.config("sub_postfix"))
        # Automatically use _sub for checks shipped on a unit with the nagios
        # charm. Mostly for backwards compatibility.
        principal_unit = hookenv.principal_unit()
        if sub_postfix == '' and principal_unit:
            md = hookenv._metadata_unit(principal_unit)
            if md and md.pop('name', None) == 'nagios':
                sub_postfix = '_sub'
        NRPE_CONFIG_SUB_TMPL = '/etc/nagios/nrpe.d/{}_*.cfg'
        NRPE_CONFIG_TMPL = '/etc/nagios/nrpe.d/{}.cfg'
        for check in checks:
            # This can be used to clean up old files before rendering the new
            # ones
            nrpe_configfiles_sub = NRPE_CONFIG_SUB_TMPL.format(
                check['cmd_name'])
            nrpe_configfiles = NRPE_CONFIG_TMPL.format(check['cmd_name'])
            check['matching_files'] = glob.glob(nrpe_configfiles_sub)
            check['matching_files'].extend(glob.glob(nrpe_configfiles))
            check['description'] += " (sub)"
            check['cmd_name'] += sub_postfix
            self['checks'].append(check)