Cloudera Manager : REST API

川长思鸟来 2022-12-11 03:20 276阅读 0赞

文章目录

  • 1, api使用指南
    • a, 查看集群信息
    • b, 查看主机信息
    • c, 查看服务状态
      • 1, 获取服务名称和状态
      • 2, 获取服务配置
      • 3, 修改服务配置
        • I, 修改cm监控配置 (调用rest接口)
        • II, 修改cdh服务配置 (调用rest接口)
        • III, 修改cdh服务内存配置 (直接修改mysql数据)
    • d, 启动/停止服务
      • 1, 整个集群服务
      • 2, 集群的单个服务
      • 3, cm 监控服务
      • 3, 查看命令执行结果
    • e, 导入集群模板:新建集群
      • 1, 集群模板参数
      • 2, 命令详情查看
  • 2, tsql监控查询
    • a, 使用指南
    • b, 在查询控制台执行sql
    • c, 使用api查询sql
  • 3, 使用java调用cm api
    • 发送get/post请求: springboot+RestTemplate
      • 1, pom.xml配置
      • 2, 具体代码

1, api使用指南

总览:http://test-c6:7180/static/apidocs/
quickstart: http://test-c6:7180/static/apidocs/tutorial.html

a, 查看集群信息

  1. [root@test-c6 ~]# curl -u admin:admin 'http://localhost:7180/api/v17/clusters'
  2. {
  3. "items" : [ {
  4. "name" : "Cluster 1",
  5. "displayName" : "Cluster 1",
  6. "version" : "CDH5",
  7. "fullVersion" : "5.12.0",
  8. "maintenanceMode" : false,
  9. "maintenanceOwners" : [ ],
  10. "clusterUrl" : "http://cdh-c6-master:7180/cmf/clusterRedirect/cluster_1",
  11. "hostsUrl" : "http://cdh-c6-master:7180/cmf/clusterRedirect/cluster_1/hosts",
  12. "entityStatus" : "UNKNOWN_HEALTH",
  13. "uuid" : "0e5f377c-2036-4cf2-a757-f39cacea8e6b"
  14. } ]
  15. }

b, 查看主机信息

  1. [root@test-c6 ~]# curl -u admin:admin http://localhost:7180/api/v17/hosts
  2. {
  3. "items" : [ {
  4. "hostId" : "68d905d8-4802-486d-9285-41e70576c05b",
  5. "ipAddress" : "192.168.56.161",
  6. "hostname" : "test-c6",
  7. "rackId" : "/default",
  8. "hostUrl" : "http://test-c6:7180/cmf/hostRedirect/68d905d8-4802-486d-9285-41e70576c05b",
  9. "maintenanceMode" : false,
  10. "maintenanceOwners" : [ ],
  11. "commissionState" : "COMMISSIONED",
  12. "numCores" : 1,
  13. "numPhysicalCores" : 1,
  14. "totalPhysMemBytes" : 5210898432
  15. }, {
  16. "hostId" : "0244fbb4-0d02-40b5-9cac-17d9e388eb97",
  17. "ipAddress" : "192.168.56.162",
  18. "hostname" : "test-c62",
  19. "rackId" : "/default",
  20. "hostUrl" : "http://test-c6:7180/cmf/hostRedirect/0244fbb4-0d02-40b5-9cac-17d9e388eb97",
  21. "maintenanceMode" : false,
  22. "maintenanceOwners" : [ ],
  23. "commissionState" : "COMMISSIONED",
  24. "numCores" : 1,
  25. "numPhysicalCores" : 1,
  26. "totalPhysMemBytes" : 2176856064
  27. } ]
  28. }

c, 查看服务状态

