# πŸ” Log Monitor β€” User Guide ## Overview The Log Monitor scans log files on a schedule, matches regex patterns, and sends alerts via Telegram, Webhooks, Microsoft Teams, or Email. It supports time-based filtering, scheduled scans (daily, hourly, continuous), and automatic attachment of full results when matches exceed a threshold. --- ## Starting the Monitor ### Option 1: Environment File ```bash cp .env.monitor .env node dist-server/server.js ``` ### Option 2: Inline ```bash LOG_MODE=monitor PORT=8082 MONITOR_SCAN_INTERVAL=30000 node dist-server/server.js ``` ### Option 3: Docker ```bash docker run -d -p 8082:8080 \ -e LOG_MODE=monitor \ -e MONITOR_SCAN_INTERVAL=30000 \ -v /var/log:/logs:ro \ logviewer ``` ### Option 4: Config File ```json { "mode": "monitor", "port": 8082, "monitor": { "enabled": true, "scanIntervalMs": 30000, "rules": [] } } ``` --- ## How It Works ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Log Files │────▢│ Scan Engine │────▢│ Pattern Match β”‚ β”‚ (sources) β”‚ β”‚ (interval) β”‚ β”‚ (regex) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ matches found? β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ ≀ maxLines > maxLines β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β” β”‚ Inline β”‚ β”‚ Inline (N) β”‚ β”‚ in alert β”‚ β”‚ + .gz attach β”‚ β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Send Notifications β”‚ β”‚ Telegram β”‚ Webhook β”‚ Teams β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` 1. **Scan** β€” Every N seconds (configurable), the monitor reads all source files 2. **Filter** β€” Only files matching the time range are scanned (today, this week, etc.) 3. **Match** β€” Each line is tested against the configured regex patterns 4. **Alert** β€” If matches are found AND cooldown has expired, notifications are sent 5. **Attach** β€” If matches exceed `maxLinesInMessage`, full results are gzipped and attached --- ## GUI Guide ### Layout ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ πŸ” Log Monitor 2 active rules Β· 3 alertsβ”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ β”‚ πŸ“‹ Rules β”‚ Rule Editor / Alert Detail β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚ β”‚ β”‚ │●Root DB β”‚β”‚ [General] β”‚ β”‚ β”‚ 2 src β”‚β”‚ Name: Root DB Access Detection β”‚ β”‚ β”‚ 6 match β”‚β”‚ Enabled: Yes β”‚ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”‚ β”‚ β”‚ │●Failed β”‚β”‚ [Sources] β”‚ β”‚ β”‚ 1 src β”‚β”‚ /var/log/db-audit.log β”‚ β”‚ β”‚ 4 match β”‚β”‚ /var/log/db-audit-*.log.gz β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚ β”‚ β”‚ β”‚ [Patterns] β”‚ β”‚ πŸ“Š Alerts β”‚ root.*login β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚ GRANT.*PRIVILEGES β”‚ β”‚ β”‚πŸš¨ Root DBβ”‚β”‚ DROP TABLE β”‚ β”‚ β”‚ 6 matchesβ”‚β”‚ β”‚ β”‚ β”‚ 10:30:01 β”‚β”‚ [Schedule] β”‚ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”‚ Mode: Daily at 07:00 β”‚ β”‚ β”‚πŸš¨ Failed β”‚β”‚ β”‚ β”‚ β”‚ 4 matchesβ”‚β”‚ [Notifications] β”‚ β”‚ β”‚ 10:30:01 β”‚β”‚ [TELEGRAM] Bot: 811... Chat: 859... β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚ [WEBHOOK] https://hooks.slack.com/... β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ [πŸ’Ύ Save] [πŸ§ͺ Test Now] [πŸ—‘ Delete] β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Creating a Rule 1. Click **"+ New"** in the sidebar 2. Fill in the fields: - **Name** β€” descriptive name for the rule - **Sources** β€” one file path per line (supports globs like `/var/log/*.log`) - **Time Range** β€” which files to scan (today, week, month, last N hours, all) - **Patterns** β€” one regex per line - **Match Logic** β€” "any" (OR) or "all" (AND) - **Schedule** β€” continuous, daily at time, or every N hours - **Max Lines** β€” how many lines to include inline in notifications - **Cooldown** β€” minimum time between alerts (prevents spam) 3. Add notification channels 4. Click **"πŸ’Ύ Save Rule"** ### Testing a Rule 1. Select a rule from the sidebar 2. Click **"πŸ§ͺ Test Now"** 3. The rule runs immediately and shows results below the form 4. This does NOT respect cooldown β€” always runs ### Testing Notifications 1. In the Notifications section, configure a channel 2. Click **"πŸ§ͺ Test Notification"** 3. A test message is sent to verify the channel works ### Viewing Alerts - Alerts appear in the **"πŸ“Š Recent Alerts"** section of the sidebar - Click an alert to see the full detail view with all matched lines - Lines are color-coded: red for ERROR/FATAL, orange for WARN --- ## Configuration Reference ### Environment Variables | Variable | Default | Description | |----------|---------|-------------| | `LOG_MODE` | `both` | Set to `monitor` | | `PORT` | `8080` | HTTP port | | `MONITOR_ENABLED` | `true` | Enable/disable scanning | | `MONITOR_SCAN_INTERVAL` | `60000` | Scan interval in ms | | `MONITOR_STORAGE_DIR` | `./monitor-data` | Where to store state | | `MONITOR_RULES` | `[]` | JSON array of rules (see below) | ### Rule Structure ```json { "id": "unique-id", "name": "Human-readable name", "enabled": true, "sources": ["/path/to/logs/*.log"], "patterns": ["regex1", "regex2"], "patternLogic": "any", "timeRange": { "type": "week", "hours": 24 }, "schedule": { "mode": "daily", "value": "07:00", "timezone": "Europe/Berlin" }, "notifications": [ { "type": "telegram", "botToken": "123:ABC", "chatId": "-100123" } ], "maxLinesInMessage": 20, "cooldownMs": 300000, "matchCount": 0, "lastTriggered": null } ``` ### Time Range Options | Type | Description | Use Case | |------|-------------|----------| | `today` | Only files modified today | Daily reports | | `week` | Files from last 7 days | Weekly audits | | `month` | Files from last 30 days | Monthly reviews | | `hours` | Files from last N hours | Recent activity | | `all` | All files regardless of age | Full history scan | ### Schedule Options | Mode | Value | Description | |------|-------|-------------| | `interval` | β€” | Runs every scan interval (continuous monitoring) | | `daily` | `07:00` | Runs once per day at the specified time | | `hourly` | `2` | Runs every N hours | ### Notification Channels **Telegram:** ```json { "type": "telegram", "botToken": "123456:ABC-DEF...", "chatId": "8599028500" } ``` **Webhook (Slack, Discord, custom):** ```json { "type": "webhook", "url": "https://hooks.slack.com/services/T.../B.../..." } ``` **Microsoft Teams:** ```json { "type": "teams", "url": "https://outlook.office.com/webhook/..." } ``` **Email (SMTP):** ```json { "type": "email", "to": "admin@company.com", "smtpHost": "smtp.gmail.com", "smtpPort": 587, "smtpUser": "alerts@company.com", "smtpPass": "app-password" } ``` --- ## Example Configurations ### 1. Real-time Root Login Detection Scans every 10 seconds, alerts immediately via Telegram: ```env LOG_MODE=monitor PORT=8082 MONITOR_SCAN_INTERVAL=10000 MONITOR_RULES=[{"id":"root-login","name":"Root Login Alert","enabled":true,"sources":["/var/log/auth.log"],"patterns":["session opened for user root","su.*root","sudo.*root"],"patternLogic":"any","timeRange":{"type":"today"},"schedule":{"mode":"interval"},"notifications":[{"type":"telegram","botToken":"YOUR_BOT_TOKEN","chatId":"YOUR_CHAT_ID"}],"maxLinesInMessage":10,"cooldownMs":60000,"matchCount":0,"lastTriggered":null}] ``` ### 2. Daily DB Audit Report at 7:00 AM Scans once per day, sends full report: ```env LOG_MODE=monitor PORT=8082 MONITOR_SCAN_INTERVAL=60000 MONITOR_RULES=[{"id":"db-audit","name":"Daily DB Audit","enabled":true,"sources":["/var/log/postgresql/audit.log","/var/log/mysql/audit*.log"],"patterns":["admin.*LOGIN","GRANT","DROP","ALTER.*TABLE","CREATE USER"],"patternLogic":"any","timeRange":{"type":"today"},"schedule":{"mode":"daily","value":"07:00","timezone":"Europe/Berlin"},"notifications":[{"type":"telegram","botToken":"TOKEN","chatId":"CHAT_ID"},{"type":"webhook","url":"https://hooks.slack.com/services/..."}],"maxLinesInMessage":20,"cooldownMs":86400000,"matchCount":0,"lastTriggered":null}] ``` ### 3. Error Spike Detection (every 5 minutes) ```env LOG_MODE=monitor PORT=8082 MONITOR_SCAN_INTERVAL=30000 MONITOR_RULES=[{"id":"error-spike","name":"Error Spike","enabled":true,"sources":["/app/logs/production.log"],"patterns":["\\bERROR\\b","\\bFATAL\\b","Exception","panic:"],"patternLogic":"any","timeRange":{"type":"hours","hours":1},"schedule":{"mode":"hourly","value":"0.083"},"notifications":[{"type":"teams","url":"https://outlook.office.com/webhook/..."}],"maxLinesInMessage":30,"cooldownMs":300000,"matchCount":0,"lastTriggered":null}] ``` ### 4. Security: Unauthorized Access Attempts ```env MONITOR_RULES=[{"id":"unauth","name":"Unauthorized Access","enabled":true,"sources":["/var/log/nginx/access.log","/var/log/nginx/error.log"],"patterns":["403 Forbidden","401 Unauthorized","\\.\\./\\.\\./","etc/passwd","