基於 Njmon + InfluxDB + Grafana 實現性能指標實時可視監控

引言

最近逛 nmon 官網時,發現了一個新工具 njmon,功能與 nmon 類似,但輸出爲 JSON 格式,可以用於服務器性能統計。

可以使用 njmon 來向 InfluxDB 存儲服務器性能統計數據,再通過 Grafana 實時讀取展示,來實現性能測試過程中的實時可視化監控服務器性能指標的目的。

當然,傳統的 nmon、InfluxDB+Grafana+Jmeter等都可以實現。

驗證環境

CentOS Linux release 7.6.1810 (Core)

整體架構

在這裏插入圖片描述

原圖鏈接:http://nmon.sourceforge.net/docs/nmon_outline_800.png

InfluxDB

InfluxDB 是一個由 InfluxData 開發的開源時序型數據。它由 Go 寫成,着力於高性能地查詢與存儲時序型數據。InfluxDB 被廣泛應用於存儲系統的監控數據,IoT 行業的實時數據等場景。

InfluxDB 的語法是類 SQL 的,增刪改查與 mysql 相同。InfluxDB 中的 measurement 對應的關係型數據庫中的 table 。默認端口是 8086。

安裝 & 啓動

官方教程:https://docs.influxdata.com/influxdb/v1.7/introduction/installation/

配置 InfluxDB 的 yum 源:

$ cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key
EOF

yum 安裝

# CentOS 7-, RHEL 7-
$ sudo yum install -y influxdb
$ sudo service influxdb start

# CentOS 7+, RHEL 7+
$ sudo yum install -y influxdb
$ sudo systemctl start influxdb

創建 njmon 庫

$ influx
> create database njmon
> show databases
name: databases
name
----
_internal
njmon
> exit

啓用用戶認證

添加用戶,設置權限。

# 查看所有用戶
> show users
user admin
---- -----

# 創建 admin 用戶,設置密碼爲 admin
> create user "admin" with password 'admin' with all privileges

# 再次查看用戶信息,發現 admin 爲 true
> show users
user admin
---- -----
admin true

InfluxDB 默認是禁用認證策略的。

# 編輯配置文件,把 [http] 下的 auth-enabled 選項設置爲 true
$ vi /etc/influxdb/influxdb.conf
[http]
  ...
  auth-enabled = true
  ...
  
# 重啓服務,配置生效
$ systemctl restart influxdb.service

njmon

njmon = nmon + JSON format + real-time push to a stats database + instant graphing of “all the stats you can eat” (AIX and Linux)

This njmon is a major overhaul of nmon for the next 10 years:

  • Load more stats
  • JSON format is self documenting, flexible and the performance stats format for many new tools
  • Direct real-time loading of the JSON into modern open source time aware databases
  • New age browser based graphing tools allow dynamic data choice and graph style per VM, per server or across the estateAll this will be covered and more including many demo’s.

與 nmon 類似,但輸出爲 JSON 格式,可以用於服務器性能統計。

安裝 njmon

官方下載總目錄:https://sourceforge.net/projects/nmon/files/

# 下載 
$ wget http://sourceforge.net/projects/nmon/files/njmon_linux_binaries_v53.zip

# 解壓 
$ unzip njmon_linux_binaries_v53.zip

# 選擇相應版本,放到 local 的 bin 下
$ mv njmon_linux_RHEL7_AMD64_v53 /usr/local/bin/njmon

# 驗證
$ njmon -?

njmon 統計的指標項

$ njmon -c 1 -s 1 | jq keys
[
  "cpu_total",
  "cpuinfo",
  "cpus",
  "disks",
  "filesystems",
  "identity",
  "lscpu",
  "networks",
  "os_release",
  "proc_meminfo",
  "proc_version",
  "proc_vmstat",
  "stat_counters",
  "timestamp",
  "uptime"
]