1, 获取服务名称和状态

  1. ##查询所有服务
  2. [root@test-c6 ~]# curl -u admin:admin http://localhost:7180/api/v17/clusters/Cluster%201/services
  3. {
  4. "items" : [ {
  5. "name" : "zookeeper",
  6. "type" : "ZOOKEEPER",
  7. "clusterRef" : {
  8. "clusterName" : "cluster"
  9. },
  10. "serviceUrl" : "http://test-c6:7180/cmf/serviceRedirect/zookeeper",
  11. "roleInstancesUrl" : "http://test-c6:7180/cmf/serviceRedirect/zookeeper/instances",
  12. "serviceState" : "STARTED",
  13. "healthSummary" : "BAD",
  14. "healthChecks" : [ {
  15. "name" : "ZOOKEEPER_CANARY_HEALTH",
  16. "summary" : "GOOD",
  17. "suppressed" : false
  18. }, {
  19. "name" : "ZOOKEEPER_SERVERS_HEALTHY",
  20. "summary" : "BAD",
  21. "suppressed" : false
  22. } ],
  23. "configStalenessStatus" : "FRESH",
  24. "clientConfigStalenessStatus" : "FRESH",
  25. "maintenanceMode" : false,
  26. "maintenanceOwners" : [ ],
  27. "displayName" : "ZooKeeper",
  28. "entityStatus" : "BAD_HEALTH"
  29. },
  30. ....
  31. ]}
  32. ## 查询单个服务
  33. [root@test-c6 ~]# curl -u admin:admin http://localhost:7180/api/v17/clusters/Cluster%201/services/hbase
  34. {
  35. "name" : "hbase",
  36. "type" : "HBASE",
  37. "clusterRef" : {
  38. "clusterName" : "cluster"
  39. },
  40. "serviceUrl" : "http://test-c6:7180/cmf/serviceRedirect/hbase",
  41. "roleInstancesUrl" : "http://test-c6:7180/cmf/serviceRedirect/hbase/instances",
  42. "serviceState" : "STOPPED",
  43. "healthSummary" : "DISABLED",
  44. "healthChecks" : [ {
  45. "name" : "HBASE_MASTER_HEALTH",
  46. "summary" : "DISABLED",
  47. "suppressed" : false
  48. }, {
  49. "name" : "HBASE_REGION_SERVERS_HEALTHY",
  50. "summary" : "DISABLED",
  51. "suppressed" : false
  52. } ],
  53. "configStalenessStatus" : "FRESH",
  54. "clientConfigStalenessStatus" : "FRESH",
  55. "maintenanceMode" : false,
  56. "maintenanceOwners" : [ ],
  57. "displayName" : "HBase",
  58. "entityStatus" : "STOPPED"
  59. }

2, 获取服务配置

在这里插入图片描述

  1. [root@cdh-c6-master ~]# curl -u admin:admin \
  2. http://localhost:7180/api/v17/clusters/cluster_1/services/yarn?view=export
  3. {
  4. "name" : "yarn",
  5. "type" : "YARN",
  6. "config" : {
  7. "items" : [ {
  8. "name" : "hadoop_secure_web_ui",
  9. "value" : "false",
  10. "sensitive" : false
  11. },
  12. .... ]
  13. },
  14. "roles" : [ ... ],
  15. "displayName" : "YARN (MR2 Included)",
  16. "roleConfigGroups" : [ {
  17. "name" : "yarn-JOBHISTORY-BASE",
  18. "displayName" : "JobHistory Server Default Group",
  19. "roleType" : "JOBHISTORY",
  20. "base" : true,
  21. "serviceRef" : {
  22. "clusterName" : "cluster_1",
  23. "serviceName" : "yarn"
  24. },
  25. "config" : {
  26. "items" : [ {
  27. "name" : "mr2_jobhistory_java_heapsize",
  28. "value" : "159383552",
  29. "sensitive" : false
  30. } ]
  31. }
  32. }, ... ]
  33. }[root@cdh-c6-master ~]#

3, 修改服务配置

I, 修改cm监控配置 (调用rest接口)

