Revision 572e52bf lib/hypervisor/hv_base.py

b/lib/hypervisor/hv_base.py
23 23

  
24 24
"""
25 25

  
26
import re
27

  
28

  
26 29
from ganeti import errors
27 30

  
28 31

  
......
187 190

  
188 191
    """
189 192
    pass
193

  
194
  def GetLinuxNodeInfo(self):
195
    """For linux systems, return actual OS information.
196

  
197
    This is an abstraction for all non-hypervisor-based classes, where
198
    the node actually sees all the memory and CPUs via the /proc
199
    interface and standard commands. The other case if for example
200
    xen, where you only see the hardware resources via xen-specific
201
    tools.
202

  
203
    @return: a dict with the following keys (values in MiB):
204
          - memory_total: the total memory size on the node
205
          - memory_free: the available memory on the node for instances
206
          - memory_dom0: the memory used by the node itself, if available
207

  
208
    """
209
    try:
210
      fh = file("/proc/meminfo")
211
      try:
212
        data = fh.readlines()
213
      finally:
214
        fh.close()
215
    except EnvironmentError, err:
216
      raise errors.HypervisorError("Failed to list node info: %s" % (err,))
217

  
218
    result = {}
219
    sum_free = 0
220
    try:
221
      for line in data:
222
        splitfields = line.split(":", 1)
223

  
224
        if len(splitfields) > 1:
225
          key = splitfields[0].strip()
226
          val = splitfields[1].strip()
227
          if key == 'MemTotal':
228
            result['memory_total'] = int(val.split()[0])/1024
229
          elif key in ('MemFree', 'Buffers', 'Cached'):
230
            sum_free += int(val.split()[0])/1024
231
          elif key == 'Active':
232
            result['memory_dom0'] = int(val.split()[0])/1024
233
    except (ValueError, TypeError), err:
234
      raise errors.HypervisorError("Failed to compute memory usage: %s" %
235
                                   (err,))
236
    result['memory_free'] = sum_free
237

  
238
    cpu_total = 0
239
    try:
240
      fh = open("/proc/cpuinfo")
241
      try:
242
        cpu_total = len(re.findall("(?m)^processor\s*:\s*[0-9]+\s*$",
243
                                   fh.read()))
244
      finally:
245
        fh.close()
246
    except EnvironmentError, err:
247
      raise errors.HypervisorError("Failed to list node info: %s" % (err,))
248
    result['cpu_total'] = cpu_total
249
    # FIXME: export correct data here
250
    result['cpu_nodes'] = 1
251
    result['cpu_sockets'] = 1
252

  
253
    return result

Also available in: Unified diff