This is not a complete script. I was only interested in scheduling a SERVICEGROUP through the Nagios command file (NAGIOSCMD). This is for obvious reasons if you ever used Nagios in large environments. It is painful to schedule and cancel downtimes.
Since I wanted to be able to delete multiple downtime entries and I used a feature added somewhere in 3.x called DEL_DOWNTIME_BY_START_TIME_COMMENT. The cancellation of downtime is done by providing a Start date and a comment.
import sys, argparse, timefrom datetime import datetime, timedelta
## EXAMPLES## # python nagios_downtime.py --action add --Servicegroup PRDVIPS --begin "2016-12-10 8:36" --duration 10 --author rrosso --comment "Test Scheduling Downtime in Nagios"## [1481387741.0] SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;PRDVIPs;1481387760.0;1481388360.0;1;0;600;rrosso;Test Scheduling Downtime in Nagios## # python nagios_downtime.py --action cancel --begin "2016-12-10 8:36" --comment="Test Scheduling Downtime in Nagios"## [1481387769.0] DEL_DOWNTIME_BY_START_TIME_COMMENT;1481387760.0;Test Scheduling Downtime in Nagios##
VERSION = "0.4"VERDATE = "2016-12-10"
NAGIOSCMD = "/usr/local/nagios/var/rw/nagios.cmd"now = datetime.now()cmd = '[' + str(time.mktime(now.timetuple())) + '] 'execline = ''
parser = argparse.ArgumentParser(description='Nagios Downtime Scheduler.')parser.add_argument('--action', dest='action', help='use add or cancel as action for downtime entries', required=True)parser.add_argument('--Servicegroup', dest='servicegroup', help ='Schedule Downtime a specific ServiceGroup')parser.add_argument('--duration', dest='duration', help='Duration of downtime, in minutes.)parser.add_argument('--begin', dest='begin', help='Beginning of Downtime. ex: 2016-12-10 18:10', required=True)parser.add_argument('--author', dest='author', default='admin', help='Author: Who is scheduling the downtime?')parser.add_argument('--comment', dest='comment', help='Comment: Reason for scheduling the downtime.', required=True)parser.add_argument('--dryrun', action='store_true', help='Dry run. Do not do anything but show action.')
args = parser.parse_args()
## need some argument checking here. what is required what conflicts etc..if (args.action not in ['add','cancel']): sys.exit(1)
if (args.begin != None): #check for proper format here... #beginTime = datetime.datetime(2016,12,8,13,0).strftime('%s') beginTime = datetime.strptime(args.begin,'%Y-%m-%d %H:%M')
if (args.action == 'add'): if (args.servicegroup): cmd = cmd + 'SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;' endTime = beginTime + timedelta(minutes=int(args.duration)) execline=cmd + 'PRDVIPs;' + str(time.mktime(beginTime.timetuple())) + ';' + str(time.mktime(endTime.timetuple())) + ';1;0;' + '600' +';' + args.author + ';' + args.comment + '\n'
if (args.action == 'cancel'): cmd = cmd + 'DEL_DOWNTIME_BY_START_TIME_COMMENT;' execline=cmd + str(time.mktime(beginTime.timetuple())) + ';' + args.comment + '\n'
print 'Nagios CMD interaction will be: ' + execline
if (args.dryrun): print "Note that this is a dry run ie so not committing transaction"else: print "Note that this is not a dry run ie --dryrun was not used so committing transaction" f = open(NAGIOSCMD,'w') f.write(execline)