Beispiel #1
0
def makeService(options):
    """ the service that wraps everything the connector needs. """
    from vigilo.connector.options import getSettings, parseSubscriptions

    settings = getSettings(options, __name__)

    from vigilo.common.logging import get_logger

    LOGGER = get_logger(__name__)

    from vigilo.common.gettext import translate

    _ = translate(__name__)

    from vigilo.connector.client import client_factory
    from vigilo.connector.handlers import buspublisher_factory

    from vigilo.connector_metro.rrdtool import RRDToolPoolManager
    from vigilo.connector_metro.rrdtool import RRDToolManager
    from vigilo.connector_metro.confdb import MetroConfDB
    from vigilo.connector_metro.threshold import ThresholdChecker
    from vigilo.connector_metro.bustorrdtool import BusToRRDtool

    root_service = service.MultiService()

    # Client du bus
    client = client_factory(settings)
    client.setServiceParent(root_service)
    providers = []

    # Configuration
    try:
        conffile = settings["connector-metro"]["config"]
    except KeyError:
        LOGGER.error(
            _("Please set the path to the configuration " "database generated by VigiConf in the settings.ini.")
        )
        sys.exit(1)
    confdb = MetroConfDB(conffile)
    confdb.setServiceParent(root_service)

    try:
        must_check_th = settings["connector-metro"].as_bool("check_thresholds")
    except KeyError:
        must_check_th = True

    # Gestion RRDTool
    rrd_base_dir = settings["connector-metro"]["rrd_base_dir"]
    rrd_path_mode = settings["connector-metro"]["rrd_path_mode"]
    rrd_bin = settings["connector-metro"].get("rrd_bin", "/usr/bin/rrdtool")
    rrdcached = settings["connector-metro"].get("rrdcached", None)
    try:
        pool_size = settings["connector-metro"].as_int("rrd_processes")
    except KeyError:
        pool_size = None
    rrdtool_pool = RRDToolPoolManager(
        rrd_base_dir, rrd_path_mode, rrd_bin, check_thresholds=must_check_th, rrdcached=rrdcached, pool_size=pool_size
    )
    rrdtool = RRDToolManager(rrdtool_pool, confdb)

    # Gestion des seuils
    if must_check_th:
        threshold_checker = ThresholdChecker(rrdtool, confdb)
        bus_publisher = buspublisher_factory(settings, client)
        bus_publisher.registerProducer(threshold_checker, streaming=True)
        providers.append(bus_publisher)
    else:
        threshold_checker = None

    # Gestionnaire principal des messages
    bustorrdtool = BusToRRDtool(confdb, rrdtool, threshold_checker)
    bustorrdtool.setClient(client)
    subs = parseSubscriptions(settings)
    queue = settings["bus"]["queue"]
    queue_messages_ttl = int(settings["bus"].get("queue_messages_ttl", 0))
    bustorrdtool.subscribe(queue, queue_messages_ttl, subs)
    providers.append(bustorrdtool)

    # Statistiques
    from vigilo.connector.status import statuspublisher_factory

    status_publisher = statuspublisher_factory(settings, client, providers=providers)

    return root_service
class FunctionalTestCase(unittest.TestCase):
    """
    Vérification que le passage d'un message produit bien un fichier RRD.
    (création du fichier)
    """


    @deferred(timeout=30)
    @patch('signal.signal') # erreurs de threads
    def setUp(self, signal):
        self.tmpdir = tempfile.mkdtemp(prefix="test-connector-metro-")
        client = ClientStub("testhostname", None, None)
        rrd_base_dir = os.path.join(self.tmpdir, "rrds")
        os.mkdir(rrd_base_dir)
        confdb = MetroConfDB(os.path.join(os.path.dirname(__file__),
                                          "connector-metro.db"))
        self.rrdtool_pool = RRDToolPoolManagerStub(rrd_base_dir,
                    "flat", "/usr/bin/rrdtool")
        rrdtool = RRDToolManager(self.rrdtool_pool, confdb)
        threshold_checker = ThresholdChecker(rrdtool, confdb)
        self.btr = BusToRRDtool(confdb, rrdtool, threshold_checker)
        self.btr.setClient(client)
        d = self.btr.startService()
        d.addCallback(lambda _x: confdb.reload())
        return d

    @deferred(timeout=30)
    def tearDown(self):
        d = self.btr.stopService()
        d.addCallback(lambda _x: rmtree(self.tmpdir))
        return d


    @deferred(timeout=30)
    def test_handled_host(self):
        """Functionnal: messages sur des hôtes déclarés."""

        rrdfile = os.path.join(self.tmpdir, "rrds",
                               "server1.example.com", "Load.rrd")
        # on vérifie que le fichier n'existe pas encore
        # (ce qui lève une exception quand on teste le fichier).
        self.assertRaises(OSError, os.stat, rrdfile)
        msg = { "type": "perf",
                "timestamp": "1165939739",
                "host": "server1.example.com",
                "datasource": "Load",
                "value": "12",
                }
        d = self.btr.processMessage(msg)
        # on vérifie que le fichier correspondant a bien été créé
        def check_created(r):
            self.assertTrue(os.path.exists(rrdfile),
                            "Le fichier n'a pas été créé")
            self.assertTrue(stat.S_ISREG(os.stat(rrdfile).st_mode))
        def check_creation_command(r):
            self.assertTrue(len(self.rrdtool_pool.commands) > 0)
            self.assertEqual(self.rrdtool_pool.commands[0],
                    ('create', rrdfile,
                    ['--step', '300', '--start', '1165939729',
                     'RRA:AVERAGE:0.5:1:600', 'RRA:AVERAGE:0.5:6:700',
                     'RRA:AVERAGE:0.5:24:775', 'RRA:AVERAGE:0.5:288:732',
                     'DS:DS:GAUGE:600:U:U']))
        def check_update_command(r):
            self.assertEqual(self.rrdtool_pool.commands[1],
                             ('update', rrdfile, '1165939739:12'))
        d.addCallback(check_created)
        d.addCallback(check_creation_command)
        d.addCallback(check_update_command)
        return d