#!/usr/bin/env python3

from sys import argv, stderr

from requests import get

try:
    r = get('http://127.0.0.1:11334/stat')
    r.raise_for_status()

    json = r.json()

    actions = set()
    for k, v in json['actions'].items():
        actions.add('{}={}i'.format(k.replace(' ', '_'), v))

    print('rspamd_actions {}'.format(','.join(sorted(actions))))

    stats = set()
    for i in {
        'scanned',
        'learned',
        'spam_count',
        'ham_count',
        'connections',
        'control_connections',
        'pools_allocated',
        'pools_freed',
        'bytes_allocated',
        'chunks_allocated',
        'shared_chunks_allocated',
        'chunks_freed',
        'chunks_oversized',
        'fragmented',
        'total_learns',
    }:
        stats.add('{}={}i'.format(i, json[i]))

    print('rspamd_stats {}'.format(','.join(sorted(stats))))

    for domain, value in json['fuzzy_hashes'].items():
        print('rspamd_fuzzy,domain={} value={}i'.format(domain, value))
except Exception as e:
    print(repr(e), file=stderr)