在这里插入图片描述

  1. [root@c7-iris2 ~]# cat a.json
  2. {
  3. "items":
  4. [{
  5. "name":"firehose_heapsize",
  6. "value":1073741824
  7. }]
  8. }
  9. [root@c7-iris2 ~]# curl -X PUT -u admin:admin \
  10. http://localhost:7180/api/v17/cm/service/roleConfigGroups/mgmt-SERVICEMONITOR-BASE/config \
  11. -H "Content-type: application/json" -d @a.json
  12. ##备注: -d @参数文件 --> 等价于直接填写参数:
  13. -d "{\"items\":[{\"name\":\"firehose_heapsize\",\"value\":1073741824}]}"
  14. ## 为方便提交命令,可以使用这种格式,如:
  15. # 1073741824 bytes:1g
  16. # 268435456 bytes:256m
  17. curl -X PUT -u admin:admin http://localhost:7180/api/v17/cm/service/roleConfigGroups/mgmt-SERVICEMONITOR-BASE/config -H "Content-type: application/json" -d \
  18. "{\"items\":[{\"name\":\"firehose_heapsize\",\"value\":1073741824}]}"
  19. curl -X PUT -u admin:admin http://localhost:7180/api/v17/cm/service/roleConfigGroups/mgmt-HOSTMONITOR-BASE/config -H "Content-type: application/json" -d \
  20. "{\"items\":[{\"name\":\"firehose_heapsize\",\"value\":1073741824}]}"
  21. curl -X PUT -u admin:admin http://localhost:7180/api/v17/cm/service/roleConfigGroups/mgmt-REPORTSMANAGER-BASE/config -H "Content-type: application/json" -d \
  22. "{\"items\":[{\"name\":\"headlamp_heapsize\",\"value\":268435456}]}"
  23. curl -X PUT -u admin:admin http://localhost:7180/api/v17/cm/service/roleConfigGroups/mgmt-EVENTSERVER-BASE/config -H "Content-type: application/json" -d \
  24. "{\"items\":[{\"name\":\"event_server_heapsize\",\"value\":268435456}]}"
  25. curl -X PUT -u admin:admin http://localhost:7180/api/v17/cm/service/roleConfigGroups/mgmt-ALERTPUBLISHER-BASE/config -H "Content-type: application/json" -d \
  26. "{\"items\":[{\"name\":\"alert_heapsize\",\"value\":268435456}]}"
  27. ## 重启监控服务,使得配置生效
  28. curl -X POST -u admin:admin http://localhost:7180/api/v17/cm/service/commands/restart

II, 修改cdh服务配置 (调用rest接口)

在这里插入图片描述

  1. ## 1,修改zookeeper 客户端最大连接数
  2. curl -X PUT -u admin:admin http://localhost:7180/api/v17/clusters/cluster_1/services/zookeeper/roleConfigGroups/zookeeper-SERVER-BASE/config -H "Content-type: application/json" -d \
  3. "{\"items\":[{\"name\":\"maxClientCnxns\",\"value\":300}]}"
  4. ## 2,修改yarn NodeManager 堆内存大小
  5. curl -X PUT -u admin:admin http://localhost:7180/api/v17/clusters/cluster_1/services/yarn/roleConfigGroups/yarn-NODEMANAGER-BASE/config -H "Content-type: application/json" -d \
  6. "{\"items\":[{\"name\":\"node_manager_java_heapsize\",\"value\":1073741824}]}"
  7. ## 3,修改yarn cpu资源 核数总大小
  8. curl -X PUT -u admin:admin http://localhost:7180/api/v17/clusters/cluster_1/services/yarn/roleConfigGroups/yarn-NODEMANAGER-BASE/config -H "Content-type: application/json" -d \
  9. "{\"items\":[{\"name\":\"yarn_nodemanager_resource_cpu_vcores\",\"value\":8}]}"
  10. curl -X PUT -u admin:admin http://localhost:7180/api/v8/clusters/cluster_1/services/yarn/roleConfigGroups/yarn-RESOURCEMANAGER-BASE/config -H "Content-type: application/json" -d \
  11. "{\"items\":[{\"name\":\"yarn_scheduler_maximum_allocation_vcores\",\"value\":8}]}"
  12. ## 4,修改 hdfs 副本数
  13. curl -X PUT -u admin:admin http://localhost:7180/api/v17/clusters/cluster_1/services/hdfs/config -H "Content-type: application/json" -d \
  14. "{\"items\":[{\"name\":\"dfs_replication\",\"value\":1}]}"
  15. ## 5, 服务自动重启 (hdfs(nn,dn), yarn{nn,rn}, hbase{HM,HR}, solr, hbase-indexer )
  16. curl -X POST -u admin:admin http://localhost:7180/api/v17/batch -H "Content-type: application/json" -d \
  17. '{ "items": [{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/hbase/roleConfigGroups/hbase-HBASETHRIFTSERVER-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" },{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/hbase/roleConfigGroups/hbase-MASTER-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" }, { "method": "PUT", "url": "/api/v8/clusters/cluster/services/hbase/roleConfigGroups/hbase-REGIONSERVER-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" },{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/yarn/roleConfigGroups/yarn-NODEMANAGER-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" },{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/yarn/roleConfigGroups/yarn-RESOURCEMANAGER-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" },{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/ks_indexer/roleConfigGroups/ks_indexer-HBASE_INDEXER-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" },{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/solr/roleConfigGroups/solr-SOLR_SERVER-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" },{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/hdfs/roleConfigGroups/hdfs-NAMENODE-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" },{ "method": "PUT", "url": "/api/v8/clusters/cluster/services/hdfs/roleConfigGroups/hdfs-SECONDARYNAMENODE-BASE/config", "body": {"items": [{"name": "process_auto_restart","value": "true"}]}, "contentType": "application/json" } ] }'

