Skip to content

Commit

Permalink
test lib add support of snmptrapd daemon
Browse files Browse the repository at this point in the history
Signed-off-by: Francois Dumontet <[email protected]>
  • Loading branch information
fdumontet6WIND committed Sep 26, 2023
1 parent 79c6bb1 commit 3cd72d3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 43 deletions.
44 changes: 37 additions & 7 deletions tests/topotests/lib/snmptest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,29 @@ def _get_snmp_oid(snmp_output):
# third token onwards is the value of the object
return tokens[0].split(".", 1)[1]

@staticmethod
def _get_snmp_oid(snmp_output):
tokens = snmp_output.strip().split()

# if len(tokens) > 5:
# return None
def _parse_notification_trap(self, snmp_out):

#we use the "=" as separator thus we will have
# element of list formated "value oid"
# value for index i is corresponding to index i-1
results = snmp_out.strip().split("=")

#remove the notification part date, notification OID
del results[0:2]

index = 0
oid_list = []
while index < len(results)-1:
raw_oid = results[index].strip().split()[-1]
value = results[index+1].strip().split("\t")[0]
#remove initial "." of oid
oid = raw_oid.split(".",1)[1]
oid_list.append((oid, value))
index += 1

return oid_list

# third token is the value of the object
return tokens[0].split(".", 1)[1]

def _parse_multiline(self, snmp_output):
results = snmp_output.strip().split("\n")
Expand All @@ -93,6 +107,19 @@ def _parse_multiline(self, snmp_output):

return out_dict, out_list


def _parse_multiline_trap(self, snmp_output):
snmp_output.replace("\t"," ")
results = snmp_output.strip().split("\n")
del results[0:2]
out_list = []
results = [elem for index, elem in enumerate(results) if index % 2 !=0]
for response in results:
oid_list = self._parse_notification_trap(response)
out_list += oid_list

return out_list

def get(self, oid):
cmd = "snmpget {0} {1}".format(self._snmp_config(), oid)

Expand All @@ -116,6 +143,9 @@ def walk(self, oid):
result = self.router.cmd(cmd)
return self._parse_multiline(result)

def trap(self, outputfile):
return self._parse_multiline_trap(outputfile)

def test_oid(self, oid, value):
print("oid: {}".format(self.get_next(oid)))
return self.get_next(oid) == value
Expand Down
5 changes: 4 additions & 1 deletion tests/topotests/lib/topogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ class TopoRouter(TopoGear):
RD_SNMP = 18
RD_PIM6 = 19
RD_MGMTD = 20
RD_TRAP = 21
RD = {
RD_FRR: "frr",
RD_ZEBRA: "zebra",
Expand All @@ -766,6 +767,7 @@ class TopoRouter(TopoGear):
RD_PATH: "pathd",
RD_SNMP: "snmpd",
RD_MGMTD: "mgmtd",
RD_TRAP : "snmptrapd",
}

