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

from pypuppetdb import connect

LOG = logging.getLogger(__file__)


class NagiosType(object):
    directives = None

    def __init__(self, db, output_dir,
                 nodefacts=None, query=None, environment=None):
        self.db = db
        self.output_dir = output_dir
        self.environment = environment
        if not nodefacts:
            self.nodefacts = self.get_nodefacts()
        else:
            self.nodefacts = nodefacts
        self.query = query
        self.file = open(self.file_name(), 'w')

    def query_string(self, nagios_type=None):
        if not nagios_type:
            nagios_type = 'Nagios_' + self.nagios_type

        if not self.query:
            return '["=", "type", "%s"]' % (nagios_type)
        query_parts = ['["=", "%s", "%s"]' % q for q in self.query]
        query_parts.append('["=", "type", "%s"]' % (nagios_type))
        return '["and", %s]' % ", ".join(query_parts)

    def file_name(self):
        return "{0}/auto_{1}.cfg".format(self.output_dir, self.nagios_type)

    def generate_name(self, resource):
        self.file.write("  %-30s %s\n" % (self.nagios_type + '_name',
                                          resource.name))

    def generate_parameters(self, resource):
        for param_name, param_value in resource.parameters.items():

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

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

            self.file.write("  %-30s %s\n" % (param_name, param_value))

    def generate(self):
        """
        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
        """

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

        for r in self.db.resources(query=self.query_string(),
                                   environment=self.environment):
            # 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)

            self.file.write("define %s {\n" % self.nagios_type)
            self.generate_name(r)
            self.generate_parameters(r)
            self.file.write("}\n")<commit_msg>Change log warning level to info<commit_after>#!/usr/bin/env python
"""
Generate all the nagios configuration files based on puppetdb information.
"""
import sys
import logging
import ConfigParser
from collections import defaultdict

from pypuppetdb import connect

LOG = logging.getLogger(__file__)


class NagiosType(object):
    directives = None

    def __init__(self, db, output_dir,
                 nodefacts=None, query=None, environment=None):
        self.db = db
        self.output_dir = output_dir
        self.environment = environment
        if not nodefacts:
            self.nodefacts = self.get_nodefacts()
        else:
            self.nodefacts = nodefacts
        self.query = query
        self.file = open(self.file_name(), 'w')

    def query_string(self, nagios_type=None):
        if not nagios_type:
            nagios_type = 'Nagios_' + self.nagios_type

        if not self.query:
            return '["=", "type", "%s"]' % (nagios_type)
        query_parts = ['["=", "%s", "%s"]' % q for q in self.query]
        query_parts.append('["=", "type", "%s"]' % (nagios_type))
        return '["and", %s]' % ", ".join(query_parts)

    def file_name(self):
        return "{0}/auto_{1}.cfg".format(self.output_dir, self.nagios_type)

    def generate_name(self, resource):
        self.file.write("  %-30s %s\n" % (self.nagios_type + '_name',
                                          resource.name))

    def generate_parameters(self, resource):
        for param_name, param_value in resource.parameters.items():

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

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

            self.file.write("  %-30s %s\n" % (param_name, param_value))

    def generate(self):
        """
        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
        """

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

        for r in self.db.resources(query=self.query_string(),
                                   environment=self.environment):
            # Make sure we do not try and make more than one resource
            # for each one.
            if r.name in unique_list:
                LOG.info("duplicate: %s" % r.name)
                continue
            unique_list.add(r.name)

            self.file.write("define %s {\n" % self.nagios_type)
            self.generate_name(r)
            self.generate_parameters(r)
            self.file.write("}\n")
{'fraction_non_alphanumeric': 0.10912765240821826, 'fraction_numerical': 0.0020208824520040417, 'mean_word_length': 1.8970731707317072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6775518', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1659, 'n_words': 454}