Skip to content

Instantly share code, notes, and snippets.

@pulecp
Last active March 7, 2016 22:15
Show Gist options
  • Select an option

  • Save pulecp/3b14e4cb6becdfa7669e to your computer and use it in GitHub Desktop.

Select an option

Save pulecp/3b14e4cb6becdfa7669e to your computer and use it in GitHub Desktop.
define downtime {
host_name zimbra8.example.com
service_description Bacula job: zimbra8.example.com
author kayn
comment Schedule downtime for services with workhours notifications
downtime_period monday 00:00-09:00,18:00-24:00
downtime_period tuesday 00:00-09:00,18:00-24:00
downtime_period wednesday 00:00-09:00,18:00-24:00
downtime_period thursday 00:00-09:00,18:00-24:00
downtime_period friday 00:00-09:00,18:00-24:00
downtime_period saturday 00:00-24:00
downtime_period sunday 00:00-24:00
propagate 1
register 1
}
define downtime {
host_name zimbra8.example.com
service_description Bacula-fd service: zimbra8.example.com
author kayn
comment Schedule downtime for services with workhours notifications
downtime_period monday 00:00-09:00,18:00-24:00
downtime_period tuesday 00:00-09:00,18:00-24:00
downtime_period wednesday 00:00-09:00,18:00-24:00
downtime_period thursday 00:00-09:00,18:00-24:00
downtime_period friday 00:00-09:00,18:00-24:00
downtime_period saturday 00:00-24:00
downtime_period sunday 00:00-24:00
propagate 1
register 1
}
#!/usr/bin/python
import urllib2
import json
url = 'https://icinga.example.com/icinga/cgi-bin/config.cgi?type=services&jsonoutput'
username = 'user'
password = 'password'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
req = urllib2.Request(url)
f = urllib2.urlopen(req)
data = f.read()
parsed_data = json.loads(data)
# print json.dumps(parsed_data, sort_keys=True, indent=4, separators=(',', ': '))
# workhours = parsed_data[0]["config"]["services"]["notification_period"]
# workhours = parsed_data['config']['services']
# print json.dumps(workhours, sort_keys=True, indent=4, separators=(',', ': '))
open('/tmp/schedule_downtimes', 'w').close() # it should erase the confent of file
f = open('/tmp/schedule_downtimes','a')
for service in parsed_data['config']['services']:
if service['notification_period'] == 'workhours':
f.write('define downtime {\n')
f.write(" host_name %s \n" % (service['host_name']) )
f.write(" service_description %s \n" % (service['service_description']) )
f.write(' author kayn\n')
f.write(' comment Schedule downtime for services with workhours notifications\n')
f.write(' downtime_period monday 00:00-09:00,18:00-24:00\n')
f.write(' downtime_period tuesday 00:00-09:00,18:00-24:00\n')
f.write(' downtime_period wednesday 00:00-09:00,18:00-24:00\n')
f.write(' downtime_period thursday 00:00-09:00,18:00-24:00\n')
f.write(' downtime_period friday 00:00-09:00,18:00-24:00\n')
f.write(' downtime_period saturday 00:00-24:00\n')
f.write(' downtime_period sunday 00:00-24:00\n')
f.write(' propagate 1\n')
f.write(' register 1\n')
f.write('}\n\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment