Ambari Metrics系统(AMS)是用来收集、聚合Hadoop集群中监控信息的。
1. 可移植、平台无关;
2. 能够和Ganglia系统兼容;
3. 保持SNMP架构;
4. 无需要Root权限;
5. HA高可用。
下面从Ambari Metrics API、Ambari Collector API、Stack里定义Metrics图表、Sink实现举例、自定义配置Metrics及Ambari Metrics显示过程这几个方面对Metrics进行详细描述。
Ambari Metrics API是Ambari提供的Metrics 接口,用于获取集群级别(Cluster)、主机节点级别(Host)、服务组件级别(Component)、主机服务节点级别(Host Component)获取Metrics信息。从广义上将,Ambari Metrics信息查询支持时间段(time range)、时间点(point in time)查询。
http://:8080/api/v1/clusters/cluster-name?fields=metrics/metrics_name[time_start,time_end,step]&_=time_now
其中:
1. cluster-name为查询的集群名字;
2. metrics_name为要查询的metrics名字,如3. load,cpu,memory
4. time_start,time_end:查询的时间段起点(精确到秒)、终点,Linux事件戳
5. step:默认的时间点间隔
6. time_now:当前时间,精确到毫秒
例如:
Get http://node1:8080/api/v1/clusters/TestCluster?fields=metrics/load[1430844925,1430848525,15]&_=1430848532904
Get http://ambari-server:8080/api/v1/clusters/cluster-name/hosts/host-name?fields=metrics/cpu/cpu_user[1430844610,1430848210,15],metrics/cpu/cpu_wio[1430844610,1430848210,15],metrics/cpu/cpu_nice[1430844610,1430848210,15],metrics/cpu/cpu_aidle[1430844610,1430848210,15],metrics/cpu/cpu_system[1430844610,1430848210,15],metrics/cpu/cpu_idle[1430844610,1430848210,15]&_=1430848217591
Get http://ambari-server:8080/api/v1/clusters/cluster-name/services/HDFS/components/DATANODE?fields=metrics/dfs/datanode/DfsUsed,metrics/dfs/datanode/Capacity&_=1430849798630
Get http://ambari-server:8080/api/v1/clusters/cluster-name/hosts/host-name/host_components/NAMENODE?fields=metrics/jvm/memHeapCommittedM[1430847303,1430850903,15],metrics/jvm/memHeapUsedM[1430847303,1430850903,15]&_=1430850903846
Ambari Collector API分为两类:Metrics操作API 和Metrics 元数据API。
提交Metrics数据:
POST http://ambari-metrics-collector:6188/ws/v1/timeline/metrics
{"metrics": [{"metricname": "{MetricsName}","appid": "{Unique_appid}","hostname": "{hostname}","timestamp": {cur_timestamp},"starttime": {time_stamp},"metrics": {"{time_stamp}": {value},"{next_time}": {time_stamp}}}]
}
其中,starttime,timestamp为时间戳,精确到毫秒,一般格式为:
{"metrics": [{"metricname": "AMBARI_METRICS.SmokeTest.FakeMetric","appid": "amssmoketestfake","hostname": "ambari20-5.c.pramod-thangali.internal","timestamp": 1432075898000,"starttime": 1432075898000,"metrics": {"1432075898000": 0.963781711428,"1432075899000": 1432075898000}}]
}
查询Metrics数据:
GET http://ambari-metrics-collector:6188/ws/v1/timeline/metrics?metricNames=MetricsName&appId=AppId&hostname=HostName&precision=seconds&startTime=StartTime&endTime=EndTime
响应数据格式:
Http response: 200 OK
Http data:
{"metrics": [{"timestamp": 1432075898089,"metricname": "AMBARI_METRICS.SmokeTest.FakeMetric","appid": "amssmoketestfake","hostname": "ambari20-5.c.pramod-thangali.internal","starttime": 1432075898000,"metrics": {"1432075898000": 0.963781711428,"1432075899000": 1432075898000}}]
}
其中precision可为:Seconds(Host Metrics 10s一个点,其他30s),Minutes(5分钟),Hours(1小时),Days(1天)。另外,在获取数据时,可选择Metrics Aggregate函数(如果没有声明,则默认使用AVG),例如:
http://
另外,在Get数据时,MetrimcsName和Hostname支持使用%符进行匹配,获取一系列的Metrics,例如:
http://
获取App id列表及App id下的Metrics列表
Get http://
获取所有主机注册的Hosts列表
Get http://
以为Hive添加metrics数据为例。使用的HDP stack版本为2.5。步骤如下:
1)修改metainfo.xml,添加timelineappid值,如在stack 2.5 metainfo中添加定义(覆盖common-services中的默认设置):
2)编写metrics.json文件
3)编写widgets.json文件
4)编写程序,发送数据到Ambari Metrics Collector中,见下一节。
从Ambari中获取Metrics图表数据,进行调试
获取Service Metrimcs 定义&#xff1a;http://<ambari-server>:<port>/api/v1/stacks/<stackName>/versions/<stackVersion>/services/<serviceName>/artifacts/metrics_descriptor
例如&#xff1a;http://localhost:8080/api/v1/stacks/HDP/versions/2.3/services/HDFS/artifacts/metrics_descriptor获取Cluster/Host Metrics 定义&#xff1a;http://<ambari-server>:<port>/api/v1/stacks/<stackName>/versions/<stackVersion>/artifacts/metrics_descriptor
例如&#xff1a;http://localhost:8080/api/v1/stacks/HDP/versions/2.3/artifacts/metrics_descriptor
一个简单的Metrics sink,在host上发送metric数据到metrics collector中。
#!/bin/sh
# sh sink.sh node1 test1 hikvisurl&#61;http://$1:6188/ws/v1/timeline/metrics
while [ 1 ]
do
millon_time&#61;$(( $(date &#43;%s%N) / 1000000 ))
random&#61;&#96;expr $RANDOM % 10&#96;
json&#61;"{
\"metrics\": [
{
\"metricname\": \"$2\",
\"appid\": \"$3\",
\"hostname\": \"localhost\",
\"timestamp\": ${millon_time},
\"starttime\": ${millon_time},
\"metrics\": {
\"${millon_time}\": ${random}
}
}
]
}"
echo $json |tee -a /root/my_metric.log
curl -i -X POST -H "Content-Type: application/json" -d "${json}" ${url}
sleep 5
done
Ambari Web 通过Ambari Server相关的接口&#xff0c;获取Metrics信息并负责显示。其中&#xff0c;具体的显示、管理是通过Widget进行操作的。简单理解&#xff0c;Widget是对Metrics的合并、分析后进行显示&#xff0c;如可在一个Widget图表中显示多个Metrics信息&#xff0c;或者根据Metrics的值决定显示的结果
&#xff08;这里Ambari底层使用的是MySQL数据库&#xff09;
相关的数据表包括&#xff1a;widget,widget_layout,widget_layout_user_widget。其中&#xff0c;各个数据表存储的数据接口如下所示&#xff1a;
1. widget表 :存储所有的widget定义员数据&#xff0c;包括id,widget_name, 对应的Metrics,widget_values,properties等数据&#xff1b;
2. widget_layout表&#xff1a;记录显示面板元数据。每个服务包括两个大的显示面板&#xff0c;serviceName_summary和serviceName_heatmap。另外&#xff0c;每个用户的又有自己的widget_layout,即每个用户可以自定义自己感兴趣的监控图表。如系统中存储用户admin&#xff0c;test用户&#xff0c;则可能有hdfs_summary和serviceName_heatmap两个section,其中对于hdfs_summary section&#xff0c;存在admin_hdfs_dashboard、test_hdfs_dashboard、admin_hdfs_heatmap和test_hdfs_heatmap &#xff08;另外&#xff0c;在数据库中&#xff0c;当test用户访问了hdfs heapmap监控区&#xff0c;数据表中才会创建出test_hdfs_heatmap layout这条记录&#xff09;。在widget_layout数据表中主要存储用户图表监控区的元数据&#xff0c;如id ,layout_name, section_name等。
查询所有的widget:
get http://ambari_server:port/api/v1/clusters/ClusterName/widgets/或&#xff1a;
get http://ambari_server:port/api/v1/clusters/ClusterName/widgets&#xff1f;widgetinfo/scope&#61;USER&widgetinfo/author&#61;admin&fields&#61;*
根据widge ID 查询某个widget 信息:
get http://ambari_server:port/api/v1/clusters/ClusterName/widgets/widget_id
对某个section中的 widget进行变更&#xff0c;保留列表中的Metrics(可用此API 在section中添加、删除metrics:
put http://ambari_server:port/api/v1/clusters/ClusterName/widget_layouts/section_id -d
&#39;{
"WidgetLayoutInfo":{"dispaly_name": "Standard Storm Dashboard","layout_name":"layout_name","scope":"USER","section_name":"STORM_SUMMARY","widgets":[{"id":133},{"id":134}] }
}&#39;
获取某个section中活跃的widget列表
get http://ambari_server:port/api/v1/users/admin/activeWidgetLayouts?WidgetLayoutInfo/section_name&#61;section_name
目前Ambari 中hdfs,storm,kafaka,yarn定义了Widget,section_name可为hdfs_summary,yarn_summary,storm_summary,yarn_summary,kafaka_summary,yarn_summary,hdfs_heatmap,yarn_heatmap,storm,yarn_heatmap,kafaka_heatmap,yarn_heatmap
默认白名单位于/etc/ambari-metrics-collector/conf.目录下&#xff0c;可通过 ams-site 中的配置项 timeline.metrics.whitelist.file &#61;path_to_whitelist_file进行配置
默认情况下&#xff0c;Metrics数据存储在HBase中&#xff0c;使用的是Metrics内置的HBase&#xff0c;且该HBase是单节点模式。
进入Metrics HBase
在Metrics Collector节点&#xff0c;进入目录/usr/lib/ams-hbase/bin目录下&#xff0c;运行&#xff1a;
./hbase –config /etc/ams-hbase/conf shell即可进入hbase shell。
通过Phoenix连接HBase
在Metrics Collector节点&#xff0c;进入目录/usr/lib/ambari-metrics-collector/bin目录下&#xff0c;运行&#xff1a;
./sqlline.py localhost:61181 即可进入Phoenix。
Metrics : https://blog.csdn.net/youyou1543724847/article/details/82725184
Metrics初衷
https://issues.apache.org/jira/browse/AMBARI-5707
Metrics Collector API:
https://cwiki.apache.org/confluence/display/AMBARI/Metrics&#43;Collector&#43;API&#43;Specification
Metrics Wiki: https://cwiki.apache.org/confluence/display/AMBARI/Metrics