开源测试工具 Jmeter 的性能监控框架搭建记录 (Grafana+Influxdb+Telegraf)

老马 · 2018年09月13日 · 最后由 远明 回复于 2023年01月06日 · 4406 次阅读

一 使用目的

Jmeter
Grafana
Influxdb
Telegraf
利用以上开源性能监控工具,可以达到以下两大方面的监控:
1 负载施压负载情况监控 (请求数 tps qps 响应时间等)
2 被压测机器的硬件资源消耗 (cpu,内存,网络,磁盘 IO 等)

二 参考内容

http://docs.grafana.org/
https://docs.influxdata.com/influxdb/v1.6/
https://docs.influxdata.com/telegraf/v1.7/

三 安装帮助

提供两种安装方式,看个人偏好按需选择。有 *** 条件的和希望维护更新简便的建议 apt 添加源的方式。
独立下载 deb 包的方式,有利于对包的版本做限制,自己根据实际需要更新的情况手动更新维护。
以下均为在 ubuntu16.04.4 或 ubuntu18.04.1 桌面版上安装部署记录.

Influxdb 安装

方法一:deb 包安装
Ubuntu & Debian
https://portal.influxdata.com/downloads

wget https://dl.influxdata.com/influxdb/releases/influxdb_1.6.0_amd64.deb
sudo dpkg -i influxdb_1.6.0_amd64.deb

方法二:apt 源

Installation of InfluxDB on Ubuntu 18.04 is done from Influxdata repository. Once the repo is added, the package can then be installed using an apt package manager. Add the InfluxData repository to the file /etc/apt/sources.list.d/influxdb.list

$ sudo gedit  /etc/apt/sources.list.d/influxdb.list  
 追加
deb https://repos.influxdata.com/ubuntu bionic stable

Import apt key:

$ sudo curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -

Update apt index and install influxdb
$ sudo apt-get update

$ sudo apt-get install influxdb
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列【新】软件包将被安装:
  influxdb
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 23.0 MB 的归档。
解压缩后会消耗 76.5 MB 的额外空间。
获取:1 https://repos.influxdata.com/ubuntu bionic/stable amd64 influxdb amd64 1.6.0-1 [23.0 MB]
已下载 23.0 MB,耗时 7秒 (3,445 kB/s)                                                              
正在选中未选择的软件包 influxdb。
(正在读取数据库 ... 系统当前共安装有 454581 个文件和目录。)
正准备解包 .../influxdb_1.6.0-1_amd64.deb  ...
正在解包 influxdb (1.6.0-1) ...
正在设置 influxdb (1.6.0-1) ...
Created symlink /etc/systemd/system/influxd.service → /lib/systemd/system/influxdb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/influxdb.service → /lib/systemd/system/influxdb.service.
正在处理用于 man-db (2.8.3-2) 的触发器 ...



Start and enable the service to start on boot up. 添加随开机自启动服务
$ sudo systemctl start influxdb

$ sudo  systemctl enable influxdb

  检查是否添加自启动成功 
$ sudo systemctl is-enabled influxdb


