|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import time |
| 7 | +import urllib |
| 8 | + |
| 9 | +from prometheus_client import start_http_server |
| 10 | +from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily, REGISTRY |
| 11 | + |
| 12 | +class TinyproxyCollector(object): |
| 13 | + def __init__(self, stathost, tinyproxy): |
| 14 | + self.stathost = stathost |
| 15 | + self.tinyproxy = tinyproxy |
| 16 | + |
| 17 | + def collect(self): |
| 18 | + handler = urllib.request.ProxyHandler({'http': self.tinyproxy}) |
| 19 | + opener = urllib.request.build_opener(handler) |
| 20 | + urllib.request.install_opener(opener) |
| 21 | + response = urllib.request.urlopen('http://{0}'.format(self.stathost)) |
| 22 | + values = [float(x) for x in re.findall(b'>(\d+).+td', response.read())] |
| 23 | + yield GaugeMetricFamily('tinyproxy_open_connections', 'Number of open connections', values[0]) |
| 24 | + yield CounterMetricFamily('tinyproxy_requests', 'Number of requests', values[1]) |
| 25 | + yield CounterMetricFamily('tinyproxy_bad_connections', 'Number of bad connections', values[2]) |
| 26 | + yield CounterMetricFamily('tinyproxy_denied_connections', 'Number of denied connections', values[3]) |
| 27 | + yield CounterMetricFamily('tinyproxy_refused_connections', 'Number of refused connections due to high load', values[4]) |
| 28 | + |
| 29 | +def parse_args(): |
| 30 | + parser = argparse.ArgumentParser(description='Prometheus exporter for Tinyproxy.') |
| 31 | + parser.add_argument( |
| 32 | + '-l', metavar='LISTEN', default=':9240', |
| 33 | + help='address on which to expose metrics (default ":9240")' |
| 34 | + ) |
| 35 | + parser.add_argument( |
| 36 | + '-s', metavar='STATHOST', default='tinyproxy.stats', |
| 37 | + help='internal statistics page address (default "tinyproxy.stats")' |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + '-t', metavar='TINYPROXY', default='127.0.0.1:8888', |
| 41 | + help='tinyproxy address (default "127.0.0.1:8888")' |
| 42 | + ) |
| 43 | + return parser.parse_args() |
| 44 | + |
| 45 | +def main(): |
| 46 | + try: |
| 47 | + args = parse_args() |
| 48 | + addr, port = args.l.split(':') |
| 49 | + REGISTRY.register(TinyproxyCollector(args.s, args.t)) |
| 50 | + start_http_server(int(port), addr) |
| 51 | + while True: |
| 52 | + time.sleep(1) |
| 53 | + except KeyboardInterrupt: |
| 54 | + print(' Interrupted') |
| 55 | + sys.exit(0) |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + main() |
0 commit comments