Explorar el Código

AMBARI-709. Getting hardware info on disks/cpu/others using facter and using it during registeration. (mahadev)it during registeration. (mahadev)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/branches/AMBARI-666@1382412 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar hace 12 años
padre
commit
39b327df9e

+ 3 - 0
AMBARI-666-CHANGES.txt

@@ -12,6 +12,9 @@ AMBARI-666 branch (unreleased changes)
 
   NEW FEATURES
 
+  AMBARI-709. Getting hardware info on disks/cpu/others using facter and using
+  it during registeration. (mahadev)
+
   AMBARI-707. More work on Node FSM and additional tests/cleanup. (hitesh)
 
   AMBARI-706. Basic tests for Node FSM. (hitesh)

+ 2 - 2
ambari-agent/src/main/python/ambari_agent/AmbariConfig.py

@@ -35,8 +35,8 @@ prefix=/tmp/ambari
 installprefix=/var/ambari/
 
 [puppet]
-puppet_home=/usr/local/bin
-facter_home=/usr/local/bin
+puppet_home=/root/workspace/puppet-install/puppet-2.7.9
+facter_home=/root/workspace/puppet-install/facter-1.6.10
 
 [command]
 maxretries=2

+ 86 - 11
ambari-agent/src/main/python/ambari_agent/Hardware.py

@@ -18,27 +18,102 @@ See the License for the specific language governing permissions and
 limitations under the License.
 '''
 
-from shell import shellRunner
 import multiprocessing
 import platform
 import AmbariConfig
+import os.path
+import shell
+import logging
+import subprocess
+
+logger = logging.getLogger()
 
 class Hardware:
   def __init__(self):
-    facterHome = AmbariConfig.config.get('puppet', 'facter_home')
-    
-    self.hardware = { 'coreCount' : 4,
-                      'cpuSpeed' : 4,
-                      'cpuFlag' : 4,
-                      'diskCount' : 3,
-                      'netSpeed' : 3,
-                      'ramSize' : 2
-                    }
+    self.hardware = {}
+    osdisks = self.osdisks()
+    self.hardware['mounts'] = osdisks
+    otherInfo = self.facterInfo()
+    self.hardware.update(otherInfo)
+    pass
+  
+  def osdisks(self):
+    """ Run df to find out the disks on the host. Only works on linux 
+    platforms. Note that this parser ignores any filesystems with spaces 
+    and any mounts with spaces. """
+    mounts = {}
+    df = subprocess.Popen(["df", "-kP"], stdout=subprocess.PIPE)
+    dfdata = df.communicate()[0]
+    lines = dfdata.splitlines()
+    for l in lines:
+      split = l.split()
+      """ this ignores any spaces in the filesystemname and mounts """
+      if (len(split)) == 6:
+        device, size, used, available, percent, mountpoint = split
+        mountinfo = { 'size' : size,
+             'used' : used,
+             'available' : available,
+             'percent' : percent,
+             'mountpoint' : mountpoint}
 
+        mounts[device ] = mountinfo
+        pass
+      pass
+    return mounts
+    
+  def facterBin(self, facterHome):
+    return facterHome + "/bin/facter"
+    pass
+  
+  def facterLib(self, facterHome):
+    return facterHome + "/lib/"
+    pass
+  
+  def parseFacterOutput(self, facterOutput):
+    retDict = {}
+    allLines = facterOutput.splitlines()
+    for line in allLines:
+      keyValue = line.split("=>")
+      if (len(keyValue) == 2):
+        """Ignoring values that are just spaces or do not confirm to the 
+        format"""
+        retDict[keyValue[0].strip()] = keyValue[1].strip()
+        pass
+      pass
+    return retDict
+  
+  def facterInfo(self):   
+    facterHome = AmbariConfig.config.get("puppet", "facter_home")
+    facterEnv = os.environ
+    logger.info("Using facter home as: " + facterHome)
+    facterInfo = {}
+    if os.path.exists(facterHome):
+      rubyLib = ""
+      if os.environ.has_key("RUBYLIB"):
+        rubyLib = os.environ["RUBYLIB"]
+        logger.info("Ruby Lib env from Env " + rubyLib)
+      rubyLib = rubyLib + ":" + self.facterLib(facterHome)
+      facterEnv["RUBYLIB"] = rubyLib
+      logger.info("Setting RUBYLIB as: " + rubyLib)
+      facter = subprocess.Popen([self.facterBin(facterHome)],
+                                 stdout=subprocess.PIPE,
+                                 stderr=subprocess.PIPE,
+                                 env=facterEnv)
+      stderr_out = facter.communicate()
+      if facter.returncode != 0:
+        logging.error("Error getting facter info: " + stderr_out[1])
+        pass
+      facterOutput = stderr_out[0]
+      infoDict = self.parseFacterOutput(facterOutput)
+      facterInfo = infoDict
+    else:
+      pass
+    return facterInfo
+  
   def get(self):
+    logger.info("Hardware Info for the agent: " + str(self.hardware))
     return self.hardware
 
-
 def main(argv=None):
   hardware = Hardware()
   print hardware.get()