Skip to content

Commit 86cf38f

Browse files
committed
Add channel scanning by channel name
1 parent df10c24 commit 86cf38f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

grafana_wtf/commands.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def run():
4141
grafana-wtf [options] plugins list [--id=]
4242
grafana-wtf [options] plugins status [--id=]
4343
grafana-wtf [options] channels [--uid=]
44+
grafana-wtf [options] channels [--name=]
4445
grafana-wtf --version
4546
grafana-wtf (-h | --help)
4647
@@ -348,6 +349,8 @@ def run():
348349
if options.channels:
349350
if options.uid:
350351
response = engine.channels_list_by_uid(options.uid)
352+
elif options.name:
353+
response = engine.channels_list_by_name(options.name)
351354
else:
352355
response = engine.channels_list()
353356
output_results(output_format, response)

grafana_wtf/core.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737

3838
class GrafanaEngine:
39-
4039
# Configure a larger HTTP request pool.
4140
# TODO: Review the pool settings and eventually adjust according to concurrency level or other parameters.
4241
# https://urllib3.readthedocs.io/en/latest/advanced-usage.html#customizing-pool-behavior
@@ -589,7 +588,11 @@ def channels_list(self):
589588
return self.grafana.notifications.lookup_channels()
590589

591590
def channels_list_by_uid(self, channel_uid):
592-
channel = self.grafana.notifications.get_channel_by_uid(channel_uid)
591+
try:
592+
channel = self.grafana.notifications.get_channel_by_uid(channel_uid)
593+
except GrafanaClientError as ex:
594+
log.error(f"Error fetching the channel {channel_uid}: {ex}")
595+
raise SystemExit(1)
593596

594597
# Scan dashboards and panels to find where the channel is used
595598
dashboards = self.scan_dashboards()
@@ -616,6 +619,19 @@ def extract_channel_related_information(channel_uid, dashboard, panel):
616619
related_information.append({"dashboard": dashboard["dashboard"]["title"], "panel": panel["title"]})
617620
return related_information
618621

622+
def channels_list_by_name(self, name):
623+
channel_list = self.channels_list()
624+
channel_uid = ""
625+
for channel in channel_list:
626+
if channel["name"] == name:
627+
channel_uid = channel["uid"]
628+
break
629+
if channel_uid:
630+
return self.channels_list_by_uid(channel_uid)
631+
else:
632+
log.info(f"Channel with the name {name} doesn't exist")
633+
raise SystemExit(0)
634+
619635

620636
class Indexer:
621637
def __init__(self, engine: GrafanaWtf):

0 commit comments

Comments
 (0)