add prometheus-publik-exporter (but do not run it automatically) (#84518)
gitea/publik-infra/pipeline/head This commit looks good Details

This commit is contained in:
Frédéric Péters 2023-12-15 10:25:13 +01:00
parent a9f9281bcf
commit 74711c464f
1 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,118 @@
#! /usr/bin/python3
import datetime
import itertools
import json
import os
import pickle
import sys
import psycopg2
from prometheus_client import CollectorRegistry, Gauge
from prometheus_client.exposition import generate_latest
base_dir = '/var/lib/wcs'
registry = CollectorRegistry()
publik_count = Gauge('eo_publik_count', 'publik_count', ['site'], registry=registry)
publik_recent_count = Gauge(
'eo_publik_recent_count', 'publik_recent_count', ['period', 'site'], registry=registry
)
publik_deprecations = Gauge(
'eo_publik_deprecations', 'publik_deprecations', ['type', 'site'], registry=registry
)
publik_loggederrors = Gauge('eo_publik_loggederrors', 'publik_loggederrors', ['site'], registry=registry)
publik_loggederrors_uniq = Gauge(
'eo_publik_loggederrors_uniq', 'publik_loggederrors_uniq', ['site'], registry=registry
)
time_5m = datetime.datetime.now() - datetime.timedelta(minutes=5)
time_1h = datetime.datetime.now() - datetime.timedelta(hours=1)
time_1d = datetime.datetime.now() - datetime.timedelta(days=1)
now = datetime.datetime.now()
for tenant in itertools.chain(os.listdir(base_dir), os.listdir(os.path.join(base_dir, 'tenants'))):
if tenant in (
'collectstatic',
'cron-logs',
'scripts',
'skeletons',
'spooler',
'tenants',
'.cache',
'.config',
):
continue
if tenant.endswith('.invalid'):
continue
dirname = os.path.join(base_dir, tenant)
if not os.path.exists(dirname):
dirname = os.path.join(base_dir, 'tenants', tenant)
if not os.path.isdir(dirname):
continue
cfg = pickle.load(open(os.path.join(dirname, 'config.pck'), 'rb'), encoding='utf-8')
if 'postgresql' not in cfg:
continue
psql_cfg = {}
for k, v in cfg['postgresql'].items():
if v and isinstance(v, (int, str)):
psql_cfg[k] = v
pgconn = psycopg2.connect(**psql_cfg)
cur = pgconn.cursor()
try:
cur.execute('''SELECT COUNT(*) from wcs_all_forms WHERE status != 'draft' ''')
except psycopg2.ProgrammingError:
pass
else:
count = cur.fetchone()[0]
publik_count.labels(tenant).set(count)
for period, time in zip(('5m', '1h', '1d'), (time_5m, time_1h, time_1d)):
cur.execute(
'''SELECT COUNT(*) from wcs_all_forms
WHERE status != 'draft'
AND receipt_time > %(time)s
AND receipt_time < %(now)s''',
{'time': time, 'now': now},
)
publik_recent_count.labels(period, tenant).set(cur.fetchone()[0])
try:
cur.execute('''SELECT SUM(occurences_count) FROM loggederrors''')
except psycopg2.ProgrammingError:
pass
else:
count = cur.fetchone()[0]
publik_loggederrors.labels(tenant).set(count or 0)
try:
cur.execute('''SELECT COUNT(*) FROM loggederrors''')
except psycopg2.ProgrammingError:
pass
else:
count = cur.fetchone()[0]
publik_loggederrors_uniq.labels(tenant).set(count)
pgconn.close()
dep_types = [
'ezt',
'jsonp',
'python-condition',
'python-expression',
'python-prefill',
'python-data-source',
'script',
'rtf',
]
dep_filepath = os.path.join(dirname, 'deprecations.json')
if os.path.exists(dep_filepath):
with open(dep_filepath) as fp:
deps = json.load(fp)
for dep_type in dep_types:
publik_deprecations.labels(dep_type, tenant).set(
len([x for x in deps['report_lines'] if x['category'] == dep_type])
)
print(generate_latest(registry).decode())