Warning
This project has been superseded by logwatch-ai-go - a complete rewrite in Go with improved performance and simpler deployment.
This Node.js version is archived and no longer maintained.
An automated system that analyzes logwatch reports using Claude AI and sends summarized insights via Telegram bot.
- Daily automated logwatch analysis using Claude AI
- Intelligent summarization of system logs
- Critical issue detection and prioritization
- Actionable recommendations
- Telegram notifications with formatted reports
- Historical trend analysis
- SQLite database for storing analysis history
- Comprehensive error handling and logging
- Prompt caching for cost optimization (16-30% savings per analysis)
- Ubuntu 24.04.2 LTS (or similar Linux distribution)
- Node.js 20 or higher
- logwatch installed (
apt-get install logwatch) - Cron for scheduled logwatch generation (see Cron Setup)
- Claude API key (Anthropic)
- Telegram Bot token and chat ID
Build a standalone executable that includes Node.js runtime and all dependencies:
# Build binary
npm install
npm run build
# Deploy
scp dist/logwatch-ai-linux-x64 user@server:/opt/logwatch-ai/
scp dist/sql-wasm.wasm user@server:/opt/logwatch-ai/
scp .env user@server:/opt/logwatch-ai/
# Run on target server (no Node.js needed!)
./logwatch-ai-linux-x64Benefits:
- No Node.js installation required on target servers
- Single executable file (~80-100MB)
- Simplified deployment
- Ideal for production servers
See Build Documentation for detailed instructions.
Traditional installation with Node.js runtime:
cd /opt
git clone https://github.com/olegiv/logwatch-ai.git logwatch-ai
# OR download and extract the project filescd logwatch-ai
chmod +x scripts/install.sh
./scripts/install.shThe installation script will:
- Check prerequisites (Node.js, npm, logwatch)
- Install Node.js dependencies
- Create the necessary directories
- Set up environment configuration
- Configure cron job for daily execution
- Optionally create systemd service
Edit the .env file and add your credentials:
nano .envRequired settings:
ANTHROPIC_API_KEY=sk-ant-your-actual-key-here
TELEGRAM_BOT_TOKEN=your-bot-token-here
TELEGRAM_CHANNEL_ARCHIVE_ID=your-archive-channel-id-here
TELEGRAM_CHANNEL_ALERTS_ID=your-alerts-channel-id-here # OptionalLogwatch AI uses two Telegram channels for smart reporting:
-
Archive Channel (required) - "Logwatch AI Archive"
- Receives all reports (full details)
- Historical record of every analysis
- Complete summary, metrics, and recommendations
-
Alerts Channel (optional) - "Logwatch AI Alerts"
- Receives full reports when system status is worse than "Good"
- Same complete report as Archive channel
- Silent when status is "Excellent" or "Good"
Benefits:
- Keep full history in Archive, get alerts only when needed
- Team members can subscribe to Alerts channel only
- Reduce notification fatigue (only alerted when status degrades)
- Better incident response workflow
- Open Telegram and search for @BotFather
- Send
/newbotand follow instructions - Choose a name (e.g., "Logwatch Reporter")
- Choose a username (e.g., "my_logwatch_bot")
- Copy the bot token - you'll need this for
.env
- In Telegram, click menu (β°) β New Channel
- Enter channel name: "Logwatch AI Archive"
- Add description: "Full daily logwatch analysis reports"
- Choose Private Channel
- Click Create
- Skip adding subscribers for now
- Create another channel: "Logwatch AI Alerts"
- Add description: "Critical issues and warnings only"
- Choose Private Channel
- Click Create
For each channel:
- Open the channel
- Click on the channel name β Administrators β Add Administrator
- Search for your bot (e.g.,
@my_logwatch_bot) - Give it "Post Messages" permission only
- Click Done
- Post a test message to Archive channel
- Post a test message to Alerts channel (if created)
- Run the helper script:
node scripts/get-channel-id.js
- The script will show both channel IDs
- Update your
.envfile:# Archive channel (required) TELEGRAM_CHANNEL_ARCHIVE_ID=-1001234567890 # Alerts channel (optional - leave empty to disable) TELEGRAM_CHANNEL_ALERTS_ID=-1009876543210
Archive Channel: Invite technical team members who need full details
Alerts Channel: Invite on-call team, managers, or anyone who needs immediate notifications
Members will have read-only access to both channels.
node scripts/test.jsThis will validate:
- Configuration files
- API connections (Claude and Telegram)
- Database operations
- File permissions
- Logwatch availability
npm startThis will run a complete analysis cycle and send a report to Telegram.
logwatch-ai/
βββ package.json # Node.js dependencies
βββ .env # Environment configuration (create from .env.template)
βββ .env.template # Environment template
βββ README.md # This file
βββ config/
β βββ config.js # Configuration loader
βββ src/
β βββ analyzer.js # Main orchestrator
β βββ claude-client.js # Claude API client
β βββ telegram-client.js # Telegram bot client
β βββ logwatch-reader.js # Logwatch file reader
β βββ storage.js # SQLite database handler
β βββ utils/
β βββ logger.js # Logging utility
β βββ prompts.js # Claude prompts
βββ logs/
β βββ app.log # Application logs
β βββ cron.log # Cron execution logs
βββ data/
β βββ summaries.db # SQLite database
βββ scripts/
βββ install.sh # Installation script
βββ test.js # Configuration test script
βββ get-channel-id.js # Helper to get Telegram channel ID
The system runs automatically via cron (default: 2:00 AM for logwatch generation, 2:15 AM for analysis):
# View current cron jobs
crontab -l
# Edit cron schedule
crontab -eSet up two cron jobs:
1. Root cron job (generates logwatch file daily at 2 AM):
# Edit root crontab
sudo crontab -e
# Add this line:
0 2 * * * /opt/logwatch-ai/scripts/generate-logwatch.sh2. User cron job (runs analyzer at 2:15 AM, after logwatch generation):
# Edit your user crontab
crontab -e
# For Linux:
15 2 * * * cd /opt/logwatch-ai && /usr/bin/node src/analyzer.js >> logs/cron.log 2>&1
# For macOS:
15 2 * * * cd /Users/yourusername/Desktop/Projects/AI/logwatch-ai && /usr/local/bin/node src/analyzer.js >> logs/cron.log 2>&1Notes:
- Replace paths according to your installation directory
- Use
which nodeto find your Node.js path - Logwatch generation MUST run as root (in root crontab)
- Analyzer runs as your user (in user crontab), 15 minutes after generation
- See docs/CRON_SETUP.md for detailed cron installation instructions
# Run analyzer
npm start
# Or with node directly
node src/analyzer.js
# Using systemd (if configured)
sudo systemctl start logwatch-ai# Application logs
tail -f logs/app.log
# Cron execution logs
tail -f logs/cron.log
# View last 100 lines
tail -n 100 logs/app.log# View database statistics
sqlite3 data/summaries.db "SELECT COUNT(*) FROM summaries;"
# View recent summaries
sqlite3 data/summaries.db "SELECT date, critical_count, warning_count FROM summaries ORDER BY timestamp DESC LIMIT 10;"
# Cleanup old entries (older than 90 days is done automatically)Edit .env to customize behavior:
# Claude API
ANTHROPIC_API_KEY=sk-ant-xxxxx
CLAUDE_MODEL=claude-sonnet-4-5-20250929
# Telegram Bot
TELEGRAM_BOT_TOKEN=xxxxx
TELEGRAM_CHANNEL_ARCHIVE_ID=xxxxx # Required - full reports
TELEGRAM_CHANNEL_ALERTS_ID=xxxxx # Optional - alerts only
# Logwatch
LOGWATCH_OUTPUT_PATH=/tmp/logwatch-output.txt
MAX_LOG_SIZE_MB=10
# Application
NODE_ENV=production
LOG_LEVEL=info # debug, info, warn, error
ENABLE_DATABASE=true
DATABASE_PATH=./data/summaries.dbComplete daily reports with:
- π Report header (host, date, timezone, status)
- π Summary: Brief overview of system health
β οΈ Critical Issues: Urgent problems requiring attention- β‘ Warnings: Non-critical but concerning issues
- π‘ Recommendations: Actionable steps
- π Key Metrics: Important numbers (failed logins, errors, disk usage)
Full reports are sent when system status is worse than "Good":
- Same complete report as an Archive channel
- Triggered when status is: Satisfactory, Bad, or Awful
- Includes all sections: Summary, Issues, Warnings, Recommendations, Metrics
Note: If status is "Excellent" or "Good", the alerts channel receives nothing.
Generate manually:
sudo ./scripts/generate-logwatch.shOr follow the Cron Setup Guide to configure automated generation.
Then update LOGWATCH_OUTPUT_PATH in .env
# Fix .env permissions
chmod 600 .env
# Fix directory ownership
sudo chown -R $USER:$USER /opt/logwatch-ai
# Fix log directory permissions
chmod 755 logs data- Verify API key is correct in
.env - Check API key has sufficient credits
- Review
logs/app.logfor detailed error messages
For Archive Channel (Required):
- Verify bot is added as administrator to the Archive channel
- Bot must have Post Messages permission
- Post a test message to the channel after adding the bot
- Run
node scripts/get-channel-id.jsto verify the channel ID - Channel ID should start with
-100(e.g.,-1001234567890) - Update
TELEGRAM_CHANNEL_ARCHIVE_IDin.env
For Alerts Channel (Optional):
- Same setup as Archive channel
- Update
TELEGRAM_CHANNEL_ALERTS_IDin.env - Leave empty to disable the alerts channel
- Reports sent when system status is worse than "Good" (Satisfactory/Bad/Awful)
General troubleshooting:
- Test with:
node scripts/test.js(tests both channels) - Verify credentials in
.envare correct - Check
logs/app.logfor detailed error messages - Try manual run:
npm start - Check bot permissions in channel settings
# Check cron service status
sudo systemctl status cron
# View cron logs
grep CRON /var/log/syslog
# Test cron job manually
cd /opt/logwatch-ai && node src/analyzer.js.envfile contains sensitive credentials - keep it secure (600 permissions)- API keys are never logged
- Database stores only analysis summaries, not raw logs
- Logwatch files are read-only access
- Consider using environment-specific API keys
Claude API costs (Sonnet 4.5 with prompt caching, as of 2025):
- Input: $3 per million tokens
- Output: $15 per million tokens
- Cache write: $3.75 per million tokens (1.25Γ input)
- Cache read: $0.30 per million tokens (0.1Γ input, 90% savings)
Per Analysis:
- First run (creates cache): ~$0.0160-0.0220
- Cached run (within 5 min): ~$0.0107-0.0154
- Savings per cached request: $0.0042-0.0066 (16-30% reduction)
Monthly Cost (Daily Automation):
- Without caching: ~$0.59/month
- With caching: ~$0.47/month
- Monthly savings: ~$0.12 (20% reduction)
Multi-Server Deployment:
- 10 servers within 5-minute window: 27% total savings
- First server creates cache, others benefit from shared cache
See CLAUDE.md for detailed cost breakdown and optimization strategies.
Logs are automatically rotated when they exceed 10MB. Maximum 5 log files are kept.
Database automatically removes entries older than 90 days during each run.
cd /opt/logwatch-ai
git pull # if using git
npm install # update dependencies# Remove cron job
crontab -e
# Delete the logwatch-ai line
# Remove systemd service (if created)
sudo systemctl stop logwatch-ai
sudo systemctl disable logwatch-ai
sudo rm /etc/systemd/system/logwatch-ai.service
sudo systemctl daemon-reload
# Remove installation directory
sudo rm -rf /opt/logwatch-aiMIT
For issues, questions, or contributions:
- Check logs:
logs/app.log - Run tests:
node scripts/test.js - Review configuration:
.envandconfig/config.js
- Standalone binary support - Build single executable with Node.js SEA
- Replaced
better-sqlite3withsql.js(pure JS/WASM) for SEA compatibility - Added esbuild bundling configuration with --keep-names
- Added automated build script for Linux x64 binaries
- No Node.js required on target servers (~80-100MB standalone binary)
- Updated all dependencies to latest stable versions
- Comprehensive build documentation in
docs/BUILD.md - Binary deployment simplifies production setup
- Prompt caching implementation - 16-30% cost savings per analysis
- Enhanced system prompt with comprehensive analysis framework
- Server-side cache sharing for multi-server deployments
- Cache statistics logging and monitoring
- Monthly cost reduction from ~$0.59 to ~$0.47
- Improved AI analysis quality with detailed guidelines
- Initial release
- Claude AI integration
- Telegram notifications
- SQLite database storage
- Historical trend analysis
- Automatic cron scheduling
- Comprehensive error handling