Heartbeat.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 json
  18. import logging
  19. import time
  20. from pprint import pformat
  21. from ActionQueue import ActionQueue
  22. import AmbariConfig
  23. import hostname
  24. from HostInfo import HostInfo
  25. logger = logging.getLogger()
  26. firstContact = True
  27. class Heartbeat:
  28. def __init__(self, actionQueue):
  29. self.actionQueue = actionQueue
  30. self.reports = []
  31. def build(self, id='-1', state_interval=-1):
  32. global clusterId, clusterDefinitionRevision, firstContact
  33. timestamp = int(time.time()*1000)
  34. queueResult = self.actionQueue.result()
  35. nodeStatus = { "status" : "HEALTHY",
  36. "cause" : "NONE"}
  37. heartbeat = { 'responseId' : int(id),
  38. 'timestamp' : timestamp,
  39. 'hostname' : hostname.hostname(),
  40. 'nodeStatus' : nodeStatus
  41. }
  42. if len(queueResult) != 0:
  43. heartbeat['reports'] = queueResult['reports']
  44. heartbeat['componentStatus'] = queueResult['componentStatus']
  45. pass
  46. logger.info("Sending heartbeat with response id: " + str(id) + " and "
  47. "timestamp: " + str(timestamp))
  48. logger.debug("Heartbeat : " + pformat(heartbeat))
  49. if (int(id) >= 0) and state_interval > 0 and (int(id) % state_interval) == 0:
  50. hostInfo = HostInfo()
  51. nodeInfo = { }
  52. # for now, just do the same work as registration
  53. hostInfo.register(nodeInfo)
  54. heartbeat['agentEnv'] = nodeInfo
  55. logger.debug("agentEnv : " + str(nodeInfo))
  56. return heartbeat
  57. def main(argv=None):
  58. actionQueue = ActionQueue(AmbariConfig.config)
  59. heartbeat = Heartbeat(actionQueue)
  60. print json.dumps(heartbeat.build('3',3))
  61. if __name__ == '__main__':
  62. main()