第十四章·Kibana深刻-Timelion画图实现系统监控

什么是Timelion?

Timelion使你能够轻松得到如下问题的答案:linux

1)随着时间的推移,每一个惟一的用户会查看多少个页面?
2)这个星期五和上周五之间的交通量有什么不一样?
3)今天有多少日本人口来到个人网站?
4)标准普尔500指数的10日均线是多少?
5)过去两年收到的全部搜索请求的累计总和是多少?redis

  Timelion是Kibana时间序列的可视化工具。时间序列可视化是可视化的,以时间顺序分析数据。Timelion可用于绘制二维图形,时间绘制在x轴上。express

  与使用简单的条形图或线条可视化相比有什么优点?Timelion采起不一样的方法。使用Timelion特定语法,您经过将功能连接在一块儿来定义图形,而不是使用可视化编辑器建立图表。该语法启用了经典点系列图不提供的一些功能,如将不一样索引或数据源的数据绘制到一个图形中。vim

可是在使用Timelion以前,咱们须要下载并安装Metricbeat浏览器

Metricbeat介绍及部署

Metricbeat介绍

Metricbeat能够按期收集操做系统和服务器的运行指标(CPU,内存,硬盘,IO,读写速度,进程等等),Metricbeat能够将收集到的指标和数据发送到你指定的输出,好比:elasticsearch,logstash,redis等等,最终达成监视服务器的目标。ruby


Metricbeat部署及配置

由于咱们使用的ES和Kibana是5版本的,因此咱们须要下载5版本的Metricbeat服务器

#RPM包下载 [root@elkstack04 ~]# wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-5.3.3-x86_64.rpm #源码包下载 [root@elkstack04 ~]# wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-5.3.3-linux-x86_64.tar.gz #安装Metricbeat [root@elkstack04 ~]# yum localinstall -y metricbeat-5.3.3-x86_64.rpm #修改配置文件 [root@elkstack04 ~]# vim /etc/metricbeat/metricbeat.yml #========================== Modules configuration ============================ metricbeat.modules: #------------------------------- System Module ------------------------------- - module: system metricsets: # CPU stats - cpu # System Load stats - load # Per CPU core stats #- core # IO stats #- diskio # Per filesystem stats - filesystem # File system summary stats - fsstat # Memory stats - memory # Network stats - network # Per process stats - process # Sockets (linux only) #- socket enabled: true period: 1m processes: ['.*'] #================================ Outputs ===================================== # Configure what outputs to use when sending the data collected by the beat. # Multiple outputs may be used. #-------------------------- Elasticsearch output ------------------------------ output.elasticsearch: # Array of hosts to connect to. hosts: ["10.0.0.51:9200"] # Optional protocol and basic auth credentials. #protocol: "https" #username: "elastic" #password: "changeme" #要加载仪表板,能够在metricbeat设置中启用仪表板加载。当仪表板加载被启用时,Metricbeat使用Kibana API来加载样本仪表板。只有当Metricbeat启动时,才会尝试仪表板加载。 # 设置kibana服务地址 setup.kibana.host: "10.0.0.54:5601" # 加载默认的仪表盘样式 setup.dashboards.enabled: true # 设置若是存在模板,则不覆盖原有模板 setup.template.overwrite: false #启动Metricbeat(CentOS6) [root@elkstack04 ~]# /etc/init.d/metricbeat start #启动Metricbeat(CentOS7) [root@elkstack04 ~]# systemctl start metricbeat #检查metricbeat是否正常运行(返回索引对应内容) [root@elkstack04 ~]# curl -XGET 'http://10.0.0.51:9200/metricbeat-*/_search?pretty' 

结果以下:
网络

打开浏览器,访问:http://10.0.0.51:9100/curl

查看Metricbeat索引socket

打开浏览器,访问:http://10.0.0.54:5601/

添加metricbeat-*索引

建立后,便可在Discover中看到Metricbeat信息

Timelion使用Metricbeat

建立时间序列可视化

使用Metricbeat的时间序列数据带你浏览Timelion提供的一些函数。

建立第一个可视化将比较在用户空间中花费的CPU时间与一小时的结果偏移量的实时百分比,为了建立这个可视化,咱们须要建立两个Timelion表达式,一个是system.cpu.user.pct的实时平均数,另外一个是1小时的平均偏移量。

