-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdbtest.py
48 lines (35 loc) · 1.57 KB
/
dbtest.py
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
# required dictionary
options = {
# <name> <value> <default value> <required> <description>
'DBSTORE_ENGINE': ('', 'sqlite', 'Yes', 'Database engine to use (PostgreSQL, Sqlite, etc.)'),
'DBSTORE_FILENAME': ('', ':memory:', 'Yes', 'Filename of the database store file'),
'DEFAULT_TIMEOUT': ('', '2', 'No', 'Default timeouts on queries')
}
"initializing testDB plugin"
from itertools import count
import time
app = None
# ------------
# Plugin commands
def do_produce_hosts(*_):
"""example: add 100 host entries to the database"""
# create hosts
try:
for n in xrange(0, 256):
ipstring = '192.168.0.%d' % n
app.print_line("add %r %r" % (ipstring, 'my.own.website'))
app.db.Host.add(app.db.get_session(), hostip=ipstring, hostname='my.own.website')
time.sleep(5)
except Exception, e:
app.print_line('do_produce_hosts(): Exception: {!r}'.format(e))
def do_consume_hosts(*_):
"""hang around and print host entries as they arrive"""
try:
for item in app.db.Host.get(app.db.get_session(), block=True, timeout=6):
app.print_line('consume_hosts(): consumed host %r %r' % (item.hostip, item.hostname))
except Exception, e:
print('consume_hosts(): Exception: {}'.format(e))
def do_count_hosts(*_):
"""print a count of hosts in the database"""
session = app.db.get_session()
app.print_line('{} hosts in the database'.format(app.db.Host.count(session)))