update alert function
amolsh opened this issue · 16 comments
Hi,
I was looking for update alert function, but I don't see such function. Is it available or not?
That's correct... I don't see one yet either. We've often been adding new methods to the Python client in response to demand. I can look at getting this added. I expect it will be generic enough to cover whatever you need, but could you let me know a specific type of update you have in mind so I can test that specifically to make sure it works for you out of the gate?
@philrz Thanks...you are life saver :) ! We often do update like timespan, sometime description, notification channels etc.
@philrz , Thanks!
restore_alert and list_alert are working as aspected. Update alert is also working fine. Changes looks good to release. But I am not quite happy with update_alert, I felt little difficult to use. I have something in my mind to simplify update_alert, let me try that first. If it works, I will suggest that approach.
@philrz ,
I was wondering about changing update alert to accept JSON file as well. For Ex. I want to update 3 alerts at a time, then I will copy JSON data for those alerts from list_alerts.py output json file, and put that data in update.json file, modify it and will give as input to update alert function.
What do you say?
@amolsh,
Your feedback got me to realize a limitation of the restore_alerts.py example as I had written it. If run a second time with the exact same JSON dump, it would fail, since it would be trying to create an Alert where one of the same name already exists. But I could instead make it recognize this condition and call update_alert() so that the Alert in the system would take on the config from the JSON dump, effectively handling a mix of creates and updates in the same script. I think this would cover the update use case you just described, but without having to change update_alert() itself.
I put a modified example in a branch https://github.com/draios/python-sdc-client/tree/restore-alerts-with-update. Could you check it out and let me know if that could work for you?
@philrz That's great, Thanks!
Updated count getting wrong. I updated 1 alert and it said "13" updated(13 is my total count of alerts). It is updating all alerts if we rerun create_alert with same jsonfile. Well, it won't cause any problem. Just wanted to notify. Otherwise everything working fine 👍
I don't think there is way to avoid updating all alerts.
@amolsh,
Yes, fine point. I suppose if we wanted to get fancy I could have it check each part of the respective Alert objects and, if they're exactly the same in all the right places, skip the update call. But I could imagine that getting sticky. Since it sounds like you're ok with using it as-is, I'll go ahead and put up a PR.
Please, go ahead for new release!
@philrz Having problem in restore_alerts.py
I am using following data.json file and it is not creating Notification channels. Is is because I am using notification channel names? If you look at my previous PR, I have solved this issues there, by changing channel names to channel ids in function itself.
{
"alerts": [
{
"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"
},
"notificationChannels" : [
{"type": "OPSGENIE", "name": "Cloud Platform OpsGenie"},
{"type": "WEBHOOK", "name": "Cloud Alert Manager"}
],
"filter": "agent.tag.location='prod-env'",
"enabled" : false,
"annotations" : {}
},
{
"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"
},
"notificationChannels" : [
{"type": "OPSGENIE", "name": "Cloud Platform OpsGenie"},
{"type": "WEBHOOK", "name": "Cloud Alert Manager"}
],
"filter": "agent.tag.location='prod-env'",
"enabled" : false,
"annotations" : {}
}
]
}
@philrz I think if change the code for restore alert, it may probably disturb it restore functionality using data from list-alert. So, no need to change. I will write script locally, which takes notification channel name convert into notification channel IDs and replaces in json data file
@amolsh, sorry for the trouble. I guess this is a hazard of trying to support multiple use cases with the same script.
If you haven't already written a local script of your own, I could address this by making the restore_alerts.py check for the presence of the notificationChannels and do the mapping to the IDs right there in the example code, while still supporting number-based notificationChannelIds, such as if someone is restoring from a straight backup dump. Let me know if this could work for you and I'll code it up.
@philrz No worries, I have resolve that issues locally. I have written local script which takes channels names and returns different channels Ids depending on imputes (environment specific imputes like different set of channels for prod, preprod etc). Then it replaces those in JSON data for alerts.
@philrz,
When are you guys going to release new version (which includes all these changes)?
Sorry for the delay. I'll package up the release right now and will update here when it's published.