-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_for_issue.py
117 lines (97 loc) · 3.88 KB
/
check_for_issue.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
import sys
import time
import json
import subprocess
sys.path.append('/vagrant/mgervais/')
import icingadb
import icinga2api
db = icingadb.IcingaDb()
api = icinga2api.Icinga2Api()
"""
pattern for host and service, e.g. examplehost
BEWARE - do not change pattern without using a fresh the vagrant box
'orphaned' objects like 'the pattern from last try' might screw up this count of db objects
"""
host_pattern = 'examplehost'
service_pattern = 'seriouservice'
host_count = 50
service_count = 20
if len(sys.argv) > 1 and sys.argv[1] == 'lazy':
print "lazy mode, will reload icinga after every 10th run"
me_so_lazy = True
else:
me_so_lazy = False
# API selfcheck
api.apiCheck()
print "---"
run = 0
while True:
start = time.time()
run += 1
print 'this is run %i' % run
# get hosts known in API
api_configured_hosts = api.rGet('/objects/hosts')
api_configured_hosts = json.loads(api_configured_hosts.text)
api_host_count = len(api_configured_hosts['results'])
print 'found %i host(s) in API - deleting...' % api_host_count
if api_host_count != 0:
# try to delete all hosts by api with cascade=1
for h in api_configured_hosts['results']:
d = api.rDeleteHostCascade(h['name'])
api_host_count -= 1
sys.stdout.write("\r\t%i\thost(s) left" % api_host_count)
sys.stdout.flush()
print '\ndeleted %i host(s) from API' % len(api_configured_hosts['results'])
# generate hosts & services
print 'creating %i host(s) with %i service(s) each in API' % (host_count, service_count)
api.genRandomHostsServices( host_pattern, host_count, service_pattern, service_count)
# get hosts known in API
api_configured_hosts = api.rGet('/objects/hosts')
api_configured_hosts = json.loads(api_configured_hosts.text)
# get services known in API
api_configured_services = api.rGet('/objects/services')
api_configured_services = json.loads(api_configured_services.text)
api_object_count = len(api_configured_hosts['results']) + len(api_configured_services['results'])
res = db.queryName1PatternIsActive(host_pattern + '%', 1)
db_object_count = len(res)
count = 0
max_loops = 10
print 'api says %i objects, icinga.icinga_objects reports %i' % (api_object_count, db_object_count)
if api_object_count < db_object_count:
print 'this is unexpected - there should not be more objects in icinga.icinga_objects than in API'
print 'maybe clear the DB or just do `vagrant -f destroy && vagrant up`'
sys.exit(1)
while api_object_count != db_object_count:
print "waiting for lost objects... "
count += 1
res = db.queryName1PatternIsActive(host_pattern + '%', 1)
db_object_count = len(res)
print "got %i objects in icinga.icinga_objects" % db_object_count
if api_object_count == db_object_count:
continue
if count < max_loops:
sleep = count
print "[%i/%i] sleep for %i second(s) for ido's grace" % (count, max_loops, sleep)
time.sleep(sleep)
elif count is max_loops:
sleep = 90
print "[extra] sleep for %i seconds for ido's grace" % sleep
time.sleep(sleep)
else:
print 'finally, an error in run %i' % run
print 'api says %i objects, icinga.icinga_objects reports %i' % (api_object_count, db_object_count)
sys.exit(1)
end = time.time()
print 'all is good in just %s seconds' % (end - start)
# feeling lazy? allright reload icinga2 every n run
if run%10 == 0 and me_so_lazy:
try:
subprocess.call(['systemctl', 'reload', 'icinga2'])
print "this is run number %i - reloaded icinga2" % run
time.sleep(2)
api.apiCheck()
except Exception as e:
print e
sys.exit(1)
print "---"