zoukankan      html  css  js  c++  java
  • JAVA查看CDH集群状态、主机信息、服务信息、服务角色信息

    pom依赖

        <dependency>
           <groupId>com.cloudera.api</groupId>
           <artifactId>cloudera-manager-api</artifactId>
           <version>6.2.0</version>
    
        <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>1.0.6</version>
        </dependency>
    
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.8</version>
        </dependency>
     </dependency>

    获取cloudera-manager中所有集群的信息

    
    public class ClouderaManagerCluster {
        static RootResourceV18 apiRoot;
    
        static {
            apiRoot = new ClouderaManagerClientBuilder().withHost(666.666.666.666).
                    withPort(Integer.valueOf(7180))
                    .withUsernamePassword(admin, admin).build().getRootV18();
        }
    
        private final static Logger LOGGER = LoggerFactory.getLogger(ClouderaManagerHost.class);
    
    
        public static void getAllCluster(){
            LOGGER.info("开始测试的时间为{},**************开始测试获取ClouderaManager集群信息**************",Utils.getCurrentTime());
            ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.FULL);
            LOGGER.info("ClouderaManager 共管理了{}个集群",apiClusterList.getClusters().size());
            for(ApiCluster apiCluster : apiClusterList){
                ApiCluster apiCluster1 = apiRoot.getClustersResource().readCluster(apiCluster.getName());
                LOGGER.info("集群名称 {}",apiCluster1.getName());
                LOGGER.info("集群显示名称 {}",apiCluster1.getDisplayName());
                LOGGER.info("CDH 版本:{}-{}",apiCluster1.getVersion(),apiCluster.getFullVersion());
                LOGGER.info("ClusterUrl {}",apiCluster1.getClusterUrl());
                LOGGER.info("HostUrl {}",apiCluster1.getHostsUrl());
                LOGGER.info("Cluster Uuid {}",apiCluster1.getUuid());
                LOGGER.info("集群运行状态 {}",apiCluster1.getEntityStatus());
            }
            LOGGER.info("结束测试的时间为{},**************结束测试获取ClouderaManager集群信息**************",Utils.getCurrentTime());
        }
    
    }
    

    获取cloudera-manager上所有集群的所有服务的状态

    备注:这里只是粗略的,不涉及时间点位的

    
    public class ClouderaManagerService {
    
        private final static Logger LOGGER = LoggerFactory.getLogger(ClouderaManagerService.class);
    
        static RootResourceV18 apiRoot;
    
        static {
            apiRoot = new ClouderaManagerClientBuilder().withHost(666.666.666.666).
                    withPort(Integer.valueOf(7180))
                    .withUsernamePassword(admin, admin).build().getRootV18();
        }
    
        public static void getAllService(){
            LOGGER.info("开始测试的时间为{},**************开始测试集群服务运行状态**************",Utils.getCurrentTime());
            ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.SUMMARY);
            for(ApiCluster apiCluster:apiClusterList){
                LOGGER.info("集群名称:{}",apiCluster.getDisplayName());
                LOGGER.info("CDH 版本:{}-{}",apiCluster.getVersion(),apiCluster.getFullVersion());
                ServicesResourceV10 servicesResourceV10 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());
                List apiServices = servicesResourceV10.readServices(DataView.FULL).getServices();
                LOGGER.info("集群总共有:{} 个service 在运行",apiServices.size());
                for(ApiService apiService:apiServices){
                    Service service = formatService(apiService);
                    LOGGER.info("***********************************");
                    LOGGER.info("service 名称 {}",service.getName());
                    LOGGER.info("service 类型 {}",service.getType());
                    for(Agent agent:service.getAgentList()) {
                        LOGGER.info("节点名称 {}", agent.getName());
                        LOGGER.info("节点状态 {}", agent.getStatus());
                    }
                    LOGGER.info("***********************************");
                }
            }
            LOGGER.info("结束测试的时间为{},**************结束测试集群服务运行状态**************",Utils.getCurrentTime());
        }
    
        public static Service formatService(ApiService apiService){
            Service service = new Service();
            List agents = new ArrayList<>();
            service.setName(apiService.getName());
            service.setType(apiService.getType());
            for(ApiHealthCheck apiHealthCheck:apiService.getHealthChecks()){
                Agent agent =new Agent();
                agent.setName(apiHealthCheck.getName());
                agent.setStatus(apiHealthCheck.getSummary());
                agents.add(agent);
            }
            service.setAgentList(agents);
            return service;
        }
    
        public static void getAllServiceRoles(){
            LOGGER.info("开始测试的时间为{},**************开始测试集群各个服务的roles运行状态**************",Utils.getCurrentTime());
            ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.SUMMARY);
            for(ApiCluster apiCluster:apiClusterList){
                LOGGER.info("集群名称:{}",apiCluster.getDisplayName());
                LOGGER.info("CDH 版本:{}-{}",apiCluster.getVersion(),apiCluster.getFullVersion());
                ServicesResourceV18 servicesResourceV18 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());
                List apiServices = servicesResourceV18.readServices(DataView.FULL).getServices();
                LOGGER.info("集群总共有:{} 个service 在运行",apiServices.size());
                for(ApiService apiService:apiServices){
                    RolesResourceV11 rolesResourceV11 = servicesResourceV18.getRolesResource(apiService.getName());
                    LOGGER.info("---------------------服务名称是{}---------------------",apiService.getName());
                    for(ApiRole apiRole :rolesResourceV11.readRoles()){
                        LOGGER.info("***************************",apiRole.getName());
                        LOGGER.info("role名称 {}",apiRole.getName());
                        LOGGER.info("role类型 {}",apiRole.getType());
                        LOGGER.info("所属集群 {}",apiRole.getServiceRef().getClusterName());
                        LOGGER.info("所属服务 {}",apiRole.getServiceRef().getServiceName());
                        LOGGER.info("主机ID {}",apiRole.getHostRef().getHostId());
                        LOGGER.info("roleUrl {}",apiRole.getRoleUrl());
                        LOGGER.info("role状态 {}",apiRole.getRoleState());
                        LOGGER.info("运行状态总结 {}",apiRole.getHealthSummary());
                        LOGGER.info("entityStatus {}",apiRole.getEntityStatus());
                        LOGGER.info("roleConfigGroupName {}",apiRole.getRoleConfigGroupRef().getRoleConfigGroupName());
                        LOGGER.info("configStalenessStatus {}",apiRole.getConfigStalenessStatus());
                        LOGGER.info("haStatus {}",apiRole.getHaStatus());
                        for(ApiHealthCheck apiHealthCheck:apiRole.getHealthChecks()){
                            LOGGER.info("health check name {}",apiHealthCheck.getName());
                            LOGGER.info("health check summary {}",apiHealthCheck.getSummary());
                            LOGGER.info("health check suppressed {}",apiHealthCheck.getSuppressed());
                        }
                        LOGGER.info("***************************");
                    }
                    LOGGER.info("--------------------------------------------------------",apiService.getName());
                }
            }
            LOGGER.info("结束测试的时间为{},**************结束测试集群各个服务的roles运行状态**************",Utils.getCurrentTime());
        }
    
    }
    
    public class Service {
        private String name;
    
        private String type;
    
        private List agentList = new ArrayList<>();
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public void setAgentList(List agentList) {
            this.agentList = agentList;
        }
    
        public String getName() {
            return name;
        }
    
        public String getType() {
            return type;
        }
    
        public List getAgentList() {
            return agentList;
        }
    }
    
    
    public class Agent {
        private String name;
    
        private ApiHealthSummary status;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setStatus(ApiHealthSummary status) {
            this.status = status;
        }
    
        public String getName() {
            return name;
        }
    
        public ApiHealthSummary getStatus() {
            return status;
        }
    }
    

    获取cloudera-manager中某个服务的时间序列点位

    
    public class ClouderaManagerServiceMetrics {
    
        private final static Logger LOGGER = LoggerFactory.getLogger(ClouderaManagerServiceMetrics.class);
    
        static RootResourceV18 apiRoot;
    
        static {
            apiRoot = new ClouderaManagerClientBuilder().withHost(666.666.666.666).
                    withPort(Integer.valueOf(7180))
                    .withUsernamePassword(admin, admin).build().getRootV18();
        }
    
        public static List formatApiTimeSeriesResponse(List apiTimeSeriesResponseList){
            List metrics = new ArrayList<>();
            for(ApiTimeSeriesResponse apiTimeSeriesResponse:apiTimeSeriesResponseList) {
                List dataList = new ArrayList<>();
                List apiTimeSeriesList = apiTimeSeriesResponse.getTimeSeries();
                for (ApiTimeSeries apiTimeSeries : apiTimeSeriesList) {
                    Metric metric = new Metric();
                    metric.setMetricName(apiTimeSeries.getMetadata().getMetricName());
                    metric.setEntityName(apiTimeSeries.getMetadata().getEntityName());
                    metric.setStartTime(apiTimeSeries.getMetadata().getStartTime().toString());
                    metric.setEndTime(apiTimeSeries.getMetadata().getEndTime().toString());
                    for (ApiTimeSeriesData apiTimeSeriesData : apiTimeSeries.getData()) {
                        Data data = new Data();
                        data.setTimestamp(apiTimeSeriesData.getTimestamp().toString());
                        data.setType(apiTimeSeriesData.getType());
                        data.setValue(apiTimeSeriesData.getValue());
                        dataList.add(data);
                    }
                    metric.setData(dataList);
                    metrics.add(metric);
                }
            }
            return metrics;
        }
    
        public static List getServiceMetrics(String query,String startTime , String endTime){
            TimeSeriesResourceV6 timeSeriesResourceV6 = apiRoot.getTimeSeriesResource();
            String[] params = new String []{query,startTime,endTime};
            LOGGER.info("query sql is {} ,startTime is {} ,endTime is now",params);
    
            LOGGER.info("开始测试的时间为{},**************开始查询某个服务点位状态**************",Utils.getCurrentTime());
            ApiTimeSeriesResponseList response = timeSeriesResourceV6.queryTimeSeries(query,startTime,endTime);
            List apiTimeSeriesResponseList = response.getResponses();
            List metrics = formatApiTimeSeriesResponse(apiTimeSeriesResponseList);
            LOGGER.info("查询时间序列点位:{}", JSON.toJSON(metrics));
            LOGGER.info("结束测试的时间为{},**************结束查询某个服务点位状态**************",Utils.getCurrentTime());
            return metrics;
        }
    
    }
    
    public class Metric {
        private String metricName;
    
        private String entityName;
    
        private String startTime;
    
        private String endTime;
    
        List data = new ArrayList<>();
    
        public void setMetricName(String metricName) {
            this.metricName = metricName;
        }
    
        public void setEntityName(String entityName) {
            this.entityName = entityName;
        }
    
        public void setStartTime(String startTime) {
            this.startTime = startTime;
        }
    
        public void setEndTime(String endTime) {
            this.endTime = endTime;
        }
    
        public void setData(List data) {
            this.data = data;
        }
    
        public String getMetricName() {
            return metricName;
        }
    
        public String getEntityName() {
            return entityName;
        }
    
        public String getStartTime() {
            return startTime;
        }
    
        public String getEndTime() {
            return endTime;
        }
    
        public List getData() {
            return data;
        }
    }
    
    public class Data {
        private String timestamp;
    
        private Double value;
    
        private String type;
    
        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }
    
        public void setValue(Double value) {
            this.value = value;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getTimestamp() {
            return timestamp;
        }
    
        public Double getValue() {
            return value;
        }
    
        public String getType() {
            return type;
        }
    }
    

    这个时间点位可能比较抽象,举个例子:


    时间点位

    这种以时间为横轴,点位为纵轴的我们统称为时间序列点位。

    让我们看看这个怎么用:

    
    ClouderaManagerServiceMetrics.getServiceMetrics("SELECT health_good_rate * 100 AS "good health" WHERE entityName = "hbase" AND category = SERVICE"
                    ,"2018-05-09","now");
    

    我们可能会说,沃日,这个sql语句我怎么知道。。
    这里写图片描述

    这里写图片描述

    这里写图片描述

    这样不就可以获取sql语句了嘛。

    最后贴上调用方法,放在main方法里面,即可让程序跑起来:

    
    ClouderaManagerCluster.getAllCluster();
    
    ClouderaManagerHost.getAllHost();
    
    ClouderaManagerService.getAllService();
    
    ClouderaManagerService.getAllServiceRoles();
    
    ClouderaManagerServiceMetrics.getServiceMetrics("SELECT health_good_rate * 100 AS "good health" WHERE entityName = "hbase" AND category = SERVICE"
                    ,"2018-05-09","now");
    

    api简单文档

    cloudera manager api 入口

    用来创建cloudera manager 的入口,需要输入地址以及账号密码来创建一个根资源的实例:

    RootResourceV10 apiRoot = new ClouderaManagerClientBuilder().withHost(“xxx.xxx.xxx.xx”).withUsernamePassword(“123”, “123”).build().getRootV10();
    

    RootResourceV10这个对象不是必须的,它还有V9,V8等等许多,只不过越向后的版本兼容前面的并且增加了新功能。

    获取主机信息

    HostsResourceV10 hostsResourceV10 = apiRoot.getHostsResource();

    通过cloudera manager 的入口api,获取一个主机资源的实例,V10的意思同上。

    List hostList = hostsResourceV10.readHosts(DataView.SUMMARY).getHosts();
    

    这样就可以得到一个apihost的一个列表对象,这里面可能获得的不全面,可以通过Apihost开面的hostId传入到

    hostsResourceV10.readHost(apiHost.getHostId())

    当中,来获取某个主机的信息。

    获取服务状态

    还是要借助cloudera manager的公共入口RootResourceV10来进行,首先要获取服务状态,你要先知道你的cloudera manager 管理了几个集群。

    ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.SUMMARY);

    ApiClusterList 是ApiCluster的列表,可以遍历ApiCluster来获取每个ApiCluster,ApiCluster里面包含了集群的详细信息,包含CDH版本,集群名称等等。

    ServicesResourceV10 servicesResourceV10 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());
    

    通过获取的集群名称,你可以获得当前集群下的服务实例

    List  apiServices = servicesResourceV10.readServices(DataView.FULL).getServices();

    然后将所有服务资源转化为ApiService的一个列报表,这样我们就可以通过遍历ApiService来获取每个服务的状态了

    获取服务中各个主节点和从节点详细状态

    ServicesResourceV10 servicesResourceV10 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());
    

    通过获取的集群名称,你可以获得当前集群下的服务实例

    List  apiServices = servicesResourceV10.readServices(DataView.FULL).getServices();
    

    通过遍历获取每个服务。

    RolesResourceV11 rolesResourceV11 = servicesResourceV18.getRolesResource(apiService.getName());
    

    然后通过RolesResourceV11获得一个service下面得所有role,然后通过

    rolesResourceV11.readRoles()

    获取一个ApiRole的链表。遍历即可获取一个service下的所有role。

    获取服务或者主机点位时间序列

    TimeSeriesResourceV6 timeSeriesResourceV6 = apiRoot.getTimeSeriesResource();

    也是依赖于cloudera manager 的公共入口RootResourceV10,获得一个TimeSeriesResource 用来查询时间序列点位,

    ApiTimeSeriesResponseList response = timeSeriesResourceV6.queryTimeSeries(query,startTime,endTime);

    可以采用queryTimeSeries方法,来进行查询,query是一个sql语句,然后是开始时间,结束时间,下面给出两个示例查询。

    “SELECT health_good_rate * 100 AS ”good health” WHERE entityName = ”hbase” AND category = SERVICE” ,”2018-05-09”,”now”

    List apiTimeSeriesResponseList=response.getResponses();
    

    获取一个ApiTimeSeriesResponse的一个列表,来获取查询结果即可。

    转自:https://blog.csdn.net/qq_31806205/article/details/80392032

  • 相关阅读:
    水煮栗子
    张至顺道长羽化登仙+说修行(道经每日清修)
    治疗口腔溃疡的穴位按摩方法
    一年四季的时令蔬菜水果表
    坐式养生八段锦口诀及练法图解
    SOA建设规划
    生鲜电商业务流程规划
    产品定义到产品推广的思路
    生鲜财务核算
    税率与存货、供应商关系
  • 原文地址:https://www.cnblogs.com/diandianquanquan/p/12313227.html
Copyright © 2011-2022 走看看