Skip to content
Snippets Groups Projects
Commit 179c9cde authored by psy's avatar psy
Browse files

use config file instead of env vars, add config option for bind address

parent 03eda25c
No related branches found
No related tags found
No related merge requests found
.idea/
venv/
config.ini
__pycache__/
BIND_ADDR = ::
PORT = 8337
SYNAPSE_URL =
ACCESS_TOKEN =
\ No newline at end of file
#! /usr/bin/env python
import os
import requests
from prometheus_client import start_http_server, Gauge
from time import sleep
import argparse
metric_members = Gauge('matrix_channel_members_total', 'Members joined in room', ['room_id', 'canonical_alias', 'name'])
metric_channel_types = Gauge('matrix_channel_types_total', 'Channels per type', ['type'])
metric_channel_version = Gauge('matrix_channel_version_total', 'Channels per version', ['version'])
metric_channel_state_events = Gauge('matrix_channel_state_events_total', 'State events per room', ['room_id', 'canonical_alias', 'name'])
metric_channel_state_events = Gauge('matrix_channel_state_events_total', 'State events per room',
['room_id', 'canonical_alias', 'name'])
def fetch_metrics():
def fetch_metrics(synapse_url, access_token):
room_type_count = {
'space': 0,
'public': 0,
'private': 0,
'dm': 0
}
room_version_count = {}
auth = {
'Authorization': f"Bearer {config['token']}"
'Authorization': f"Bearer {access_token}"
}
room_version_count = {}
rooms = requests.get(f"https://{config['synapse_url']}/_synapse/admin/v1/rooms", headers=auth).json()['rooms']
for room in sorted(rooms, key=lambda item: item['joined_members'], reverse=True):
rooms = requests.get(f"https://{synapse_url}/_synapse/admin/v1/rooms", headers=auth).json()['rooms']
for room in rooms:
if room['version'] not in room_version_count:
room_version_count[room['version']] = 0
......@@ -59,18 +60,31 @@ def fetch_metrics():
metric_channel_types.labels(type=room_type).set(count)
def parse_config(config):
config_parsed = {}
for line in config.splitlines():
if not line.startswith('#') and len(line) > 0:
parts = line.split('=', 1)
config_parsed[parts[0].strip()] = parts[1].strip()
return config_parsed
if __name__ == '__main__':
config = {
'port': int(os.environ.get('PORT')),
'token': os.environ.get('ACCESS_TOKEN'),
'synapse_url': os.environ.get('SYNAPSE_URL')
}
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config',
default='config.ini',
type=argparse.FileType('r'))
args = parser.parse_args()
config = parse_config(args.config.read())
start_http_server(config['port'], addr='::')
start_http_server(int(config['PORT']), addr=config['BIND_ADDR'])
while True:
try:
fetch_metrics()
fetch_metrics(config['SYNAPSE_URL'], config['ACCESS_TOKEN'])
except Exception as e:
print('Exception:\n' + str(e))
sleep(20)
requests
prometheus-client
argparse
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment