Cloud Monitoring --- grafana create a comprehensive analysis dashboard-- use mysql data source

Introduction of the dashboard grafana

Grafana is an open source application written in languages ​​go, mainly for large-scale visual display indicator data, applications and network architecture analysis is the most popular time-series data presentation tool, now supports most commonly used sequence database.

Official website
operating documentation
grafana installation

Infrastructure often used as time-series data analysis and visualization applications.

Grafana main characteristics:
rich and flexible graphics options;
you can mix a variety of styles;
supports multiple data sources;
has a wealth of plug-in extensions;
support user rights management.

Grafana has a very nice graphics and layout display, full-featured dashboard dashboard metrics and graphical editors.

This chapter to learn how to customize our visual monitoring dashboard dashboard.

Here Insert Picture Description

As described above, in a basic Dashboard a visualization unit is a Panel (panel), Panel as through trend graph shows the form of visualization thermodynamic data of FIG. And in Dashboard Each Panel is a completely separate part, data sources, and data query by the Panel of Query Editor (Query Editor) we can query for each Panel myself, for example, if the Prometheus as a data source, that in query Editor, we actually use PromQL, and the Panel will be responsible for querying the corresponding data from a particular Prometheus, and its visualization. Panel Since each is completely independent, and therefore in a Dashboard, often may contain data from a plurality of Data Source.

Panel Grafana provided to achieve a variety of plug-through form, such as the commonly used: Graph Panel, Heatmap Panel, SingleStat Panel Panel the Table and the like. The user may also install more types of plug Panel panel.

除了Panel以外,在Dashboard页面中,我们还可以定义一个Row(行),来组织和管理一组相关的Panel。

除了Panel, Row这些对象以外,Grafana还允许用户为Dashboard定义Templating variables(模板参数),从而实现可以与用户动态交互的Dashboard页面。同时Grafana通过JSON数据结构管理了整个Dasboard的定义,因此这些Dashboard也是非常方便进行共享的。Grafana还专门为Dashboard提供了一个共享服务:
https://grafana.com/dashboards
通过该服务用户可以轻松实现Dashboard的共享,同时我们也能快速的从中找到我们希望的Dashboard实现,并导入到自己的Grafana中。

panel

Panel是Grafana中最基本的可视化单元。每一种类型的面板都提供了相应的查询编辑器(Query Editor),让用户可以从不同的数据源(如Prometheus)中查询出相应的监控数据,并且以可视化的方式展现。

Grafana中所有的面板均以插件的形式进行使用,当前内置了5种类型的面板,分别是:Graph,Singlestat,Heatmap, Dashlist,Table以及Text。

其中像Graph这样的面板允许用户可视化任意多个监控指标以及多条时间序列。而Siglestat则必须要求查询结果为单个样本。Dashlist和Text相对比较特殊,它们与特定的数据源无关。

通过Grafana UI用户可以在一个Dashboard下添加Panel,点击Dashboard右上角的“Add Panel”按钮。

参考
grafana基本概念

新增数据源

Grafana支持许多不同的数据源。每个数据源都有一个特定的查询编辑器,该编辑器定制的特性和功能是公开的特定数据来源。

官方支持以下数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB。

每个数据源的查询语言和能力都是不同的。
你可以把来自多个数据源的数据组合到一个仪表板dashboard。

登录grafana 界面

默认用户和密码均为admin

在左侧栏中选中Configuration --> Data sources

Here Insert Picture Description
进入页面后 点击 add data source

Here Insert Picture Description

选择 符合自己的 数据库类型

Here Insert Picture Description

填入相关信息即可

Here Insert Picture Description

新增dashboard

两个入口,可以点击左侧栏的加号新增 dashboard

Here Insert Picture Description
也可以先点击左侧栏dashboard 进入 manage页面

点击 new dashboard 按钮

Here Insert Picture Description

Here Insert Picture Description

可以看到有两个按钮,一个先新建查询条件,一个是先选择展示的视图。 可以任意选择一个进入,编辑过程中可以切换。

使用官方的模版

如果是常用的监控,我们没有必要 一步步的构建我们的视图和查询条件,可以直接使用官方的模版或者 其他用户已经编辑好的模版。

官方模版地址

以mysql各项指标监控为例子。

通过访问官方模版地址来查看已有仪表盘模板,选取合适的使用。

Here Insert Picture Description

有些模版 使用前有相应的 操作说明,比如需要安装my2。

https://github.com/meob/my2Collector

复制模版的id备用

Here Insert Picture Description

回到自己的Grafana节点,打开Import界面

Here Insert Picture Description