Check service status
$ systemctl status influxdb
● influxdb.service - InfluxDB is an open-source, distributed, time series database
   Loaded: loaded (/lib/systemd/system/influxdb.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-07-11 17:20:11 CST; 6s ago
     Docs: https://docs.influxdata.com/influxdb/
 Main PID: 9774 (influxd)
    Tasks: 14 (limit: 4915)
   CGroup: /system.slice/influxdb.service
           └─9774 /usr/bin/influxd -config /etc/influxdb/influxdb.conf
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.221964Z lvl=info msg="Starting precreation
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.221989Z lvl=info msg="Starting snapshot ser
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.222005Z lvl=info msg="Starting continuous q
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.222021Z lvl=info msg="Starting HTTP service
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.222036Z lvl=info msg="opened HTTP access lo
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.222218Z lvl=info msg="Listening on HTTP" lo
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.222258Z lvl=info msg="Starting retention po
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.222852Z lvl=info msg="Listening for signals
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.261043Z lvl=info msg="Sending usage statist
7月 11 17:20:12 TR influxd[9774]: ts=2018-07-11T09:20:12.260262Z lvl=info msg="Storing statistics" l

备份和配置 influxdb.conf

$ sudo systemctl stop influxdb

$ sudo cp /etc/influxdb/influxdb.conf /etc/influxdb/influxdb.conf.backup

  大致修改的内容如下
$ sudo gedit /etc/influxdb/influxdb.conf 

[http]
  # Determines whether HTTP endpoint is enabled.
    enabled = true

  # The bind address used by the HTTP service.
    bind-address = ":8086"


[[graphite]]
  # Determines whether the graphite endpoint is enabled.
    enabled = true
    database = "jmeter"
    retention-policy = ""
    bind-address = ":2003"
    protocol = "tcp"
    consistency-level = "one"
启动influxd:
$ sudo  influxd   -config /etc/influxdb/influxdb.conf 
或
 $ sudo systemctl start influxdb

先创建 influxdb 数据库 jmeter


cmd@TR:~$ influx
Connected to http://localhost:8086 version 1.6.0
InfluxDB shell version: 1.6.0

> CREATE DATABASE jmeter

> SHOW DATABASES
name: databases
name
----
telegraf
_internal
jmeter
> use jmeter
Using database jmeter

遇到问题:

cmd@TR:~$ curl http://localhost:8083
curl: (7) Failed to connect to localhost port 8083: 拒绝连接


cmd@TR:~$ curl http://localhost:8086
404 page not found


cmd@TR:~$ sudo ufw allow 8086/tcp
[sudo] cmd 的密码:
防火墙规则已更新
规则已更新(v6)


cmd@TR:~$ sudo ufw allow 8083/tcp
防火墙规则已更新
规则已更新(v6)

修改防火墙后,curl 问题依旧


cmd@TR:~$ sudo ufw disable
防火墙在系统启动时自动禁用

重启系统, 启动 sudo systemctl start influxdb curl 问题依旧
后续用 Grafana 配置添加 DataSources 到是连通 jmeter 数据库正常。暂时不管该问题。
后来发现,原来,官方 1.2 以上都移除了 admin web 管理页面监控,当然访问失败。不用管这个 url 了.

用下边命令来检查 influxd 必要启动的相关服务端口 8086 8088 2003 等,具体用途详见官方说明

cmd@TR:~$ sudo netstat -tlnpu |grep influxd
tcp        0      0 127.0.0.1:8088          0.0.0.0:*               LISTEN      17275/influxd       
tcp6       0      0 :::2003                 :::*                    LISTEN      17275/influxd       
tcp6       0      0 :::8086                 :::*                    LISTEN      17275/influxd

Grafana 安装

http://docs.grafana.org/installation/debian/

方法一:deb 包安装

wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.1.4_amd64.deb
sudo apt-get install -y adduser libfontconfig
sudo dpkg -i grafana_5.1.4_amd64.deb
Open Port on Firewall
Grafana default http port is 3000, you’ll need to allow access to this port on the firewall. Ubuntu comes with ufw firewall. For Debian, you can install it using:
$ sudo apt-get install ufw

Then enable the firewall service:
$ sudo ufw enable

Open the port on the firewall:
$ sudo ufw allow 3000/tcp 
Rule added
Rule added (v6)

To allow access only from a specific subnet, use:
$ sudo ufw allow from 192.168.50.0/24 to any port 3000
Accessing Grafana Dashboard
Access Grafana Dashboard using the server IP address or hostname and port 3000.

Default logins are:
Username: admin
Password: admin

方法二:apt 源

Installing Grafana on Ubuntu 18.04 / Debian 9 using APT repository
Add the following line to /etc/apt/sources.list.d/grafana.list
$ cat /etc/apt/sources.list.d/grafana.list
deb https://packagecloud.io/grafana/stable/debian/ stretch main

Then add the Package Cloud key. This allows you to install signed packages.
$ sudo curl https://packagecloud.io/gpg.key | sudo apt-key add -

Update apt index and install grafana
$ sudo apt-get update

Run the command below to install Grafana on Ubuntu 18.04:
$ sudo apt-get install grafana --fix-missing
正在读取软件包列表... 完成
正在分析软件包的依赖关系树      
正在读取状态信息... 完成      
下列【新】软件包将被安装:
 grafana
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 54.5 MB 的归档。
解压缩后会消耗 166 MB 的额外空间。
获取:1 https://packagecloud.io/grafana/stable/debian stretch/main amd64 grafana amd64 5.2.1 [54.5 MB]
已下载 54.5 MB,耗时 24秒 (2,256 kB/s)                                                            
正在选中未选择的软件包 grafana。
(正在读取数据库 ... 系统当前共安装有 454607 个文件和目录。)
正准备解包 .../grafana_5.2.1_amd64.deb  ...
正在解包 grafana (5.2.1) ...
正在处理用于 ureadahead (0.100.0-20) 的触发器 ...
正在处理用于 systemd (237-3ubuntu10) 的触发器 ...
正在设置 grafana (5.2.1) ...
正在添加系统用户"grafana" (UID 123)...
正在将新用户"grafana" (UID 123)添加到组"grafana"...
无法创建主目录"/usr/share/grafana"
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server
### You can start grafana-server by executing
sudo /bin/systemctl start grafana-server
正在处理用于 ureadahead (0.100.0-20) 的触发器 ...
正在处理用于 systemd (237-3ubuntu10) 的触发器 ...


Start and enable grafana-server service to start on boot:
$ sudo systemctl start grafana-server
Enable it to start on boot:
$ sudo /bin/systemctl daemon-reload
$ sudo /bin/systemctl enable grafana-server
Synchronizing state of grafana-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /usr/lib/systemd/system/grafana-server.service.


Check status to confirm that it is running:
$ sudo systemctl status grafana-server
● grafana-server.service - Grafana instance
  Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; enabled; vendor preset: enabled)
  Active: active (running) since Wed 2018-07-11 17:22:27 CST; 14s ago
    Docs: http://docs.grafana.org
