@@ -447,7 +447,6 @@ def write_to_textfile(path, registry):
447447 os .rename (tmppath , path )
448448
449449
450-
451450class ProcessCollector (object ):
452451 """Collector for Standard Exports such as cpu and memory."""
453452 def __init__ (self , namespace = '' , pid = 'self' , proc = '/proc' , registry = REGISTRY ):
@@ -465,9 +464,12 @@ def __init__(self, namespace='', pid='self', proc='/proc', registry=REGISTRY):
465464 except (ValueError , TypeError ):
466465 pass
467466
468- self ._can_read_proc = os .access (os .path .join (self ._proc , 'stat' ), os .R_OK )
469- if self ._can_read_proc :
467+ # This is used to test if we can access /proc.
468+ self ._btime = 0
469+ try :
470470 self ._btime = self ._boot_time ()
471+ except IOError :
472+ pass
471473 if registry :
472474 registry .register (self )
473475
@@ -478,7 +480,7 @@ def _boot_time(self):
478480 return float (line .split ()[1 ])
479481
480482 def collect (self ):
481- if not self ._can_read_proc :
483+ if not self ._btime :
482484 return []
483485
484486 result = []
@@ -491,15 +493,15 @@ def collect(self):
491493 rss .add_sample (self ._prefix + 'resident_memory_bytes' , {}, float (parts [21 ]) * self ._pagesize )
492494 start_time = Metric (self ._prefix + 'start_time_seconds' ,
493495 'Start time of the process since unix epoch in seconds.' , 'gauge' )
494- start_time_secs = float (parts [18 ]) / self ._ticks
496+ start_time_secs = float (parts [19 ]) / self ._ticks
495497 start_time .add_sample (self ._prefix + 'start_time_seconds' ,{} , start_time_secs + self ._btime )
496498 utime = float (parts [11 ]) / self ._ticks
497499 stime = float (parts [12 ]) / self ._ticks
498500 cpu = Metric (self ._prefix + 'cpu_seconds_total' ,
499501 'Total user and system CPU time spent in seconds.' , 'counter' )
500502 cpu .add_sample (self ._prefix + 'cpu_seconds_total' , {}, utime + stime )
501503 result .extend ([vmem , rss , start_time , cpu ])
502- except ( IOError ) :
504+ except IOError :
503505 pass
504506
505507 try :
@@ -510,19 +512,18 @@ def collect(self):
510512 max_fds .add_sample (self ._prefix + 'max_fds' , {}, float (line .split ()[3 ]))
511513 break
512514 open_fds = Metric (self ._prefix + 'open_fds' , 'Number of open file descriptors.' , 'gauge' )
513- open_fds .add_sample (self ._prefix + 'open_fds' , {}, os .listdir (os .path .join (self ._pid , 'fd' )))
515+ open_fds .add_sample (self ._prefix + 'open_fds' , {}, len ( os .listdir (os .path .join (self ._pid , 'fd' ) )))
514516 result .extend ([open_fds , max_fds ])
515517 except IOError :
516518 pass
517519
518520 return result
519521
520522
521- PROCESS_COLLECTOR = ProcessCollector (proc = '.' )
523+ PROCESS_COLLECTOR = ProcessCollector ()
522524"""Default ProcessCollector in default Registry REGISTRY."""
523525
524526
525-
526527if __name__ == '__main__' :
527528 c = Counter ('cc' , 'A counter' )
528529 c .inc ()
0 commit comments