Skip to content

Logging

ANTA catalog for logging tests

Test functions related to the EOS various logging settings

NOTE: ‘show logging’ does not support json output yet

VerifyLoggingAccounting

Bases: 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.
Source code in anta/tests/logging.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
class VerifyLoggingAccounting(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_test
    def test(self) -> None:
        """
        Run VerifyLoggingAccountingvalidation.
        """
        pattern = r"cmd=show aaa accounting logs"
        output = self.instance_commands[0].text_output

        if re.search(pattern, output):
            self.result.is_success()
        else:
            self.result.is_failure("AAA accounting logs are not generated")

test

test() -> None

Run VerifyLoggingAccountingvalidation.

Source code in anta/tests/logging.py
279
280
281
282
283
284
285
286
287
288
289
290
@AntaTest.anta_test
def test(self) -> None:
    """
    Run VerifyLoggingAccountingvalidation.
    """
    pattern = r"cmd=show aaa accounting logs"
    output = self.instance_commands[0].text_output

    if re.search(pattern, output):
        self.result.is_success()
    else:
        self.result.is_failure("AAA accounting logs are not generated")

VerifyLoggingHostname

Bases: 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.
Source code in anta/tests/logging.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
class VerifyLoggingHostname(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_test
    def test(self) -> None:
        """
        Run VerifyLoggingHostname validation.
        """
        output_hostname = self.instance_commands[0].json_output
        output_logging = self.instance_commands[2].text_output
        fqdn = output_hostname["fqdn"]
        lines = output_logging.strip().split("\n")[::-1]

        log_pattern = r"ANTA VerifyLoggingHostname validation"

        last_line_with_pattern = ""
        for line in lines:
            if re.search(log_pattern, line):
                last_line_with_pattern = line
                break

        if fqdn in last_line_with_pattern:
            self.result.is_success()
        else:
            self.result.is_failure("Logs are not generated with the device FQDN")

test

test() -> None

Run VerifyLoggingHostname validation.

Source code in anta/tests/logging.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
@AntaTest.anta_test
def test(self) -> None:
    """
    Run VerifyLoggingHostname validation.
    """
    output_hostname = self.instance_commands[0].json_output
    output_logging = self.instance_commands[2].text_output
    fqdn = output_hostname["fqdn"]
    lines = output_logging.strip().split("\n")[::-1]

    log_pattern = r"ANTA VerifyLoggingHostname validation"

    last_line_with_pattern = ""
    for line in lines:
        if re.search(log_pattern, line):
            last_line_with_pattern = line
            break

    if fqdn in last_line_with_pattern:
        self.result.is_success()
    else:
        self.result.is_failure("Logs are not generated with the device FQDN")

VerifyLoggingHosts

Bases: 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.
Source code in anta/tests/logging.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class VerifyLoggingHosts(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_test
    def test(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'.
        """
        if not hosts or not vrf:
            self.result.is_skipped(f"{self.__class__.name} did not run because hosts or vrf were not supplied")
            return

        output = self.instance_commands[0].text_output

        not_configured = []

        for host in hosts:
            pattern = rf"Logging to '{host}'.*VRF {vrf}"
            if not re.search(pattern, _get_logging_states(self.logger, output)):
                not_configured.append(host)

        if not not_configured:
            self.result.is_success()
        else:
            self.result.is_failure(f"Syslog servers {not_configured} are not configured in VRF {vrf}")

test

test(
    hosts: Optional[List[str]] = None, vrf: str = "default"
) -> None

Run VerifyLoggingHosts validation.

Parameters:

Name Type Description Default
hosts Optional[List[str]]

List of hosts (syslog servers) IP addresses.

None
vrf str

The name of the VRF to transport log messages. Defaults to ‘default’.

'default'
Source code in anta/tests/logging.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
@AntaTest.anta_test
def test(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'.
    """
    if not hosts or not vrf:
        self.result.is_skipped(f"{self.__class__.name} did not run because hosts or vrf were not supplied")
        return

    output = self.instance_commands[0].text_output

    not_configured = []

    for host in hosts:
        pattern = rf"Logging to '{host}'.*VRF {vrf}"
        if not re.search(pattern, _get_logging_states(self.logger, output)):
            not_configured.append(host)

    if not not_configured:
        self.result.is_success()
    else:
        self.result.is_failure(f"Syslog servers {not_configured} are not configured in VRF {vrf}")

VerifyLoggingLogsGeneration

Bases: 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.
Source code in anta/tests/logging.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class VerifyLoggingLogsGeneration(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_test
    def test(self) -> None:
        """
        Run VerifyLoggingLogs validation.
        """
        log_pattern = r"ANTA VerifyLoggingLogsGeneration validation"

        output = self.instance_commands[1].text_output
        lines = output.strip().split("\n")[::-1]

        for line in lines:
            if re.search(log_pattern, line):
                self.result.is_success()
                return

        self.result.is_failure("Logs are not generated")

test

test() -> None

Run VerifyLoggingLogs validation.

Source code in anta/tests/logging.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@AntaTest.anta_test
def test(self) -> None:
    """
    Run VerifyLoggingLogs validation.
    """
    log_pattern = r"ANTA VerifyLoggingLogsGeneration validation"

    output = self.instance_commands[1].text_output
    lines = output.strip().split("\n")[::-1]

    for line in lines:
        if re.search(log_pattern, line):
            self.result.is_success()
            return

    self.result.is_failure("Logs are not generated")

VerifyLoggingPersistent

Bases: 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.
Source code in anta/tests/logging.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class VerifyLoggingPersistent(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_test
    def test(self) -> None:
        """
        Run VerifyLoggingPersistent validation.
        """
        self.result.is_success()

        log_output = self.instance_commands[0].text_output
        dir_flash_output = self.instance_commands[1].text_output

        if "Persistent logging: disabled" in _get_logging_states(self.logger, log_output):
            self.result.is_failure("Persistent logging is disabled")
            return

        pattern = r"-rw-\s+(\d+)"
        persist_logs = re.search(pattern, dir_flash_output)

        if not persist_logs or int(persist_logs.group(1)) == 0:
            self.result.is_failure("No persistent logs are saved in flash")

test

test() -> None

Run VerifyLoggingPersistent validation.

Source code in anta/tests/logging.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@AntaTest.anta_test
def test(self) -> None:
    """
    Run VerifyLoggingPersistent validation.
    """
    self.result.is_success()

    log_output = self.instance_commands[0].text_output
    dir_flash_output = self.instance_commands[1].text_output

    if "Persistent logging: disabled" in _get_logging_states(self.logger, log_output):
        self.result.is_failure("Persistent logging is disabled")
        return

    pattern = r"-rw-\s+(\d+)"
    persist_logs = re.search(pattern, dir_flash_output)

    if not persist_logs or int(persist_logs.group(1)) == 0:
        self.result.is_failure("No persistent logs are saved in flash")

VerifyLoggingSourceIntf

Bases: 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.
Source code in anta/tests/logging.py
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
class VerifyLoggingSourceIntf(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_test
    def test(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'.
        """
        if not intf or not vrf:
            self.result.is_skipped(f"{self.__class__.name} did not run because intf or vrf was not supplied")
            return

        output = self.instance_commands[0].text_output

        pattern = rf"Logging source-interface '{intf}'.*VRF {vrf}"

        if re.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}")

test

test(
    intf: Optional[str] = None, vrf: str = "default"
) -> None

Run VerifyLoggingSrcDst validation.

Parameters:

Name Type Description Default
intf Optional[str]

Source-interface to use as source IP of log messages.

None
vrf str

The name of the VRF to transport log messages. Defaults to ‘default’.

'default'
Source code in anta/tests/logging.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@AntaTest.anta_test
def test(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'.
    """
    if not intf or not vrf:
        self.result.is_skipped(f"{self.__class__.name} did not run because intf or vrf was not supplied")
        return

    output = self.instance_commands[0].text_output

    pattern = rf"Logging source-interface '{intf}'.*VRF {vrf}"

    if re.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}")

VerifyLoggingTimestamp

Bases: 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.
Source code in anta/tests/logging.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
class VerifyLoggingTimestamp(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_test
    def test(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_output

        lines = output.strip().split("\n")[::-1]

        last_line_with_pattern = ""
        for line in lines:
            if re.search(log_pattern, line):
                last_line_with_pattern = line
                break

        if re.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")

test

test() -> None

Run VerifyLoggingTimestamp validation.

Source code in anta/tests/logging.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
@AntaTest.anta_test
def test(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_output

    lines = output.strip().split("\n")[::-1]

    last_line_with_pattern = ""
    for line in lines:
        if re.search(log_pattern, line):
            last_line_with_pattern = line
            break

    if re.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")

Last update: July 19, 2023