博主 “开着拖拉机回家”带您 Go to New World.✨

个人主页——开着拖拉机回家_大数据运维-CSDN博客 ✨

希望本文能够给您带来一定的帮助文章粗浅,敬请批评指正!

感谢点赞和关注 ,每天进步一点点!加油!

目录

一、概述

二、集群版本信息

三、组件状态信息获取

四、DataNode 启动

五、Python 实现 Rest API获取组件状态并告警


一、概述


Ambari 借鉴了很多成熟分布式软件的 API 设计。Rest API 就是一个很好地体现。通过 Ambari 的 Rest API,可以在脚本中通过 curl 维护整个集群。并且,我们可以用 Rest API 实现一些无法在 Ambari GUI 上面做的操作。


二、集群版本信息



三、组件状态信息获取


curl -u admin:admin -i -H X-Requested-By:ambari -XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp106/host_components/NODEMANAGERcurl -u admin:admin -i -H X-Requested-By:ambari -XGET http://192.168.2.153:8080/api/v1/clusters/winner/services/HIVEcurl -u admin:admin -i -H X-Requested-By:ambari -XGET http://192.168.2.153:8080/api/v1/clusters/winner/services/HIVEcurl -u admin:admin -i -H X-Requested-By:ambari -XGET http://192.168.2.153:8080/api/v1/clusters/winner/services/TEZcurl -u admin:admin -i -H X-Requested-By:ambari -XGET http://192.168.2.153:8080/api/v1/clusters/winner/services/HBASEcurl -u admin:admin -i -H X-Requested-By:ambari -XGET http://192.168.2.153:8080/api/v1/clusters/winner/services/HDFScurl -u admin:admin -i -H X-Requested-By:ambari -XGET http://192.168.2.153:8080/api/v1/clusters/winner/services/ZOOKEEPER
  • -u Ambari登录用户:密码
  • -i -H获取http请求的完整头部信息,包括请求方法、请求地址、请求头信息等
  • -X 同时想发 HEAD、GET 或 POST 请求,需在 -X 中声明要使用的请求方式

获取hdp106服务器上NODEMANAGER 的状态信息

[winner_spark@hdp105 root]$ curl -u admin:admin -i -H X-Requested-By:ambari -XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp106/host_components/NODEMANAGERHTTP/1.1 200 OKDate: Tue, 29 Aug 2023 06:15:38 GMTX-Frame-Options: DENYX-XSS-Protection: 1; mode=blockX-Content-Type-Options: nosniffCache-Control: no-storePragma: no-cacheSet-Cookie: AMBARISESSIONID=node0146ihmo69ytgk12k48wrpwrt0v5.node0;Path=/;HttpOnlyExpires: Thu, 01 Jan 1970 00:00:00 GMTUser: 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 -u admin:admin -i -H'X-Requested-By:ambari'-X PUT -d'{"RequestInfo":{"context":"Start DATANODE via REST"},"Body" : {"ServiceInfo" : {"state":"STARTED"}}}' http://192.168.2.153:8080/api/v1/clusters/winner/services/HDFS

datanode 启动 返回 Accepted

ambari 页面显示: Start DATANODE via REST 表示我们执行是成功的。


五、Python 实现 Rest API获取组件状态并告警


import timeimport requestsimport json"""~~~~~~~~~~~~author: kanglldate: 2023/8/25 17:22desc: Ambari rest api 获取组件告警信息-- curl 请求,如下为测试链接curl -u admin:admin -i -H X-Requested-By:ambari -XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/winner-- datanode 启动curl -u admin:admin -i -H'X-Requested-By:ambari'-X PUT -d'{"RequestInfo":{"context":"Start RESOURCEMANAGER via REST"},"Body" : {"ServiceInfo" : {"state":"STARTED"}}}' http://192.168.2.153:8080/api/v1/clusters/winner/services/HDFS"""__author__ = 'kanglilong'# Ambari rest api 访问地址control_url = "http://192.168.2.153:8080/api/v1/clusters/winner/hosts"# ambari web 登录账号AUTH = ("admin", "admin")headers = {'Content-Type': 'application/json;charset=utf-8'}# 钉钉URLapi_url = "https://oapi.dingtalk.com/robot/send" /> 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)

钉钉告警发送成功: