Register.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python2.6
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import sys, os
  18. import json
  19. from Hardware import Hardware
  20. from ActionQueue import ActionQueue
  21. from ServerStatus import ServerStatus
  22. import hostname
  23. import time
  24. import urllib2
  25. import subprocess
  26. from HostInfo import HostInfo
  27. firstContact = True
  28. class Register:
  29. """ Registering with the server. Get the hardware profile and
  30. declare success for now """
  31. def __init__(self, config):
  32. self.hardware = Hardware()
  33. self.config = config
  34. def build(self, id='-1'):
  35. global clusterId, clusterDefinitionRevision, firstContact
  36. timestamp = int(time.time()*1000)
  37. hostInfo = HostInfo()
  38. agentEnv = { }
  39. hostInfo.register(agentEnv)
  40. version = self.read_agent_version()
  41. register = { 'responseId' : int(id),
  42. 'timestamp' : timestamp,
  43. 'hostname' : hostname.hostname(),
  44. 'publicHostname' : hostname.public_hostname(),
  45. 'hardwareProfile' : self.hardware.get(),
  46. 'agentEnv' : agentEnv,
  47. 'agentVersion' : version
  48. }
  49. return register
  50. def read_agent_version(self):
  51. data_dir = self.config.get('agent', 'prefix')
  52. ver_file = os.path.join(data_dir, 'version')
  53. f = open(ver_file, "r")
  54. version = f.read()
  55. f.close()
  56. return version
  57. def doExec(vals, key, command, preLF=False):
  58. template = "{0}: {1} {2}"
  59. try:
  60. osStat = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  61. out, err = osStat.communicate()
  62. if 0 != osStat.returncode or 0 == len(out.strip()):
  63. print template.format(key, "UNAVAILABLE", "")
  64. else:
  65. if (preLF):
  66. print template.format(key, "ok,\n", out.strip())
  67. else:
  68. print template.format(key, "ok,", out.strip())
  69. except:
  70. print template.format(key, "UNAVAILABLE", "")
  71. # Linux only
  72. def machineInfo():
  73. vals = { }
  74. doExec(vals, 'hostname', ["hostname", "-f"])
  75. doExec(vals, 'ip', ["hostname", "-i"])
  76. doExec(vals, 'cpu', ["sh", "-c", "cat /proc/cpuinfo | grep 'model name' | awk -F': ' '{ print $2; }'"])
  77. doExec(vals, 'memory', ["sh", "-c", "cat /proc/meminfo | grep MemTotal | awk -F': ' '{ print $2/1024/1024 \" GB\"; }'"])
  78. doExec(vals, 'disks', ["df", "-h"], True)
  79. doExec(vals, 'os', ["sh", "-c", "cat /etc/issue.net | head -1"])
  80. doExec(vals, 'iptables', ["iptables", "-vnL"], True)
  81. doExec(vals, 'selinux', ["sh", "-c", "cat /etc/selinux/config | grep ^SELINUX"])
  82. rpm_req = { }
  83. for REQ in (["yum", "rpm", "openssl", "curl", "wget", "net-snmp", "net-snmp-utils", "ntpd"]):
  84. doExec(rpm_req, REQ, ["rpm", "-qa", REQ])
  85. vals["required_packages"] = rpm_req
  86. rpm_opt = { }
  87. for OPT in (["ruby", "puppet", "nagios", "ganglia", "passenger", "hadoop"]):
  88. doExec(rpm_opt, OPT, ["rpm", "-qa", OPT])
  89. vals["optional_packages"] = rpm_opt
  90. doExec(vals, "yum_repos", ["sh", "-c", "yum -C repolist enabled | egrep \"(AMBARI|HDP)\""], True)
  91. # for SUSE-based agents
  92. doExec(vals, "zypper_repos", ["sh", "-c", "zypper repos | egrep \"(AMBARI|HDP)\""], True)