Skip to content

Commit

Permalink
Added files/varnishstat.py
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosabalde committed Feb 4, 2025
1 parent d2fed0d commit 248d63f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ While `varnishmon` **doesn't replace a comprehensive monitoring solutions like P
> Use the `--no-api` flag (or the `api.enabled` setting) to prevent the web interface from starting. You can still collect metrics and store them in the database.

- **Can I customize the metrics collected by `varnishmon`?**
> Not directly, as `varnishmon` is designed to collect comprehensive information. However, you can use the `--varnishstat` flag (or the `scraper.varnishstat` setting) to specify a wrapper script that filters the `varnishstat` output (or an inline command if you're up for the quoting challenge).
> Not directly, as `varnishmon` is designed to collect comprehensive information. However, you can use the `--varnishstat` flag (or the `scraper.varnishstat` setting) to specify a wrapper script that filters the `varnishstat` output (or an inline command if you're up for the quoting challenge). Check out [this example wrapper script](files/varnishstat.py) for inspiration on how to filter and/or extend the metrics collected.
> ```bash
> varnishmon \
> --period 15s \
Expand Down
1 change: 1 addition & 0 deletions files/varnishmon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ scraper:
period: 5s
timeout: 5s
varnishstat: /mnt/host/files/varnishstat.sh
# varnishstat: /usr/local/bin/uv run --quiet --python 3.12 --with psutil==6.1.1 /mnt/host/files/varnishstat.py

api:
enabled: true
Expand Down
62 changes: 62 additions & 0 deletions files/varnishstat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import re
import subprocess

import psutil

def varnishstat():
result = subprocess.run(
['/usr/bin/varnishstat', '-1', '-j'],
capture_output=True,
text=True)
assert result.returncode == 0, f'Command failed with exit code {result.returncode}'
return json.loads(result.stdout)

def main():
# Get 'varnishstat' metrics.
metrics = varnishstat()

# Filter out some metrics we prefer to ignore.
blacklist = re.compile(r'^(?:LCK|MEMPOOL)[.]')
metrics = {k: v for k, v in metrics.items() if not blacklist.match(k)}

# Add some additional ad-hoc metrics following the 'varnishstat' output
# format.
for k, v in psutil.cpu_times(percpu=False)._asdict().items():
metrics[f'CPU.time.{k}'] = {
'description': f"psutil.cpu_times(percpu=False)._asdict()['{k}'] * 1000",
'flag': 'c',
'format': 'i',
'value': int(v * 1000),
}
for k, v in psutil.virtual_memory()._asdict().items():
metrics[f'MEMORY.{k}'] = {
'description': f"psutil.virtual_memory()._asdict()['{k}']",
'flag': 'g',
'format': 'i' if k in ['percent'] else 'B',
'value': int(v),
}
for k, v in psutil.swap_memory()._asdict().items():
metrics[f'SWAP.{k}'] = {
'description': f"psutil.swap_memory()._asdict()['{k}']",
'flag': 'g' if k in ['total', 'used', 'free', 'percent'] else 'c',
'format': 'i' if k in ['percent'] else 'B',
'value': int(v),
}
for nic, counters in psutil.net_io_counters(pernic=True).items():
for k, v in counters._asdict().items():
metrics[f'NET.{nic}.{k}'] = {
'description': f"psutil.net_io_counters(pernic=True)['{nic}']._asdict()['{k}']",
'flag': 'c',
'format': 'B' if 'bytes' in k else 'i',
'value': int(v),
}

# Print metrics as JSON.
print(json.dumps(metrics))

if __name__ == '__main__':
main()
2 changes: 0 additions & 2 deletions files/varnishstat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
# - Beware of JSON numbers being limited to float64 & possible precision loss.
#

/usr/bin/varnishstat -1 -j && exit 0

cat <<EOF
{
"timestamp": "2024-01-01T13:00:00",
Expand Down

0 comments on commit 248d63f

Please sign in to comment.