Skip to content

Instantly share code, notes, and snippets.

@junaid18183
Created March 11, 2014 06:27
Show Gist options
  • Save junaid18183/9480570 to your computer and use it in GitHub Desktop.
Save junaid18183/9480570 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# +----------------------------------------------------------------------+
# | |
# | _ _ __ __ |
# | | | | | | \/ | |
# | | |_ _ _ __ ___ __| | | \ / | ___ _ __ ___ ___ _ __ |
# | _ | | | | | '_ \ / _ \/ _` | | |\/| |/ _ \ '_ ` _ \ / _ \| '_ \ |
# | | |__| | |_| | | | | __/ (_| | | | | | __/ | | | | | (_) | | | | |
# | \____/ \__,_|_| |_|\___|\__,_| |_| |_|\___|_| |_| |_|\___/|_| |_| |
# | |
# | Author : Juned Memon Email : junedm@glam.com |
# +----------------------------------------------------------------------+
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
# +----------------------------------------------------------------------+
# | Version : 1.0 Date: 07-Mar-2014
# | Script : set_downtime.py
# | Notes : This script is to set and remove the downtime in Check_mk Multisite using the
# | multisite api.
# Requires -H host - for which downtime needs to be set/remove
# -M mode - mode in which to operate i.e. set/remove
# -S services - Optional list of services to set/remove downtime
# -D duration - Default to 60 minutes, duration for which to set downtime
# +----------------------------------------------------------------------+
import os,sys,urllib2,base64,argparse,urllib,re
from datetime import datetime,timedelta
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
omd_site = 'watchdog'
omd_host = 'monitor.juned.com'
username = 'omdadmin'
password = 'omd'
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
# DO NOT EDIT ANYTHING BELOW THIS LINE #
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
#Functions
def bail_out(reason):
sys.stderr.write(reason + "\n")
sys.exit(1)
def verbose(text):
opt_verbose=True
if opt_verbose:
sys.stdout.write(text + "\n")
def make_url(base, variables):
vartext = "&".join([ "%s=%s" % e for e in variables ])
return base + "?" + vartext
def downtime(variables):
base_url = "http://" + omd_host+ "/" + omd_site + "/check_mk/"
auth_encoded = base64.encodestring('%s:%s' % (username, password))[:-1]
call = urllib.urlencode(variables)
url = base_url + "view.py?" + call
#verbose("URL: " + url)
req = urllib2.Request(url)
req.add_header('Authorization', 'Basic %s' % auth_encoded)
try:
response = urllib2.urlopen(req)
if re.match("(.*)MESSAGE: Successfully(.*)", response.readline()):
verbose("Success")
else :
bail_out("Error")
except Exception, e:
bail_out("Cannot call Multisite URL: %s" % e)
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
def main ():
parser = argparse.ArgumentParser(description="This script will set/remove the downtime on specified host.")
parser.add_argument("-M","--mode",help="Mode to operate [set|remove] downtime",required=True) #Positional Parameter
parser.add_argument("-H", "--host",help="Host to set/remove downtime",required=True) #This will have single argument
parser.add_argument("-S", "--services",help="Services to set/remove downtime",required=False,nargs='+') #This can have multiple arguments
parser.add_argument("-D", "--duration",help="Duration to set downtime in Minutes(Default 60 Minute)",type=int,default=60,required=False) #
parser.add_argument("-C", "--comment",help="Commnet to downtime",default="Added via script",required=False) #This can have multiple arguments
parser.add_argument("-v", "--verbose",help="verbose on",action="store_true") #This does not require any options with -v
args = parser.parse_args()
mode=args.mode
host=args.host
services=args.services
duration=args.duration
comment=args.comment
opt_verbose=args.verbose
opt_all=False
base_url = "http://" + omd_host+ "/" + omd_site + "/check_mk/"
variables = {
"_transid" : "-1" ,
"_do_confirm" : "yes" ,
"_do_actions" : "yes" ,
"host" : host ,
"output_format" :"json",
}
if mode == 'remove':
variables.update ({
"view_name" : "downtimes",
"_remove_downtimes" : "Remove",
})
else:
start=datetime.now()
end=start+timedelta(minutes=duration)
start_date=start.strftime('%Y-%m-%d')
start_time=start.strftime('%H:%M')
end_date=end.strftime('%Y-%m-%d')
end_time=end.strftime('%H:%M')
#( "_down_duration",duration ),
#( "_down_from_now", "yes" ),
#( "_down_minutes", duration ),Both of the above does not work for the older version of CMK
variables.update ({
"_down_comment" : comment ,
"_down_custom" : "Custom+time+range",
"_down_from_date" : start_date ,
"_down_from_time" : start_time ,
"_down_to_date" : end_date ,
"_down_to_time" : end_time ,
})
if services:
variables.update({"view_name" : "service" })
else:
variables.append({"view_name" : "hoststatus" })
if services:
for service in services:
variables.update({"service" : service })
downtime(variables)
else:
downtime(variables, [])
if opt_all:
if mode == 'set':
downtime(variables, [("view_name", "service")])
#-------------------------------------------------------------------------------------------------------------------------------------------------------------
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment