Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from cm_api.api_client import ApiResource
- import sys
- import re
- from datetime import datetime, timedelta
- g_cm_host = "bender1"
- g_cm_api = ApiResource(g_cm_host, version=10, username="admin", password="admin")
- g_cm_hosts = g_cm_api.get_all_hosts()
- ###############################
- # Colores
- ###############################
- class COLORS:
- RED = "\x1b[91m"
- BLUE = "\x1b[94m"
- YELLOW = "\x1b[93m"
- GREEN = "\x1b[92m"
- RESET = "\x1b[0m"
- def coloring(v_st, v_msg):
- 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
- v_status = "[" + v_color + v_st + COLORS.RESET + "]"
- return v_status.ljust(20, ' ') + v_msg
- ######################################################################
- #Monitoring
- ######################################################################
- def check_services_state(p_cluster):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = p_cluster.get_all_services()
- for v_srv in v_services: print(coloring(v_srv.serviceState, v_srv.type))
- def check_service_state(p_cluster, p_service):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_service = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else: print coloring(v_service[0].ServiceState, v_srv.type)
- ######################################
- def check_services_health(p_cluster):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = p_cluster.get_all_services()
- for v_srv in v_services: print(coloring(v_srv.healthSummary, v_srv.type))
- def check_service_health(p_cluster, p_service):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else:
- print("*" + v_services[0].type + ":")
- print coloring(v_services[0].healthSummary, v_srv.type)
- ##################################
- def check_roles_state(p_cluster, p_service):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else:
- print("*" + v_services[0].type + ":")
- for v_role in v_services[0].get_all_roles():
- print(coloring(v_role.roleState, filter(lambda x: x.hostId==v_role.hostRef.hostId, g_cm_hosts)[0].hostname) +":\t" +v_role.type)
- def check_role_state(p_cluster, p_service, p_role):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else:
- print("*" + v_services[0].type + ":")
- v_roles = filter(lambda x: x.type == p_role, v_services[0].get_all_roles())
- 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)
- def check_roles_health(p_cluster, p_service):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else:
- print("*" + v_services[0].type + ":")
- for v_role in v_services[0].get_all_roles():
- print(coloring(v_role.healthSummary, filter(lambda x: x.hostId==v_role.hostRef.hostId, g_cm_hosts)[0].hostname) +":\t" +v_role.type)
- def check_role_health(p_cluster, p_service, p_role):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else:
- print("*" + v_services[0].type + ":")
- v_roles = filter(lambda x: x.type == p_role, v_services[0].get_all_roles())
- 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)
- ######################################################################
- # Restarting services
- ######################################################################
- ############################################################################
- # Capturo la excepcion en caso de que el servicio no permita rolling restart
- def rolling_restart_service(p_cluster, p_service):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else:
- try:
- cmd = v_services[0].rolling_restart().wait()
- if re.match("^.*success: False\)$", str(cmd)):
- print "The service restart failed"
- elif re.match("^.*success: True\)$", str(cmd)):
- print "The service restart worked"
- else:
- print "Error: It has to be checked"
- except:
- if re.match("Command not valid for", str(sys.exc_info()[1])):
- print "It's not possible to use Rolling Restart in this service."
- else: raise
- def restart_service(p_cluster, p_service):
- print('----------------------------------\n' + p_cluster.displayName + ':\n')
- v_services = filter(lambda x: x.type == p_service, p_cluster.get_all_services())
- if not v_services: print("Error: Service not found")
- else:
- cmd = v_services[0].restart().wait()
- if re.match("^.*success: False\)$", str(cmd)):
- print "The service restart failed"
- elif re.match("^.*success: True\)$", str(cmd)):
- print "The service restart worked"
- else:
- print "Error: It has to be checked"
- ##############################################################################################
- # Impala Queries
- ##############################################################################################
- def f_get_impala_queries(p_cluster, p_args):
- p_start_time = p_args[0]
- p_end_time = p_args[1]
- p_filter_type = p_args[2]
- p_filter_value = p_args[3]
- p_limit = int(p_args[4])
- v_impala = filter(lambda x: x.type == 'IMPALA', p_cluster.get_all_services())[0]
- if not v_impala:
- print("Error: Impala service doesnt exist in this cluster.")
- return
- 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')
- else:
- print("Error. startTime format is not valid.")
- return
- 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')
- else:
- print("Error. startTime format is not valid.")
- return
- if p_filter_type == "user" and type(p_filter_value) == str: v_filter_str = 'user = ' + p_filter_value
- elif p_filter_type == "duration" and re.match("^[<>=]\d+[hms]$"): v_filter_str = 'queryDuration ' + p_filter_value
- elif p_filter_type == "state" and p_filter_value in ('CREATED', 'INITIALIZED', 'COMPILED', 'RUNNING', 'FINISHED', 'EXCEPTION', 'UNKNOWN'):
- v_filter_str = 'queryState = ' + p_filter_value
- else:
- print("Error: Filter is not valid.")
- return
- if type(p_limit) == int and p_limit < 201: v_limit = p_limit
- else:
- print("Error: Limit is not valid. It must be > 0 and <= 200")
- return
- v_queries = v_impala.get_impala_queries(v_start_time, v_end_time, v_filter_str, v_limit).queries
- for vq in v_queries:
- v_coordinator = filter(lambda x: x.hostId == vq.coordinator.hostId, g_cm_hosts)[0].hostname
- v_output = "######################################################################\n"
- v_output += vq.queryType + ": " + vq.queryId + " by " + vq.user + " on " + vq.database + " -- " + vq.queryState + ":\n"
- 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"
- v_output += vq.statement + "\n"
- v_output += "The coordinator deamon was: " + v_coordinator + " and the rows produced was: " + str(vq.rowsProduced) + "\n"
- print(v_output)
- #############################################
- #
- #############################################
- def run_function(p_cluster, argument, p_args):
- switcher = {
- '--status': 'f_status',
- '--restart': 'f_restart',
- '--start': 'f_start',
- '--stop': 'f_stop',
- '--rolling_restart': 'f_rolling_restart',
- '--get_impala_queries': 'f_get_impala_queries'
- }
- func = switcher.get(argument, "Invalid Argument")
- return globals()[func](p_cluster, p_args)
- def f_status(p_cluster, p_args):
- if p_args[0] == 'services' and p_args[1] in ('state', 'health'):
- globals()['check_'+p_args[0]+'_'+p_args[1]](p_cluster)
- elif p_args[0] == 'roles' and p_args[1] in ('state', 'health'):
- globals()['check_'+p_args[0]+'_'+p_args[1]](p_cluster, p_args[2])
- elif 3 < len(p_args) and p_args[0] in ('service', 'role') and p_args[1] in ('state', 'health'):
- globals()['check_'+p_args[0]+'_'+p_args[1]](p_cluster, p_args[2], p_args[3])
- else: print("Error: Invalid parameters. Use the option --help for more information.")
- def f_help():
- print("This script connect with CM Api to collect the services state. Stop|Start|Restart and other metrics.")
- print("handler_cm_api --cluster cluster_name [OPTIONS].")
- print("\t--status: handler_cm_api --cluster cluster_name --status [services|service|roles|role]. Examples:")
- print("\t\thandler_cm_api --cluster cluster_name --status services [health|state]")
- print("\t\thandler_cm_api --cluster cluster_name --status service [health|state] service_name")
- print("\t\thandler_cm_api --cluster cluster_name --status roles [health|state] service_name")
- print("\t\thandler_cm_api --cluster cluster_name --status role [health|state] service_name role_name\n")
- print("\t--stop: handler_cm_api --cluster cluster_name --stop [cluster|service|role]. Examples:")
- print("\t\thandler_cm_api --cluster cluster_name --stop cluster")
- print("\t\thandler_cm_api --cluster cluster_name --stop service service_name")
- print("\t\thandler_cm_api --cluster cluster_name --stop role service_name service_role\n")
- print("\t--start: handler_cm_api --cluster cluster_name --start [cluster|service|role]. Examples:")
- print("\t\thandler_cm_api --cluster cluster_name --start cluster")
- print("\t\thandler_cm_api --cluster cluster_name --start service service_name")
- print("\t\thandler_cm_api --cluster cluster_name --start role service_name service_role\n")
- print("\t--restart: handler_cm_api --cluster cluster_name --restart [cluster|service|role]. Examples:")
- print("\t\thandler_cm_api --cluster cluster_name --restart cluster")
- print("\t\thandler_cm_api --cluster cluster_name --restart service service_name")
- print("\t\thandler_cm_api --cluster cluster_name --restart role service_name service_role\n")
- print("\t--rolling_restart: handler_cm_api --cluster cluster_name --rolling_restart [cluster|service]. Examples:")
- print("\t\thandler_cm_api --cluster cluster_name --rolling_restart cluster")
- print("\t\thandler_cm_api --cluster cluster_name --rolling_restart service service_name\n")
- print("\t--get_impala_queries: handler_cm_api --cluster cluster_name --get_impala_queries start_time end_time filter_type filter_value limit")
- 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.")
- 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.")
- print("\t\tfilter_type: it can be 'user', 'duration' or 'state'")
- 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.")
- 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.")
- print("\t\t\tIn case 'filter_type=state' then filter_value can take the next values: CREATED, INITIALIZED, COMPILED, RUNNING, FINISHED, EXCEPTION, and UNKNOWN.")
- print("\t\tlimit: The max number of queries which will be showed. The max is 200")
- ################
- #Pruebas
- ################
- def main():
- if sys.argv[1] == '--help':
- f_help()
- return
- if len(sys.argv) < 5 or sys.argv[1] != '--cluster':
- print("Error: Use help for more information")
- return
- elif sys.argv[2] == 'all':
- v_clusters = g_cm_api.get_all_clusters()
- else:
- v_clusters = filter(lambda x: x.displayName == sys.argv[2], g_cm_api.get_all_clusters())
- if not v_clusters:
- print("Error: That cluster is not valid.")
- return
- else:
- if 4 < len(sys.argv):
- run_function(v_clusters[0], sys.argv[3], sys.argv[4:])
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement