Ejemplo n.º 1
0
    def post(self, url, body):
        data_json = {}
        try:
            data_from_monitor = requests.post(
                url,
                auth=HTTPBasicAuth(self.user, self.passwd),
                verify=False,
                headers={
                    'Content-Type': 'application/json',
                    'X-HTTP-Method-Override': 'GET'
                },
                data=json.dumps(body),
                timeout=self.timeout)
            data_json = json.loads(data_from_monitor.content)
            log.debug('API call: ' + data_from_monitor.url)
            data_from_monitor.raise_for_status()

            if data_from_monitor.status_code != 200 and data_from_monitor.status_code != 201:
                log.warn("Not a valid response - {}:{}".format(
                    str(data_from_monitor.content),
                    data_from_monitor.status_code))
            else:
                log.info(
                    "call api {}".format(url), {
                        'status': data_from_monitor.status_code,
                        'response_time':
                        data_from_monitor.elapsed.total_seconds()
                    })
        except requests.exceptions.RequestException as err:
            log.error("{}".format(str(err)))

        return data_json
Ejemplo n.º 2
0
def after_request_func(response):
    total_requests.inc()

    call_status = {
        'remote_addr': request.remote_addr,
        'url': request.url,
        'user_agent': request.user_agent,
        'content_length': response.content_length,
        'status': response.status_code
    }
    log.info('Access', call_status)

    return response
Ejemplo n.º 3
0
async def get_metrics():
    #log.info(request.url)
    target = request.args.get('target')

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

    # Fetch performance data from Monitor
    start_time = time.monotonic()
    try:
        loop = asyncio.get_event_loop()
        fetch_perfdata_task = loop.create_task(
            monitor_data.get_service_metrics())

        if monitorconnection.MonitorConfig().get_enable_scrape_metadata():
            fetch_metadata_task = loop.create_task(
                monitor_data.get_host_metrics())
            await fetch_metadata_task

        await fetch_perfdata_task

        scrape_duration = time.monotonic() - start_time
        monitor_data.add_perfdata(
            "scrape_duration_seconds", {
                'hostname': target,
                'server': monitorconnection.MonitorConfig().get_url()
            }, scrape_duration)
        log.info("scrape", {
            'target': target,
            'url': request.url,
            'scrape_time': scrape_duration
        })
        target_metrics = monitor_data.prometheus_format()

        resp = Response(target_metrics)
        resp.headers['Content-Type'] = CONTENT_TYPE_LATEST
        # after_request_func(resp)
        return resp
    except monitorconnection.ScrapeExecption as err:
        log.warn(
            f"{err.message}", {
                'target': target,
                'url': request.url,
                'remote_url': err.url,
                'err': err.err
            })
        resp = Response("")
        resp.status_code = 500
        return resp
Ejemplo n.º 4
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 = 9638

    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 = Quart(__name__)
    app.register_blueprint(icinga2, url_prefix='')

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

    app.run(host='0.0.0.0', port=port)
Ejemplo n.º 5
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)

    monitorconnection.MonitorConfig(configuration)

    app = Quart(__name__)
    app.register_blueprint(icinga2, url_prefix='')

    log.configure_logger(configuration)
    log.info('Starting web app')

    return app