diff --git a/mprom/BUILD b/mprom/BUILD new file mode 100644 index 0000000..8052e8c --- /dev/null +++ b/mprom/BUILD @@ -0,0 +1,20 @@ +# vim: syntax=python:expandtab:shiftwidth=2:softtabstop=2:tabstop=2 +python_binary( + name='mumble-prometheus', + dependencies=[ + ':murmur', + ':ext-deps', + ], + source='server.py', +) +python_library( + name='murmur', + sources=['Murmur.py', 'Murmur_ice.py'], +) +python_requirement_library( + name='ext-deps', + requirements=[ + python_requirement('prometheus-client>=0.0.13'), + python_requirement('zeroc-ice>=3.6.2.1'), + ] +) diff --git a/Murmur/__init__.py b/mprom/Murmur.py similarity index 100% rename from Murmur/__init__.py rename to mprom/Murmur.py diff --git a/Murmur_ice.py b/mprom/Murmur_ice.py similarity index 100% rename from Murmur_ice.py rename to mprom/Murmur_ice.py diff --git a/mprom/server.py b/mprom/server.py index 39a8e25..9f2cb2f 100644 --- a/mprom/server.py +++ b/mprom/server.py @@ -5,15 +5,26 @@ import sys import prometheus_client as node from contextlib import contextmanager -import argparse, time +import argparse, time, logging import Ice, Murmur +logger = logging.getLogger('mumble-prometheus') @contextmanager -def ice_connect(host, port): +def ice_connect(host, port, secret=None): prxstr = "Meta:tcp -h %s -p %d -t 1000" % (host, port) - ic = Ice.initialize(sys.argv) + props = Ice.createProperties(sys.argv) + props.setProperty("Ice.ImplicitContext", "Shared") + props.setProperty('Ice.Default.EncodingVersion', '1.0') + + idata = Ice.InitializationData() + idata.properties = props + + ic = Ice.initialize(idata) + if secret: + ic.getImplicitContext().put("secret", secret) + base = ic.stringToProxy(prxstr) meta = Murmur.MetaPrx.checkedCast(base) if not meta: @@ -32,6 +43,8 @@ def main(): parser.add_argument('-H', '--host', help='Host of the Ice interface', default='127.0.0.1') parser.add_argument('-p', '--port', help='Port of the Ice interface', default=6502, type=int) parser.add_argument('-i', '--interval', help='Interval in seconds', default=60, type=int) + parser.add_argument('--secret', help='The read secret', default=None) + parser.add_argument('-v', '--verbose', help='Verbose', action='store_true') args = parser.parse_args() node.start_http_server(args.listen) @@ -43,18 +56,28 @@ def main(): ['ice_server', 'server_id']), } - ice_server = '%s:%d' % (ags.host, args.port) - while True: - t1 = time.time() - with ice_connect(args.host, args.port) as meta: - for server in meta.getBootedServers(): - labels = {'server_id': server.id(), 'ice_server': ice_server} - gauges['users'].labels(labels).set(len(server.getUsers())) - gauges['uptime'].labels(labels).set(server.getUptime()) + if args.verbose: + logging.basicConfig(level=logging.DEBUG) + else: + logging.basicConfig(level=logging.INFO) - time_to_wait = args.interval - (time.time() - t1) - if time_to_wait > 0: - time.sleep(time_to_wait) + ice_server = '%s:%d' % (args.host, args.port) + with ice_connect(args.host, args.port, args.secret) as meta: + while True: + logger.info('gathering info') + t1 = time.time() + for server in meta.getBootedServers(): + g_user = len(server.getUsers()) + g_uptime = server.getUptime() + logger.debug('mumble_user_connected: %d' % g_user) + logger.debug('mumble_uptime: %d' % g_uptime) + labels = {'server_id': server.id(), 'ice_server': ice_server} + gauges['users'].labels(labels).set(g_user) + gauges['uptime'].labels(labels).set(g_uptime) + + time_to_wait = args.interval - (time.time() - t1) + if time_to_wait > 0: + time.sleep(time_to_wait) return 0 if __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..71bfc6b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +prometheus-client==0.0.13 +zeroc-ice==3.6.2.1