Skip to content

Commit

Permalink
test: read syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Nov 26, 2024
1 parent 7bd8758 commit c8191f5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ jobs:
cmake --build build
./build/ua2f --version
sudo python scripts/test.py ./build/ua2f
- name: Upload log
uses: actions/upload-artifact@v4
with:
name: integration-test-log
path: logs.txt

build:
name: Build ${{ matrix.arch }}
Expand Down
67 changes: 67 additions & 0 deletions scripts/journal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import systemd.journal
from typing import List, Dict, Optional

class SystemdLogReader:
def __init__(self):
self.journal = systemd.journal.Reader()

def _format_timestamp(self, timestamp):
"""
格式化时间戳
:param timestamp: systemd日志时间戳
:return: 格式化的日期时间字符串
"""
return timestamp.strftime('%Y-%m-%d %H:%M:%S') if timestamp else None

def read_logs(
self,
count: int = 50,
priority: Optional[int] = None,
match_unit: Optional[str] = None,
match_identifier: Optional[str] = None
) -> List[Dict]:
logs = []

# 配置日志过滤条件
if priority is not None:
self.journal.add_match(PRIORITY=priority)

if match_unit:
self.journal.add_match(_SYSTEMD_UNIT=match_unit)

if match_identifier:
self.journal.add_match(SYSLOG_IDENTIFIER=match_identifier)

# 按时间倒序排序
self.journal.seek_tail()
self.journal.get_previous()

# 读取日志
for entry in self.journal:
log_entry = {
'timestamp': self._format_timestamp(entry.get('__REALTIME_TIMESTAMP')),
'message': entry.get('MESSAGE', ''),
'unit': entry.get('_SYSTEMD_UNIT', ''),
'process_name': entry.get('SYSLOG_IDENTIFIER', ''),
'pid': entry.get('_PID', ''),
'hostname': entry.get('_HOSTNAME', ''),
'priority_level': entry.get('PRIORITY', '')
}

logs.append(log_entry)

if len(logs) >= count:
break

return logs

def filter_logs_by_keyword(
self,
logs: List[Dict],
keyword: str
) -> List[Dict]:
return [
log for log in logs
if keyword.lower() in log['message'].lower()
]
3 changes: 2 additions & 1 deletion scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
fake-useragent
tqdm
tqdm
systemd-python
7 changes: 7 additions & 0 deletions scripts/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import atexit
import http.server
import json
import logging
import os
import socketserver
Expand All @@ -12,6 +13,8 @@
from fake_useragent import UserAgent
from tqdm import tqdm

from scripts.journal import SystemdLogReader

ua = UserAgent()

PORT = 37491
Expand Down Expand Up @@ -88,6 +91,10 @@ def cleanup_iptables():
assert response.ok
assert response.text == str(len(nxt))

logger = SystemdLogReader()
logs = logger.read_logs(100)
with open("logs.txt", "w") as f:
f.write(json.dumps(logs, indent=4))

# clean
cleanup_iptables()

0 comments on commit c8191f5

Please sign in to comment.