########################################################################### # CLOUD SERVER API DOCUMENTATION # # Name: createMonitoringPolicy # Language: Python # # Description: this function creates a new monitoring policy ########################################################################### from urllib.request import Request, urlopen import urllib import json #ID access to API TOKEN = "{YOUR_API_TOKEN}" # e.g.: "f03c3c76cc853ee690b879909c9c6f2a" url = "https://cloudpanel-api.1and1.com/v1" def _createMonitoringPolicy(content): #Configure the request _command = url + "/monitoring_policies" _method = 'POST' request = Request(_command, data=content.encode(encoding='utf_8'), headers={'X-TOKEN':TOKEN, 'content-type':'application/json'}, method=_method) #Try to get the response try: response = urlopen(request) content = response.read() return (content.decode()) #Fetch error except urllib.error.URLError as e: return("Error " + str(e.code) + ":" + e.reason) #PARAMETERS name = 'My monitoring policy' email = 'test@gmail.com' agent = True thresholds = {"cpu":{"warning":{"value":90, "alert":False}, "critical":{"value":95, "alert":True}}, "ram":{"warning":{"value":90, "alert":True}, "critical":{"value":95, "alert":True}}, "disk":{"warning":{"value":80, "alert":True}, "critical":{"value":90, "alert":True}}, "transfer":{"warning":{"value":100, "alert":True}, "critical":{"value":2000, "alert":True}}, "internal_ping":{"warning":{"value":50, "alert":True}, "critical":{"value":100, "alert":True}}}; ports = [{"protocol":"TCP", "port":22, "alert_if":"RESPONDING", "email_notification": True}] processes = [{"process":"Test", "alert_if":"RUNNING", "email_notification": True}] data = json.dumps({'name':name, 'email':email, 'agent':agent, 'thresholds':thresholds, 'ports':ports, 'processes':processes}) #Create monitoring policy print(_createMonitoringPolicy(data))