diff --git a/Changelog b/Changelog index 4c6609c..465b1ca 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,6 @@ +* 2024-09-30 10.2.2 + add method run_git_init to the datarecipient baseclass + add IGNORE to traps which should be completely ignored * 2024-02-16 10.2.1.3 show the class dump only with --debug * 2024-02-16 10.2.1.2 diff --git a/coshsh/datarecipient.py b/coshsh/datarecipient.py index d362285..1e57a16 100644 --- a/coshsh/datarecipient.py +++ b/coshsh/datarecipient.py @@ -11,6 +11,9 @@ import io import re import logging +from subprocess import Popen, PIPE, STDOUT +import string +import random import coshsh logger = logging.getLogger('coshsh') @@ -156,3 +159,36 @@ def too_much_delta(self): if self.max_delta[1] >= 0 and abs(self.delta_services) > self.max_delta[1]: return True return False + + def run_git_init(self, path): + save_dir = os.getcwd() + os.chdir(path) + print("git init------------------") + process = Popen(["git", "init", "."], stdout=PIPE, stderr=STDOUT, universal_newlines=True) + output, unused_err = process.communicate() + retcode = process.poll() + print(output) + init_file = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(16)) + open(init_file, "w").close() + print("git add {}------------------".format(init_file)) + process = Popen(["git", "add", init_file], stdout=PIPE, stderr=STDOUT, universal_newlines=True) + output, unused_err = process.communicate() + retcode = process.poll() + print(output) + commitmsg = "initial dummy-commit add" + process = Popen(["git", "commit", "-a", "-m", commitmsg], stdout=PIPE, stderr=STDOUT, universal_newlines=True) + output, unused_err = process.communicate() + retcode = process.poll() + print(output) + print("git rm {}------------------".format(init_file)) + process = Popen(["git", "rm", init_file], stdout=PIPE, stderr=STDOUT, universal_newlines=True) + output, unused_err = process.communicate() + retcode = process.poll() + print(output) + commitmsg = "initial dummy-commit rm" + process = Popen(["git", "commit", "-a", "-m", commitmsg], stdout=PIPE, stderr=STDOUT, universal_newlines=True) + output, unused_err = process.communicate() + retcode = process.poll() + print(output) + os.chdir(save_dir) + diff --git a/recipes/default/classes/datarecipient_coshsh_default.py b/recipes/default/classes/datarecipient_coshsh_default.py index 9228449..1c1feaa 100644 --- a/recipes/default/classes/datarecipient_coshsh_default.py +++ b/recipes/default/classes/datarecipient_coshsh_default.py @@ -164,13 +164,9 @@ def output(self, filter=None): self.analyze_output(output) elif not os.path.exists(self.dynamic_dir + '/.git') and self.recipe_git_init and [p for p in os.environ["PATH"].split(os.pathsep) if os.path.isfile(os.path.join(p, "git"))]: logger.debug("dynamic_dir will be made a git repository") + self.run_git_init(self.dynamic_dir) save_dir = os.getcwd() os.chdir(self.dynamic_dir) - print("git init------------------") - process = Popen(["git", "init", "."], stdout=PIPE, stderr=STDOUT, universal_newlines=True) - output, unused_err = process.communicate() - retcode = process.poll() - print(output) print("git add------------------") process = Popen(["git", "add", "--all", "."], stdout=PIPE, stderr=STDOUT, universal_newlines=True) output, unused_err = process.communicate() diff --git a/setup.py b/setup.py index f442fd9..b94944f 100755 --- a/setup.py +++ b/setup.py @@ -54,7 +54,7 @@ def run(self): setup(name='coshsh', - version='10.2.1.3', + version='10.2.2', setup_requires=['wheel'], description='Coshsh - config generator for monitoring systems', long_description=open('README.md').read(), diff --git a/tests/recipes/test20/data/csv20.e_applicationdetails.csv b/tests/recipes/test20/data/csv20.e_applicationdetails.csv new file mode 100644 index 0000000..af8c38a --- /dev/null +++ b/tests/recipes/test20/data/csv20.e_applicationdetails.csv @@ -0,0 +1 @@ +host_name,name,type,monitoring_type,monitoring_0,monitoring_1 diff --git a/tests/recipes/test20/data/csv20.e_applications.csv b/tests/recipes/test20/data/csv20.e_applications.csv new file mode 100644 index 0000000..50c3d6a --- /dev/null +++ b/tests/recipes/test20/data/csv20.e_applications.csv @@ -0,0 +1 @@ +host_name,name,type diff --git a/tests/recipes/test20/data/csv20.e_hosts.csv b/tests/recipes/test20/data/csv20.e_hosts.csv new file mode 100644 index 0000000..cf1ad18 --- /dev/null +++ b/tests/recipes/test20/data/csv20.e_hosts.csv @@ -0,0 +1 @@ +host_name,address,alias diff --git a/tests/recipes/testsnmptt/classes/datasource_snmptt.py b/tests/recipes/testsnmptt/classes/datasource_snmptt.py index a2eaf39..a7d438c 100755 --- a/tests/recipes/testsnmptt/classes/datasource_snmptt.py +++ b/tests/recipes/testsnmptt/classes/datasource_snmptt.py @@ -206,28 +206,30 @@ def read(self, filter=None, objects={}, force=False, **kwargs): # save the last event if there is one if last_match: last_match.append(last_eventtext) # add the format of this match - try: - mib_traps[mib].append({ - 'name': last_eventname, - 'oid': last_oid, - 'text': last_eventtext, - 'recovers': last_recovers, - 'match': last_match, - 'nagioslevel': last_nagios, - 'nodes': last_nodes, - 'matches_nodes': create_matches_nodes(last_nodes), - }) - except Exception: - mib_traps[mib] = [{ - 'name': last_eventname, - 'oid': last_oid, - 'text': last_eventtext, - 'recovers': last_recovers, - 'match': last_match, - 'nagioslevel': last_nagios, - 'nodes': last_nodes, - 'matches_nodes': create_matches_nodes(last_nodes), - }] + if last_nagios != -1 and "MODE=IGNORE" not in last_nodes: + # if not HIDDEN + try: + mib_traps[mib].append({ + 'name': last_eventname, + 'oid': last_oid, + 'text': last_eventtext, + 'recovers': last_recovers, + 'match': last_match, + 'nagioslevel': last_nagios, + 'nodes': last_nodes, + 'matches_nodes': create_matches_nodes(last_nodes), + }) + except Exception: + mib_traps[mib] = [{ + 'name': last_eventname, + 'oid': last_oid, + 'text': last_eventtext, + '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) last_severity = eventname_m.group(3).upper() @@ -252,7 +254,8 @@ def read(self, filter=None, objects={}, force=False, **kwargs): 'CRITICAL': 2, 'UNKNOWN': 3, - 'HIDDEN': -1, + 'HIDDEN': -1, # better use IGNORE + 'IGNORE': -1, }[last_severity] except Exception as e: logger.debug('trap severity %s unknown' % eventname_m.group(3)) @@ -277,28 +280,29 @@ def read(self, filter=None, objects={}, force=False, **kwargs): last_nodes.append(nodes_m.group(1)) if last_eventname: # save the last event if there is one - try: - mib_traps[mib].append({ - 'name': last_eventname, - 'oid': last_oid, - 'text': last_eventtext, - 'recovers': last_recovers, - 'match': last_match, - 'nagioslevel': last_nagios, - 'nodes': last_nodes, - 'matches_nodes': create_matches_nodes(last_nodes), - }) - except Exception: - mib_traps[mib] = [{ - 'name': last_eventname, - 'oid': last_oid, - 'text': last_eventtext, - 'recovers': last_recovers, - 'match': last_match, - 'nagioslevel': last_nagios, - 'nodes': last_nodes, - 'matches_nodes': create_matches_nodes(last_nodes), - }] + if last_nagios != -1 and "MODE=IGNORE" not in last_nodes: + try: + mib_traps[mib].append({ + 'name': last_eventname, + 'oid': last_oid, + 'text': last_eventtext, + 'recovers': last_recovers, + 'match': last_match, + 'nagioslevel': last_nagios, + 'nodes': last_nodes, + 'matches_nodes': create_matches_nodes(last_nodes), + }) + except Exception: + mib_traps[mib] = [{ + 'name': last_eventname, + 'oid': last_oid, + 'text': last_eventtext, + '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]))) except Exception as e: diff --git a/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-7-MIB.snmptt b/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-7-MIB.snmptt index 35fdca2..c68266f 100644 --- a/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-7-MIB.snmptt +++ b/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-7-MIB.snmptt @@ -12426,44 +12426,46 @@ EDESC ## ## ## -#EVENT panBFDAdminDownTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3503 "Status Events" Normal -#FORMAT Administrative down $* -#SDESC -#Administrative down -#Variables: -# 1: panReceiveTime -# 2: panSerial -# 3: panHostname -# 4: panEventType -# 5: panEventSubType -# 6: panVsys -# 7: panSeqno -# 8: panActionflags -# 9: panSystemEventId -# 10: panSystemObject -# 11: panSystemModule -# 12: panSystemSeverity -# 13: panSystemDescription -#EDESC +EVENT panBFDAdminDownTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3503 "Status Events" Normal +NODES MODE=IGNORE +FORMAT Administrative down $* +SDESC +Administrative down +Variables: + 1: panReceiveTime + 2: panSerial + 3: panHostname + 4: panEventType + 5: panEventSubType + 6: panVsys + 7: panSeqno + 8: panActionflags + 9: panSystemEventId + 10: panSystemObject + 11: panSystemModule + 12: panSystemSeverity + 13: panSystemDescription +EDESC ## ## ## -#EVENT panBFDSessionStateChangeTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3504 "Status Events" Normal -#FORMAT Session state change $* -#SDESC -#Session state change -#Variables: -# 1: panReceiveTime -# 2: panSerial -# 3: panHostname -# 4: panEventType -# 5: panEventSubType -# 6: panVsys -# 7: panSeqno -# 8: panActionflags -# 9: panSystemEventId -# 10: panSystemObject -# 11: panSystemModule -# 12: panSystemSeverity -# 13: panSystemDescription -#EDESC +EVENT panBFDSessionStateChangeTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3504 "Status Events" Normal +NODES MODE=NEG +FORMAT Session state change $* +SDESC +Session state change +Variables: + 1: panReceiveTime + 2: panSerial + 3: panHostname + 4: panEventType + 5: panEventSubType + 6: panVsys + 7: panSeqno + 8: panActionflags + 9: panSystemEventId + 10: panSystemObject + 11: panSystemModule + 12: panSystemSeverity + 13: panSystemDescription +EDESC diff --git a/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-8-MIB.snmptt b/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-8-MIB.snmptt index 7c7a015..1903429 100644 --- a/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-8-MIB.snmptt +++ b/tests/recipes/testsnmptt/data/snmptt/PAN-TRAPS-8-MIB.snmptt @@ -12426,44 +12426,45 @@ EDESC ## ## ## -#EVENT panBFDAdminDownTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3503 "Status Events" Normal -#FORMAT Administrative down $* -#SDESC -#Administrative down -#Variables: -# 1: panReceiveTime -# 2: panSerial -# 3: panHostname -# 4: panEventType -# 5: panEventSubType -# 6: panVsys -# 7: panSeqno -# 8: panActionflags -# 9: panSystemEventId -# 10: panSystemObject -# 11: panSystemModule -# 12: panSystemSeverity -# 13: panSystemDescription -#EDESC +EVENT panBFDAdminDownTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3503 "Status Events" Normal +FORMAT Administrative down $* +SDESC +Administrative down +Variables: + 1: panReceiveTime + 2: panSerial + 3: panHostname + 4: panEventType + 5: panEventSubType + 6: panVsys + 7: panSeqno + 8: panActionflags + 9: panSystemEventId + 10: panSystemObject + 11: panSystemModule + 12: panSystemSeverity + 13: panSystemDescription +EDESC ## ## ## -#EVENT panBFDSessionStateChangeTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3504 "Status Events" Normal -#FORMAT Session state change $* -#SDESC -#Session state change -#Variables: -# 1: panReceiveTime -# 2: panSerial -# 3: panHostname -# 4: panEventType -# 5: panEventSubType -# 6: panVsys -# 7: panSeqno -# 8: panActionflags -# 9: panSystemEventId -# 10: panSystemObject -# 11: panSystemModule -# 12: panSystemSeverity -# 13: panSystemDescription -#EDESC +EVENT panBFDSessionStateChangeTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3504 "Status Events" IGNORE +NODES MODE=NEG +FORMAT Session state change $* +SDESC +Session state change +Variables: + 1: panReceiveTime + 2: panSerial + 3: panHostname + 4: panEventType + 5: panEventSubType + 6: panVsys + 7: panSeqno + 8: panActionflags + 9: panSystemEventId + 10: panSystemObject + 11: panSystemModule + 12: panSystemSeverity + 13: panSystemDescription +EDESC diff --git a/tests/test_git.py b/tests/test_git.py index 32943df..50699aa 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -1,5 +1,6 @@ import os import io +import shutil from tests.common_coshsh_test import CommonCoshshTest class CoshshTest(CommonCoshshTest): @@ -39,3 +40,52 @@ def test_create_recipe_multiple_sources_no_git(self): # git_init is yes by default self.assertTrue(not os.path.exists("var/objects/test10/dynamic/.git")) + # wenn null objekte generiert werden, dann muss dennoch das objects_dir + # als vollwertiges git-Repository initialisiert werden, sonst treten beim + # check_git_updates Fehler auf + def test_no_objects_init_empty_dir(self): + self.setUpConfig("etc/coshsh13.cfg", "test20gitno") + super(CoshshTest, self).tearDown() + test20 = self.generator.recipes['test20gitno'].datarecipients[0].objects_dir + test20prom = self.generator.recipes['test20gitno'].datarecipients[1].objects_dir + if os.path.exists(test20prom): + shutil.rmtree(test20prom, True) + if os.path.exists(test20): + shutil.rmtree(test20, True) + r = self.generator.get_recipe("test20gitno") + r.collect() + r.assemble() + r.render() + os.makedirs(test20, 0o755) + os.makedirs(test20prom, 0o755) + self.assertTrue(os.path.exists(test20)) + self.assertTrue(os.path.exists(test20prom)) + r.output() + self.assertTrue(os.path.exists(os.path.join(test20, "dynamic"))) + self.assertTrue(os.path.exists(os.path.join(test20prom, "dynamic"))) + self.assertFalse(os.path.exists(os.path.join(test20, "dynamic", ".git"))) + self.assertFalse(os.path.exists(os.path.join(test20prom, "dynamic", "targets", ".git"))) + + def test_no_objects_init_empty_git(self): + self.setUpConfig("etc/coshsh13.cfg", "test20gityes") + super(CoshshTest, self).tearDown() + test20 = self.generator.recipes['test20gityes'].datarecipients[0].objects_dir + test20prom = self.generator.recipes['test20gityes'].datarecipients[1].objects_dir + if os.path.exists(test20prom): + shutil.rmtree(test20prom, True) + if os.path.exists(test20): + shutil.rmtree(test20, True) + r = self.generator.get_recipe("test20gityes") + r.collect() + r.assemble() + r.render() + os.makedirs(test20, 0o755) + os.makedirs(test20prom, 0o755) + self.assertTrue(os.path.exists(test20)) + self.assertTrue(os.path.exists(test20prom)) + r.output() + self.assertTrue(os.path.exists(os.path.join(test20, "dynamic"))) + self.assertTrue(os.path.exists(os.path.join(test20prom, "dynamic"))) + self.assertTrue(os.path.exists(os.path.join(test20, "dynamic", ".git"))) + #kennt kein git_init#self.assertTrue(os.path.exists(os.path.join(test20prom, "dynamic", "targets", ".git"))) + diff --git a/tests/test_snmptt.py b/tests/test_snmptt.py index 2fce9ac..19787b3 100644 --- a/tests/test_snmptt.py +++ b/tests/test_snmptt.py @@ -128,6 +128,32 @@ def test_nodes_in_snmptt(self): self.assertTrue('PAN-TRAPS-8-MIB' in os_paloalto_traps_cfg) self.assertTrue('_OID .1.3.6.1.4.1.25461.2.1.3.2.0.51531' in os_paloalto_traps_cfg) + def test_ignored_traps(self): + # PAN-TRAPS-8-MIB.snmptt + # EVENT panBFDSessionStateChangeTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3504 "Status Events" HIDDEN + # PAN-TRAPS-7-MIB.snmptt + # EVENT panBFDSessionStateChangeTrap .1.3.6.1.4.1.25461.2.1.3.2.0.3504 "Status Events" Normal + # HIDDEN = completely ignore this trap + # it will never become a service definition + # its oid will not be checked in the check_logfiles scan + self.setUpConfig("etc/coshsh.cfg", "testsnmptt_nodes") + r = self.generator.get_recipe("testsnmptt_nodes") + r.count_before_objects() + r.cleanup_target_dir() + r.prepare_target_dir() + r.collect() + r.assemble() + r.render() + r.output() + with io.open("etc/check_logfiles/snmptt/PAN-TRAPS-7-MIB.cfg") as f: + check_logfiles_cfg = f.read() + self.assertTrue('panBFDSessionStateChangeTrap' in check_logfiles_cfg) + self.assertFalse('panBFDAdminDownTrap' in check_logfiles_cfg) + with io.open("etc/check_logfiles/snmptt/PAN-TRAPS-8-MIB.cfg") as f: + check_logfiles_cfg = f.read() + self.assertFalse('panBFDSessionStateChangeTrap' in check_logfiles_cfg) + self.assertTrue('panBFDAdminDownTrap' in check_logfiles_cfg) + def tearDown(self): pass # PAN-TRAP-MIB .1.3.6.1.4.1.25461.2.1.3.2.0.1531