new function "create_alert_from_jsonfile"
amolsh opened this issue · 1 comments
Hi,
I wrote one create alert function which accepts JSON file and creates alerts.
Can you please add this function to sdc client?
Benefits: You don't have to create alert python script for each alert separately. Instead store all your alerts in one json file and pass that file to function.
Function definition:-
def create_alert_from_jsonfile(self, filename,notify=None):
with open(filename) as file:
alert_json_data = json.load(file)
#
# Get the list of alerts from the server
#
res = requests.get(self.url + '/api/alerts', headers=self.hdrs, verify=self.ssl_verify)
if not self.__checkResponse(res):
return [False, self.lasterr]
j = res.json()
#
# Populate the alert information,Create the new alert
#
for keys,values in alert_json_data.items():
alert_json = {}
alert_json["alert"] = values
if notify != None:
alert_json['alert']['notificationChannelIds'] = notify
res = requests.post(self.url + '/api/alerts', headers=self.hdrs, data=json.dumps(alert_json), verify=self.ssl_verify)
if not self.__checkResponse(res):
#return [False, self.lasterr]
print(self.lasterr)
else:
print(res.json())
print("\n")
return [True]
Example Usage:-
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdcClient
if len(sys.argv) != 2:
print 'usage: %s <sysdig-token>' % sys.argv[0]
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
sys.exit(1)
sdc_token = sys.argv[1]
#
# Instantiate the SDC client
#
sdclient = SdcClient(sdc_token)
notify_channels = [ {'type': 'OPSGENIE', 'name': 'Cloud Platform OpsGenie'},
{'type': 'WEBHOOK', 'name': 'Cloud Alert Manager'}
]
res = sdclient.get_notification_ids(notify_channels)
if not res[0]:
print "Could not get IDs and hence not creating the alert: " + res[1]
sys.exit(-1)
notification_channel_ids = res[1]
res=sdclient.create_alert_from_jsonfile("data", notification_channel_ids)
if not res[0]:
sys.exit(1)
data file(in json) :-
{
"alert" : {
"type" : "MANUAL",
"name" : "prod-env host down",
"description" : "triggers when host down",
"severity" : 2,
"timespan" : 300000000,
"condition" : "timeAvg(uptime) = 0",
"segmentBy" : ["host.mac"],
"segmentCondition" : {
"type": "ANY"
},
"filter": "agent.tag.location='prod-env'",
"enabled" : false,
"annotations" : {}
},
"alert2" : {
"type" : "MANUAL",
"name" : "prod-env memory high",
"description" : "triggers when memory high",
"severity" : 2,
"timespan" : 300000000,
"condition" : "timeAvg(memory.used.percent) >= 90",
"segmentBy" : ["host.mac"],
"segmentCondition" : {
"type": "ANY"
},
"filter": "agent.tag.location='prod-env'",
"enabled" : false,
"annotations" : {}
}
}
Sorry for my delay in getting back to you on this and the Alert update request. I've got a PR up for review on the latter, BTW.
I like what you've proposed here, and I saw your PR. After looking it over, I had some thoughts on a slightly different approach. There was already a list_alerts.py example that I was able to extend to dump its output to a JSON file, so it effectively becomes a backup mechanism. Then I created a restore_alerts.py example similar to yours that reads in the JSON file of that format (so the same format could be used for wholly new Alerts like your use case) to create those Alerts. Then I tweaked create_alert() to take the fully-formed object as an alternative to the individual parameters. Therefore the main difference is that I kept the file awareness in the example and didn't have to bring it into the client itself, which I think keeps things a little DRY'er.
This is covered in a branch https://github.com/draios/python-sdc-client/tree/alerts_from_obj_dump. Could you give that a look and let me know if you're ok with that approach? If so, I'll put up a PR and have it reviewed by my colleagues.