Skip to content
Snippets Groups Projects
Commit 472bad8b authored by psy's avatar psy
Browse files

initial

parents
Branches
No related tags found
No related merge requests found
.idea/
venv/
\ 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
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'])
def fetch_metrics():
room_type_count = {
'space': 0,
'public': 0,
'private': 0,
'dm': 0
}
auth = {
'Authorization': f'Bearer {config['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):
if room['version'] not in room_version_count:
room_version_count[room['version']] = 0
room_version_count[room['version']] += 1
if room['room_type'] == 'm.space':
room_type_count['space'] += 1
elif room['join_rules'] == 'public':
room_type_count['public'] += 1
elif room['name'] and room['join_rules'] == 'invite':
room_type_count['private'] += 1
else:
room_type_count['dm'] += 1
metric_members.labels(
room_id=room['room_id'],
canonical_alias=room['canonical_alias'] if room['canonical_alias'] else '',
name=room['name'] if room['name'] else ''
).set(room['joined_members'])
for room_version, count in room_version_count.items():
metric_channel_version.labels(version=room_version).set(count)
for room_type, count in room_type_count.items():
metric_channel_types.labels(type=room_type).set(count)
if __name__ == '__main__':
config = {
'port': int(os.environ.get('PORT')),
'token': os.environ.get('ACCESS_TOKEN'),
'synapse_url': os.environ.get('SYNAPSE_URL')
}
start_http_server(config['port'])
while True:
try:
fetch_metrics()
except Exception as e:
print('Exception:\n' + str(e))
sleep(20)
requests
prometheus-client
\ 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