Skip to content

Commit

Permalink
add a testcase for snmptt datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
lausser committed Jul 9, 2024
1 parent 1e696b8 commit 73d5969
Show file tree
Hide file tree
Showing 12 changed files with 25,171 additions and 8 deletions.
11 changes: 11 additions & 0 deletions tests/etc/coshsh.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dir = ./recipes/test10/data
type = csv
dir = ./recipes/testsnmptt/data

[datasource_CSVpaloalto]
type = csv
dir = ./recipes/testsnmptt/data

[datasource_CSVGROW]
type = csv
dir = ./recipes/test15/data
Expand Down Expand Up @@ -248,6 +252,13 @@ templates_dir = ./recipes/testsnmptt/templates
datasources = CSVunity,snmptt
datarecipients = >>>,checklogfiles_mibs

[recipe_TESTsnmptt_nodes]
objects_dir = ./var/objects/testsnmptt
classes_dir = ./recipes/testsnmptt/classes
templates_dir = ./recipes/testsnmptt/templates
datasources = CSVunity,CSVpaloalto,snmptt
datarecipients = >>>,checklogfiles_mibs

[recipe_TESTjinja]
objects_dir = ./var/objects/test4
classes_dir = ./recipes/test4/classes
Expand Down
54 changes: 52 additions & 2 deletions tests/recipes/testsnmptt/classes/datasource_snmptt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,39 @@
logger = logging.getLogger('coshsh')

def __ds_ident__(params={}):
if coshsh.util.compare_attr("type", params, "snmptt"):
if coshsh.util.compare_attr("type", params, "^snmptt$"):
return SNMPTT
elif coshsh.util.compare_attr("type", params, "ragpickify"):
elif coshsh.util.compare_attr("type", params, "^ragpickify$"):
return SNMPTTRagpickify


def create_matches_nodes(nodes=[]):
# Clean and process nodes at the time of factory creation
nodes = [node.strip() for string in nodes for node in string.split()]
mode_positive = True
if "MODE=NEG" in nodes:
mode_positive = False
nodes = [n for n in nodes if not n.startswith("MODE=")]

def matches_nodes(application):
match_found = False
if not nodes and mode_positive:
# no NODES found, this trap is for all
return True
elif not nodes and not mode_positive:
# no NODES found, except one MODE=NEG. Non should get this trap
return False
if application.host_name in nodes and mode_positive:
return True
elif application.host_name not in nodes and not mode_positive:
# my name is not listed in the nodes (nodes to exclude)
return True
# do more complitated things like comparing networks, addresses
# and hostgroups which is new!
return False
return matches_nodes