III, 修改cdh服务内存配置 (直接修改mysql数据)

  1. [root@c7-iris2 sh]# cat java_heap.sh
  2. mysql_root_pass=123456
  3. mysql -uroot -p$mysql_root_pass -e 'show databases'
  4. #单位转换
  5. g1_bytes=$((1*1024*1024*1024))
  6. g1_half_bytes=$((g1_bytes/2))
  7. g1_half_half_bytes=$((g1_bytes/2/2))
  8. g2_bytes=$((g1_bytes*2))
  9. g50_mb=$((g1_bytes*50/1024))
  10. #zk
  11. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_half_bytes where attr='zookeeper_server_java_heapsize'"
  12. #hdfs
  13. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='datanode_java_heapsize'"
  14. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='secondary_namenode_java_heapsize'"
  15. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='namenode_java_heapsize'"
  16. #hbase
  17. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='hbase_master_java_heapsize'"
  18. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='hbase_regionserver_java_heapsize'"
  19. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_half_bytes where attr='hbase_thriftserver_java_heapsize'"
  20. #yarn
  21. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='node_manager_java_heapsize'"
  22. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='resource_manager_java_heapsize'"
  23. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_half_bytes where attr='mr2_jobhistory_java_heapsize'"
  24. #
  25. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g50_mb where attr='yarn_nodemanager_resource_memory_mb'"
  26. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g50_mb where attr='yarn_scheduler_maximum_allocation_mb'"
  27. #oozie,hive
  28. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_half_half_bytes where attr='oozie_java_heapsize'"
  29. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_half_bytes where attr='hive_metastore_java_heapsize'"
  30. mysql -uroot -p"$mysql_root_pass" -e "update scm.CONFIGS set value=$g1_bytes where attr='hiveserver2_java_heapsize'"
  31. ## 重启整个集群:使得配置生效
  32. curl -X POST -u admin:admin \
  33. http://localhost:7180/api/v17/clusters/cluster_1/commands/restart \
  34. -H "Content-type: application/json" \
  35. -d "{\"restartOnlyStaleServices\":true,\"redeployClientConfiguration\":true}"

在这里插入图片描述

d, 启动/停止服务