首先,须要在第一个表达式中定义indextimefieldmetric,并在Timelion查询栏中输入如下表达式。

.es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct') 

如今须要添加另外一个具备前一小时数据的系列,以便进行比较,为此,你必须向.es()函数添加一个offset参数,offset将用日期表达式偏移序列检索。对于本例,你但愿将数据偏移一小时,并使用日期表达式-1h,使用逗号分隔这两个系列,在Timelion查询栏中输入如下表达式:

.es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct'), .es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct') 

很难区分这两个系列,自定义标签以便于区分它们,你老是能够将.label()函数附加到任何表达式以添加自定义标签,在Timelion查询栏中输入如下表达式来定制标签:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour'), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour') 

保存完整的Timelion工做表做为Metricbeat示例,做为一种最佳实践,你应该在完成本教程的过程当中保存对本工做表所作的任何重要更改。


定制和格式化可视化

Timelion有不少定制选项,你几乎可使用可用的函数对图表的每一个方面进行个性化设置,执行如下修改。

1)添加一个标题
2)更改系列类型
3)改变一个系列的颜色和不透明度
4)修改图例

以前用两个系列建立了一个时间轴图表,让咱们继续定制这个可视化。

在进行任何其余修改以前,将title()函数附加到表达式的末尾,以添加具备有意义名称的标题,这将使不熟悉的用户更容易理解可视化目的。对于这个示例,将title('CPU usage')添加到原始系列中,在Timelion 查询栏中使用如下表达式:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour'), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage') 

为了进一步区分过去一小时系列,你将把图表类型更改成区域图表,为了作到这一点,你须要使用.lines()函数来定制折线图,你将设置fill和width参数,分别设置折线图的填充和折线宽度。在本例中,你将经过添加.lines(fill=1,width=0.5)将填充级别设置为1,边框宽度设置为0.5,在Timelion查询栏中使用如下表达式:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour').lines(fill=1,width=0.5), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage') 

让咱们给这些系列涂上颜色,使当前的小时系列比过去一个小时系列流行一点,color()函数可用于更改任何系列的颜色,并接受标准颜色名称、十六进制值或分组系列的颜色模式。对于这个示例,你将在过去一个小时使用.color(gray),而在当前小时使用.color(#1E90FF),在Timelion查询栏中输入如下表达式进行调整:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour').lines(fill=1,width=0.5).color(gray), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage').color(#1E90FF) 

最后但并不是最不重要,调整图例,使其占用尽量小的空间,你可使用.legend()函数来设置图例的位置和样式。在本例中,经过将.legend(columns=2, position=nw)两列追加到原始系列,将图例放置在可视化的西北位置,使用如下表达式进行调整:

.es(offset=-1h,index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('last hour').lines(fill=1,width=0.5).color(gray), .es(index=metricbeat-*, timefield='@timestamp', metric='avg:system.cpu.user.pct').label('current hour').title('CPU usage').color(#1E90FF).legend(columns=2, position=nw) 

保存下来,再建立一个新的。


使用数学函数

在前两部分中,已经学习了如何建立和样式化Timelion可视化,本节将探索Timelion提供的数学函数。你将继续使用Metricbeat数据为入站和出站网络流量建立新的Timelion可视化,首先,须要在工做表中添加一个新的Timelion可视化。

在顶部菜单中,单击Add添加第二个可视化,当添加到工做表中时,你会注意到查询栏已经被替换为默认的.es(*)表达式,这是由于查询与你选择的Timelion工做表上的可视化相关联。

开始跟踪入站/出站网络流量,你的第一个表达式将计算system.network.in.bytes的最大值,将下面的表达式输入到你的Timelion查询栏:

.es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.in.bytes) 

在绘制变化率时,监视网络流量更有价值,derivative()函数就是这样作的 - 绘制值随时间的变化,经过在表达式末尾添加.derivative()能够很容易地作到这一点,使用如下表达式来更新你的可视化:

 

如今是出站流量,你须要为system.network.out.bytes添加相似的计算,因为出站流量将离开你的机器,所以将此指标表示为负数是有意义的,.multiply()函数将系列乘以一个数字,这个数字是系列或系列列表的结果。对于本例,你将使用.multiply(-1)将出站网络流量转换为负值,使用如下表达式来更新你的可视化:

.es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.in.bytes).derivative(), .es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.out.bytes).derivative().multiply(-1) 

