Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Živković committed Apr 18, 2017
1 parent ed63ede commit 54b0f6b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# tinyproxy_exporter
Prometheus exporter for Tinyproxy
# Tinyproxy exporter
Prometheus exporter for Tinyproxy, written in Python.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prometheus-client==0.0.19
58 changes: 58 additions & 0 deletions tinyproxy_exporter
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

import argparse
import re
import sys
import time
import urllib

from prometheus_client import start_http_server
from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily, REGISTRY

class TinyproxyCollector(object):
def __init__(self, stathost, tinyproxy):
self.stathost = stathost
self.tinyproxy = tinyproxy

def collect(self):
handler = urllib.request.ProxyHandler({'http': self.tinyproxy})
opener = urllib.request.build_opener(handler)
urllib.request.install_opener(opener)
response = urllib.request.urlopen('http://{0}'.format(self.stathost))
values = [float(x) for x in re.findall(b'>(\d+).+td', response.read())]
yield GaugeMetricFamily('tinyproxy_open_connections', 'Number of open connections', values[0])
yield CounterMetricFamily('tinyproxy_requests', 'Number of requests', values[1])
yield CounterMetricFamily('tinyproxy_bad_connections', 'Number of bad connections', values[2])
yield CounterMetricFamily('tinyproxy_denied_connections', 'Number of denied connections', values[3])
yield CounterMetricFamily('tinyproxy_refused_connections', 'Number of refused connections due to high load', values[4])

def parse_args():
parser = argparse.ArgumentParser(description='Prometheus exporter for Tinyproxy.')
parser.add_argument(
'-l', metavar='LISTEN', default=':9240',
help='address on which to expose metrics (default ":9240")'
)
parser.add_argument(
'-s', metavar='STATHOST', default='tinyproxy.stats',
help='internal statistics page address (default "tinyproxy.stats")'
)
parser.add_argument(
'-t', metavar='TINYPROXY', default='127.0.0.1:8888',
help='tinyproxy address (default "127.0.0.1:8888")'
)
return parser.parse_args()

def main():
try:
args = parse_args()
addr, port = args.l.split(':')
REGISTRY.register(TinyproxyCollector(args.s, args.t))
start_http_server(int(port), addr)
while True:
time.sleep(1)
except KeyboardInterrupt:
print(' Interrupted')
sys.exit(0)

if __name__ == '__main__':
main()

0 comments on commit 54b0f6b

Please sign in to comment.