Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. ## ********************************************************************************
  4. ## get-yarn-usage.py
  5. ##
  6. ## Usage: ./get-hive-usage-user.py
  7. ##
  8. ## Edit the settings below to connect to your Cluster
  9. ##
  10. ## ********************************************************************************
  11.  
  12. import sys
  13. from datetime import datetime, timedelta
  14. import pprint
  15. from cm_api.api_client import ApiResource
  16.  
  17. ## Settings to connect to the cluster
  18.  
  19. cm_host = "mdonsky-1.gce.cloudera.com"
  20.  
  21. cm_port = "7180"
  22.  
  23. cm_login = "admin"
  24.  
  25. cm_password = "admin"
  26.  
  27. cluster_name = "Cluster 1"
  28.  
  29.  
  30.  
  31. ## Get command line args
  32.  
  33. user_name = None
  34. if len(sys.argv) == 2:
  35. user_name = sys.argv[1]
  36. else:
  37. print " Usage: ./get-hive-usage-user.py <user_name>"
  38. quit(1)
  39.  
  40.  
  41. ## Used for formatting dates
  42. fmt = '%Y-%m-%d %H:%M:%S %Z'
  43.  
  44. # pretty printer for printing JSON attribute lists
  45. pp = pprint.PrettyPrinter(indent=4)
  46.  
  47. ## Connect to CM
  48. print "\nConnecting to Cloudera Manager at " + cm_host + ":" + cm_port
  49. api = ApiResource(server_host=cm_host, server_port=cm_port, username=cm_login, password=cm_password)
  50.  
  51. ## Get Cluster
  52. cluster = None
  53. clusters = api.get_all_clusters()
  54. for c in clusters:
  55. if c.displayName == cluster_name:
  56. cluster = c
  57. break
  58. if cluster is None:
  59. print "\nError: Cluster '" + cluster_name + "' not found"
  60. quit(1)
  61.  
  62.  
  63. ## Get YARN Service
  64. yarn_service = None
  65. service_list = cluster.get_all_services()
  66. for service in service_list:
  67. if service.type == "YARN":
  68. yarn_service = service
  69. break
  70. if yarn_service is None:
  71. print "Error: Could not locate YARN Service"
  72. quit(1)
  73.  
  74. now = datetime.utcnow()
  75. start = now - timedelta(days=60)
  76.  
  77. filterStr = 'user = '+user_name
  78.  
  79. yarn_apps_response = yarn_service.get_yarn_applications(start_time=start, end_time=now, filter_str=filterStr, limit=1000)
  80. yarn_apps = yarn_apps_response.applications
  81.  
  82. ## Iterate over the jobs
  83. for i in range (0, len(yarn_apps)):
  84. yarn_app = yarn_apps[i]
  85.  
  86. ## Get the Hive SQL
  87. hive_query_string = yarn_app.attributes.get("hive_query_string",None)
  88. if hive_query_string is not None:
  89. print "\n-- YARN Job ID: " + yarn_app.applicationId + " --------------"
  90. print "YARN App Name: " + yarn_app.name
  91. print "YARN User: " + yarn_app.user
  92. print "State: " + yarn_app.state
  93. print "Hive Query: " + hive_query_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement