PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/service/lib/py/fb303_scripts/fb303_simple_mgmt.py

#
Python | 195 lines | 164 code | 6 blank | 25 comment | 5 complexity | b525aa2860045856c3046fbea5023598 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. #!/usr/bin/env python
  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. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. import sys, os
  21. from optparse import OptionParser
  22. from thrift.Thrift import *
  23. from thrift.transport import TSocket
  24. from thrift.transport import TTransport
  25. from thrift.protocol import TBinaryProtocol
  26. from fb303 import *
  27. from fb303.ttypes import *
  28. def service_ctrl(
  29. command,
  30. port,
  31. trans_factory = None,
  32. prot_factory = None):
  33. """
  34. service_ctrl is a generic function to execute standard fb303 functions
  35. @param command: one of stop, start, reload, status, counters, name, alive
  36. @param port: service's port
  37. @param trans_factory: TTransportFactory to use for obtaining a TTransport. Default is
  38. TBufferedTransportFactory
  39. @param prot_factory: TProtocolFactory to use for obtaining a TProtocol. Default is
  40. TBinaryProtocolFactory
  41. """
  42. if command in ["status"]:
  43. try:
  44. status = fb303_wrapper('status', port, trans_factory, prot_factory)
  45. status_details = fb303_wrapper('get_status_details', port, trans_factory, prot_factory)
  46. msg = fb_status_string(status)
  47. if (len(status_details)):
  48. msg += " - %s" % status_details
  49. print msg
  50. if (status == fb_status.ALIVE):
  51. return 2
  52. else:
  53. return 3
  54. except:
  55. print "Failed to get status"
  56. return 3
  57. # scalar commands
  58. if command in ["version","alive","name"]:
  59. try:
  60. result = fb303_wrapper(command, port, trans_factory, prot_factory)
  61. print result
  62. return 0
  63. except:
  64. print "failed to get ",command
  65. return 3
  66. # counters
  67. if command in ["counters"]:
  68. try:
  69. counters = fb303_wrapper('counters', port, trans_factory, prot_factory)
  70. for counter in counters:
  71. print "%s: %d" % (counter, counters[counter])
  72. return 0
  73. except:
  74. print "failed to get counters"
  75. return 3
  76. # Only root should be able to run the following commands
  77. if os.getuid() == 0:
  78. # async commands
  79. if command in ["stop","reload"] :
  80. try:
  81. fb303_wrapper(command, port, trans_factory, prot_factory)
  82. return 0
  83. except:
  84. print "failed to tell the service to ", command
  85. return 3
  86. else:
  87. if command in ["stop","reload"]:
  88. print "root privileges are required to stop or reload the service."
  89. return 4
  90. print "The following commands are available:"
  91. for command in ["counters","name","version","alive","status"]:
  92. print "\t%s" % command
  93. print "The following commands are available for users with root privileges:"
  94. for command in ["stop","reload"]:
  95. print "\t%s" % command
  96. return 0;
  97. def fb303_wrapper(command, port, trans_factory = None, prot_factory = None):
  98. sock = TSocket.TSocket('localhost', port)
  99. # use input transport factory if provided
  100. if (trans_factory is None):
  101. trans = TTransport.TBufferedTransport(sock)
  102. else:
  103. trans = trans_factory.getTransport(sock)
  104. # use input protocol factory if provided
  105. if (prot_factory is None):
  106. prot = TBinaryProtocol.TBinaryProtocol(trans)
  107. else:
  108. prot = prot_factory.getProtocol(trans)
  109. # initialize client and open transport
  110. fb303_client = FacebookService.Client(prot, prot)
  111. trans.open()
  112. if (command == 'reload'):
  113. fb303_client.reinitialize()
  114. elif (command == 'stop'):
  115. fb303_client.shutdown()
  116. elif (command == 'status'):
  117. return fb303_client.getStatus()
  118. elif (command == 'version'):
  119. return fb303_client.getVersion()
  120. elif (command == 'get_status_details'):
  121. return fb303_client.getStatusDetails()
  122. elif (command == 'counters'):
  123. return fb303_client.getCounters()
  124. elif (command == 'name'):
  125. return fb303_client.getName()
  126. elif (command == 'alive'):
  127. return fb303_client.aliveSince()
  128. trans.close()
  129. def fb_status_string(status_enum):
  130. if (status_enum == fb_status.DEAD):
  131. return "DEAD"
  132. if (status_enum == fb_status.STARTING):
  133. return "STARTING"
  134. if (status_enum == fb_status.ALIVE):
  135. return "ALIVE"
  136. if (status_enum == fb_status.STOPPING):
  137. return "STOPPING"
  138. if (status_enum == fb_status.STOPPED):
  139. return "STOPPED"
  140. if (status_enum == fb_status.WARNING):
  141. return "WARNING"
  142. def main():
  143. # parse command line options
  144. parser = OptionParser()
  145. commands=["stop","counters","status","reload","version","name","alive"]
  146. parser.add_option("-c", "--command", dest="command", help="execute this API",
  147. choices=commands, default="status")
  148. parser.add_option("-p","--port",dest="port",help="the service's port",
  149. default=9082)
  150. (options, args) = parser.parse_args()
  151. status = service_ctrl(options.command, options.port)
  152. sys.exit(status)
  153. if __name__ == '__main__':
  154. main()