class MIB(coshsh.item.Item):
id = 100
template_rules = [
Expand Down Expand Up @@ -151,6 +178,7 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
eventtext_pat = re.compile(r'^FORMAT (.*)')
match_pat = re.compile(r'^MATCH (\$.*)')
matchmode_pat = re.compile(r'^MATCH MODE=(.*)')
nodes_pat = re.compile(r'^NODES (.*)')
edesc_pat = re.compile(r'^EDESC')
for ttfile in snmpttfiles:
unsorted_traps = {}
Expand All @@ -160,6 +188,7 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
last_name = None
last_matchmode = 'and'
last_recovers = None
last_nodes = []
#
# Namenlose Events von 1.3.6.1.4.1.1981_ bekommen
# EventName="EventMonitorTrapError"
Expand All @@ -170,6 +199,7 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
eventtext_m = eventtext_pat.search(line)
match_m = match_pat.search(line)
matchmode_m = matchmode_pat.search(line)
nodes_m = nodes_pat.search(line)
edesc_m = edesc_pat.search(line)
if eventname_m:
if last_eventname:
Expand All @@ -184,6 +214,8 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
'recovers': last_recovers,
'match': last_match,
'nagioslevel': last_nagios,
'nodes': last_nodes,
'matches_nodes': create_matches_nodes(last_nodes),
})
except Exception:
mib_traps[mib] = [{
Expand All @@ -193,6 +225,8 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
'recovers': last_recovers,
'match': last_match,
'nagioslevel': last_nagios,
'nodes': last_nodes,
'matches_nodes': create_matches_nodes(last_nodes),
}]
last_eventname = eventname_m.group(1).replace(' ', '')
last_oid = eventname_m.group(2)
Expand Down Expand Up @@ -226,6 +260,7 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
last_eventtext = None
last_match = None
last_recovers = None
last_nodes = []
elif eventtext_m:
last_eventtext = eventtext_m.group(1).replace("'", '"')
elif match_m:
Expand All @@ -238,6 +273,8 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
last_matchmode = matchmode_m.group(1)
elif recovers_m:
last_recovers = recovers_m.group(1)
elif nodes_m:
last_nodes.append(nodes_m.group(1))
if last_eventname:
# save the last event if there is one
try:
Expand All @@ -248,6 +285,8 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
'recovers': last_recovers,
'match': last_match,
'nagioslevel': last_nagios,
'nodes': last_nodes,
'matches_nodes': create_matches_nodes(last_nodes),
})
except Exception:
mib_traps[mib] = [{
Expand All @@ -257,6 +296,8 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
'recovers': last_recovers,
'match': last_match,
'nagioslevel': last_nagios,
'nodes': last_nodes,
'matches_nodes': create_matches_nodes(last_nodes),
}]
try:
logger.debug('mib %s counts %d traps' % (mib, len(mib_traps[mib])))
Expand Down Expand Up @@ -359,6 +400,15 @@ def read(self, filter=None, objects={}, force=False, **kwargs):
address = self.get('hosts', application.host_name).address
for svcmib, mib in application_mibs_known:
mobj = self.get('mibconfigs', mib)
# Here we create the dict which contains all the
# passive services an application will get.
# Keys are the mibs, values are a list of traps.
# There is a NODES attribute which can enable/disable
# certain events for certain hosts. We could filter
# mobj.events here in order to assign only the wanted
# events to an application. But there might be more
# datasources to come and maybe hostgroups re not
# completed yet
trap_events[svcmib] = [e for e in mobj.events]
mobj.add_agent([
address,
Expand Down
23 changes: 23 additions & 0 deletions tests/recipes/testsnmptt/classes/os_paloalto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re
import coshsh
from coshsh.application import Application
from coshsh.templaterule import TemplateRule
from coshsh.util import compare_attr

def __mi_ident__(params={}):
if coshsh.util.compare_attr("type", params, "Paloalto"):
return Unity


class Unity(coshsh.application.Application):
template_rules = [
coshsh.templaterule.TemplateRule(needsattr=None,
template="os_paloalto_default"),
coshsh.templaterule.TemplateRule(needsattr='trap_events',
template="os_paloalto_traps"),
]
implements_mibs = ['PAN-TRAPS-MIB:PAN-TRAPS-7-MIB']

def wemustrepeat(self):
if hasattr(self, "version"):
self.implements_mibs = ['PAN-TRAPS-MIB:PAN-TRAPS-'+str(self.version)+'-MIB']
14 changes: 14 additions & 0 deletions tests/recipes/testsnmptt/data/csvpaloalto_applications.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
host_name,name,type,component,version,check_period
pa_1,os,PaloAlto,,7,7x24
pa_2,os,PaloAlto,,7,7x24
pa_3,os,PaloAlto,,7,7x24
pa_4,os,PaloAlto,,7,7x24
pa_4,os,PaloAlto,,7,7x24
pa_6,os,PaloAlto,,7,7x24
pa_7,os,PaloAlto,,7,7x24
pa_8,os,PaloAlto,,7,7x24
pa_9,os,PaloAlto,,7,7x24
pa_a,os,PaloAlto,,7,7x24
pa_b,os,PaloAlto,,8,7x24
pa_c,os,PaloAlto,,7,7x24
pa_d,os,PaloAlto,,7,7x24
15 changes: 15 additions & 0 deletions tests/recipes/testsnmptt/data/csvpaloalto_hosts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
host_name,address,type,os,hardware,virtual,notification_period,location,departm
ent
pa_1,10.0.0.1,firewall,,,ps,7x24,dc,net
pa_2,10.0.0.2,firewall,,,ps,7x24,dc,net
pa_3,10.0.0.3,firewall,,,ps,7x24,dc,net
pa_4,10.0.0.4,firewall,,,ps,7x24,dc,net
pa_5,10.0.0.5,firewall,,,ps,7x24,dc,net
pa_6,10.0.0.6,firewall,,,ps,7x24,dc,net
pa_7,10.0.0.7,firewall,,,ps,7x24,dc,net
pa_8,10.0.0.8,firewall,,,ps,7x24,dc,net
pa_9,10.0.0.9,firewall,,,ps,7x24,dc,net
pa_a,10.0.0.10,firewall,,,ps,7x24,dc,net
pa_b,10.0.0.11,firewall,,,ps,7x24,dc,net
pa_c,10.0.0.12,firewall,,,ps,7x24,dc,net
pa_d,10.0.0.13,firewall,,,ps,7x24,dc,net
Loading

0 comments on commit 73d5969

Please sign in to comment.