Example #1
0
 def test_singleton(self):
     mcon1 = Monitor.MonitorConfig(self.config)
     mcon2 = Monitor.MonitorConfig()
     self.assertEqual(mcon2.get_url(), 'https://monitor.xyz')
     # Make sure it same object - singleton
     self.assertEqual(id(mcon1), id(mcon2))
     mcon2 = Monitor.MonitorConfig(self.config)
     self.assertNotEqual(id(mcon1), id(mcon2))
Example #2
0
def start_scheduler(configuration):

    if 'cache' in configuration:
        scheduler = AsyncIOScheduler()
        seconds = default_interval if configuration.get('cache').get('interval') is None \
            else configuration.get('cache').get('interval')
        ttl = default_ttl if configuration.get('cache').get('ttl') is None \
            else configuration.get('cache').get('ttl')
        log.info(f"Monitor collector will run every {seconds} sec")
        # Run once at start up
        monitorconnection.MonitorConfig().collect_cache(ttl)
        scheduler.add_job(monitorconnection.MonitorConfig().collect_cache, trigger='interval', args=[ttl], seconds=seconds)
        scheduler.start()
Example #3
0
def start():
    """
    Used from __main__ to start as simple flask app
    :return:
    """
    parser = argparse.ArgumentParser(description='monitor_exporter')

    parser.add_argument('-f',
                        '--configfile',
                        dest="configfile",
                        help="configuration file")

    parser.add_argument('-p', '--port', dest="port", help="Server port")

    args = parser.parse_args()

    port = 9631

    config_file = 'config.yml'
    if args.configfile:
        config_file = args.configfile

    configuration = config.read_config(config_file)
    if 'port' in configuration:
        port = configuration['port']

    if args.port:
        port = args.port

    log.configure_logger(configuration)

    monitorconnection.MonitorConfig(configuration)
    # Need to create a event loop for apscheduler
    loop = asyncio.new_event_loop()
    # Set the event loop
    asyncio.set_event_loop(loop)

    start_scheduler(configuration)

    log.info(f"Starting web app on port {port}")

    app = Quart(__name__)

    app.register_blueprint(proxy.app, url_prefix='')

    # Use the existing event loop
    app.run(host='0.0.0.0', port=port, loop=loop)
Example #4
0
async def get_metrics():
    before_request_func(request)
    target = request.args.get('target')

    log.info('Collect metrics', {'target': target})

    monitor_data = Perfdata(monitorconnection.MonitorConfig(), target)

    # Fetch performance data from Monitor
    await asyncio.get_event_loop().create_task(monitor_data.get_perfdata())

    target_metrics = monitor_data.prometheus_format()

    resp = Response(target_metrics)
    resp.headers['Content-Type'] = CONTENT_TYPE_LATEST

    return resp
Example #5
0
def get_metrics():
    log.info(request.url)
    target = request.args.get('target')

    log.info('Collect metrics', {'target': target})

    monitor_data = Perfdata(monitorconnection.MonitorConfig(), target)

    # Fetch performance data from Monitor
    monitor_data.get_perfdata()

    target_metrics = monitor_data.prometheus_format()

    resp = Response(target_metrics)
    resp.headers['Content-Type'] = CONTENT_TYPE_LATEST

    return resp
Example #6
0
    def test_get_perfdata_from_monitor(self):
        mcon = Monitor.MonitorConfig(self.config)

        # Create Mock
        mcon.get_host_data = AsyncMock(return_value=get_perf_mock_file())


        perf = perfdata.Perfdata(mcon, 'google.se')

        # Get the data from Monitor
        monitor_data = _run(perf.get_perfdata())
        self.assertEqual(len(monitor_data), 11)

        self.assertTrue(
            'monitor_host_state{hostname="google.se", environment="production"}' in monitor_data)
        self.assertTrue(
            'monitor_check_disk_local_mb_slash_bytes{hostname="google.se", service="disk_root", environment="production"}' in monitor_data)
        self.assertTrue(
            'monitor_check_ping_rta_seconds{hostname="google.se", service="pingit", environment="production"}' in monitor_data)
    def test_get_perfdata_from_monitor(self):
        mcon = Monitor.MonitorConfig(self.config)

        # Create Mock
        mcon.get_perfdata = MagicMock(return_value=get_perf_mock_file())
        mcon.get_host_custom_vars = MagicMock(return_value=get_custom_vars_mock_file())

        perf = perfdata.Perfdata(mcon, 'dn.se')

        # Get the data from Monitor
        monitor_data = perf.get_perfdata()
        self.assertEqual(len(monitor_data), 3)

        self.assertTrue(
            'monitor_check_ping_rta_seconds{hostname="dn.se", service="PING", environment="prod", dc="sto"}' in monitor_data)
        self.assertTrue(
            'monitor_check_ping_pl_ratio{hostname="dn.se", service="PING", environment="prod", dc="sto"}' in monitor_data)
        self.assertTrue(
            'monitor_check_tcp_time_seconds{hostname="dn.se", service="https", environment="prod", dc="sto"}' in monitor_data)
Example #8
0
def start():
    """
    Used from __main__ to start as simple flask app
    :return:
    """
    parser = argparse.ArgumentParser(description='monitor_exporter')

    parser.add_argument('-f',
                        '--configfile',
                        dest="configfile",
                        help="configuration file")

    parser.add_argument('-p', '--port', dest="port", help="Server port")

    args = parser.parse_args()

    port = 9631

    config_file = 'config.yml'
    if args.configfile:
        config_file = args.configfile

    configuration = config.read_config(config_file)
    if 'port' in configuration:
        port = configuration['port']

    if args.port:
        port = args.port

    log.configure_logger(configuration)
    ##

    monitorconnection.MonitorConfig(configuration)
    log.info('Starting web app on port: ' + str(port))

    app = Flask(__name__)

    app.register_blueprint(proxy.app, url_prefix='/')
    app.run(host='0.0.0.0', port=port)
Example #9
0
def create_app(config_path=None):
    """
    Used typical from gunicorn if need to pass config file different from default, e.g.
    gunicorn -b localhost:5000 --access-logfile /dev/null -w 4 "wsgi:create_app('/tmp/config.yml')"
    :param config_path:
    :return:
    """
    config_file = 'config.yml'
    if config_path:
        config_file = config_path

    configuration = config.read_config(config_file)

    log.configure_logger(configuration)

    monitorconnection.MonitorConfig(configuration)
    log.info('Starting web app')

    app = Flask(__name__)

    app.register_blueprint(proxy.app, url_prefix='/')

    return app
Example #10
0
 def test_get_perfdata_from_monitor(self, mock_get):
     mcon = Monitor.MonitorConfig(self.config)
     response = mcon.get('sunet.se')
     self.assertEqual(response, {"TEST": "test"})
Example #11
0
 def test_with_config(self):
     mcon = Monitor.MonitorConfig(self.config)
     self.assertEqual(mcon.get_url(), 'https://monitor.xyz')
Example #12
0
 def test_default_config(self):
     mcon = Monitor.MonitorConfig()
     self.assertEqual(mcon.get_url(), '')