Update README
[vzubcmon.git] / parser.py
blob4b88ae40682d2094000692796d90dbe8b16327ba
1 # coding: utf-8
2 """
3 Author: Enrico Tröger
4 License: GPLv2
5 """
7 from resource import VzUbcMonResource
10 ###########################################################################
11 class VzUbcMonParser(object):
12 """
13 Parser to read /proc/user_beancounters and hold the values
14 in a sequence of VzUbcMonResource
15 """
17 #----------------------------------------------------------------------
18 def __init__(self, filename):
19 self._filename = filename
20 self._bc_version = None
21 self._resources = None
23 #----------------------------------------------------------------------
24 def get_resources(self):
25 """
26 Parse and return the current UBC values for the system
28 | **return** resources (dict)
29 """
30 if self._resources is None:
31 self._parse()
33 return self._resources
35 #----------------------------------------------------------------------
36 def get_information(self):
37 """
38 Return the version field of the parsed bean counters file and the container ID
40 | **return** beancounter_version, container_id (tuple)
41 """
42 if self._resources is None:
43 self._parse()
45 return self._bc_version
47 #----------------------------------------------------------------------
48 def _parse(self):
49 """
50 Parse /proc/user_beancounters and store the values in the object
51 """
52 ubc_file = open(self._filename, 'r')
53 self._resources = {}
54 current_ctid = 0
55 for line in ubc_file:
56 values = line.strip().split()
57 values_len = len(values)
58 if values_len == 2:
59 self._bc_version = values[1]
60 elif values_len >= 6:
61 if values_len == 7:
62 ctid = values[0][:-1]
63 if ctid.isdigit():
64 # we got the container id
65 current_ctid = ctid
66 self._resources[current_ctid] = {}
67 else:
68 # header line
69 continue
70 del values[0]
71 resource_name = values[0]
72 resource_values = values[1:] # skip the name
73 self._resources[current_ctid][resource_name] = \
74 VzUbcMonResource(resource_seq=resource_values)
76 ubc_file.close()
79 if __name__ == '__main__':
80 from pprint import pprint
81 pprint(VzUbcMonParser('sample_host').get_resources())