forked from omergertel/pyformance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_sysmetrics.py
99 lines (82 loc) · 3.7 KB
/
example_sysmetrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
import os
import platform
import socket
import time
import json
import psutil
from pyformance import global_registry
class Collector(object):
# TODO: use meters and histograms instead of gauges if possible
def __init__(self, registry=None):
if registry is None:
registry = global_registry()
self.registry = registry
def collect_disk_io(self, whitelist=[]):
stats = psutil.disk_io_counters(perdisk=True)
for entry, stat in stats.iteritems():
if not whitelist or entry in whitelist:
for k, v in stat._asdict().iteritems():
self.registry.gauge("disk-%s.%s" % (entry, k)).set_value(v)
def collect_network_io(self, whitelist=[]):
stats = psutil.network_io_counters(pernic=True)
for entry, stat in stats.iteritems():
if not whitelist or entry in whitelist:
for k, v in stat._asdict().iteritems():
self.registry.gauge("nic-%s.%s" % (entry.replace(" ", "_"), k)).set_value(v)
def collect_cpu_times(self, whitelist=[]):
stats = psutil.cpu_times(percpu=True)
for entry, stat in enumerate(stats):
if not whitelist or entry in whitelist:
for k, v in stat._asdict().iteritems():
self.registry.gauge("cpu%d.%s" % (entry, k)).set_value(v)
def collect_phymem_usage(self):
stats = psutil.phymem_usage()
for k, v in stats._asdict().iteritems():
self.registry.gauge("phymem.%s" % k).set_value(v)
def collect_swap_usage(self):
stats = psutil.swap_memory()
for k, v in stats._asdict().iteritems():