141 lines
4.8 KiB
Python
141 lines
4.8 KiB
Python
import argparse
|
|
import csv
|
|
import requests
|
|
import yaml
|
|
from decouple import config
|
|
import sys
|
|
import json
|
|
|
|
def put_request(url, headers,body):
|
|
#try:
|
|
response = requests.put(url, body, headers=headers,verify=False)
|
|
response.raise_for_status()
|
|
# except requests.exceptions.HTTPError as errh:
|
|
# return "An Http Error occurred:" + repr(errh)
|
|
# except requests.exceptions.ConnectionError as errc:
|
|
# return "An Error Connecting to the API occurred:" + repr(errc)
|
|
# except requests.exceptions.Timeout as errt:
|
|
# return "A Timeout Error occurred:" + repr(errt)
|
|
# except requests.exceptions.RequestException as err:
|
|
# return "An Unknown Error occurred" + repr(err)
|
|
|
|
return response
|
|
|
|
def get_request(url, headers):
|
|
#try:
|
|
response = requests.get(url, headers=headers)
|
|
response.raise_for_status()
|
|
# except requests.exceptions.HTTPError as errh:
|
|
# return "An Http Error occurred:" + repr(errh)
|
|
# except requests.exceptions.ConnectionError as errc:
|
|
# return "An Error Connecting to the API occurred:" + repr(errc)
|
|
# except requests.exceptions.Timeout as errt:
|
|
# return "A Timeout Error occurred:" + repr(errt)
|
|
# except requests.exceptions.RequestException as err:
|
|
# return "An Unknown Error occurred" + repr(err)
|
|
|
|
return response
|
|
|
|
def init_parser():
|
|
parser = argparse.ArgumentParser(description='Activate or deactivate host monitoring by host list')
|
|
parser.add_argument('-i', '--inputfile', required=True, help='path to the host list as csv')
|
|
parser.add_argument('-a', '--activate', action='store_true', help='activate host monitoring')
|
|
parser.add_argument('-d', '--deactivate', action='store_true', help='deactivate host monitoring')
|
|
return parser
|
|
|
|
def getObject(DTURL,DTTOKEN,hostid):
|
|
try:
|
|
#get objectid
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Api-Token ' + DTTOKEN
|
|
}
|
|
|
|
apiurl=DTURL+"/api/v2/settings/objects?fields=objectId%2Cvalue&scopes="+hostid
|
|
object_result=get_request(apiurl,headers).json()
|
|
|
|
if len(object_result["items"])==0:
|
|
print(hostid+" object could not be modified by script!")
|
|
return
|
|
|
|
return object_result['items'][0]
|
|
|
|
except Exception as err:
|
|
print(hostid +"failed during following Exception! "+repr(err))
|
|
|
|
def activate(DTURL,DTTOKEN,objectH, hostid):
|
|
try:
|
|
#get objectid
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Api-Token ' + DTTOKEN
|
|
}
|
|
|
|
apiurl=DTURL+"/api/v2/settings/objects/"+objectH["objectId"]
|
|
|
|
objectH["value"]["enabled"]=True
|
|
body={"value": objectH["value"]}
|
|
|
|
object_result=put_request(apiurl,headers,json.dumps(body)).json()
|
|
|
|
except Exception as err:
|
|
print(hostid+" failed during following Exception! "+repr(err))
|
|
|
|
def deactivate(DTURL,DTTOKEN,objectH, hostid):
|
|
try:
|
|
#get objectid
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Api-Token ' + DTTOKEN
|
|
}
|
|
|
|
apiurl=DTURL+"/api/v2/settings/objects/"+objectH["objectId"]
|
|
|
|
objectH["value"]["enabled"]=False
|
|
body={"value": objectH["value"]}
|
|
|
|
object_result=put_request(apiurl,headers,json.dumps(body)).json()
|
|
|
|
except Exception as err:
|
|
print(hostid+" failed during following Exception! "+repr(err))
|
|
|
|
|
|
|
|
def main():
|
|
parser = init_parser()
|
|
args = parser.parse_args()
|
|
|
|
if args.activate and args.deactivate:
|
|
print("You can only activate or deactivate host monitoring, not both at the same time.")
|
|
exit()
|
|
|
|
if not args.activate and not args.deactivate:
|
|
print("You need to specify whether to activate or deactivate host monitoring.")
|
|
exit()
|
|
|
|
hosts = []
|
|
with open(args.inputfile, newline='') as csvfile:
|
|
reader = csv.DictReader(csvfile,["HOSTID", "ENV"])
|
|
for row in reader:
|
|
hosts.append(row)
|
|
|
|
with open('./environment.yaml') as file:
|
|
env_doc = yaml.safe_load(file)
|
|
|
|
for row in hosts:
|
|
dturl=env_doc[row["ENV"]][1]["env-url"]
|
|
token = config(env_doc[row["ENV"]][2]["env-token-name"])
|
|
objectH=getObject(dturl,token,row["HOSTID"])
|
|
|
|
if objectH:
|
|
if args.activate:
|
|
activate(dturl,token,objectH,row["HOSTID"])
|
|
print(row["HOSTID"] +" monitoring successfully enabled!")
|
|
|
|
elif args.deactivate:
|
|
deactivate(dturl,token,objectH,row["HOSTID"])
|
|
print(row["HOSTID"] +" monitoring successfully disabled!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |