个人主页——开着拖拉机回家_Linux,大数据运维-CSDN博客✨
感谢点赞和关注 ,每天进步一点点!加油!
目录
一、概述
二、集群版本信息
三、组件状态信息获取
三、告警实现
一、概述
Ambari 借鉴了很多成熟分布式软件的 API 设计。Rest API 就是一个很好地体现。通过 Ambari 的 Rest API,可以在脚本中通过 curl 维护整个集群。并且,我们可以用 Rest API 实现一些无法在 Ambari GUI 上面做的操作。
二、集群版本信息
三、组件状态信息获取
参考:【Ambari】Python调用Rest API 获取集群状态信息并发送钉钉告警-CSDN博客
RESOURCEMANAGER停止
curl -u admin:admin -i -H'X-Requested-By:ambari'-X PUT -d'{"RequestInfo":{"context":"Restart RESOURCEMANAGER via REST"},"Body" : {"HostRoles" : {"state":"INSTALLED"}}}' http://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp105/host_components/RESOURCEMANAGER
- -u Ambari登录用户:密码
- -i -H获取http请求的完整头部信息,包括请求方法、请求地址、请求头信息等
- -X 同时想发 HEAD、GET 或 POST 请求,需在 -X 中声明要使用的请求方式
ambari页面显示停止成功调用
ambari页面RM服务停止
RESOURCEMANAGER启动
curl -u admin:admin -i -H'X-Requested-By:ambari'-X PUT -d'{"RequestInfo":{"context":"Restart RESOURCEMANAGER via REST"},"Body" : {"HostRoles" : {"state":"STARTED"}}}' http://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp105/host_components/RESOURCEMANAGER
直接使用RESTART
curl -uadmin:admin -H 'X-Requested-By: ambari' -X POST -d '{"RequestInfo":{"command":"RESTART","context":"Restart RESOURCEMANAGER via REST","operation_level":{"level":"HOST","cluster_name":"winner"}},"Requests/resource_filters":[{"service_name":"YARN","component_name":"RESOURCEMANAGER","hosts":"hdp103"}]}' http://192.168.2.153:8080/api/v1/clusters/winner/requests
请求响应接受
四、告警实现
python 脚本实现RM HA的监控告警
# -*- coding: utf-8 -*-import loggingimport timefrom imp import reloadimport requestsimport jsonimport sys"""~~~~~~~~~~~~author: kanglldate: 2023/12/6 13:29 desc: -- curl 请求,如下为测试链接curl -u admin:admin -i -H X-Requested-By:ambari -XGEThttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/winnerhttp://192.168.2.153:8080/api/v1/clusters/winner/hosts/hdp105/host_components/RESOURCEMANAGER-- 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"""reload(sys)sys.setdefaultencoding('utf8')__author__ = 'kanglilong'logger = logging.getLogger('mylogger')logger.setLevel(level=logging.INFO)# 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" />
钉钉告警发送成功
RM 重启一台RM操作完成,触发HA的强制切换
需要添加配置 ambari.properties
vi /etc/ambari-server/conf/ambari.properties# 添加如下配置api.csrfPrevention.enabled=false# 重启ambari-server restart
request请求返回的状态码