Export Lynis audit metrics to Prometheus for monitoring security posture over time.
Exported Metrics (4):
lynis_hardening_index- Security score (0-100)lynis_tests_done- Total tests performedlynis_warnings- Critical issues countlynis_suggestions- Recommendations count
Update Frequency: Weekly (default) or on-demand
Required:
- Prometheus installed
- node_exporter with textfile collector enabled
Verify node_exporter:
systemctl status node_exporter
# Check textfile collector directory
ls -la /var/lib/node_exporter/textfile_collector/If missing: Install node_exporter with --collector.textfile.directory=/var/lib/node_exporter/textfile_collector.
# Make script executable
chmod +x ../scripts/lynis-metrics-exporter.sh
# Copy to system path (optional)
sudo cp ../scripts/lynis-metrics-exporter.sh /usr/local/bin/
# Or run from component directory
sudo ../scripts/lynis-metrics-exporter.sh --run-auditsudo tee /etc/systemd/system/lynis-metrics-exporter.service > /dev/null << 'EOF'
[Unit]
Description=Lynis Prometheus Metrics Exporter
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/lynis-metrics-exporter.sh --run-audit
StandardOutput=journal
StandardError=journal
EOFsudo tee /etc/systemd/system/lynis-metrics-exporter.timer > /dev/null << 'EOF'
[Unit]
Description=Weekly Lynis Metrics Export
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
EOFsudo systemctl daemon-reload
sudo systemctl enable lynis-metrics-exporter.timer
sudo systemctl start lynis-metrics-exporter.timer
# Verify timer
sudo systemctl status lynis-metrics-exporter.timerFile: /var/lib/node_exporter/textfile_collector/lynis.prom
Example:
# HELP lynis_hardening_index Lynis Hardening Index (0-100)
# TYPE lynis_hardening_index gauge
lynis_hardening_index 80
# HELP lynis_tests_done Total number of tests performed
# TYPE lynis_tests_done counter
lynis_tests_done 275
# HELP lynis_warnings Number of warnings found
# TYPE lynis_warnings gauge
lynis_warnings 1
# HELP lynis_suggestions Number of suggestions made
# TYPE lynis_suggestions gauge
lynis_suggestions 38
Edit: /etc/prometheus/prometheus.yml
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100'] # node_exporter with textfile collectorReload Prometheus:
sudo systemctl reload prometheusPrometheus Web UI: http://localhost:9090
Query:
lynis_hardening_index
Expected: Graph showing hardening index over time.
Query:
lynis_hardening_index
Visualization: Gauge Min: 0 Max: 100 Thresholds:
- Red: 0-59
- Yellow: 60-79
- Green: 80-100
Query:
lynis_warnings
lynis_suggestions
Visualization: Graph Legend: {{name}}
Query:
lynis_tests_done
Visualization: Stat
Query:
lynis_hardening_index
Visualization: Graph Time Range: Last 90 days
{
"type": "gauge",
"title": "Lynis Hardening Index",
"targets": [
{
"expr": "lynis_hardening_index",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"min": 0,
"max": 100,
"thresholds": {
"steps": [
{ "color": "red", "value": 0 },
{ "color": "yellow", "value": 60 },
{ "color": "green", "value": 80 }
]
}
}
}
}File: /etc/prometheus/rules/lynis_alerts.yml
groups:
- name: lynis_alerts
interval: 1h
rules:
- alert: LynisHardeningIndexLow
expr: lynis_hardening_index < 60
for: 1h
labels:
severity: warning
annotations:
summary: "Lynis Hardening Index below 60"
description: "Current index: {{ $value }}. Review hardening guide."
- alert: LynisWarningsIncreased
expr: delta(lynis_warnings[7d]) > 5
for: 1h
labels:
severity: warning
annotations:
summary: "Lynis warnings increased by 5+ in 7 days"
description: "New warnings detected. Review report."Reload Prometheus:
sudo systemctl reload prometheussudo ../scripts/lynis-metrics-exporter.shsudo ../scripts/lynis-metrics-exporter.sh --run-auditcat /var/lib/node_exporter/textfile_collector/lynis.promWeekly audits balance freshness with performance overhead.
sudo systemctl enable lynis-metrics-exporter.timer# Edit timer
sudo systemctl edit lynis-metrics-exporter.timer
# Change:
[Timer]
OnCalendar=daily# Run after hardening changes
sudo /usr/local/bin/lynis-metrics-exporter.sh --run-auditCheck:
# Timer status
sudo systemctl status lynis-metrics-exporter.timer
# Service logs
sudo journalctl -u lynis-metrics-exporter.service -n 50
# Metrics file permissions
ls -la /var/lib/node_exporter/textfile_collector/lynis.promCheck:
# node_exporter running?
systemctl status node_exporter
# Textfile collector enabled?
ps aux | grep node_exporter | grep textfileFix:
# Force fresh audit
sudo lynis audit system --quick
sudo ../scripts/lynis-metrics-exporter.sh
# Restart node_exporter (re-reads files)
sudo systemctl restart node_exporter- SETUP.md - Installation & first audit
- HARDENING_GUIDE.md - Improve hardening index
- CUSTOM_PROFILES.md - Reduce false-positives
- TROUBLESHOOTING.md - Common issues