Main PID: 10889 (grafana-server)
   Tasks: 13 (limit: 4915)
  CGroup: /system.slice/grafana-server.service
          └─10889 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/gra
7月 11 17:22:41 TR grafana-server[10889]: t=2018-07-11T17:22:41+0800 lvl=info msg="Executing migrati
7月 11 17:22:41 TR grafana-server[10889]: t=2018-07-11T17:22:41+0800 lvl=info msg="Executing migrati
7月 11 17:22:41 TR grafana-server[10889]: t=2018-07-11T17:22:41+0800 lvl=info msg="Executing migrati
7月 11 17:22:41 TR grafana-server[10889]: t=2018-07-11T17:22:41+0800 lvl=info msg="Executing migrati
7月 11 17:22:41 TR grafana-server[10889]: t=2018-07-11T17:22:41+0800 lvl=info msg="Executing migrati
7月 11 17:22:41 TR grafana-server[10889]: t=2018-07-11T17:22:41+0800 lvl=info msg="Executing migrati
7月 11 17:22:41 TR grafana-server[10889]: t=2018-07-11T17:22:41+0800 lvl=info msg="Executing migrati
7月 11 17:22:42 TR grafana-server[10889]: t=2018-07-11T17:22:42+0800 lvl=info msg="Executing migrati
7月 11 17:22:42 TR grafana-server[10889]: t=2018-07-11T17:22:42+0800 lvl=info msg="Executing migrati
7月 11 17:22:42 TR grafana-server[10889]: t=2018-07-11T17:22:42+0800 lvl=info msg="Executing migrati
lines 1-19/19 (END)

启动服务成功后,grafana 默认访问地址
http://localhost:3000

Telegraf 安装

方法一:deb 包安装

Ubuntu & Debian
wget https://dl.influxdata.com/telegraf/releases/telegraf_1.7.1-1_amd64.deb
sudo dpkg -i telegraf_1.7.1-1_amd64.deb

方法二:apt 源

Installation of telegraf on Ubuntu 18.04 is done from Influxdata repository. Once the repo is added, the package can then be installed using an apt package manager. Add the InfluxData repository to the file  /etc/apt/sources.list.d/influxdata.list
$ cat /etc/apt/sources.list.d/influxdata.list
deb https://repos.influxdata.com/ubuntu bionic stable

Import apt key:
$ sudo curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -

Update apt index and install telegraf
$ sudo apt-get update

