<commit_before>#!/usr/bin/env python
"""
Generate all the nagios configuration files based on puppetdb information.
"""
import sys
import logging
from collections import defaultdict

from pypuppetdb import connect

LOG = logging.getLogger(__file__)

TMP_FILES = "/tmp/nagios_tmp"


def get_nodefacts(db):
    """
    Get all the nodes & facts from puppetdb.

    This can be used to construct hostgroups, etc.

    {
     'hostname': {
            'factname': factvalue,
            'factname': factvalue,
            }
    }
    """
    nodefacts = {}
    for node in db.nodes():
        nodefacts[node.name] = {}
        for f in node.facts():
            nodefacts[node.name][f.name] = f.value
    return nodefacts


def generate_nagios_cfg_type(db, nagios_type, nodefacts):
    """
    Generate a nagios configuration for a single type

    The output of this will be a single file for each type.
    eg.
      auto_hosts.cfg
      auto_checks.cfg
    """

    # This is the namaed used for the configuration files.
    nagios_define_type = nagios_type.replace('Nagios_', '')

    # Write out the generate configuration into files.
    tmp_file = "{0}/auto_{1}.cfg".format(TMP_FILES, nagios_define_type)
    f = open(tmp_file, 'w')

    # Query puppetdb only throwing back the resource that match
    # the Nagios type.
    unique_list = set([])

    # Keep track of sevice to hostname
    servicegroups = defaultdict(list)

    for r in db.resources(query='["=", "type", "%s"]' % nagios_type):

        # Make sure we do not try and make more than one resource for each one.
        if r.name in unique_list:
            LOG.warning("duplicate: %s" % r.name)
            continue
        unique_list.add(r.name)

        # Add servies to service group
        if nagios_define_type == 'service':
            host_name = r.parameters['host_name']
            servicegroups[r.parameters['service_description']].append(host_name)

        # ignore workstatins, laptops and desktops
        if 'workstation' in r.tags and nagios_define_type == 'host':
            hostname = r.name
            if hostname in nodefacts and 'ipaddress_eth0' in nodefacts[hostname]:
                r.parameters['address'] = nodefacts[hostname]['ipaddress_eth0']
            else:
                r.parameters['address'] = "127.0.0.1"

        f.write("define %s {\n" % nagios_define_type)

        # puppet stores the command_name as command, we rewrite this back
        if nagios_define_type == 'command':
            f.write("  %-30s %s\n" % ("command_name", r.name))

        if nagios_define_type == 'host':
            f.write("  %-30s %s\n" % ("host_name", r.name))

        for param_name, param_value in r.parameters.items():

            if not param_value:
                continue
            if param_name in ['target', 'require', 'tag', 'notify', 'ensure']:
                continue

            # Convert all lists into csv values
            if isinstance(param_value, list):
                param_value = ",".join(param_value)

            f.write("  %-30s %s\n" % (param_name, param_value))
        f.write("}\n")
    f.close()

    for servicegroup_name, host_list in servicegroups.items():
        tmp_file = "{0}/auto_servicegroup_{1}.cfg".format(TMP_FILES, servicegroup_name)

        members = []
        for host in host_list:
            members.append("%s,%s" % (host, servicegroup_name))

        f = open(tmp_file, 'w')
        f.write("define servicegroup {\n")
        f.write(" servicegroup_name %s\n" % servicegroup_name)
        f.write(" alias %s\n" % servicegroup_name)<commit_msg>Ignore mode argument, which is used by naginator<commit_after>#!/usr/bin/env python
"""
Generate all the nagios configuration files based on puppetdb information.
"""
import sys
import logging
from collections import defaultdict

from pypuppetdb import connect

LOG = logging.getLogger(__file__)

TMP_FILES = "/tmp/nagios_tmp"


def get_nodefacts(db):
    """
    Get all the nodes & facts from puppetdb.

    This can be used to construct hostgroups, etc.

    {
     'hostname': {
            'factname': factvalue,
            'factname': factvalue,
            }
    }
    """
    nodefacts = {}
    for node in db.nodes():
        nodefacts[node.name] = {}
        for f in node.facts():
            nodefacts[node.name][f.name] = f.value
    return nodefacts


def generate_nagios_cfg_type(db, nagios_type, nodefacts):
    """
    Generate a nagios configuration for a single type

    The output of this will be a single file for each type.
    eg.
      auto_hosts.cfg
      auto_checks.cfg
    """

    # This is the namaed used for the configuration files.
    nagios_define_type = nagios_type.replace('Nagios_', '')

    # Write out the generate configuration into files.
    tmp_file = "{0}/auto_{1}.cfg".format(TMP_FILES, nagios_define_type)
    f = open(tmp_file, 'w')

    # Query puppetdb only throwing back the resource that match
    # the Nagios type.
    unique_list = set([])

    # Keep track of sevice to hostname
    servicegroups = defaultdict(list)

    for r in db.resources(query='["=", "type", "%s"]' % nagios_type):

        # Make sure we do not try and make more than one resource for each one.
        if r.name in unique_list:
            LOG.warning("duplicate: %s" % r.name)
            continue
        unique_list.add(r.name)

        # Add servies to service group
        if nagios_define_type == 'service':
            host_name = r.parameters['host_name']
            servicegroups[r.parameters['service_description']].append(host_name)

        # ignore workstatins, laptops and desktops
        if 'workstation' in r.tags and nagios_define_type == 'host':
            hostname = r.name
            if hostname in nodefacts and 'ipaddress_eth0' in nodefacts[hostname]:
                r.parameters['address'] = nodefacts[hostname]['ipaddress_eth0']
            else:
                r.parameters['address'] = "127.0.0.1"

        f.write("define %s {\n" % nagios_define_type)

        # puppet stores the command_name as command, we rewrite this back
        if nagios_define_type == 'command':
            f.write("  %-30s %s\n" % ("command_name", r.name))

        if nagios_define_type == 'host':
            f.write("  %-30s %s\n" % ("host_name", r.name))

        for param_name, param_value in r.parameters.items():

            if not param_value:
                continue
            if param_name in set(['target', 'require', 'tag', 'notify',
                                  'ensure', 'mode']):
                continue

            # Convert all lists into csv values
            if isinstance(param_value, list):
                param_value = ",".join(param_value)

            f.write("  %-30s %s\n" % (param_name, param_value))
        f.write("}\n")
    f.close()

    for servicegroup_name, host_list in servicegroups.items():
        tmp_file = "{0}/auto_servicegroup_{1}.cfg".format(TMP_FILES, servicegroup_name)

        members = []
        for host in host_list:
            members.append("%s,%s" % (host, servicegroup_name))

        f = open(tmp_file, 'w')
        f.write("define servicegroup {\n")
        f.write(" servicegroup_name %s\n" % servicegroup_name)
        f.write(" alias %s\n" % servicegroup_name)
{'fraction_non_alphanumeric': 0.11373626373626373, 'fraction_numerical': 0.004945054945054945, 'mean_word_length': 2.335318369216674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5845916', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2194, 'n_words': 611}