def test_sendStatus(self): """Envoi de l'état (sendStatus)""" client = ClientStub("testhost", None, None) self.settings["connector"]["status_service"] = "testservice" sp = statuspublisher_factory(self.settings, client, [ProviderStub()]) sp.isConnected = lambda: True d = client.stub_connect() d.addCallback(lambda _x: sp.sendStatus()) def check(r): output = client.channel.sent print output self.assertEqual(len(output), 2) msg_perf = json.loads(output[0]["content"].body) self.assertEqual(msg_perf["type"], "perf") self.assertEqual(msg_perf["host"], self.localhn) self.assertEqual(msg_perf["datasource"], "testservice-dummykey") self.assertEqual(msg_perf["value"], "dummyvalue") msg_cmd = json.loads(output[1]["content"].body) self.assertEqual(msg_cmd["type"], "nagios") self.assertEqual(msg_cmd["cmdname"], "PROCESS_SERVICE_CHECK_RESULT") self.assertTrue(msg_cmd["value"].startswith( "%s;testservice;0;OK:" % self.localhn)) d.addCallback(check) return d
def test_send_stats(self): """Relai d'un ensemble de statistiques""" client = ClientStub("testhost", None, None) self.settings["connector"]["status_service"] = "testsvc" sp = statuspublisher_factory(self.settings, client) sp.isConnected = lambda: True client.stub_connect() stats = {"key1": "value1", "key2": "value2", "key3": "value3"} msg = {"type": "perf"} sp._sendStats(stats, msg) def check(r_): output = client.channel.sent print output self.assertEqual(len(output), 3) msg_out = [ json.loads(m["content"].body) for m in output ] msg_in = [] for k, v in stats.iteritems(): m = msg.copy() m.update({"datasource": "testsvc-%s" % k, "value": v}) msg_in.append(m) self.assertEqual(msg_in, msg_out) d = wait(0.2) d.addCallback(check) return d
def test_force_exchange(self): """On force le nom de l'exchange à utiliser pour un message de perf""" client = ClientStub("testhost", None, None) self.settings["connector"]["self_monitoring_perf_exchange"] = "foo" self.settings["connector"]["self_monitoring_nagios_exchange"] = "foo" self.settings["connector"]["status_service"] = "dummyservice" sp = statuspublisher_factory(self.settings, client, [ProviderStub()]) sp.isConnected = lambda: True d = client.stub_connect() d.addCallback(lambda _x: sp.sendStatus()) def check(r): output = client.channel.sent for msg in output: print msg self.assertEqual(msg["exchange"], "foo") d.addCallback(check) return d
def test_servicename(self): """On force le nom du service à utiliser""" client = ClientStub("testhost", None, None) self.settings["connector"]["hostname"] = "changedhost" self.settings["connector"]["status_service"] = "changedsvc" sp = statuspublisher_factory(self.settings, client, [ProviderStub()]) sp.isConnected = lambda: True d = client.stub_connect() d.addCallback(lambda _x: sp.sendStatus()) def check(r): output = client.channel.sent msg_perf = json.loads(output[0]["content"].body) self.assertEqual(msg_perf["type"], "perf") self.assertEqual(msg_perf["datasource"], "changedsvc-dummykey") msg_cmd = json.loads(output[1]["content"].body) self.assertEqual(msg_cmd["type"], "nagios") self.assertTrue(msg_cmd["value"].startswith( "changedhost;changedsvc;")) d.addCallback(check) return d
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
sys.exit(1) root_service = service.MultiService() client = client_factory(settings) client.setServiceParent(root_service) # Configuration nagiosconf = nagiosconffile_factory(settings) nagiosconf.setServiceParent(root_service) # Nagios vers le bus : # socket_listener -> backup_provider -> bus_publisher socket_listener = socketlistener_factory(socket_filename) backup_provider = backupprovider_factory(settings, socket_listener) backup_provider.setServiceParent(root_service) bus_publisher = buspublisher_factory(settings, client) bus_publisher.registerProducer(backup_provider, True) # Du bus vers Nagios ncmdh = nagioscmdh_factory(settings, client, nagiosconf) # Statistiques from vigilo.connector.status import statuspublisher_factory statuspublisher_factory(settings, client, providers=[bus_publisher, backup_provider, ncmdh]) return root_service