How to disable Grafana alerts during certain times or days

Hello,

I’m using Grafana OSS version 8.3.x and I could not find an option to do the following:

  • Disable individual alerts (other than deleting it or putting an unreasonable filter)
  • Restrict alerts based on a date/ time or day of the week

I’m quite new to Grafana, specially to alerts so any help is greatly appreciated.

–Jose

Welcome

Maybe leverage the following with a cron-ed process using something like python

Thanks

Hello,

Thanks for your quick reply. I will give it a shot, should not be difficult to put together with a little bit of Bash and curl magic.

Will post an example here once done.

1 Like

I tried the API to see if I could get the list of alerts, but for some reason it always comes empty:

http_proxy= curl --location --user myadmin --header "Accept: application/json" --header "Content-Type: application/json" --request GET http://myhost.com:3000/api/alerts/          
Enter host password for user 'myadmin':
[]

So I tried the nuclear option to pause ALL the alerts but none was selected either:

http_proxy= curl --location --user myadmin --header "Accept: application/json" --header "Content-Type: application/json" --request POST --data '{"paused": true}'  http://myhost.com:3000/api/admin/pause-all-alerts
Enter host password for user 'myadmin':
{"alertsAffected":0,"message":"alerts paused","state":"paused"}

And the API doesn’t support adding/ removing “silences”.

Maybe Grafana 9 is better? Would consider to upgrade as it seems 8.3.xx has some limitations.

1 Like

Welcome to the forum. I presume you read through (and maybe tried) this? Am guessing you could find a way to automatically silence alerts based on a given schedule.

Hello Grant2,

Yes, I went through the documentation for Silences but found 2 things:

  1. There is no way to specify relative silences based on the day of the week (like weekends), or the ability to use a “holiday” calendar
  2. There is no REST API to create/delete silences. That, with a little bit of work, would make it easier to create silences for alerts using absolute dates, etc. (like pre-populate silences for the week).

I keep reading mentions of external alerts like alertmanager on the documentation, is that the way to go with this?

Thanks!

Hi @jnunez2 I am using Grafana OSS 9.0.5 and literally just getting started with Silences (set up my first one yesterday), but like you, I kept seeing references in the documentation to alert managers (which honestly I do not know what they are referring to). So maybe in the coming days I will have a better answer. My guess is that they either have created or will create something (Rest API or whatever) to specify the time/day/week/etc. that a silence should go into effect.

On a side note, have you used Node-RED? If an http interface for the silences exist, I am thinking it would be a piece of cake to set one’s “calendar” in Node-RED and then inject that info regularly to Grafana. For example: M - F, turn on silence for alerts A & B from 12 noon to 1 pm, Sat - Sun, turn on silence for alerts C & D from 12 am to 11:59 pm, etc.

OK, should have done some reading before my last post.

The link that @yosiasz sent was for Grafana 7.5 (not sure what has changed since then on this particular subject). I found the updated docs for 9.0.x and learned that one can create mute timings via http API. So I will definitely use Node-RED and give this a try today or this weekend.

From the docs…

Similar to silences, mute timings do not prevent alert rules from being evaluated, nor do they stop alert instances from being shown in the user interface. They only prevent notifications from being created.

2 Likes

This is a good resource to use for new alerting and it is working for me

https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/pkg/services/ngalert/api/tooling/post.json

here is your basic get alerts using insomnia.

1 Like

If you are using Grafana v9, you should be able to setup a mute timings in the Notification Policies without using the API.

[EDIT] Doesn’t seem to work as no labels are shared :confused:
I had the same problem and just figured out how to create a “mask” metric which will disable alerts outside working hours.
Here it is :

(vector(1) and on() hour() >= vector(6) <= vector(16) and on() day_of_week() >= vector(1) <= vector(5)) or vector(0)

It looks like this over 10 days :

Then create an expression to compute the alerting state :

$QueryDurationAvg < ($QueryDurationTreshold*$OpenHoursMask)

Should do the trick :slight_smile:

1 Like

Update, it works :slight_smile:

You need an intermediate mean reduce expression on the mask before using it in the alert expression (solution found here)

Then it should work fine !