Skip to content

Commit

Permalink
Merge pull request #122 from juztas/annotationssiterm
Browse files Browse the repository at this point in the history
Submit annotations to dashboard for all pings issued by siterm
  • Loading branch information
juztas authored Jun 25, 2024
2 parents 26a87b4 + de4e0f2 commit 9ab3497
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
5 changes: 4 additions & 1 deletion autogole-api/src/python/RTMon/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def submit_exe(self, filename, fout):
return
# 3. Create the dashboard and template
try:
template = self.t_createTemplate(instance, manifest, **fout)
template, dashbInfo = self.t_createTemplate(instance, manifest, **fout)
fout["dashbInfo"] = dashbInfo
#except Exception as ex:
except IOError as ex:
self.logger.error('Failed to create template: %s', ex)
Expand All @@ -69,6 +70,7 @@ def submit_exe(self, filename, fout):
tmpOut = self.sr_submit_ping(instance=instance, manifest=manifest)
if tmpOut:
fout['ping'] = tmpOut
self.g_submitAnnotation(sitermOut=tmpOut, dashbInfo=fout["dashbInfo"])
# 6. Update State to Running
fout['state'] = 'running'
fout.setdefault('retries', 0)
Expand Down Expand Up @@ -108,6 +110,7 @@ def running_exe(self, filename, fout):
tmpOut = self.sr_submit_ping(instance=fout.get('instance', {}), manifest=fout.get('manifest', {}))
if tmpOut:
fout['ping'] = tmpOut
self.g_submitAnnotation(sitermOut=tmpOut, dashbInfo=fout["dashbInfo"])
self._updateState(filename, fout)
return
# Need to update the dashboard with new template_tag
Expand Down
26 changes: 26 additions & 0 deletions autogole-api/src/python/RTMonLibs/GrafanaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,29 @@ def g_getFolderID(self, name):
# General is default and not returned by Grafana API
self.logger.warning(f"Folder {name} is not configured.")
return None

def _g_addAnnotation(self, **kwargs):
"""Add annotation for dashboard based on uid"""
# Check that all params present;
if not all([kwargs.get('dashboard_uid'), kwargs.get('panelId'), kwargs.get('time_from'),
kwargs.get('time_to'), kwargs.get('tags'), kwargs.get('text')]):
self.logger.error("Missing params for annotation")
return
self.grafanaapi.annotations.add_annotation(
dashboard_uid=kwargs['dashboard_uid'], panel_id=kwargs['panelId'],
time_from=kwargs['time_from'], time_to=kwargs['time_to'],
tags=kwargs['tags'], text=kwargs['text'])

def g_submitAnnotation(self, **kwargs):
"""Submit annotation"""
dashbuid = kwargs['dashbInfo']['uid']
for item in kwargs['sitermOut']:
for annid in kwargs['dashbInfo']['annotation_panels']:
# Now we have panelId and loop for each panel and add annotation
txt = f"Here is SiteRM Ping Request info for {item['sitename']} {item['hostname']}: {item['submit_out']}"
self._g_addAnnotation(dashboard_uid=dashbuid, panelId=annid,
time_from=item['submit_time']*1000,
time_to=item['submit_time']*1000,
tags=["SiteRM-Ping-Start"],
text=txt)
return True
31 changes: 21 additions & 10 deletions autogole-api/src/python/RTMonLibs/Template.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ def __init__(self, **kwargs):
self.t_dsourceuid = 0
self.nextid = 0
self.gridPos = {"x": 0, "y": 0, "w": 24, "h": 8}
self.annotationids = []

def _clean(self):
"""Clean previous generated data"""
self.generated = {}
self.annotationids = []
self.nextid = 0

def __getTitlesUrls(self, site, link):
"""Get Titles and URLs"""
Expand All @@ -260,24 +267,26 @@ def __getTitlesUrls(self, site, link):
url = url.replace("$$REPLACEMESITENAME$$", site)
return title, url

def _getNextID(self):
def _getNextID(self, recordAnnotations=False):
"""Get Next ID"""
self.nextid += 1
if recordAnnotations:
self.annotationids.append(self.nextid)
return self.nextid

def _getNextRowID(self):
"""Increase Row ID"""
self.nextid += 100
return self.nextid

def addRowPanel(self, row, panels):
def addRowPanel(self, row, panels, recordAnnotations=False):
"""Add Panel to the Row (Depending on collapsed or not)"""
# https://github.com/grafana/grafana/issues/50855
out = []
if not row['collapsed']:
out.append(row)
for pan in panels:
pan["id"] = self._getNextID()
pan["id"] = self._getNextID(recordAnnotations)
if 'gridPos' not in pan:
pan["gridPos"] = self.gridPos
if row['collapsed']:
Expand Down Expand Up @@ -381,7 +390,7 @@ def t_createHostFlow(self, *args):
panels = panels.replace("REPLACEME_HOSTNAME", hostname)
panels = panels.replace("REPLACEME_INTERFACE", intfline)
panels = loadJson(panels, self.logger)
out += self.addRowPanel(row, panels)
out += self.addRowPanel(row, panels, True)
return out

def t_createSwitchFlow(self, *args):
Expand All @@ -399,15 +408,18 @@ def t_createSwitchFlow(self, *args):
raise Exception("Sitehost not in correct format") from ex
sitename = sitehost.split(":")[0]
hostname = sitehost.split(":")[1]
intfline = "|".join(interfaces.keys())
intfs = list(interfaces.keys())
if "?port_name?" in intfs:
intfs.remove("?port_name?")
intfline = "|".join(intfs)
row = self.t_addRow(*args, title=f"Switch Flow Summary: {sitehost}")
panels = dumpJson(self._t_loadTemplate("switchflow.json"), self.logger)
panels = panels.replace("REPLACEME_DATASOURCE", str(self.t_dsourceuid))
panels = panels.replace("REPLACEME_SITENAME", sitename)
panels = panels.replace("REPLACEME_HOSTNAME", hostname)
panels = panels.replace("REPLACEME_INTERFACE", intfline)
panels = loadJson(panels, self.logger)
out += self.addRowPanel(row, panels)
out += self.addRowPanel(row, panels, True)
return out

def t_createMermaid(self, *args):
Expand Down Expand Up @@ -548,12 +560,11 @@ def t_addL2Debugging(self, *args):
for sitehost, interfaces in self.m_groups['Switches'].items():
self.logger.debug(f"Adding L2 Debugging for Switch: {sitehost}, {interfaces}")
out += self._t_addSwitchL2Debugging(sitehost, interfaces, refID)
return self.addRowPanel(row, out)
return self.addRowPanel(row, out, True)

def t_createTemplate(self, *args, **kwargs):
"""Create Grafana Template"""
self.generated = {}
self.nextid = 0
self._clean()
self._t_getDataSourceUid(*args)
self.generated = self.t_createDashboard(*args, **kwargs)
# Add Mermaid (Send copy of args, as t_createMermaid will modify it by del items)
Expand All @@ -569,4 +580,4 @@ def t_createTemplate(self, *args, **kwargs):
self.generated['panels'] += self.t_createSwitchFlow(*args)
# Add L2 Debugging
self.generated['panels'] += self.t_addL2Debugging(*args)
return {"dashboard": self.generated}
return {"dashboard": self.generated}, {"uid": self.generated['uid'], "annotation_panels": self.annotationids}

0 comments on commit 9ab3497

Please sign in to comment.