粘贴id,点击文本框外 如果找到该模版,会自动跳转。

Here Insert Picture Description

Here Insert Picture Description

选择相应数据源和 放置的目录即可。

成功展示如图:
Here Insert Picture Description

导入导出dashboard的json

有时候我们想要使用的模版并不是官网的,或者因为版本的问题和 没有外网等原因,grafana可能找不到。

Here Insert Picture Description

这种情况可以使用json的方式导入。

步骤如下:

点击获取 【Download JSON】

Here Insert Picture Description

导入时 点击 upload json 选中下载到本地的json即可。

Here Insert Picture Description

假如我们有一个做好的模版想要复用或者备份,也可以导出json。

点击 右上角的 share dashboard

Here Insert Picture Description

点击export 中的 view json即可

Here Insert Picture Description

Here Insert Picture Description

自定义表格

比如我们在mysql数据中有一个表格如下:

Here Insert Picture Description

想要在 grafana中展示,则需要使用同样的sql

select 
   id   as '序号',
   apartment   as '型号',
   area   as '面积',
   floor   as '层数',
   buildtime   as '建造时间',
   all_price   as '总价',
   unit_price   as '单价'
from 
   `basic`

根据之前的步骤新建dashboard,选择add query。

Here Insert Picture Description

在Query的下拉框 选中我们的数据源。

这里 sql语句有两种模式,一种是 build模式,填入相应的字段即可。

不过sql模式 更直观一些。

点击Edit SQL。

输入如上 sql语句。

点击左侧栏的visualiztion,选中table。

Here Insert Picture Description

回到 sql界面,format as 选中table即可

Here Insert Picture Description

点击右上角的保存按钮。

即可在 dashboard中查看如图,点击右下角可以拖拽 拉长拉宽。

Here Insert Picture Description

Here Insert Picture Description

自定义图形

我们已经尝试了展示表格,实际上 图形也是常用的展示,以下 举一些常用的例子。

重点说明 graph的参数,其他图形 可以参考graph的参数。

变化趋势:Graph面板

Graph面板是最常用的一种可视化面板,其通过折线图或者柱状图的形式显示监控样本随时间而变化的趋势。例如,当需要查看主机CPU、内存使用率的随时间变化的情况时,可以使用Graph面板。同时,Graph还可以非常方便的支持多个数据之间的对比。

创建Graph —曲线图

点击左侧栏的创建Create ----》 dashboard --》Choose Visualization 选中graph。

点击左侧大图标的 Queries。

在Query下拉框中选中 数据源。

点击 Edit SQL

输入 想要展示的sql语句,需要注意的是,语句中需要包含time_sec 和 metric 以及 value 。

按照如下格式:

SELECT
  UNIX_TIMESTAMP(<time_column>) as time_sec,
  <value column> as value,
  <series name column> as metric
FROM <table name>
WHERE $__timeFilter(time_column)
ORDER BY <time_column> ASC

time_sec 表示发生的时间,与x轴对应,该字段的数据必须为时间格式。
value 表示 数值 与 y轴对应
metric 表示 系列,也就是有几根线,可以看成是分组

我的语句如下:

SELECT
  UNIX_TIMESTAMP(insert_time) as time_sec,
  unit_price as value,
  concat(area,"平米") as metric
FROM basic
WHERE $__timeFilter(insert_time)
ORDER BY insert_time ASC

$__timeFilter() 表示只选中该字段值是时间的 数据。
UNIX_TIMESTAMP()则表示 转换为 时间戳

注意 如果我们的数据不是持续产生的话, 需要 在右上角 展示时间段内 调整下 展示时间段, 比如我的数据只有4月份的,所以需要调整成 展示最近1年。

展示如下:
Here Insert Picture Description

创建Graph —柱状图

柱状图 的创建方式跟 曲线图一致,区别在于 需要在visualization 中 把模式Draw modes 调整成 bar。

Here Insert Picture Description

这时候 我们发现 如果按照 insert_time 作为 x轴坐标,不符合我们想要看的 纬度,则可以 对 sql 语句进行修改,重新制定分组,然后 在Axes 中 调整X-Axis调整 x轴的mode 为 按系列分,选中series 。 如下:

SELECT
  now() as time_sec,
  unit_price as value,
  concat(area,"平米") as metric
FROM basic
WHERE unit_price < 20000
group by metric
ORDER BY metric ASC

Here Insert Picture Description

结果如图:
Here Insert Picture Description

如果数据很多,x轴排列不下,我们可以取消 x轴的文字显示,而是在下面使用标注的显示方式。

