Skip to content

Commit

Permalink
Escape special chars, like + in prometheus query
Browse files Browse the repository at this point in the history
  • Loading branch information
juztas committed Jul 24, 2024
1 parent fbf81dc commit 025ff9e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions autogole-api/src/python/RTMonLibs/GeneralLibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def getWebContentFromURL(url, logger, raiseEx=True):
time.sleep(1)
return out

def escape(invalue):
"""Escape special characters for regex matching"""
return invalue.replace("+", "\\+")

class ExceptionTemplate(Exception):
"""Exception template."""
Expand Down
12 changes: 6 additions & 6 deletions autogole-api/src/python/RTMonLibs/Template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""Grafana Template Generation"""
import copy
import os.path
from RTMonLibs.GeneralLibs import loadJson, dumpJson, dumpYaml
from RTMonLibs.GeneralLibs import loadJson, dumpJson, dumpYaml, escape

def _processName(name):
"""Process Name for Mermaid and replace all special chars with _"""
Expand Down Expand Up @@ -402,7 +402,7 @@ def findIntf(interfaces):

if "?port_name?" == intfname:
continue
elif "JointNetwork" in intfdata:
if "JointNetwork" in intfdata:
print(f"JointNetwork: {intfdata['JointNetwork']}")
# ifDescr=~"newy32aoa-cr6::1/1/c13/1|newy32aoa-cr6::.*1/1/c13/1-2015"
# 'JointNetwork': 'newy32aoa-cr6|1_1_c13_1|fabric'
Expand Down Expand Up @@ -434,8 +434,8 @@ def findIntf(interfaces):
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 = panels.replace("REPLACEME_HOSTNAME", escape(hostname))
panels = panels.replace("REPLACEME_INTERFACE", escape(intfline))
panels = loadJson(panels, self.logger)
out += self.addRowPanel(row, panels, True)
return out
Expand Down Expand Up @@ -537,7 +537,7 @@ def _t_addSwitchL2Debugging(self, sitehost, interfaces, refid):
query = copy.deepcopy(origin_query)
# Add state and check if it receives information from snmp monitoring
query['datasource']['uid'] = str(self.t_dsourceuid)
query['expr'] = f'count(interface_statistics{{sitename="{sitename}", hostname="{hostname}"}}) OR on() vector(0)'
query['expr'] = f'count(interface_statistics{{sitename="{sitename}", hostname="{escape(hostname)}"}}) OR on() vector(0)'
query['legendFormat'] = f'SNMP Data available for {sitename} {hostname}'
query['refId'] = f'A{refid}'
refid += 1
Expand All @@ -551,7 +551,7 @@ def _t_addSwitchL2Debugging(self, sitehost, interfaces, refid):
for mhost, macaddr in self.mac_addresses.items():
query = copy.deepcopy(origin_query)
query['datasource']['uid'] = str(self.t_dsourceuid)
query['expr'] = f'sum(mac_table_info{{sitename="{sitename}",hostname="{hostname}", macaddress="{macaddr}", vlan="{vlan}"}}) OR on() vector(0)'
query['expr'] = f'sum(mac_table_info{{sitename="{sitename}",hostname="{escape(hostname)}", macaddress="{macaddr}", vlan="{vlan}"}}) OR on() vector(0)'
query['legendFormat'] = f'MAC address of {mhost} visible in mac table ({vlan})'
query['refId'] = f'A{refid}'
refid += 1
Expand Down

0 comments on commit 025ff9e

Please sign in to comment.