關於 jq 的功能和使用,可以參見我之前寫的文章 “linux 下強大的 JSON 解析命令 jq”。

安裝 njmon_tools

# 下載
$ wget http://sourceforge.net/projects/nmon/files/njmon_tools_v50.zip

# 解壓
$ unzip njmon_tools_v50.zip
Archive:  njmon_tools_v50.zip
  inflating: line2pretty.py
  inflating: njmon2influx.py
  inflating: njmond.conf
  inflating: njmond.py
  inflating: njmonold2line.py
  inflating: pretty2line.py

採集數據到 InfluxDB

官方設置了多種採集方式,本教程基於 njmon2influx.py 採集方式。

修改配置文件 njmond.conf

{
    "njmon_port": 8181,
    "njmon_secret": "ignore",
    "data_inject": false,
    "data_json": true,
    "directory": "/home/njmon/data",
    "influx_host": "localhost",
    "influx_port": 8086,
    "influx_user": "admin",
    "influx_password": "admin",
    "influx_dbname": "njmon",
    "workers": 2,
    "debug": true,
    # for njmon2influx.py
    "batch": 100
}

採集數據

# 間隔 5 秒,一直採集數據
$ nohup njmon -s 5 | ./njmon2influx.py njmond.conf >/dev/null 2>&1 &

# 監控 log
$ tail -f /home/njmon/data/njmon2influx.log

InfluxDB 查詢數據

# 用戶名密碼登錄
$ influx -username admin -password admin
Connected to http://localhost:8086 version 1.7.10
InfluxDB shell version: 1.7.10

# 查看庫
> show databases
name: databases
name
----
_internal
njmon

# 使用 njmon
> use njmon
Using database njmon

# 查看 measurements,有數據表示已經採集到
> show measurements
name: measurements
name
----
cpu_total
cpuinfo
cpus
disks
filesystems
identity
lscpu
networks
os_release
proc_meminfo
proc_version
proc_vmstat
stat_counters
timestamp
uptime

Grafana

Grafana 是一個跨平臺的開源的度量分析和可視化工具,可以通過將採集的數據查詢然後可視化的展示,並及時通知

安裝

官方教程:https://grafana.com/grafana/download

$ wget https://dl.grafana.com/oss/release/grafana-6.6.2-1.x86_64.rpm
$ sudo yum install -y grafana-6.6.2-1.x86_64.rpm

啓動

# 啓動並驗證
$ sudo systemctl daemon-reload
$ sudo systemctl start grafana-server
$ sudo systemctl status grafana-server

# 配置自啓動
$ sudo systemctl enable grafana-server.service

默認密碼:admin / admin,登錄地址:http://xx.xx.xx.xx:3000。

安裝插件

# 安裝插件 grafana-clock-panel 
# 插件鏈接:https://grafana.com/grafana/plugins/grafana-clock-panel
$ grafana-cli plugins install grafana-clock-panel

# 安裝插件 grafana-piechart-panel
# 插件鏈接:https://grafana.com/grafana/plugins/grafana-piechart-panel
$ grafana-cli plugins install grafana-piechart-panel

# 重啓生效
$ service grafana-server restart

配置

添加數據源

在這裏插入圖片描述

選擇 InfluxDB,並配置

在這裏插入圖片描述

導入儀表盤模板

njmon 模板鏈接:https://grafana.com/grafana/dashboards?search=njmon

選擇 “njmon Single Host njmon for Linux v11” 模板:

在這裏插入圖片描述

複製 ID ,在 Grafana 中導入即可:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
選擇 InfluxDB。

在這裏插入圖片描述

導入完成。

查看監控數據

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

參考資料

  • http://nmon.sourceforge.net/pmwiki.php?n=site.njmon
  • https://docs.influxdata.com/influxdb/v1.7/introduction/installation/
  • https://www.readkong.com/page/njmon-is-nmon-but-saving-to-json-format-for-modern-4222619
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章