def __init__(self, tgen, cls, name, **params):
Expand Down Expand Up @@ -842,14 +844,15 @@ def load_config(self, daemon, source=None, param=None):
TopoRouter.RD_RIPNG, TopoRouter.RD_OSPF, TopoRouter.RD_OSPF6,
TopoRouter.RD_ISIS, TopoRouter.RD_BGP, TopoRouter.RD_LDP,
TopoRouter.RD_PIM, TopoRouter.RD_PIM6, TopoRouter.RD_PBR,
TopoRouter.RD_SNMP, TopoRouter.RD_MGMTD.
TopoRouter.RD_SNMP, TopoRouter.RD_MGMTD, TopoRouter.RD_TRAP.
Possible `source` values are `None` for an empty config file, a path name which is
used directly, or a file name with no path components which is first looked for
directly and then looked for under a sub-directory named after router.
This API unfortunately allows for source to not exist for any and
all routers.
"""
daemonstr = self.RD.get(daemon)
self.logger.debug('loading "{}" configuration: {}'.format(daemonstr, source))
Expand Down
85 changes: 50 additions & 35 deletions tests/topotests/lib/topotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,7 @@ def __init__(self, name, *posargs, **params):
"pathd": 0,
"snmpd": 0,
"mgmtd": 0,
"snmptrapd": 0,
}
self.daemons_options = {"zebra": ""}
self.reportCores = True
Expand Down Expand Up @@ -1884,44 +1885,56 @@ def start_daemon(daemon, extra_opts=None):
) + "{}.pid -x /etc/frr/agentx".format(runbase)
# check_daemon_files.append(runbase + ".pid")
else:
binary = os.path.join(self.daemondir, daemon)
check_daemon_files.extend([runbase + ".pid", runbase + ".vty"])

cmdenv = "ASAN_OPTIONS="
if asan_abort:
cmdenv += "abort_on_error=1:"
cmdenv += "log_path={0}/{1}.asan.{2} ".format(
self.logdir, self.name, daemon
)
if daemon == "snmptrapd":
binary = "/usr/sbin/snmptrapd"
cmdenv = ""
cmdopt = "{} ".format(daemon_opts)\
+ "-C -c /etc/frr/snmptrapd.conf"\
+ " -p /etc/{}/snmptrapd.pid".format(
self.routertype,self.name
) + " -LF 6-7 {}/{}/snmptrapd.log".format(
self.logdir, self.name
)
else:
binary = os.path.join(self.daemondir, daemon)
check_daemon_files.extend([runbase + ".pid", runbase + ".vty"])

cmdenv = "ASAN_OPTIONS="
if asan_abort:
cmdenv += "abort_on_error=1:"
cmdenv += "log_path={0}/{1}.asan.{2} ".format(
self.logdir, self.name, daemon
)

if valgrind_memleaks:
this_dir = os.path.dirname(
os.path.abspath(os.path.realpath(__file__))
)
supp_file = os.path.abspath(
os.path.join(this_dir, "../../../tools/valgrind.supp")
)
cmdenv += " /usr/bin/valgrind --num-callers=50 --log-file={1}/{2}.valgrind.{0}.%p --leak-check=full --suppressions={3}".format(
daemon, self.logdir, self.name, supp_file
)
if valgrind_extra:
cmdenv += (
" --gen-suppressions=all --expensive-definedness-checks=yes"
if valgrind_memleaks:
this_dir = os.path.dirname(
os.path.abspath(os.path.realpath(__file__))
)
supp_file = os.path.abspath(
os.path.join(this_dir, "../../../tools/valgrind.supp")
)
cmdenv += " /usr/bin/valgrind --num-callers=50 --log-file={1}/{2}.valgrind.{0}.%p --leak-check=full --suppressions={3}".format(
daemon, self.logdir, self.name, supp_file
)
if valgrind_extra:
cmdenv += (
" --gen-suppressions=all --expensive-definedness-checks=yes"
)
elif daemon in strace_daemons or "all" in strace_daemons:
cmdenv = "strace -f -D -o {1}/{2}.strace.{0} ".format(
daemon, self.logdir, self.name
)
elif daemon in strace_daemons or "all" in strace_daemons:
cmdenv = "strace -f -D -o {1}/{2}.strace.{0} ".format(
daemon, self.logdir, self.name
)

cmdopt = "{} --command-log-always ".format(daemon_opts)
cmdopt += "--log file:{}.log --log-level debug".format(daemon)
cmdopt = "{} --command-log-always ".format(daemon_opts)
cmdopt += "--log file:{}.log --log-level debug".format(daemon)

if daemon in logd_options:
logdopt = logd_options[daemon]
if "all" in logdopt or self.name in logdopt:
tail_log_files.append(
"{}/{}/{}.log".format(self.logdir, self.name, daemon)
)

if daemon in logd_options:
logdopt = logd_options[daemon]
if "all" in logdopt or self.name in logdopt:
tail_log_files.append(
"{}/{}/{}.log".format(self.logdir, self.name, daemon)
)
if extra_opts:
cmdopt += " " + extra_opts

Expand Down Expand Up @@ -1970,7 +1983,7 @@ def start_daemon(daemon, extra_opts=None):
"%s: %s %s started with perf", self, self.routertype, daemon
)
else:
if daemon != "snmpd":
if daemon != "snmpd" and daemon != "snmptrapd":
cmdopt += " -d "
cmdopt += rediropt

Expand Down Expand Up @@ -2213,6 +2226,8 @@ def checkRouterRunning(self):
for daemon in self.daemons:
if daemon == "snmpd":
continue
if daemon == "snmptrapd":
continue
if (self.daemons[daemon] == 1) and not (daemon in daemonsRunning):
sys.stderr.write("%s: Daemon %s not running\n" % (self.name, daemon))
if daemon == "staticd":
Expand Down

0 comments on commit 3cd72d3

Please sign in to comment.