找回密码
 会员注册
查看: 34|回复: 0

AmbariPython调用RestAPI获取集群状态信息并发送钉钉告警

[复制链接]

2万

主题

0

回帖

7万

积分

超级版主

积分
73812
发表于 2024-9-3 09:34:02 | 显示全部楼层 |阅读模式
🍁博主"开着拖拉机回家"带您GotoNewWorld.✨🍁🦄个人主页——🎐开着拖拉机回家_大数据运维-CSDN博客🎐✨🍁🪁🍁希望本文能够给您带来一定的帮助🌸文章粗浅,敬请批评指正!🍁🐥🪁🍁🪁🍁🪁🍁🪁🍁🪁🍁🪁🍁🪁🍁🪁🪁🍁🪁🍁🪁🍁🪁🍁🪁🍁🪁🍁感谢点赞和关注,每天进步一点点!加油!目录一、概述二、集群版本信息三、组件状态信息获取四、DataNode启动五、Python实现RestAPI获取组件状态并告警一、概述Ambari借鉴了很多成熟分布式软件的API设计。RestAPI就是一个很好地体现。通过Ambari的RestAPI,可以在脚本中通过curl维护整个集群。并且,我们可以用RestAPI实现一些无法在AmbariGUI上面做的操作。二、集群版本信息三、组件状态信息获取curl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp106/host_components/NODEMANAGERcurl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/services/HIVEcurl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/services/HIVEcurl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/services/TEZcurl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/services/HBASEcurl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/services/HDFScurl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/services/ZOOKEEPER-uAmbari登录用户:密码-i-H获取http请求的完整头部信息,包括请求方法、请求地址、请求头信息等-X同时想发HEAD、GET或POST请求,需在-X中声明要使用的请求方式获取hdp106服务器上NODEMANAGER的状态信息[winner_spark@hdp105root]$curl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp106/host_components/NODEMANAGERHTTP/1.1200OKDate:Tue,29Aug202306:15:38GMTX-Frame-OptionsENYX-XSS-Protection:1;mode=blockX-Content-Type-Options:nosniffCache-Control:no-storePragma:no-cacheSet-Cookie:AMBARISESSIONID=node0146ihmo69ytgk12k48wrpwrt0v5.node0ath=/;HttpOnlyExpires:Thu,01Jan197000:00:00GMTUser:adminContent-Type:text/plain;charset=utf-8X-Content-Type-Options:nosniffVary:Accept-Encoding,User-AgentTransfer-Encoding:chunked{"href":"http://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp106/host_components/NODEMANAGER","HostRoles":{"cluster_name":"winner","component_name":"NODEMANAGER","desired_admin_state":"INSERVICE","desired_repository_version":"3.1.4.0-315","desired_stack_id":"HDP-3.1","desired_state":"STARTED","display_name":"NodeManager","host_name":"hdp106","maintenance_state":"OFF","public_host_name":"hdp106","reload_configs":false,"service_name":"YARN","stale_configs":false,"state":"STARTED","upgrade_state":"NONE","version":"3.1.4.0-315","actual_configs":{}},"host":{"href":"http://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp106"},"component":[{"href":"http://192.168.2.153:8080/api/v1/clusters/winner/services/YARN/components/NODEMANAGER","ServiceComponentInfo":{"cluster_name":"winner","component_name":"NODEMANAGER","service_name":"YARN"}}],"processes":[]}四、DataNode启动curl-uadmin:admin-i-H'X-Requested-By:ambari'-XPUT-d'{"RequestInfo":{"context":"StartDATANODEviaREST"},"Body":{"ServiceInfo":{"state":"STARTED"}}}'http://192.168.2.153:8080/api/v1/clusters/winner/services/HDFSdatanode启动返回Acceptedambari页面显示:StartDATANODEviaREST表示我们执行是成功的。五、Python实现RestAPI获取组件状态并告警importtimeimportrequestsimportjson"""~~~~~~~~~~~~author:kanglldate:2023/8/2517:22desc:Ambarirestapi获取组件告警信息--curl请求,如下为测试链接curl-uadmin:admin-i-HX-Requested-By:ambari-XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/winner--datanode启动curl-uadmin:admin-i-H'X-Requested-By:ambari'-XPUT-d'{"RequestInfo":{"context":"StartRESOURCEMANAGERviaREST"},"Body":{"ServiceInfo":{"state":"STARTED"}}}'http://192.168.2.153:8080/api/v1/clusters/winner/services/HDFS"""__author__='kanglilong'#Ambarirestapi访问地址control_url="http://192.168.2.153:8080/api/v1/clusters/winner/hosts"#ambariweb登录账号AUTH=("admin","admin")headers={'Content-Type':'application/json;charset=utf-8'}#钉钉URLapi_url="https://oapi.dingtalk.com/robot/send?access_token=f4e0f344306ce9b6eec60bec95d5aa7c57f4264a791458dc09121dd7e948ac64"hostname="hdp105"ambari_server_ip="192.168.2.153"defgetComponentStatus(host,component):"""获取某个节点组件的状态:paramhost:主机名:paramcomponent:组件:return:状态"""get_component_status_url=control_url+"/{}/host_components/{}".format(host,component)try:rep=requests.get(get_component_status_url,auth=AUTH)ifrep.status_code==200:jsonRep=json.loads(rep.text)status=jsonRep['HostRoles']['state']returnstatuselse:print("获取组件状态返回异常")exceptExceptionase:print(e)defgetHostComponentsStatus(host):"""获取某个服务器上某个组件的状态信息:paramhost::return:component_dict组件与其状态status当前节点状态是否符合期望,getStatus是否获取到了状态"""component_dict={}get_host_components_status_url=control_url+"/{}/host_components".format(host)try:rep=requests.get(get_host_components_status_url,auth=AUTH)#如果状态码是20x则获取成功print(rep.status_code)ifstr(rep.status_code).startswith("20"):jsonrep=json.loads(rep.text)items=jsonrep['items']foritemJsoninitems:item=itemJson['HostRoles']['component_name']#排除client角色,与SQOOP等一直是启动状态的客户端,这些不需要启动,也不需要判断状态if"CLIENT"notinitemand"SQOOP"notinitemand"INFRA_SOLR"notinitem:component_status=getComponentStatus(host,item)#INSTALLED表示已安装没有启动,我们默认INSTALLED的组件没有STARTED就是停止,要发告警信息ifcomponent_status=="INSTALLED":#{'DATANODE':'STARTED','HBASE_REGIONSERVER':'STARTED'}component_dict[item]=component_statuselse:#没有正常获取到状态print("没有正常获取到状态")exceptExceptionase:print(e)returncomponent_dictdefmsg(text,api_url):""":paramtext:告警文本:paramapi_url:钉钉URL:return:"""json_text={"msgtype":"text","text":{"content":text},"at":{"atMobiles":["1786881xxxx"]}}requests.post(api_url,json.dumps(json_text),headers=headers).contentcomponent_dict=getHostComponentsStatus(hostname)forcompo_dictincomponent_dict.items():compo_dict_len=int(len(component_dict))ifcompo_dict_len>50:#告警信息条数判断,告警信息太频繁钉钉告警可能会阻塞告警time.sleep(30)component_name=compo_dict[0]now_time=time.localtime(time.time())formatted_time=time.strftime('%Y-%m-%d%H:%M:%S',now_time)text="告警对象:IP:"+ambari_server_ip+'主机名:'+hostname+'\n组件名称:'+component_name+"\n告警内容:HDP集群组件"+component_name+"停止运行"+"\n告警时间:"+formatted_timetime.sleep(2)#告警匀速发出msg(text,api_url)'运行运行钉钉告警发送成功:
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-13 17:37 , Processed in 2.079423 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表