Add node cpu count to gnt-node list
authorIustin Pop <iustin@google.com>
Wed, 30 Apr 2008 15:53:19 +0000 (15:53 +0000)
committerIustin Pop <iustin@google.com>
Wed, 30 Apr 2008 15:53:19 +0000 (15:53 +0000)
This patch adds the backend and frontend changes needed for being able
to list the cpu count.

Reviewed-by: ultrotter

lib/cmdlib.py
lib/constants.py
lib/hypervisor.py
scripts/gnt-node

index 40c1e2f..49b33bb 100644 (file)
@@ -1463,9 +1463,12 @@ class LUQueryNodes(NoHooksLU):
     This checks that the fields required are valid output fields.
 
     """
-    self.dynamic_fields = frozenset(["dtotal", "dfree",
-                                     "mtotal", "mnode", "mfree",
-                                     "bootid"])
+    self.dynamic_fields = frozenset([
+      "dtotal", "dfree",
+      "mtotal", "mnode", "mfree",
+      "bootid",
+      "ctotal",
+      ])
 
     _CheckOutputFields(static=["name", "pinst_cnt", "sinst_cnt",
                                "pinst_list", "sinst_list",
@@ -1496,6 +1499,7 @@ class LUQueryNodes(NoHooksLU):
             "mfree": utils.TryConvert(int, nodeinfo['memory_free']),
             "dtotal": utils.TryConvert(int, nodeinfo['vg_size']),
             "dfree": utils.TryConvert(int, nodeinfo['vg_free']),
+            "ctotal": utils.TryConvert(int, nodeinfo['cpu_total']),
             "bootid": nodeinfo['bootid'],
             }
         else:
index c6fa97d..d970ec3 100644 (file)
@@ -25,7 +25,7 @@ from ganeti import _autoconf
 
 # various versions
 CONFIG_VERSION = 3
-PROTOCOL_VERSION = 11
+PROTOCOL_VERSION = 12
 RELEASE_VERSION = _autoconf.PACKAGE_VERSION
 OS_API_VERSION = 5
 EXPORT_VERSION = 0
index 179ef76..ad24d15 100644 (file)
@@ -25,6 +25,7 @@
 
 import time
 import os
+import re
 from cStringIO import StringIO
 
 from ganeti import utils
@@ -287,6 +288,8 @@ class XenHypervisor(BaseHypervisor):
           result['memory_total'] = int(val)
         elif key == 'free_memory':
           result['memory_free'] = int(val)
+        elif key == 'nr_cpus':
+          result['cpu_total'] = int(val)
     dom0_info = self.GetInstanceInfo("Domain-0")
     if dom0_info is not None:
       result['memory_dom0'] = dom0_info[2]
@@ -551,8 +554,20 @@ class FakeHypervisor(BaseHypervisor):
           sum_free += int(val.split()[0])/1024
         elif key == 'Active':
           result['memory_dom0'] = int(val.split()[0])/1024
-
     result['memory_free'] = sum_free
+
+    cpu_total = 0
+    try:
+      fh = open("/proc/cpuinfo")
+      try:
+        cpu_total = len(re.findall("(?m)^processor\s*:\s*[0-9]+\s*$",
+                                   fh.read()))
+      finally:
+        fh.close()
+    except EnvironmentError, err:
+      raise HypervisorError("Failed to list node info: %s" % err)
+    result['cpu_total'] = cpu_total
+
     return result
 
   @staticmethod
index e9c6a04..9a8faeb 100755 (executable)
@@ -55,12 +55,15 @@ def ListNodes(opts, args):
   output = SubmitOpCode(op)
 
   if not opts.no_headers:
-    headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
-               "pinst_list": "PriInstances", "sinst_list": "SecInstances",
-               "pip": "PrimaryIP", "sip": "SecondaryIP",
-               "dtotal": "DTotal", "dfree": "DFree",
-               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
-               "bootid": "BootID"}
+    headers = {
+      "name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
+      "pinst_list": "PriInstances", "sinst_list": "SecInstances",
+      "pip": "PrimaryIP", "sip": "SecondaryIP",
+      "dtotal": "DTotal", "dfree": "DFree",
+      "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
+      "bootid": "BootID",
+      "ctotal": "CTotal",
+      }
   else:
     headers = None
 
@@ -71,7 +74,8 @@ def ListNodes(opts, args):
 
   numfields = ["dtotal", "dfree",
                "mtotal", "mnode", "mfree",
-               "pinst_cnt", "sinst_cnt"]
+               "pinst_cnt", "sinst_cnt",
+               "ctotal"]
 
   # change raw values to nicer strings
   for row in output:
@@ -305,7 +309,8 @@ commands = {
            "", "Lists the nodes in the cluster. The available fields"
            " are (see the man page for details): name, pinst_cnt, pinst_list,"
            " sinst_cnt, sinst_list, pip, sip, dtotal, dfree, mtotal, mnode,"
-           " mfree, bootid. The default field list is (in order): name,"
+           " mfree, bootid, cpu_count. The default field list is"
+           " (in order): name,"
            " dtotal, dfree, mtotal, mnode, mfree, pinst_cnt, sinst_cnt."),
   'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
              "<node_name>", "Removes a node from the cluster"),