Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.00 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from cm_api.api_client import ApiResource
  4. import sys
  5. import re
  6. from datetime import datetime, timedelta
  7.  
  8. g_cm_host = "bender1"
  9. g_cm_api = ApiResource(g_cm_host, version=10, username="admin", password="admin")
  10. g_cm_hosts = g_cm_api.get_all_hosts()
  11.  
  12. ###############################
  13. # Colores
  14. ###############################
  15. class COLORS:
  16.     RED = "\x1b[91m"
  17.     BLUE = "\x1b[94m"
  18.     YELLOW = "\x1b[93m"
  19.     GREEN = "\x1b[92m"
  20.     RESET = "\x1b[0m"
  21.  
  22.  
  23. def coloring(v_st, v_msg):
  24.     v_color = COLORS.GREEN if v_st in ("STARTED", "GOOD", "MantMode") else COLORS.RED if v_st in ("STOPPED", "BAD") else COLORS.BLUE if v_st in ("INFO", "NA") else COLORS.YELLOW
  25.    
  26.     v_status = "[" + v_color + v_st + COLORS.RESET + "]"
  27.     return v_status.ljust(20, ' ') + v_msg
  28.  
  29.  
  30. ######################################################################
  31. #Monitoring
  32. ######################################################################
  33. def check_services_state(p_cluster):       
  34.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  35.     v_services = p_cluster.get_all_services()
  36.  
  37.     for v_srv in v_services:    print(coloring(v_srv.serviceState, v_srv.type))
  38.  
  39. def check_service_state(p_cluster, p_service):
  40.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  41.     v_service = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  42.  
  43.     if not v_services:  print("Error: Service not found")
  44.     else:           print coloring(v_service[0].ServiceState, v_srv.type)
  45.  
  46. ######################################
  47. def check_services_health(p_cluster):
  48.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  49.     v_services = p_cluster.get_all_services()
  50.     for v_srv in v_services:    print(coloring(v_srv.healthSummary, v_srv.type))
  51.  
  52.  
  53. def check_service_health(p_cluster, p_service):
  54.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  55.     v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  56.  
  57.     if not v_services:  print("Error: Service not found")
  58.     else:          
  59.                 print("*" + v_services[0].type + ":")
  60.         print coloring(v_services[0].healthSummary, v_srv.type)
  61.  
  62.  
  63. ##################################
  64. def check_roles_state(p_cluster, p_service):
  65.         print('----------------------------------\n' + p_cluster.displayName + ':\n')
  66.         v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  67.  
  68.         if not v_services:      print("Error: Service not found")
  69.         else:
  70.                 print("*" + v_services[0].type + ":")
  71.                 for v_role in v_services[0].get_all_roles():
  72.                         print(coloring(v_role.roleState, filter(lambda x: x.hostId==v_role.hostRef.hostId, g_cm_hosts)[0].hostname) +":\t" +v_role.type)
  73.  
  74. def check_role_state(p_cluster, p_service, p_role):
  75.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  76.     v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  77.  
  78.     if not v_services:  print("Error: Service not found")
  79.     else:
  80.                 print("*" + v_services[0].type + ":")
  81.         v_roles = filter(lambda x: x.type == p_role, v_services[0].get_all_roles())
  82.                 for v_role in v_roles:  print(coloring(v_role.roleState, filter(lambda x: x.hostId==v_role.hostRef.hostId, g_cm_hosts)[0].hostname) + ":\t" + v_role.type)
  83.  
  84.  
  85. def check_roles_health(p_cluster, p_service):
  86.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  87.     v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  88.  
  89.     if not v_services:  print("Error: Service not found")
  90.     else:
  91.         print("*" + v_services[0].type + ":")
  92.         for v_role in v_services[0].get_all_roles():
  93.             print(coloring(v_role.healthSummary, filter(lambda x: x.hostId==v_role.hostRef.hostId, g_cm_hosts)[0].hostname) +":\t" +v_role.type)
  94.  
  95. def check_role_health(p_cluster, p_service, p_role):
  96.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  97.     v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  98.  
  99.     if not v_services:  print("Error: Service not found")
  100.     else:
  101.                 print("*" + v_services[0].type + ":")
  102.         v_roles = filter(lambda x: x.type == p_role, v_services[0].get_all_roles())
  103.         for v_role in v_roles:  print(coloring(v_role.healthSummary, filter(lambda x: x.hostId==v_role.hostRef.hostId, g_cm_hosts)[0].hostname) + ":\t" + v_role.type)
  104.  
  105.  
  106. ######################################################################
  107. # Restarting services
  108. ######################################################################
  109.  
  110. ############################################################################
  111. # Capturo la excepcion en caso de que el servicio no permita rolling restart
  112. def rolling_restart_service(p_cluster, p_service):
  113.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  114.     v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  115.     if not v_services:  print("Error: Service not found")
  116.  
  117.     else:
  118.         try:
  119.             cmd = v_services[0].rolling_restart().wait()
  120.             if re.match("^.*success: False\)$", str(cmd)):
  121.                 print "The service restart failed"
  122.             elif re.match("^.*success: True\)$", str(cmd)):
  123.                 print "The service restart worked"
  124.             else:
  125.                 print "Error: It has to be checked"
  126.         except:
  127.             if re.match("Command not valid for", str(sys.exc_info()[1])):
  128.                 print "It's not possible to use Rolling Restart in this service."
  129.             else:   raise
  130.  
  131.  
  132. def restart_service(p_cluster, p_service):
  133.     print('----------------------------------\n' + p_cluster.displayName + ':\n')
  134.     v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
  135.     if not v_services:  print("Error: Service not found")
  136.     else:
  137.         cmd = v_services[0].restart().wait()
  138.         if re.match("^.*success: False\)$", str(cmd)):
  139.             print "The service restart failed"
  140.         elif re.match("^.*success: True\)$", str(cmd)):
  141.             print "The service restart worked"
  142.         else:
  143.             print "Error: It has to be checked"
  144.  
  145.  
  146.  
  147. ##############################################################################################
  148. # Impala Queries
  149. ##############################################################################################
  150. def f_get_impala_queries(p_cluster, p_args):
  151.     p_start_time = p_args[0]
  152.     p_end_time = p_args[1]
  153.     p_filter_type = p_args[2]
  154.     p_filter_value = p_args[3]
  155.     p_limit = int(p_args[4])
  156.  
  157.     v_impala = filter(lambda x: x.type == 'IMPALA', p_cluster.get_all_services())[0]
  158.  
  159.     if not v_impala:
  160.         print("Error: Impala service doesnt exist in this cluster.")
  161.         return
  162.  
  163.     if re.match("^\d{2}/\d{2}/20\d{2}_\d{2}:\d{2}:\d{2}$", p_start_time):   v_start_time = datetime.strptime(p_start_time, '%d/%m/%Y_%H:%M:%S')
  164.     else:
  165.         print("Error. startTime format is not valid.")
  166.         return
  167.  
  168.     if re.match("^\d{2}/\d{2}/20\d{2}_\d{2}:\d{2}:\d{2}$", p_start_time):   v_end_time = datetime.strptime(p_end_time, '%d/%m/%Y_%H:%M:%S')
  169.     else:
  170.         print("Error. startTime format is not valid.")
  171.         return 
  172.  
  173.     if p_filter_type == "user" and type(p_filter_value) == str:         v_filter_str = 'user = ' + p_filter_value
  174.     elif p_filter_type == "duration" and re.match("^[<>=]\d+[hms]$"):   v_filter_str = 'queryDuration ' + p_filter_value
  175.     elif p_filter_type == "state" and p_filter_value in ('CREATED', 'INITIALIZED', 'COMPILED', 'RUNNING', 'FINISHED', 'EXCEPTION', 'UNKNOWN'):
  176.         v_filter_str = 'queryState = ' + p_filter_value
  177.  
  178.     else:
  179.         print("Error: Filter is not valid.")
  180.         return
  181.  
  182.     if type(p_limit) == int and p_limit < 201:  v_limit = p_limit
  183.     else:
  184.         print("Error: Limit is not valid. It must be > 0 and <= 200")
  185.         return
  186.  
  187.     v_queries = v_impala.get_impala_queries(v_start_time, v_end_time, v_filter_str, v_limit).queries
  188.  
  189.     for vq in v_queries:
  190.         v_coordinator = filter(lambda x: x.hostId == vq.coordinator.hostId, g_cm_hosts)[0].hostname
  191.  
  192.         v_output = "######################################################################\n"
  193.         v_output += vq.queryType + ": " + vq.queryId + " by " + vq.user + " on " + vq.database + " -- " + vq.queryState + ":\n"
  194.         v_output += "Starts at: " + vq.startTime.strftime("%d/%m/%Y_%H:%M:%S") + " and finnished at: " + vq.endTime.strftime("%d/%m/%Y_%H:%M:%S") + "\n"
  195.         v_output += vq.statement + "\n"
  196.         v_output += "The coordinator deamon was: " + v_coordinator + " and the rows produced was: " + str(vq.rowsProduced) + "\n"
  197.  
  198.         print(v_output)
  199.  
  200. #############################################
  201. #
  202. #############################################
  203. def run_function(p_cluster, argument, p_args):
  204.     switcher = {
  205.         '--status': 'f_status',
  206.         '--restart': 'f_restart',
  207.         '--start': 'f_start',
  208.         '--stop': 'f_stop',
  209.         '--rolling_restart': 'f_rolling_restart',
  210.         '--get_impala_queries': 'f_get_impala_queries'
  211.     }
  212.     func = switcher.get(argument, "Invalid Argument")
  213.     return globals()[func](p_cluster, p_args)
  214.  
  215. def f_status(p_cluster, p_args):
  216.     if p_args[0] == 'services' and p_args[1] in ('state', 'health'):
  217.         globals()['check_'+p_args[0]+'_'+p_args[1]](p_cluster)
  218.  
  219.     elif p_args[0] == 'roles' and p_args[1] in ('state', 'health'):
  220.         globals()['check_'+p_args[0]+'_'+p_args[1]](p_cluster, p_args[2])
  221.  
  222.     elif 3 < len(p_args) and p_args[0] in ('service', 'role') and p_args[1] in ('state', 'health'):
  223.         globals()['check_'+p_args[0]+'_'+p_args[1]](p_cluster, p_args[2], p_args[3])
  224.  
  225.     else: print("Error: Invalid parameters. Use the option --help for more information.")
  226.  
  227. def f_help():
  228.     print("This script connect with CM Api to collect the services state. Stop|Start|Restart and other metrics.")
  229.     print("handler_cm_api --cluster cluster_name [OPTIONS].")
  230.     print("\t--status: handler_cm_api --cluster cluster_name  --status [services|service|roles|role]. Examples:")
  231.     print("\t\thandler_cm_api --cluster cluster_name --status services [health|state]")
  232.     print("\t\thandler_cm_api --cluster cluster_name --status service [health|state] service_name")
  233.     print("\t\thandler_cm_api --cluster cluster_name --status roles [health|state] service_name")
  234.     print("\t\thandler_cm_api --cluster cluster_name --status role [health|state] service_name role_name\n")
  235.     print("\t--stop: handler_cm_api --cluster cluster_name --stop [cluster|service|role]. Examples:")
  236.     print("\t\thandler_cm_api --cluster cluster_name --stop cluster")
  237.         print("\t\thandler_cm_api --cluster cluster_name --stop service service_name")
  238.         print("\t\thandler_cm_api --cluster cluster_name --stop role service_name service_role\n")
  239.         print("\t--start: handler_cm_api --cluster cluster_name --start [cluster|service|role]. Examples:")
  240.         print("\t\thandler_cm_api --cluster cluster_name --start cluster")
  241.         print("\t\thandler_cm_api --cluster cluster_name --start service service_name")
  242.         print("\t\thandler_cm_api --cluster cluster_name --start role service_name service_role\n")
  243.     print("\t--restart: handler_cm_api --cluster cluster_name --restart [cluster|service|role]. Examples:")
  244.         print("\t\thandler_cm_api --cluster cluster_name --restart cluster")
  245.         print("\t\thandler_cm_api --cluster cluster_name --restart service service_name")
  246.         print("\t\thandler_cm_api --cluster cluster_name --restart role service_name service_role\n")
  247.         print("\t--rolling_restart: handler_cm_api --cluster cluster_name --rolling_restart [cluster|service]. Examples:")
  248.         print("\t\thandler_cm_api --cluster cluster_name --rolling_restart cluster")
  249.         print("\t\thandler_cm_api --cluster cluster_name --rolling_restart service service_name\n")
  250.     print("\t--get_impala_queries: handler_cm_api --cluster cluster_name --get_impala_queries start_time end_time filter_type filter_value limit")
  251.     print("\t\tstart_time: day/month/year_hour:min_secs. Example: 01/01/2018_23:59:59. This value will be used for searching from this date.")
  252.         print("\t\tend_time: day/month/year_hour:min_secs. Example: 02/01/2018_23:59:59. This value will be used for searching till this date.")
  253.     print("\t\tfilter_type: it can be 'user', 'duration' or 'state'")
  254.     print("\t\tfilter_value: In case 'filter_type=user' then it will be a string which will be used for filtering the queries launched by this user.")
  255.     print("\t\t\tIn case 'filter_type=duration' it will be a string like this <50s. It means the query was executed in less than 50 seconds. You can use [<>=] and [hms] for time.")
  256.     print("\t\t\tIn case 'filter_type=state' then filter_value can take the next values: CREATED, INITIALIZED, COMPILED, RUNNING, FINISHED, EXCEPTION, and UNKNOWN.")
  257.     print("\t\tlimit: The max number of queries which will be showed. The max is 200")
  258.  
  259.  
  260. ################
  261. #Pruebas
  262. ################
  263. def main():
  264.     if sys.argv[1] == '--help':
  265.         f_help()
  266.         return
  267.  
  268.     if len(sys.argv) < 5 or sys.argv[1] != '--cluster':
  269.         print("Error: Use help for more information")
  270.         return
  271.  
  272.     elif sys.argv[2] == 'all':
  273.         v_clusters = g_cm_api.get_all_clusters()
  274.  
  275.     else:
  276.         v_clusters = filter(lambda x: x.displayName == sys.argv[2], g_cm_api.get_all_clusters())
  277.         if not v_clusters:
  278.             print("Error: That cluster is not valid.")
  279.             return
  280.  
  281.         else:
  282.             if 4 < len(sys.argv):
  283.                 run_function(v_clusters[0], sys.argv[3], sys.argv[4:])
  284.    
  285. if __name__ == '__main__':
  286.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement