{"id":1000,"date":"2016-12-10T12:35:07","date_gmt":"2016-12-10T18:35:07","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1000"},"modified":"2016-12-10T12:35:07","modified_gmt":"2016-12-10T18:35:07","slug":"nagios-downtime-using-a-servicegroup","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/nagios-downtime-using-a-servicegroup\/","title":{"rendered":"Nagios Downtime using a ServiceGroup"},"content":{"rendered":"
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. <\/p>\n
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.<\/p>\n
\r\nimport sys, argparse, time\r\nfrom datetime import datetime, timedelta\r\n\r\n## EXAMPLES\r\n## # python nagios_downtime.py --action add --Servicegroup PRDVIPS --begin "2016-12-10 8:36" --duration 10 --author rrosso --comment "Test Scheduling Downtime in Nagios"\r\n## [1481387741.0] SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;PRDVIPs;1481387760.0;1481388360.0;1;0;600;rrosso;Test Scheduling Downtime in Nagios\r\n## # python nagios_downtime.py --action cancel --begin "2016-12-10 8:36" --comment="Test Scheduling Downtime in Nagios"\r\n## [1481387769.0] DEL_DOWNTIME_BY_START_TIME_COMMENT;1481387760.0;Test Scheduling Downtime in Nagios\r\n##\r\n\r\nVERSION = "0.4"\r\nVERDATE = "2016-12-10"\r\n\r\nNAGIOSCMD = "\/usr\/local\/nagios\/var\/rw\/nagios.cmd"\r\nnow = datetime.now()\r\ncmd = '[' + str(time.mktime(now.timetuple())) + '] '\r\nexecline = ''\r\n\r\nparser = argparse.ArgumentParser(description='Nagios Downtime Scheduler.')\r\nparser.add_argument('--action', dest='action', help='use add or cancel as action for downtime entries', required=True)\r\nparser.add_argument('--Servicegroup', dest='servicegroup', help ='Schedule Downtime a specific ServiceGroup')\r\nparser.add_argument('--duration', dest='duration', help='Duration of downtime, in minutes.)\r\nparser.add_argument('--begin', dest='begin', help='Beginning of Downtime. ex: 2016-12-10 18:10', required=True)\r\nparser.add_argument('--author', dest='author', default='admin', help='Author: Who is scheduling the downtime?')\r\nparser.add_argument('--comment', dest='comment', help='Comment: Reason for scheduling the downtime.', required=True)\r\nparser.add_argument('--dryrun', action='store_true', help='Dry run. Do not do anything but show action.')\r\n\r\nargs = parser.parse_args()\r\n\r\n## need some argument checking here. what is required what conflicts etc..\r\nif (args.action not in ['add','cancel']):\r\n sys.exit(1)\r\n\r\nif (args.begin != None):\r\n #check for proper format here...\r\n #beginTime = datetime.datetime(2016,12,8,13,0).strftime('%s')\r\n beginTime = datetime.strptime(args.begin,'%Y-%m-%d %H:%M')\r\n\r\nif (args.action == 'add'):\r\n if (args.servicegroup):\r\n cmd = cmd + 'SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;'\r\n endTime = beginTime + timedelta(minutes=int(args.duration))\r\n execline=cmd + 'PRDVIPs;' + str(time.mktime(beginTime.timetuple())) + ';' + str(time.mktime(endTime.timetuple())) + ';1;0;' + '600' +';' + args.author + ';' + args.comment + '\\n'\r\n\r\nif (args.action == 'cancel'):\r\n cmd = cmd + 'DEL_DOWNTIME_BY_START_TIME_COMMENT;'\r\n execline=cmd + str(time.mktime(beginTime.timetuple())) + ';' + args.comment + '\\n'\r\n\r\nprint 'Nagios CMD interaction will be: ' + execline\r\n\r\nif (args.dryrun):\r\n print "Note that this is a dry run ie so not committing transaction"\r\nelse:\r\n print "Note that this is not a dry run ie --dryrun was not used so committing transaction"\r\n f = open(NAGIOSCMD,'w')\r\n f.write(execline)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"This is not a complete script. I was only interested in scheduling a SERVICEGROUP through the Nagios command file (NAGIOSCMD).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57,13],"tags":[],"class_list":["post-1000","post","type-post","status-publish","format-standard","hentry","category-nagios","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1000"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1000\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}