在Axes 中 调整X-Axis调整 x轴的show 为 关闭
Here Insert Picture Description

在Legend 中 调整Options 的show 为 打开

Here Insert Picture Description
效果如图:
Here Insert Picture Description

配置说明

Graph里面的选项有:

General(常规选择)、Axes(坐标轴)、Legend(图例)、 Display(显示样式)、Alert(告警)、Time range(时间范围)

不同版本会有差异。

1)General(常规选择:http://docs.grafana.org/features/panels/graph/)

Here Insert Picture Description

General允许定制面板的外观和菜单选项。

General Options

Title:仪表盘上的面板标题

Description:仪表盘描述信息

Transparent :是否透明,选择之后会把该图的背景去掉,即透明状态

Repeat panel:是否重复panel,填写是重复的变量(参考后文变量配置),即这个标题名中添加对应的变量,引用该变量需添加$,图标题即可随着自选的变量而变化。

panel links(为当前panel增加超链接)

(2)Axes(坐标轴)

Here Insert Picture Description
Left Y和Right Y可以自定义,即可以设置多重坐标轴,方便对比查看

Show:是否显示,可以通过从显示轴中取消适当的框来隐藏轴。

Unit:y轴的显示单元

Scale:Y轴的间隔度。选择“log base 2”以double的速度递增,(0、1、2、4、8…),选择“log base 32”,就是(0、1、32…)

Y-Min:Y轴的最小值(默认atuo)

Y-Max:Y轴的最大值(默认atuo)

Lable:Y轴的文本标签

(3)Legend(图例)

Here Insert Picture Description

通过选择显示复选框隐藏图例。如果它被显示,它可以通过检查表复选框显示为一个值表。没有值的系列可以使用隐藏空复选框,从而在图例中隐藏。

options:

show:是否显示图例

as table:作为表格样式显示

to the right:显示在右边

values:

在图例中,显示每个series的max\min\avg\total\current,能设置小数点位数。在图上显示,

通过点击列标题(如果保存的话,这个选项将会被持久化)来排序,通过min/max/avg来排序。

点击图例,也可单独显示某个series。

(4)Display(显示样式)

Here Insert Picture Description

1、Draw option

Draw Modes:Bars(柱状图)、lines(折线图)、Points(点)

Mode Options:Fill(充满区域透明度),Line Width(线宽度),Staircase(是否阶梯),Point Radius(点的半径,以此控制点的大小)

Hover tooltip

Mode:All series(鼠标移到点上显示所有图例的值),single(鼠标移到点上显示该series图例的值)

Sort order:None(按图例排列顺序显示),increaseing(值的从小到大递增),Decreasing(值从大到小递减)

Stacking&Null value :stack(多series是否堆叠显示),percent(百分比),Null value:空值怎么显示(connected:null值被忽略,直线直接跳转到下一个值,null:空值被保留为空,这将在图中留下空白区域,null as zero:空值被绘制为零值)

2、series overrieds:多坐标轴重写,即可以在这里设置y轴正负轴或z轴的正负轴,需要选择对应的指标,而且因指标、指标别名的变化需重新设置,设置完成后对比效果更好

alias or regex:series图例名称

Y-axis:显示在Y轴左右哪边,

z-index(多series显示前后位置),

stack:堆叠,可选择,metric中A\B\C\D哪个。

transform(negative-Y:将值显示为负数)

fill below to:(将两者值充满颜色 ,由上往下从大值到小值,所以不可以写成min fill below tu max写法如图:)

3、Thresholds & Time regions:

Here Insert Picture Description

Thresholds 可以写多条临界值在界面上显示,大于400,和大于600两个。如设置了alert,就不能设置这个。

Time regions 时间区域允许你突出特定的时间区域图的,更容易看到例如周末营业时间和/或工作时间。

(5)Alert(告警)

Here Insert Picture Description

Alert Config

Name & Evaluation interval:在这里可以指定警报规则的名称,以及调度器应该多长时间对警报规则进行评估。

Conditions:目前唯一存在的条件类型是一个查询条件,允许您指定查询字母(metric里查询语句的字母,代表哪个查询语句)、时间范围和聚合函数。

Notifications

在警告选项卡中,还可以指定警报规则通知,以及关于警报规则的详细信息。这个消息可以包含任何信息,关于如何解决这个问题的信息,链接到runbook等。实际的通知被配置并在多个警报之间共享。

State History

警戒状态的变化都被记录在内部注释Grafana的数据库表。状态更改可视为警报规则的图形面板中的注释。

graph更多解析可参考:https://www.jianshu.com/p/7e7e0d06709b

分布统计:Heatmap面板

Heatmap是是Grafana v4.3版本以后新添加的可视化面板,通过热图可以直观的查看样本的分布情况。
在Grafana v5.1版本中Heatmap完善了对Prometheus的支持。

创建步骤:

点击左侧栏的创建Create ----》 dashboard --》Choose Visualization 选中 Heatmap。

点击左侧大图标的 Queries。

在Query下拉框中选中 数据源。

点击 Edit SQL

输入 想要展示的sql语句,需要注意的是,语句中需要包含time_sec 和 metric 以及 value 。

按照如下格式:

SELECT
  UNIX_TIMESTAMP(<time_column>) as time_sec,
  <value column> as value,
  <series name column> as metric
FROM <table name>
WHERE $__timeFilter(time_column)
ORDER BY <time_column> ASC

语句可以相应修改,比如我需要看 不同的时间段中面积的分布。

使用sql如下:

SELECT
  UNIX_TIMESTAMP(insert_time) as time_sec,
  unit_price as value
FROM basic
WHERE 1=1

效果如图:

目前 Heatmap 只支持使用时间作为 X轴,暂时没发现可以不使用时间作为X轴的方法。

Here Insert Picture Description

当前状态:SingleStat面板

Singlem Panel侧重于展示系统的当前状态而非变化趋势。如下所示,在以下场景中特别适用于使用SingleStat:

当前系统中所有服务的运行状态;
当前基础设施资源的使用量;
当前系统中某些事件发生的次数或者资源数量等。

创建步骤:

点击左侧栏的创建Create ----》 dashboard --》Choose Visualization 选中 SingleStat。

点击左侧大图标的 Queries。

在Query下拉框中选中 数据源。

点击 Edit SQL

输入 想要展示的sql语句,需要注意的是,语句中需要包含time_sec 和 metric 以及 value 。

使用SQL如下:

SELECT
  now() as time,
  sum(all_price) as value
FROM basic
WHERE 1=1

需要注意的是 SingleStat使用的sql,除了必备的time字段外, 必须只有一个值,也就是总的数值。

如下所示,是使用SingleStat进行数据可视化的显示效果:

Here Insert Picture Description

使用变量

在Grafana中可以为Dashboard定义一组变量(Variables),变量一般包含一个到多个可选值。

通过Dashboard页面的Settings选项,可以进入Dashboard的配置页面并且选择Variables子菜单:

Here Insert Picture Description
用户需要指定变量的名称,后续用户就可以通过$variable_name的形式引用该变量。

Grafana目前支持7种不同的变量类型。

类型 工作方式
Query 允许用户通过Datasource查询表达式的返回值动态生成变量的可选值
Interval 该变量代表时间跨度,通过Interval类型的变量,可以动态改变PromQL区间向量表达式中的时间范围。如rate(node_cpu[2m])
Datasource 允许用户动态切换当前Dashboard的数据源,特别适用于同一个Dashboard展示多个数据源数据的情况
Custom 用户直接通过手动的方式,定义变量的可选值
Constant 常量,在导入Dashboard时,会要求用户设置该常量的值
Text box 输入框,允许用户输入
Ad hoc filters 非常特殊的变量,目前仅适用于某些数据源,InfluxDB和Elasticsearch。它允许您添加键/值过滤器,这些过滤器将自动添加到使用指定数据源的所有度量标准查询中

我们以Text box演示变量的用法,以SingleStat面板为例。

首先设置好环境变量名和类型如下:

Here Insert Picture Description

点击Add 按钮 看到创建变量成功如下:

Here Insert Picture Description

点击左边的save按钮,填入版本描述信息。

点击左上的箭头回到 编辑图表界面。

点击左侧的General按钮,在title栏里输入 单价小于$max_price的房子总价,发现变量生效了。

Here Insert Picture Description

sql中使用如下:

SELECT
  now() as time,
  sum(all_price) as value
FROM basic
WHERE unit_price < '${max_price}'

保存dashboard后 在 界面上即可通过输入 变量 进行 筛选了。

Here Insert Picture Description

更多图表json参考

Custom components -Dashboard List Panel and other
https://grafana.com/docs/grafana/latest/features/panels/dashlist/

Template
https://github.com/percona/grafana-dashboards/tree/master/dashboards

Template
http://grafana.openstack.org/dashboards

Template
http://47.96.36.117:3000/d/000000002/nginxfang-wen-tong-ji?orgId=1

Published 805 original articles · won praise 897 · Views 5.24 million +

Guess you like

Origin blog.csdn.net/q383965374/article/details/103619565