classVerifyLoggingAccounting(AntaTest):""" Verifies if AAA accounting logs are generated. Expected Results: * success: The test will pass if AAA accounting logs are generated. * failure: The test will fail if AAA accounting logs are NOT generated. """name="VerifyLoggingAccounting"description="Verifies if AAA accounting logs are generated."categories=["logging"]commands=[AntaCommand(command="show aaa accounting logs | tail",ofmt="text")]@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingAccountingvalidation. """pattern=r"cmd=show aaa accounting logs"output=self.instance_commands[0].text_outputifre.search(pattern,output):self.result.is_success()else:self.result.is_failure("AAA accounting logs are not generated")
@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingAccountingvalidation. """pattern=r"cmd=show aaa accounting logs"output=self.instance_commands[0].text_outputifre.search(pattern,output):self.result.is_success()else:self.result.is_failure("AAA accounting logs are not generated")
classVerifyLoggingHostname(AntaTest):""" Verifies if logs are generated with the device FQDN. Expected Results: * success: The test will pass if logs are generated with the device FQDN. * failure: The test will fail if logs are NOT generated with the device FQDN. """name="VerifyLoggingHostname"description="Verifies if logs are generated with the device FQDN."categories=["logging"]commands=[AntaCommand(command="show hostname"),AntaCommand(command="send log level informational message ANTA VerifyLoggingHostname validation"),AntaCommand(command="show logging informational last 30 seconds | grep ANTA",ofmt="text"),]@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingHostname validation. """output_hostname=self.instance_commands[0].json_outputoutput_logging=self.instance_commands[2].text_outputfqdn=output_hostname["fqdn"]lines=output_logging.strip().split("\n")[::-1]log_pattern=r"ANTA VerifyLoggingHostname validation"last_line_with_pattern=""forlineinlines:ifre.search(log_pattern,line):last_line_with_pattern=linebreakiffqdninlast_line_with_pattern:self.result.is_success()else:self.result.is_failure("Logs are not generated with the device FQDN")
@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingHostname validation. """output_hostname=self.instance_commands[0].json_outputoutput_logging=self.instance_commands[2].text_outputfqdn=output_hostname["fqdn"]lines=output_logging.strip().split("\n")[::-1]log_pattern=r"ANTA VerifyLoggingHostname validation"last_line_with_pattern=""forlineinlines:ifre.search(log_pattern,line):last_line_with_pattern=linebreakiffqdninlast_line_with_pattern:self.result.is_success()else:self.result.is_failure("Logs are not generated with the device FQDN")
classVerifyLoggingHosts(AntaTest):""" Verifies logging hosts (syslog servers) for a specified VRF. Expected Results: * success: The test will pass if the provided syslog servers are configured in the specified VRF. * failure: The test will fail if the provided syslog servers are NOT configured in the specified VRF. * skipped: The test will be skipped if syslog servers or VRF are not provided. """name="VerifyLoggingHosts"description="Verifies logging hosts (syslog servers) for a specified VRF."categories=["logging"]commands=[AntaCommand(command="show logging",ofmt="text")]@AntaTest.anta_testdeftest(self,hosts:Optional[List[str]]=None,vrf:str="default")->None:""" Run VerifyLoggingHosts validation. Args: hosts: List of hosts (syslog servers) IP addresses. vrf: The name of the VRF to transport log messages. Defaults to 'default'. """ifnothostsornotvrf:self.result.is_skipped(f"{self.__class__.name} did not run because hosts or vrf were not supplied")returnoutput=self.instance_commands[0].text_outputnot_configured=[]forhostinhosts:pattern=rf"Logging to '{host}'.*VRF {vrf}"ifnotre.search(pattern,_get_logging_states(self.logger,output)):not_configured.append(host)ifnotnot_configured:self.result.is_success()else:self.result.is_failure(f"Syslog servers {not_configured} are not configured in VRF {vrf}")
@AntaTest.anta_testdeftest(self,hosts:Optional[List[str]]=None,vrf:str="default")->None:""" Run VerifyLoggingHosts validation. Args: hosts: List of hosts (syslog servers) IP addresses. vrf: The name of the VRF to transport log messages. Defaults to 'default'. """ifnothostsornotvrf:self.result.is_skipped(f"{self.__class__.name} did not run because hosts or vrf were not supplied")returnoutput=self.instance_commands[0].text_outputnot_configured=[]forhostinhosts:pattern=rf"Logging to '{host}'.*VRF {vrf}"ifnotre.search(pattern,_get_logging_states(self.logger,output)):not_configured.append(host)ifnotnot_configured:self.result.is_success()else:self.result.is_failure(f"Syslog servers {not_configured} are not configured in VRF {vrf}")
classVerifyLoggingLogsGeneration(AntaTest):""" Verifies if logs are generated. Expected Results: * success: The test will pass if logs are generated. * failure: The test will fail if logs are NOT generated. """name="VerifyLoggingLogsGeneration"description="Verifies if logs are generated."categories=["logging"]commands=[AntaCommand(command="send log level informational message ANTA VerifyLoggingLogsGeneration validation"),AntaCommand(command="show logging informational last 30 seconds | grep ANTA",ofmt="text"),]@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingLogs validation. """log_pattern=r"ANTA VerifyLoggingLogsGeneration validation"output=self.instance_commands[1].text_outputlines=output.strip().split("\n")[::-1]forlineinlines:ifre.search(log_pattern,line):self.result.is_success()returnself.result.is_failure("Logs are not generated")
@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingLogs validation. """log_pattern=r"ANTA VerifyLoggingLogsGeneration validation"output=self.instance_commands[1].text_outputlines=output.strip().split("\n")[::-1]forlineinlines:ifre.search(log_pattern,line):self.result.is_success()returnself.result.is_failure("Logs are not generated")
classVerifyLoggingPersistent(AntaTest):""" Verifies if logging persistent is enabled and logs are saved in flash. Expected Results: * success: The test will pass if logging persistent is enabled and logs are in flash. * failure: The test will fail if logging persistent is disabled or no logs are saved in flash. """name="VerifyLoggingPersistent"description="Verifies if logging persistent is enabled and logs are saved in flash."categories=["logging"]commands=[AntaCommand(command="show logging",ofmt="text"),AntaCommand(command="dir flash:/persist/messages",ofmt="text"),]@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingPersistent validation. """self.result.is_success()log_output=self.instance_commands[0].text_outputdir_flash_output=self.instance_commands[1].text_outputif"Persistent logging: disabled"in_get_logging_states(self.logger,log_output):self.result.is_failure("Persistent logging is disabled")returnpattern=r"-rw-\s+(\d+)"persist_logs=re.search(pattern,dir_flash_output)ifnotpersist_logsorint(persist_logs.group(1))==0:self.result.is_failure("No persistent logs are saved in flash")
@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingPersistent validation. """self.result.is_success()log_output=self.instance_commands[0].text_outputdir_flash_output=self.instance_commands[1].text_outputif"Persistent logging: disabled"in_get_logging_states(self.logger,log_output):self.result.is_failure("Persistent logging is disabled")returnpattern=r"-rw-\s+(\d+)"persist_logs=re.search(pattern,dir_flash_output)ifnotpersist_logsorint(persist_logs.group(1))==0:self.result.is_failure("No persistent logs are saved in flash")
classVerifyLoggingSourceIntf(AntaTest):""" Verifies logging source-interface for a specified VRF. Expected Results: * success: The test will pass if the provided logging source-interface is configured in the specified VRF. * failure: The test will fail if the provided logging source-interface is NOT configured in the specified VRF. * skipped: The test will be skipped if source-interface or VRF is not provided. """name="VerifyLoggingSourceInt"description="Verifies logging source-interface for a specified VRF."categories=["logging"]commands=[AntaCommand(command="show logging",ofmt="text")]@AntaTest.anta_testdeftest(self,intf:Optional[str]=None,vrf:str="default")->None:""" Run VerifyLoggingSrcDst validation. Args: intf: Source-interface to use as source IP of log messages. vrf: The name of the VRF to transport log messages. Defaults to 'default'. """ifnotintfornotvrf:self.result.is_skipped(f"{self.__class__.name} did not run because intf or vrf was not supplied")returnoutput=self.instance_commands[0].text_outputpattern=rf"Logging source-interface '{intf}'.*VRF {vrf}"ifre.search(pattern,_get_logging_states(self.logger,output)):self.result.is_success()else:self.result.is_failure(f"Source-interface '{intf}' is not configured in VRF {vrf}")
@AntaTest.anta_testdeftest(self,intf:Optional[str]=None,vrf:str="default")->None:""" Run VerifyLoggingSrcDst validation. Args: intf: Source-interface to use as source IP of log messages. vrf: The name of the VRF to transport log messages. Defaults to 'default'. """ifnotintfornotvrf:self.result.is_skipped(f"{self.__class__.name} did not run because intf or vrf was not supplied")returnoutput=self.instance_commands[0].text_outputpattern=rf"Logging source-interface '{intf}'.*VRF {vrf}"ifre.search(pattern,_get_logging_states(self.logger,output)):self.result.is_success()else:self.result.is_failure(f"Source-interface '{intf}' is not configured in VRF {vrf}")
classVerifyLoggingTimestamp(AntaTest):""" Verifies if logs are generated with the approprate timestamp. Expected Results: * success: The test will pass if logs are generated with the appropriated timestamp. * failure: The test will fail if logs are NOT generated with the appropriated timestamp. """name="VerifyLoggingTimestamp"description="Verifies if logs are generated with the appropriate timestamp."categories=["logging"]commands=[AntaCommand(command="send log level informational message ANTA VerifyLoggingTimestamp validation"),AntaCommand(command="show logging informational last 30 seconds | grep ANTA",ofmt="text"),]@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingTimestamp validation. """log_pattern=r"ANTA VerifyLoggingTimestamp validation"timestamp_pattern=r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}-\d{2}:\d{2}"output=self.instance_commands[1].text_outputlines=output.strip().split("\n")[::-1]last_line_with_pattern=""forlineinlines:ifre.search(log_pattern,line):last_line_with_pattern=linebreakifre.search(timestamp_pattern,last_line_with_pattern):self.result.is_success()else:self.result.is_failure("Logs are not generated with the appropriate timestamp format")
@AntaTest.anta_testdeftest(self)->None:""" Run VerifyLoggingTimestamp validation. """log_pattern=r"ANTA VerifyLoggingTimestamp validation"timestamp_pattern=r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}-\d{2}:\d{2}"output=self.instance_commands[1].text_outputlines=output.strip().split("\n")[::-1]last_line_with_pattern=""forlineinlines:ifre.search(log_pattern,line):last_line_with_pattern=linebreakifre.search(timestamp_pattern,last_line_with_pattern):self.result.is_success()else:self.result.is_failure("Logs are not generated with the appropriate timestamp format")