-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2fed0d
commit 248d63f
Showing
4 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters