<commit_before>try:
    from numbers import Number
    import pymongo
except ImportError:
    Number = None

import diamond


class MongoDBCollector(diamond.collector.Collector):
    """Collects data from MongoDB's db.serverStatus() command

    Collects all number values from the db.serverStatus() command, other
    values are ignored.

    """
    
    def get_default_config(self):
        """
        Returns the default collector settings
        """
        return {
            'path':     'mongo',
            'host':     'localhost'
        }    
    
    def collect(self):
        """Collect number values from db.serverStatus()"""

        if Number is None:
            self.log.error('Unable to import either Number or pymongo')
            return {}

        conn = pymongo.Connection(self.config['host'],slave_okay=True)
        data = conn.db.command('serverStatus')
        for key in data:
            self._publish_metrics([], key, data)

    def _publish_metrics(self, prev_keys, key, data):
        """Recursively publish keys"""
        value = data[key]
        keys = prev_keys + [key]
        if isinstance(value, dict):
            for new_key in value:
                self._publish_metrics(keys, new_key, value)
        elif isinstance(value, Number):
            self.publish('.'.join(keys), value)
<commit_msg>Replace deprecated slave_ok for read_preference in pymongo.Connection<commit_after>try:
    from numbers import Number
    import pymongo
    from pymongo import ReadPreference
except ImportError:
    Number = None

import diamond


class MongoDBCollector(diamond.collector.Collector):
    """Collects data from MongoDB's db.serverStatus() command

    Collects all number values from the db.serverStatus() command, other
    values are ignored.

    """
    
    def get_default_config(self):
        """
        Returns the default collector settings
        """
        return {
            'path':     'mongo',
            'host':     'localhost'
        }    
    
    def collect(self):
        """Collect number values from db.serverStatus()"""

        if Number is None:
            self.log.error('Unable to import either Number or pymongo')
            return {}

        conn = pymongo.Connection(self.config['host'],read_preference=ReadPreference.SECONDARY)
        data = conn.db.command('serverStatus')
        for key in data:
            self._publish_metrics([], key, data)

    def _publish_metrics(self, prev_keys, key, data):
        """Recursively publish keys"""
        value = data[key]
        keys = prev_keys + [key]
        if isinstance(value, dict):
            for new_key in value:
                self._publish_metrics(keys, new_key, value)
        elif isinstance(value, Number):
            self.publish('.'.join(keys), value)

{'fraction_non_alphanumeric': 0.09907341411261582, 'fraction_numerical': 0.0, 'mean_word_length': 2.2601626016260163, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2362983', 'n_tokens_mistral': 775, 'n_tokens_neox': 746, 'n_words': 216}