为了使这个可视化更容易使用,将这个系列从字节转换为兆字节,Timelion有一个.divide()函数可使用,.divide()接受与.multiply()相同的输入,并将这个系列除以所定义的除数,使用如下表达式来更新你的可视化:

使用上一节中学习的格式化函数.title().label().color().lines().legend(),让咱们稍微整理一下这个可视化,使用如下表达式来更新你的可视化:

.es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.in.bytes).derivative().divide(1048576).lines(fill=2, width=1).color(green).label("Inbound traffic").title("Network traffic (MB/s)"), .es(index=metricbeat*, timefield=@timestamp, metric=max:system.network.out.bytes).derivative().multiply(-1).divide(1048576).lines(fill=2, width=1).color(blue).label("Outbound traffic").legend(columns=2, position=nw) 

保存,开启新的,画图


使用条件逻辑和跟踪趋势

在本节中,你将学习如何使用条件逻辑修改时间序列数据,并使用移动平均值建立趋势,这有助于随着时间的推移很容易地发现异常值和模式。

对于本教程,你将继续使用Metricbeat数据添加另外一个监控内存消耗的可视化,首先,使用如下表达式绘制system.memory.actual.used.bytes的最大值。

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes') 

让咱们建立两个阈值来监视使用的内存数量,在本教程中,警告阈值为234MB,严重阈值为235MB,当使用内存的最大数量超过这些阈值中的任何一个时,将相应地对该系列进行着色。

 

若是你的计算机的阈值太高或太低,请相应地进行调整。

要配置这两个阈值,可使用Timelion的条件逻辑,在本教程中,你将使用if()将每一个点与一个数字进行比较,若是条件的值为true,则调整样式,若是条件的值为false,则使用默认样式,Timelion提供了如下六个操做符值进行比较。

操做符 含义
eq 相等
ne 不相等
lt 小于
gt 大于
lte 小于等于
gte 大于等于

因为有两个阈值,所以对它们进行不一样的样式是有意义的,使用gt操做符将警告阈值用.color('#FFCC11')涂成黄色,将严重阈值用.color('red')涂成红色,在Timelion查询栏中输入如下表达式,以应用条件逻辑和阈值样式:

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,234000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('warning').color('#FFCC11'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,235000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('serious').color('red') 

如今你已经定义了阈值来轻松地识别异常值,让咱们建立一个新的系列来肯定真正的趋势是什么,Timelion的mvavg()函数容许计算给定窗口上的移动平均值,这对嘈杂的时间序列特别有用,对于本教程,你将使用.mvavg(10)来建立具备10个数据点窗口的移动平均线,使用如下表达式建立最大内存使用量的移动平均值:

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,234000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('warning').color('#FFCC11'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,235000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('serious').color('red'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').mvavg(10) 

如今你已经有了阈值和移动平均值,让咱们格式化可视化,以便更容易使用,和最后一部分同样,使用.color().line().title().legend()函数相应地更新可视化:

.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').label('max memory').title('Memory consumption over time'), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,234000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('warning').color('#FFCC11').lines(width=5), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').if(gt,235000000,.es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes'),null).label('serious').color('red').lines(width=5), .es(index=metricbeat-*, timefield='@timestamp', metric='max:system.memory.actual.used.bytes').mvavg(10).label('mvavg').lines(width=2).color(#5E5E5E).legend(columns=4, position=nw) 

保存


展现至Dashboard

整合,其实最终咱们须要的是,网络,cpu,内存优化后的图

你已经正式利用了Timelion的功能来建立时间序列可视化,本教程的最后一步是向仪表盘添加你新的可视化,下面,本节将向你展现如何从Timelion工做表中保存可视化,并将其添加到现有的仪表盘中。

要将Timelion可视化保存为仪表盘面板,请执行如下步骤。

1)选择要添加到一个(或多个)仪表盘上的可视化视图。
2)点击顶部菜单中的Save选项。
3)选择Save current expression as Kibana dashboard panel
4)命名你的面板并点击Save以做为仪表盘可视化。

如今你能够将这个仪表盘面板添加到任何你想要的仪表盘上,这个可视化如今将在可视化列表中列出,继续并按照你建立的其余可视化效果的相同过程进行操做。

建立一个新的仪表盘或打开一个现有的仪表盘,以添加Timelion可视化,就像其余任何可视化同样。

最终展现

相关文章
相关标签/搜索