<commit_before>#!/usr/bin/python

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
# <NAME>, <EMAIL>
# <NAME>, <EMAIL>
# <NAME>, <EMAIL>
# <NAME>, <EMAIL>
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

"""
This module job is to get configuration data (mostly hosts) from a mongodb database.
"""

# This module imports hosts and services configuration from a MySQL Database
# Queries for getting hosts and services are pulled from shinken-specific.cfg configuration file.
try:
    import uuid
except ImportError:
    uuid = None

try:
    from pymongo.connection import Connection
except ImportError:
    Connection = None

from shinken.basemodule import BaseModule
from shinken.log import logger

properties = {
    'daemons': ['arbiter', 'webui', 'skonf'],
    'type': 'mongodb',
    'external': False,
    'phases': ['configuration'],
}


# called by the plugin manager to get a module instance
def get_instance(plugin):
    logger.debug("[MongoDB Module]: Get Mongodb instance for plugin %s" % plugin.get_name())
    if not Connection:
        raise Exception('Cannot find the module python-pymongo. Please install it.')
    uri = plugin.uri
    database = plugin.database
    try:
        username = plugin.username
        password = <PASSWORD>
    except:
        username = ''
        password = ''
    
    instance = Mongodb_generic(plugin, uri, database, username, password)
    return instance


# Retrieve hosts from a Mongodb
class Mongodb_generic(BaseModule):
    def __init__(self, mod_conf, uri, database, username, password):
        BaseModule.__init__(self, mod_conf)
        self.uri = uri
        self.database = database
        self.username = username
        self.password = password
        # Some used variable init
        self.con = None
        self.db = None

    # Called by Arbiter to say 'let's prepare yourself guy'
    def init(self):
        logger.info("[Mongodb Module]: Try to open a Mongodb connection to %s:%s" % (self.uri, self.database))
        try:
            self.con = Connection(self.uri)
            self.db = getattr(self.con, self.database)
            try:
                self.db.authenticate(self.username, self.password)
            except:
                pass
        except Exception, e:
            logger.error("Mongodb Module: Error %s:" % e)
            raise
        logger.info("[Mongodb Module]: Connection OK")

################################ Arbiter part #################################

    # Main function that is called in the CONFIGURATION phase
    def get_objects(self):
        if not self.db:
            logger.error("[Mongodb Module]: Problem during init phase")
            return {}

        r = {}

        tables = ['hosts', 'services', 'contacts', 'commands', 'timeperiods']
        for t in tables:
            r[t] = []

            cur = getattr(self.db, t).find({'_state': {'$ne': 'disabled'}})
            for h in cur:<commit_msg>removed try/except, with no user/pass working fine, with user/pass working fine, with user/wrong-pass you find bad authentication in broker log<commit_after>#!/usr/bin/python

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
# <NAME>, <EMAIL>
# <NAME>, <EMAIL>
# Gregory Starck, <EMAIL>
# <NAME>, <EMAIL>
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

"""
This module job is to get configuration data (mostly hosts) from a mongodb database.
"""

# This module imports hosts and services configuration from a MySQL Database
# Queries for getting hosts and services are pulled from shinken-specific.cfg configuration file.
try:
    import uuid
except ImportError:
    uuid = None

try:
    from pymongo.connection import Connection
except ImportError:
    Connection = None

from shinken.basemodule import BaseModule
from shinken.log import logger

properties = {
    'daemons': ['arbiter', 'webui', 'skonf'],
    'type': 'mongodb',
    'external': False,
    'phases': ['configuration'],
}


# called by the plugin manager to get a module instance
def get_instance(plugin):
    logger.debug("[MongoDB Module]: Get Mongodb instance for plugin %s" % plugin.get_name())
    if not Connection:
        raise Exception('Cannot find the module python-pymongo. Please install it.')
    uri = plugin.uri
    database = plugin.database
    username = getattr(plugin, 'username', '')
    password = getattr(plugin, 'password', '')
    
    instance = Mongodb_generic(plugin, uri, database, username, password)
    return instance


# Retrieve hosts from a Mongodb
class Mongodb_generic(BaseModule):
    def __init__(self, mod_conf, uri, database, username, password):
        BaseModule.__init__(self, mod_conf)
        self.uri = uri
        self.database = database
        self.username = username
        self.password = password
        # Some used variable init
        self.con = None
        self.db = None

    # Called by Arbiter to say 'let's prepare yourself guy'
    def init(self):
        logger.info("[Mongodb Module]: Try to open a Mongodb connection to %s:%s" % (self.uri, self.database))
        try:
            self.con = Connection(self.uri)
            self.db = getattr(self.con, self.database)
            if self.username != '' and self.password != '':
                self.db.authenticate(self.username, self.password)
        except Exception, e:
            logger.error("Mongodb Module: Error %s:" % e)
            raise
        logger.info("[Mongodb Module]: Connection OK")

################################ Arbiter part #################################

    # Main function that is called in the CONFIGURATION phase
    def get_objects(self):
        if not self.db:
            logger.error("[Mongodb Module]: Problem during init phase")
            return {}

        r = {}

        tables = ['hosts', 'services', 'contacts', 'commands', 'timeperiods']
        for t in tables:
            r[t] = []

            cur = getattr(self.db, t).find({'_state': {'$ne': 'disabled'}})
            for h in cur:
{'fraction_non_alphanumeric': 0.11782644857222069, 'fraction_numerical': 0.0027723870252287217, 'mean_word_length': 2.9383187772925763, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6912096', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1969, 'n_words': 752}