summaryrefslogtreecommitdiffstats
path: root/munin/publik_count
blob: 6f61867882ea22d9cce1f1df367a7b93508374fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /usr/bin/env python
#%# family=auto
#%# capabilities=autoconf

import cPickle
import os
import psycopg2
import itertools
import sys

base_dir = None
for dirname in os.listdir('/var/lib/machines/'):
    if os.path.exists(os.path.join('/var/lib/machines/', dirname, 'var/lib/wcs')):
        base_dir = os.path.join('/var/lib/machines/', dirname, 'var/lib/wcs')
        break

if len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
    if base_dir:
        print 'yes'
    else:
        print 'no'
    sys.exit(0)

if len(sys.argv) == 2 and sys.argv[1] == 'config':
    print '''graph_title Count of submitted forms
graph_category entrouvert
count.label Count
'''
    sys.exit(0)

total = 0

for tenant in itertools.chain(os.listdir(base_dir), os.listdir(os.path.join(base_dir, 'tenants')):
    if tenant in ('collectstatic', 'scripts', 'skeletons', 'spooler', 'tenants'):
        continue
    if tenant.endswith('.invalid'):
        continue
    if not os.path.isdir(os.path.join(base_dir, tenant)):
        continue
    cfg = cPickle.load(open(os.path.join(base_dir, tenant, 'config.pck')))
    if not 'postgresql' in cfg:
        continue
    psql_cfg = {}
    for k, v in cfg['postgresql'].items():
        if v and isinstance(v, basestring):
            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:
        total += cur.fetchone()[0]
    pgconn.close()

print 'count.value', total