Skip to content

Instantly share code, notes, and snippets.

@kfatehi
Created August 2, 2023 00:13
Show Gist options
  • Save kfatehi/0550f811f9ef52d17c45df50f2b31b6f to your computer and use it in GitHub Desktop.
Save kfatehi/0550f811f9ef52d17c45df50f2b31b6f to your computer and use it in GitHub Desktop.
Reveal dashboards and panels that have alerts but no notification channels
# Reveal dashboards and panels that have alerts but no notification channels
# Works on Grafana v8 Alerts (v9 changes alerts a lot)
# This script is useful for when you accidentally change alert notification channel
import requests
from config import base_url, api_key
headers = {'Authorization': f'Bearer {api_key}'}
def get_dashboards():
url = f'{base_url}/api/search'
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def get_dashboard_by_uid(uid):
url = f'{base_url}/api/dashboards/uid/{uid}'
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def get_notification_channel(channel_uid):
url = f'{base_url}/api/alert-notifications/uid/{channel_uid}'
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def get_panels_from_rows(rows):
panels = []
for row in rows:
if 'panels' in row:
panels.extend(row['panels'])
return panels
dashboards = get_dashboards()
for db in dashboards:
dashboard = get_dashboard_by_uid(db['uid'])
panels = []
if 'panels' in dashboard['dashboard']:
panels.extend(dashboard['dashboard']['panels'])
if 'rows' in dashboard['dashboard']:
panels.extend(get_panels_from_rows(dashboard['dashboard']['rows']))
for panel in panels:
if 'alert' in panel:
alert = panel['alert']
notification_channels = []
for notification in alert['notifications']:
notification_channel = get_notification_channel(notification['uid'])
notification_channels.append(notification_channel['name'])
if len(notification_channels) == 0:
print(f"Dashboard: {dashboard['meta']['slug']} Panel: {panel['title']} has no notification channels, go to {base_url}{dashboard['meta']['url']}?editPanel={panel['id']}&tab=alert to add one.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment