-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.py
executable file
·61 lines (55 loc) · 2.32 KB
/
wiki.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
#!/usr/bin/env python
# Must use Python 2.x
"""Import list of servers and services from Icinga and make pages for a Wiki.
It will overwrite existing pages."""
import json
import urllib2
# See http://sourceforge.net/apps/mediawiki/mwclient/index.php?title=Main_Page
import mwclient
# Edit these
wikiusername = "admin"
wikipassword = "admin123"
icingausername = "admin"
icingapassword = "admin123"
# Needs to be the location of your status.cgi
icingaurl = "https://icinga.example.com/cgi-bin/icinga/status.cgi"
# I'm assuming you use HTTPS. You really should.
wikihost = ('https','en.example.org')
# Config for the Wiki
# You might need to change the path, but this is the default.
site = mwclient.Site(wikihost, path='/w/')
site.login(wikiusername, wikipassword)
# This gets through the HTTP Basic Auth in Icinga
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, icingaurl+"?style=hostdetail&jsonoutput", icingausername, icingapassword)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
# This is the list of hosts.
req = urllib2.Request(icingaurl+"?style=hostdetail&jsonoutput")
data = urllib2.urlopen(req)
json_data = data.read()
j = json.loads(json_data)
# Start a counter for looping through the json listing of hosts
x = int(0)
for i in j['status']['host_status']:
hostname = j['status']['host_status'][x]['host']
passman2 = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman2.add_password(None, icingaurl + "?host=" + hostname + "&jsonoutput", icingausername, icingapassword)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
# This is the lists of services on each host.
req2 = urllib2.Request(icingaurl + "?host=" + hostname + "&jsonoutput")
data2 = urllib2.urlopen(req2)
json_data2 = data2.read()
j2 = json.loads(json_data2)
# Start a counter for looping through the json listing of services on each host
y = int(0)
# Start building text for the Page.
page = site.Pages[hostname]
pagetext = "== Services ==\r\n\r\n"
for i in j2['status']['service_status']:
pagetext += "=== "+ j2['status']['service_status'][y]['service'] + " ===\r\n\r\n"
y += 1
pagetext += "[[Category:Icinga]]\r\n\r\n"
# Save the pagetext.
page.save(pagetext, summary='Creating new page')
x += 1
print "Done"