3
3
from __future__ import unicode_literals
4
4
5
5
import os
6
- import time
7
- import threading
8
6
9
7
from . import core
10
8
try :
11
- import resource
12
- _PAGESIZE = resource .getpagesize ()
9
+ import resource
10
+ _PAGESIZE = resource .getpagesize ()
13
11
except ImportError :
14
- # Not Unix
15
- _PAGESIZE = 4096
12
+ # Not Unix
13
+ _PAGESIZE = 4096
16
14
17
15
18
16
class ProcessCollector (object ):
@@ -38,7 +36,7 @@ def __init__(self, namespace='', pid=lambda: 'self', proc='/proc', registry=core
38
36
except IOError :
39
37
pass
40
38
if registry :
41
- registry .register (self )
39
+ registry .register (self )
42
40
43
41
def _boot_time (self ):
44
42
with open (os .path .join (self ._proc , 'stat' )) as stat :
@@ -50,27 +48,25 @@ def collect(self):
50
48
if not self ._btime :
51
49
return []
52
50
53
- try :
54
- pid = os .path .join (self ._proc , str (self ._pid ()).strip ())
55
- except :
56
- # File likely didn't exist, fail silently.
57
- raise
58
- return []
51
+ pid = os .path .join (self ._proc , str (self ._pid ()).strip ())
59
52
60
53
result = []
61
54
try :
62
55
with open (os .path .join (pid , 'stat' )) as stat :
63
56
parts = (stat .read ().split (')
10000
' )[- 1 ].split ())
64
57
vmem = core .GaugeMetricFamily (self ._prefix + 'virtual_memory_bytes' ,
65
- 'Virtual memory size in bytes.' , value = float (parts [20 ]))
66
- rss = core .GaugeMetricFamily (self ._prefix + 'resident_memory_bytes' , 'Resident memory size in bytes.' , value = float (parts [21 ]) * _PAGESIZE )
58
+ 'Virtual memory size in bytes.' , value = float (parts [20 ]))
59
+ rss = core .GaugeMetricFamily (self ._prefix + 'resident_memory_bytes' , 'Resident memory size in bytes.' ,
60
+ value = float (parts [21 ]) * _PAGESIZE )
67
61
start_time_secs = float (parts [19 ]) / self ._ticks
68
62
start_time = core .GaugeMetricFamily (self ._prefix + 'start_time_seconds' ,
69
- 'Start time of the process since unix epoch in seconds.' , value = start_time_secs + self ._btime )
63
+ 'Start time of the process since unix epoch in seconds.' ,
64
+ value = start_time_secs + self ._btime )
70
65
utime = float (parts [11 ]) / self ._ticks
71
66
stime = float (parts [12 ]) / self ._ticks
72
67
cpu = core .CounterMetricFamily (self ._prefix + 'cpu_seconds_total' ,
73
- 'Total user and system CPU time spent in seconds.' , value = utime + stime )
68
+ 'Total user and system CPU time spent in seconds.' ,
69
+ value = utime + stime )
74
70
result .extend ([vmem , rss , start_time , cpu ])
75
71
except IOError :
76
72
pass
@@ -80,10 +76,12 @@ def collect(self):
80
76
for line in limits :
81
77
if line .startswith ('Max open file' ):
82
78
max_fds = core .GaugeMetricFamily (self ._prefix + 'max_fds' ,
83
- 'Maximum number of open file descriptors.' , value = float (line .split ()[3 ]))
79
+ 'Maximum number of open file descriptors.' ,
80
+ value = float (line .split ()[3 ]))
84
81
break
85
82
open_fds = core .GaugeMetricFamily (self ._prefix + 'open_fds' ,
86
- 'Number of open file descriptors.' , len (os .listdir (os .path .join (pid , 'fd' ))))
83
+ 'Number of open file descriptors.' ,
84
+ len (os .listdir (os .path .join (pid , 'fd' ))))
87
85
result .extend ([open_fds , max_fds ])
88
86
except IOError :
89
87
pass
0 commit comments