132 lines
4.1 KiB
Python
Executable File
132 lines
4.1 KiB
Python
Executable File
#! /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
|
|
)
|
|
publik_afterjobs = Gauge('eo_publik_afterjobs', 'publik_afterjobs', ['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.startswith('.')
|
|
or tenant.startswith('_')
|
|
or tenant.endswith('.invalid')
|
|
or tenant
|
|
in (
|
|
'collectstatic',
|
|
'cron-logs',
|
|
'scripts',
|
|
'skeletons',
|
|
'spooler',
|
|
'tenants',
|
|
)
|
|
):
|
|
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()
|
|
|
|
afterjobs_dir = os.path.join(dirname, 'afterjobs')
|
|
publik_afterjobs.labels(tenant).set(
|
|
len(os.listdir(afterjobs_dir)) if os.path.exists(afterjobs_dir) else 0
|
|
)
|
|
|
|
dep_types = [
|
|
'ezt',
|
|
'jsonp',
|
|
'python-condition',
|
|
'python-expression',
|
|
'python-prefill',
|
|
'python-data-source',
|
|
'script',
|
|
'rtf',
|
|
'actions',
|
|
'fields',
|
|
'csv-connector',
|
|
'json-data-store',
|
|
]
|
|
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())
|