Skip to content

Commit 85727b4

Browse files
nikodemasamotl
authored andcommitted
Update channel scanning
1 parent e01dcad commit 85727b4

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

grafana_wtf/commands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def run():
4040
grafana-wtf [options] log [<dashboard_uid>] [--number=<count>] [--head=<count>] [--tail=<count>] [--reverse] [--sql=<sql>]
4141
grafana-wtf [options] plugins list [--id=]
4242
grafana-wtf [options] plugins status [--id=]
43-
grafana-wtf [options] channels [--id=]
43+
grafana-wtf [options] channels [--uid=]
4444
grafana-wtf --version
4545
grafana-wtf (-h | --help)
4646
@@ -346,8 +346,8 @@ def run():
346346
output_results(output_format, response)
347347

348348
if options.channels:
349-
if options.id:
350-
response = engine.channels_list_by_id(options.id)
349+
if options.uid:
350+
response = engine.channels_list_by_uid(options.uid)
351351
else:
352352
response = engine.channels_list()
353353
output_results(output_format, response)

grafana_wtf/core.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,34 @@ def plugins_status_by_id(self, plugin_id):
588588
def channels_list(self):
589589
return self.grafana.notifications.lookup_channels()
590590

591-
def channels_list_by_id(self, channel_id):
592-
channel = self.grafana.notifications.get_channel_by_id(channel_id)
591+
def channels_list_by_uid(self, channel_uid):
592+
channel = self.grafana.notifications.get_channel_by_uid(channel_uid)
593+
594+
# Scan dashboards and panels to find where the channel is used
595+
dashboards = self.scan_dashboards()
596+
related_panels = []
597+
for dashboard in dashboards:
598+
for panel in dashboard["dashboard"].get("panels", []):
599+
if "alert" in panel and panel["alert"]["notifications"]:
600+
related_panels += self.extract_channel_related_information(channel_uid, dashboard, panel)
601+
602+
# Some dashboards have a deeper nested structure
603+
elif "panels" in panel:
604+
for subpanel in panel["panels"]:
605+
if "alert" in subpanel and subpanel["alert"]["notifications"]:
606+
related_panels += self.extract_channel_related_information(channel_uid, dashboard, subpanel)
607+
if related_panels:
608+
channel["related_panels"] = related_panels
593609
return channel
594610

611+
@staticmethod
612+
def extract_channel_related_information(channel_uid, dashboard, panel):
613+
related_information = []
614+
for notification in panel["alert"]["notifications"]:
615+
if "uid" in notification and notification["uid"] == channel_uid:
616+
related_information.append({"dashboard": dashboard["dashboard"]["title"], "panel": panel["title"]})
617+
return related_information
618+
595619

596620
class Indexer:
597621
def __init__(self, engine: GrafanaWtf):

0 commit comments

Comments
 (0)