1, 整个集群服务

  1. [root@hadoop04 ~]# curl -X POST -u admin:admin http://localhost:7180/api/v17/clusters/Cluster%201/commands/start
  2. {
  3. "id" : 3096,
  4. "name" : "Start",
  5. "startTime" : "2021-02-05T01:39:57.522Z",
  6. "active" : true,
  7. "clusterRef" : {
  8. "clusterName" : "cluster"
  9. }

2, 集群的单个服务

  1. [root@test-c6 ~]# curl -X POST -u admin:admin \
  2. 'http://localhost:7180/api/v1/clusters/Cluster%201/services/hdfs/commands/start'
  3. {
  4. "id" : 377,
  5. "name" : "HdfsStartWithFailovers",
  6. "startTime" : "2020-09-16T11:54:12.652Z",
  7. "active" : true,
  8. "serviceRef" : {
  9. "clusterName" : "cluster",
  10. "serviceName" : "hdfs"
  11. }
  12. }

3, cm 监控服务

  1. [root@cdh-c6-master ~]# curl -X POST -u admin:admin \
  2. 'http://localhost:7180/api/v17/cm/service/commands/stop'
  3. {
  4. "id" : 441,
  5. "name" : "Stop",
  6. "startTime" : "2020-10-22T08:30:58.925Z",
  7. "endTime" : "2020-10-22T08:30:58.925Z",
  8. "active" : false,
  9. "success" : false,
  10. "resultMessage" : "At least one role must be started.",
  11. "serviceRef" : {
  12. "serviceName" : "mgmt"
  13. }
  14. }[root@cdh-c6-master ~]#

3, 查看命令执行结果

  1. ############## 查看命令执行结果
  2. [root@test-c6 ~]# curl -u admin:admin 'http://localhost:7180/api/v1/commands/377'
  3. {
  4. "id" : 377,
  5. "name" : "HdfsStartWithFailovers",
  6. "startTime" : "2020-09-16T11:54:12.652Z",
  7. "endTime" : "2020-09-16T11:54:35.444Z",
  8. "active" : false,
  9. "success" : true,
  10. "resultMessage" : "Successfully started HDFS service",
  11. "serviceRef" : {
  12. "clusterName" : "cluster",
  13. "serviceName" : "hdfs"
  14. },
  15. "children" : {
  16. "items" : [ {
  17. "id" : 378,
  18. "name" : "Start",
  19. "startTime" : "2020-09-16T11:54:12.656Z",
  20. "endTime" : "2020-09-16T11:54:35.444Z",
  21. "active" : false,
  22. "success" : true,
  23. "resultMessage" : "Successfully started service.",
  24. "serviceRef" : {
  25. "clusterName" : "cluster",
  26. "serviceName" : "hdfs"
  27. }
  28. } ]
  29. }
  30. }

e, 导入集群模板:新建集群

由于模板配置比较复杂,参考ansible-cloudera-manager 安装和配置cdh集群模板的【高级版部分】 https://blog.csdn.net/eyeofeagle/article/details/106121932

1, 集群模板参数

  1. [root@c7-iris2 ~]# cat template.json
  2. {
  3. "cdhVersion": "5.12.2",
  4. "cmVersion": "5.12.2",
  5. "displayName": "cluster_1",
  6. "products": [
  7. {
  8. "product": "CDH",
  9. "version": "5.12.2-1.cdh5.12.2.p0.4"
  10. }
  11. ],
  12. "services": [
  13. {
  14. "refName": "ZOOKEEPER-1",
  15. "roleConfigGroups": [
  16. {
  17. "base": true,
  18. "configs": [],
  19. "refName": "ZOOKEEPER-1-SERVER-BASE",
  20. "roleType": "SERVER"
  21. }
  22. ],
  23. "serviceConfigs": [
  24. {
  25. "name": "zookeeper_datadir_autocreate",
  26. "value": "true"
  27. },
  28. {
  29. "name": "enableSecurity",
  30. "value": "false"
  31. }
  32. ],
  33. "serviceType": "ZOOKEEPER"
  34. }
  35. ],
  36. "hostTemplates": [
  37. {
  38. "cardinality": 1,
  39. "refName": "HostTemplate-zk",
  40. "roleConfigGroupsRefNames": [
  41. "ZOOKEEPER-1-SERVER-BASE"
  42. ]
  43. }
  44. ],
  45. "instantiator": {
  46. "clusterName": "cluster_1",
  47. "hosts": [
  48. {
  49. "hostName": "c7-iris2",
  50. "hostTemplateRefName": "HostTemplate-zk"
  51. }
  52. ],
  53. "variables": [
  54. ]
  55. }
  56. }
  57. [root@c7-iris2 ~]# curl -X POST -H "Content-type: application/json" \
  58. -u admin:admin http://localhost:7180/api/v17/cm/importClusterTemplate \
  59. -d @template.json
  60. {
  61. "id" : 591,
  62. "name" : "ClusterTemplateImport",
  63. "startTime" : "2020-10-23T13:52:33.386Z",
  64. "active" : true,
  65. "children" : {
  66. "items" : [ ]
  67. }
  68. }
  69. [root@c7-iris2 ~]#

2, 命令详情查看

在这里插入图片描述
在这里插入图片描述

2, tsql监控查询

a, 使用指南

http://test-c6:7180/static/help/topics/cm_dg_tsquery.html
在这里插入图片描述

b, 在查询控制台执行sql

http://test-c6:7180/cmf/views/search, 执行sql查询


































查询项 sql参数
物理内存 physical_memory_total, physical_memory_used, physical_memory_memfree, physical_memory_buffers
交换内存 swap_total, swap_used, swap_free
负载 load_1, load_5, load_15
cpu使用率 cpu_percent
网络吞吐速率 total_bytes_receive_rate_across_network_interfaces, total_bytes_transmit_rate_across_network_interfaces
磁盘读写速率 read_bytes_rate ,write_bytes_rate

在这里插入图片描述

c, 使用api查询sql

  1. [root@test-c6 ~]# curl -u admin:admin http://localhost:7180/api/v17/timeseries?query=select%20cpu_percent
  2. {
  3. "items" : [ {
  4. "timeSeries" : [ {
  5. "metadata" : {
  6. "metricName" : "cpu_percent",
  7. "entityName" : "test-c6",
  8. "startTime" : "2020-09-16T14:13:45.172Z",
  9. "endTime" : "2020-09-16T14:18:45.172Z",
  10. "attributes" : {
  11. "hostname" : "test-c6",
  12. "clusterDisplayName" : "Cluster 1",
  13. "entityName" : "68d905d8-4802-486d-9285-41e70576c05b",
  14. "clusterName" : "cluster",
  15. "hostId" : "68d905d8-4802-486d-9285-41e70576c05b",
  16. "active" : "true",
  17. "category" : "HOST",
  18. "version" : "CDH 5.12.0",
  19. "rackId" : "/default"
  20. },
  21. "unitNumerators" : [ "percent" ],
  22. "unitDenominators" : [ ],
  23. "expression" : "SELECT cpu_percent WHERE entityName = \"68d905d8-4802-486d-9285-41e70576c05b\" AND category = HOST",
  24. "metricCollectionFrequencyMs" : 60000,
  25. "rollupUsed" : "RAW"
  26. },
  27. "data" : [ {
  28. "timestamp" : "2020-09-16T14:14:00.000Z",
  29. "value" : 5.5,
  30. "type" : "SAMPLE"
  31. }, {
  32. "timestamp" : "2020-09-16T14:15:00.000Z",
  33. "value" : 3.5,
  34. "type" : "SAMPLE"
  35. }, {
  36. "timestamp" : "2020-09-16T14:16:00.000Z",
  37. "value" : 3.5,
  38. "type" : "SAMPLE"
  39. }, {
  40. "timestamp" : "2020-09-16T14:17:00.000Z",
  41. "value" : 3.3,
  42. "type" : "SAMPLE"
  43. }, {
  44. "timestamp" : "2020-09-16T14:18:00.000Z",
  45. "value" : 3.6,
  46. "type" : "SAMPLE"
  47. } ]
  48. }, {
  49. "metadata" : {
  50. "metricName" : "cpu_percent",
  51. "entityName" : "test-c62",
  52. "startTime" : "2020-09-16T14:13:45.172Z",
  53. "endTime" : "2020-09-16T14:18:45.172Z",
  54. "attributes" : {
  55. "hostname" : "test-c62",
  56. "clusterDisplayName" : "Cluster 1",
  57. "entityName" : "0244fbb4-0d02-40b5-9cac-17d9e388eb97",
  58. "clusterName" : "cluster",
  59. "hostId" : "0244fbb4-0d02-40b5-9cac-17d9e388eb97",
  60. "active" : "true",
  61. "category" : "HOST",
  62. "version" : "CDH 5.12.0",
  63. "rackId" : "/default"
  64. },
  65. "unitNumerators" : [ "percent" ],
  66. "unitDenominators" : [ ],
  67. "expression" : "SELECT cpu_percent WHERE entityName = \"0244fbb4-0d02-40b5-9cac-17d9e388eb97\" AND category = HOST",
  68. "metricCollectionFrequencyMs" : 60000,
  69. "rollupUsed" : "RAW"
  70. },
  71. "data" : [ ]
  72. } ],
  73. "warnings" : [ ],
  74. "timeSeriesQuery" : "select cpu_percent"
  75. } ]
  76. }

3, 使用java调用cm api

发送get/post请求: springboot+RestTemplate

1, pom.xml配置

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.74</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-tomcat</artifactId>
  13. <scope>provided</scope>
  14. </dependency>

2, 具体代码

在这里插入图片描述

  • http请求工具类

    import com.alibaba.fastjson.JSON;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.client.RestTemplate;

    import javax.xml.bind.DatatypeConverter;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;

    public class CmRestUtil {

    1. private final static Logger LOGGER = LoggerFactory.getLogger(CmRestUtil.class);
    2. static RestTemplate customRestTemplate;
    3. static HttpHeaders headers;
    4. static {
    5. customRestTemplate = new RestTemplate();
    6. headers = new HttpHeaders();
    7. headers.setContentType(MediaType.APPLICATION_JSON);
    8. headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    9. try {
    10. //username:password--->访问的用户名,密码,并使用base64进行加密,将加密的字节信息转化为string类型
    11. String format = String.format("%s:%s", "admin", "admin");
    12. String encoding = DatatypeConverter.printBase64Binary(format.getBytes() );
    13. headers.set("Authorization", "Basic " + encoding);
    14. }catch (Exception e){
    15. throw e;
    16. }
    17. }
    18. public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
    19. //1,查看集群信息
    20. getClusterInfo();
    21. //2,查看主机信息
    22. getHostInfo();
    23. //3,查看服务/角色信息
    24. getServiceInfo(null);
    25. //4,启动服务/角色
    26. startService("hbase");
    27. //5,tsql监控查询
    28. timeseriesSqlQuery("select * where roleType=DATANODE");
    29. }
    30. public static ResponseEntity<Object> timeseriesSqlQuery(String sql) throws UnsupportedEncodingException {
    31. Map<String, Object> params = new HashMap<>();
    32. //String sql="select * where roleType=DATANODE";
    33. params.put("query",sql);
    34. org.springframework.http.HttpEntity<Object> requestEntity = new org.springframework.http.HttpEntity<>(params, headers);
    35. ResponseEntity<Object> response = customRestTemplate.exchange(
    36. URLDecoder.decode("http://192.168.56.161:7180/api/v17/timeseries", "UTF-8"),
    37. HttpMethod.POST,
    38. requestEntity,
    39. Object.class);
    40. LOGGER.info("===========code:{}, response {} ", response.getStatusCodeValue(), JSON.toJSONString(response.getBody()));
    41. return response;
    42. }
    43. public static ResponseEntity<Object> startService(String serviceName) throws UnsupportedEncodingException, InterruptedException {
    44. Map<String, Object> params = new HashMap<>();
    45. //params.put("query",query);
    46. org.springframework.http.HttpEntity<Object> requestEntity = new org.springframework.http.HttpEntity<>(params, headers);
    47. ResponseEntity<Object> response = customRestTemplate.exchange(
    48. URLDecoder.decode("http://192.168.56.161:7180/api/v1/clusters/Cluster 1/services/"+serviceName+"/commands/start", "UTF-8"),
    49. HttpMethod.POST,
    50. requestEntity,
    51. Object.class);
    52. LOGGER.info("===========code:{}, response {} ", response.getStatusCodeValue(), JSON.toJSONString(response.getBody()));
    53. //查看命令执行结果

    // Thread.sleep(2000);
    // response = customRestTemplate.exchange(
    // URLDecoder.decode(“http://192.168.56.161:7180/api/v1/commands/593“, “UTF-8”),
    // HttpMethod.GET,
    // requestEntity,
    // Object.class);
    // LOGGER.info(“command =========== code:{}, response {} “, response.getStatusCodeValue(), JSON.toJSONString(response.getBody()));

    1. return response;
    2. }
  1. public static ResponseEntity<Object> getServiceInfo(String serviceName) throws UnsupportedEncodingException {
  2. Map<String, Object> params = new HashMap<>();
  3. //params.put("query",query);
  4. org.springframework.http.HttpEntity<Object> requestEntity = new org.springframework.http.HttpEntity<>(params, headers);
  5. /查询所有服务, 查询单个服务
  6. String url="http://192.168.56.161:7180/api/v17/clusters/Cluster 1/services";
  7. // http://192.168.56.161:7180/api/v17/clusters/Cluster 1/services/hbase
  8. if (serviceName !=null && ! serviceName.equals("") ){
  9. url+=("/"+serviceName);
  10. }
  11. ResponseEntity<Object> response = customRestTemplate.exchange(
  12. URLDecoder.decode(url, "UTF-8"),
  13. HttpMethod.GET,
  14. requestEntity,
  15. Object.class);
  16. LOGGER.info("===========code:{}, response {} ", response.getStatusCodeValue(), JSON.toJSONString(response.getBody()));
  17. return response;
  18. }
  19. public static ResponseEntity<Object> getClusterInfo() throws UnsupportedEncodingException {
  20. Map<String, Object> params = new HashMap<>();
  21. //params.put("query",query);
  22. org.springframework.http.HttpEntity<Object> requestEntity = new org.springframework.http.HttpEntity<>(params, headers);
  23. ResponseEntity<Object> response = customRestTemplate.exchange(
  24. URLDecoder.decode("http://192.168.56.161:7180/api/v1/clusters", "UTF-8"),
  25. HttpMethod.GET,
  26. requestEntity,
  27. Object.class);
  28. LOGGER.info("===========code:{}, response {} ", response.getStatusCodeValue(), JSON.toJSONString(response.getBody()));
  29. return response;
  30. }
  31. public static ResponseEntity<Object> getHostInfo() throws UnsupportedEncodingException {
  32. Map<String, Object> params = new HashMap<>();
  33. //params.put("query",query);
  34. org.springframework.http.HttpEntity<Object> requestEntity = new org.springframework.http.HttpEntity<>(params, headers);
  35. ResponseEntity<Object> response = customRestTemplate.exchange(
  36. URLDecoder.decode("http://192.168.56.161:7180/api/v17/hosts", "UTF-8"),
  37. HttpMethod.GET,
  38. requestEntity,
  39. Object.class);
  40. LOGGER.info("===========code:{}, response {} ", response.getStatusCodeValue(), JSON.toJSONString(response.getBody()));
  41. return response;
  42. }
  43. }
  • ApiContorller

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import web.CmRestUtil;

    import java.io.UnsupportedEncodingException;

    @Controller
    @RequestMapping(value = “/api”)
    public class CmApiController {

    1. //1,查看集群信息
    2. @RequestMapping(value = "/clusterInfo", method = { RequestMethod.GET})
    3. @ResponseBody
    4. public Object getClusterInfo() throws UnsupportedEncodingException {
    5. return CmRestUtil.getClusterInfo();
    6. }
    7. //2,查看主机信息
    8. @RequestMapping(value = "/hostInfo", method = { RequestMethod.GET})
    9. @ResponseBody
    10. public Object getHostInfo() throws UnsupportedEncodingException {
    11. return CmRestUtil.getHostInfo();
    12. }
    13. //3,查看服务/角色信息
    14. @RequestMapping(value = "/serviceInfo", method = { RequestMethod.GET})
    15. @ResponseBody
    16. public Object getServiceInfo(@RequestParam(name = "serviceName",required = false) String serviceName) throws UnsupportedEncodingException {
    17. return CmRestUtil.getServiceInfo(serviceName);
    18. }
    19. //4,启动服务/角色
    20. @RequestMapping(value = "/start", method = { RequestMethod.GET})
    21. @ResponseBody
    22. public Object getClusterInfo( @RequestParam(name = "serviceName") String serviceName) throws UnsupportedEncodingException, InterruptedException {
    23. return CmRestUtil.startService(serviceName);
    24. }
    25. //5,tsql监控查询
    26. @RequestMapping(value = "/tsql", method = { RequestMethod.GET})
    27. @ResponseBody
    28. public Object timeseriesSqlQuery( @RequestParam(name = "query") String query) throws UnsupportedEncodingException, InterruptedException {
    29. return CmRestUtil.timeseriesSqlQuery(query);
    30. }

    }

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读

    相关 安装Cloudera Manager

    cloudera manager有四大功能: (1)管理:对集群进行管理,如添加、删除节点等操作。 (2)监控:监控集群的健康情况,对设置的各种指标和系统运行

    相关 搭建Cloudera Manager环境

    本文主要介绍如何搭建Cloudera Manager环境来管理大数据集群。这里注重的是搭建的过程,所以小编没用公司的服务器,而是在自己电脑上建了三台虚拟机(使用的是CentOS