|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Syslog Pattern Matching |
| 4 | +
|
| 5 | +Verify the select-match feature: filtering syslog messages based on |
| 6 | +pattern-match (POSIX regex) on message content. Tests both simple |
| 7 | +substring matching and complex regex patterns. |
| 8 | +""" |
| 9 | + |
| 10 | +import infamy |
| 11 | +import time |
| 12 | + |
| 13 | +with infamy.Test() as test: |
| 14 | + with test.step("Set up topology and attach to target DUT"): |
| 15 | + env = infamy.Env() |
| 16 | + target = env.attach("target", "mgmt") |
| 17 | + tgtssh = env.attach("target", "mgmt", "ssh") |
| 18 | + |
| 19 | + with test.step("Clean up old log files from previous test runs"): |
| 20 | + tgtssh.runsh("sudo rm -f /var/log/{errors,routers,all-messages}") |
| 21 | + |
| 22 | + with test.step("Configure syslog with pattern-match filters"): |
| 23 | + target.put_config_dicts({ |
| 24 | + "ietf-syslog": { |
| 25 | + "syslog": { |
| 26 | + "actions": { |
| 27 | + "file": { |
| 28 | + "log-file": [{ |
| 29 | + "name": "file:errors", |
| 30 | + "pattern-match": "ERROR|CRITICAL", |
| 31 | + }, { |
| 32 | + "name": "file:routers", |
| 33 | + "pattern-match": "router[0-9]+", |
| 34 | + "facility-filter": { |
| 35 | + "facility-list": [{ |
| 36 | + "facility": "all", |
| 37 | + "severity": "info" |
| 38 | + }] |
| 39 | + } |
| 40 | + }, { |
| 41 | + "name": "file:all-messages", |
| 42 | + "facility-filter": { |
| 43 | + "facility-list": [{ |
| 44 | + "facility": "all", |
| 45 | + "severity": "info" |
| 46 | + }] |
| 47 | + } |
| 48 | + }] |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + with test.step("Send test messages with various patterns"): |
| 56 | + tgtssh.runsh("logger -t test -p daemon.info 'ERROR: Connection failed on interface eth0'") |
| 57 | + tgtssh.runsh("logger -t test -p daemon.info 'CRITICAL: System temperature high'") |
| 58 | + tgtssh.runsh("logger -t test -p daemon.info 'Status update from router1: link up'") |
| 59 | + tgtssh.runsh("logger -t test -p daemon.info 'Status update from router42: link down'") |
| 60 | + tgtssh.runsh("logger -t test -p daemon.info 'INFO: Normal operation message'") |
| 61 | + tgtssh.runsh("logger -t test -p daemon.info 'DEBUG: Verbose logging enabled'") |
| 62 | + time.sleep(1) |
| 63 | + |
| 64 | + with test.step("Verify errors log contains ERROR and CRITICAL messages"): |
| 65 | + rc = tgtssh.runsh("grep -c 'ERROR\\|CRITICAL' /var/log/errors 2>/dev/null") |
| 66 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 67 | + if count != 2: |
| 68 | + test.fail(f"Expected 2 ERROR/CRITICAL messages in /var/log/errors, got {count}") |
| 69 | + |
| 70 | + # Verify it does NOT contain other messages |
| 71 | + rc = tgtssh.runsh("grep -c 'router1\\|Normal operation\\|Verbose' /var/log/errors 2>/dev/null") |
| 72 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 73 | + if count != 0: |
| 74 | + test.fail(f"Expected 0 non-error messages in /var/log/errors, got {count}") |
| 75 | + |
| 76 | + with test.step("Verify routers log contains matching router[0-9]+ pattern"): |
| 77 | + rc = tgtssh.runsh("grep -c 'router[0-9]\\+' /var/log/routers 2>/dev/null") |
| 78 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 79 | + if count != 2: |
| 80 | + test.fail(f"Expected 2 router messages in /var/log/routers, got {count}") |
| 81 | + |
| 82 | + # Verify both router1 and router42 are present |
| 83 | + rc = tgtssh.runsh("grep -q 'router1' /var/log/routers 2>/dev/null") |
| 84 | + if rc.returncode != 0: |
| 85 | + test.fail("Expected router1 message in /var/log/routers") |
| 86 | + |
| 87 | + rc = tgtssh.runsh("grep -q 'router42' /var/log/routers 2>/dev/null") |
| 88 | + if rc.returncode != 0: |
| 89 | + test.fail("Expected router42 message in /var/log/routers") |
| 90 | + |
| 91 | + # Verify it does NOT contain error or normal messages |
| 92 | + rc = tgtssh.runsh("grep -c 'ERROR\\|CRITICAL\\|Normal operation\\|Verbose' /var/log/routers 2>/dev/null") |
| 93 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 94 | + if count != 0: |
| 95 | + test.fail(f"Expected 0 non-router messages in /var/log/routers, got {count}") |
| 96 | + |
| 97 | + with test.step("Verify all-messages log contains all test messages"): |
| 98 | + rc = tgtssh.runsh("grep -c 'test' /var/log/all-messages 2>/dev/null") |
| 99 | + count = int(rc.stdout.strip()) if rc.returncode == 0 else 0 |
| 100 | + if count != 6: |
| 101 | + test.fail(f"Expected 6 total messages in /var/log/all-messages, got {count}") |
| 102 | + |
| 103 | + test.succeed() |
0 commit comments