Install telegraf:
$ sudo apt-get install telegraf
正在读取软件包列表... 完成
正在分析软件包的依赖关系树      
正在读取状态信息... 完成      
下列【新】软件包将被安装:
 telegraf
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 10.3 MB 的归档。
解压缩后会消耗 30.5 MB 的额外空间。
获取:1 https://repos.influxdata.com/ubuntu bionic/stable amd64 telegraf amd64 1.7.1-1 [10.3 MB]
已下载 10.3 MB,耗时 3秒 (3,140 kB/s)                
正在选中未选择的软件包 telegraf。
(正在读取数据库 ... 系统当前共安装有 455988 个文件和目录。)
正准备解包 .../telegraf_1.7.1-1_amd64.deb  ...
正在解包 telegraf (1.7.1-1) ...
正在设置 telegraf (1.7.1-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/telegraf.service → /lib/systemd/system/telegraf.service.

Start and enable the service to start on boot up.
$ sudo systemctl start telegraf
$ sudo  systemctl enable telegraf
$ sudo systemctl is-enabled telegraf

Check service status:
~$ sudo systemctl status telegraf
● telegraf.service - The plugin-driven server agent for reporting metrics into InfluxDB
  Loaded: loaded (/lib/systemd/system/telegraf.service; enabled; vendor preset: enabled)
  Active: active (running) since Wed 2018-07-11 17:25:06 CST; 10s ago
    Docs: https://github.com/influxdata/telegraf
Main PID: 12161 (telegraf)
   Tasks: 20 (limit: 4915)
  CGroup: /system.slice/telegraf.service
          └─12161 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d
7月 11 17:25:06 TR systemd[1]: Started The plugin-driven server agent for reporting metrics into InfluxDB.
7月 11 17:25:06 TR telegraf[12161]: 2018-07-11T09:25:06Z I! Starting Telegraf v1.7.1
7月 11 17:25:06 TR telegraf[12161]: 2018-07-11T09:25:06Z I! Loaded inputs: inputs.kernel inputs.mem inputs.processes inputs.swap inputs
7月 11 17:25:06 TR telegraf[12161]: 2018-07-11T09:25:06Z I! Loaded aggregators:
7月 11 17:25:06 TR telegraf[12161]: 2018-07-11T09:25:06Z I! Loaded processors:
7月 11 17:25:06 TR telegraf[12161]: 2018-07-11T09:25:06Z I! Loaded outputs: influxdb
7月 11 17:25:06 TR telegraf[12161]: 2018-07-11T09:25:06Z I! Tags enabled: host=TR
7月 11 17:25:06 TR telegraf[12161]: 2018-07-11T09:25:06Z I! Agent Config: Interval:10s, Quiet:false, Hostname:"TR", Flush Interval:10s
lines 1-17/17 (END)

备份和修改 telegraf.conf

$ sudo cp /etc/telegraf/telegraf.conf /etc/telegraf/telegraf.conf.backup

$ sudo gedit /etc/telegraf/telegraf.conf
# Configuration for sending metrics to InfluxDB
[[outputs.influxdb]]
 ## The full HTTP or UDP URL for your InfluxDB instance.
 ##
 ## Multiple URLs can be specified for a single cluster, only ONE of the
 ## urls will be written to each interval.
 # urls = ["unix:///var/run/influxdb.sock"]
 # urls = ["udp://127.0.0.1:8089"]
 # 你安装的Influxdb地址  我安装在了本机配置可以是127.0.0.1 或 localhost 或你本机配置的固定ip 接:8086
   urls = ["http://192.168.***.***:8086"]

 ## The target database for metrics; will be created as needed.
 #  你想要监控host硬件资源机器的存储数据库名 ,比如要监控固定ip机器69.113 便可起名IP_66_113
   database = "IP_66_113"

访问 grafana

最后 打开以上三个服务后,访问 grafana http://localhost:3000

添加数据源类型 influxdb 的 jmeter 数据库, 也就是 influxdb.conf 中配置的为了记录 jmeter 负载情况的。

添加数据源类型 influxdb 的 IP_66_113 数据库 ,也就是 telegraf.conf 中配置为了记录 host 机硬件资源情况的。

三 Grafana Dashboard 监控配置

Grafana import Dashboard 监控面板
导入前可以自行 create Folder jmeter 和 IP_66_113 以便于区分存储和记录,也注意选择需要存储的 DB name (jmeter 和 IP_66_113 )
一个是 监控 jmeter 负载情况的 https://grafana.com/dashboards/4026

一个是 监控 host 硬件资源情况的 https://grafana.com/dashboards/3967 照着修改配置文件 telegraf.conf 内容

监控本机 host TR 硬件资源效果图

四 Jmeter BackendListerer 监控配置

配置 Jmeter BackendListerer

启动 jmeter4.0 随便设置个线程组 创建个 http 请求访问 baidu
BackendListerer 主要配置如下

如何确认 jmeter 可以被 influxdb 记录并监控了:
show measurements 看到 jmeter 相关字段的话即可

cmd@TR:~$ influx
Connected to http://localhost:8086 version 1.6.0
InfluxDB shell version: 1.6.0
> SHOW DATABASES
name: databases
name
----
telegraf
_internal
jmeter
IP_66_113
> USE jmeter
Using database jmeter
> show measurements
name: measurements
name
----
events
jmeter
jmeter.all.a.avg
jmeter.all.a.count
jmeter.all.a.max
jmeter.all.a.min
jmeter.all.a.pct90
jmeter.all.a.pct95
jmeter.all.a.pct99
jmeter.all.h.count
jmeter.all.ko.avg
...

然后,可以线程组里设置些循环 ,然后启动执行该 testplan

Jmeter Dashboard 搜集负载情况监控数据效果图

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 20 条回复 时间 点赞

小马哥您好,如果我要将多台服务器接入监控的话,看一下我的方式是否正确:
1.在其他服务器上安装 Telgraf
2.在 Influxdb 中创建对应机器的 IP 的表(不需要再添加 jmeter 表)
3.Grafana 中配置 DATA SOURSES
4.针对不同目标服务器导入模板

不错,支持原创~
但是如果需要监控的服务器比较多,每个服务器都要装这个,就不太方便了啊

假如,不用 jmeter 做性能测试,这个监控框架是否有用?比如我不做步骤四的 jmeter 配置,前三步都配置好了。那么框架是否能只展示内存,网络,cpu 和磁盘 io?

老马 #15 · 2018年09月14日 Author
黑山老妖 回复

是否能只展示内存,网络,cpu 和磁盘 io?

那就是 Telegraf 是负责监控硬件资源的 你看下他的官方配置文件. 你只导入这个面板 就可以了啊 . 如果你不需要 负载请求监控的话
你自己设计面板 或 去找别人弄好的面板 https://grafana.com/dashboards 自己按需再改改.

黑山老妖 回复

监控方案有很多:
1jmeter 有自带的好多 就是那个查看结果树里的一些
https://jmeter-plugins.org/ 插件里也有一些 比如 https://jmeter-plugins.org/wiki/PerfMon/ 这个插件

2 用专业的 运维工具 如 楼上 提到的 zabbix 不同公司的运维人员 用的可能有点差异 统一找你们运维了解 和 使用一个标准的来统计和监控 一致性比较好

3 Grafana+Influxdb+Telegraf 实际也是运维常用的监控工具 可配置性 和 面板界面 实时性 统计准确性 比较一致
我看有人 测试对比过 jmeter 自带 或 linux 命令 监控的 和 Grafana+Influxdb+Telegraf 监控的对比 一致性非常好.
这里说的是 负载监控 和 硬件资源监控 一致性都非常好 所以 我鼓捣了下这套 可配置性也非常强

4 坛子里 我看还有大神 自己写的监控 好像主要面向硬件资源消耗的监控 https://github.com/mzky/easyNmon 利用 nmon 命令开发的.

这里遇到一个问题 在 centOS 上所有东西都部署好了,telegraf 启动后遇到一个问题:

2018-09-20T01:46:15Z E! Error in plugin [inputs.sysstat]: failed to run command  -S DISK 14 2 /tmp/sysstat-1537407975: fork/exec : no such file or directory - 
2018-09-20T01:46:30Z E! failed to remove tmp file after  -S DISK 14 2 /tmp/sysstat-1537407990 command: remove /tmp/sysstat-1537407990: no such file or directory

是否有遇到过?

战 神 回复

问题已解决:

 [[inputs.sysstat]]
#   ## Path to the sadc command.
#   #
#   ## Common Defaults:
#   ##   Debian/Ubuntu: /usr/lib/sysstat/sadc
#   ##   Arch:          /usr/lib/sa/sadc
 #   ##     RHEL/CentOS:   /usr/lib64/sa/sadc
   sadc_path = "/usr/lib64/sa/sadc" # required
   interval = "15s"

需要在 [[inputs.sysstat]] 标签下 加入 linux sadc 命令行驱动方式

  1. 服务器数量多,安装成本高,同时看十几个监控页面是什么样体验?
  2. zabbix,粒度太大了,用来看 db load 是不错选择。
老马 #11 · 2018年09月21日 Author
cctodd 回复

docker 化部署 实时性 Grafana 你可以问问你们运维

楼主麻烦问下,4026 这个 Dashboad 有没有遇到过分布式压测时,监控数据不全的情况?

楼主,请问一下,jmeter 分布式的时候,如何监控每一台执行机的情况

楼主,跟着配置跑起来了,但没生成下面 jmeter.开头的表,请教一下知道什么原因吗?
jmeter.all.a.avg
jmeter.all.a.count
jmeter.all.a.max
jmeter.all.a.min
jmeter.all.a.pct90
jmeter.all.a.pct95
jmeter.all.a.pct99
jmeter.all.h.count
jmeter.all.ko.avg

老马 #15 · 2019年04月18日 Author
kell 回复

配置 Jmeter BackendListerer 沒配对 influbdb 对应的记录 db=jmeter 的库 就没这些

小马哥出品,必属精品👍

你好,我按你的导入了模板(3967),也按模板配置了 conf,但是没有数据

TesterRoad 回复

你好,请问你的问题解决了没,我的也是这个问题

周逸粮 回复

这个问题很简单啊?你解决了吗?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册