問題 SQL Server在很多企業中部署在多個平臺上(Windows,Linux和Container),需要一種能支持多平臺的解決方案用於收集和展示相關的監控指標。 我選擇企業中比較流行的監控展示工具Grafana和監控指標收集工具Telegraf進行實現。這也是為了方便與企業中已經在存在監控平臺 ...
問題
SQL Server在很多企業中部署在多個平臺上(Windows,Linux和Container),需要一種能支持多平臺的解決方案用於收集和展示相關的監控指標。
我選擇企業中比較流行的監控展示工具Grafana和監控指標收集工具Telegraf進行實現。這也是為了方便與企業中已經在存在監控平臺進行整合和對接。
如上圖所示,Telegraf部署在SQL所在host,收集數據發送給時序資料庫Influxdb存儲,然後Grafana用於展示數據。
解決方案
-
安裝和配置InfluxDB
我將InfluxDB和Grafana安裝在同一臺CentOS主機上,生產環境中最好是分開。
# 下載1.8的stable version後進行安裝
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.0.x86_64.rpm
chmod 755 influxdb-1.8.0.x86_64.rpm
yum localinstall influxdb-1.8.0.x86_64.rpm
# 啟動並設置自啟動
systemctl start influxdb
systemctl enable influxdb
# 8086用於客戶端的HTTP連接,8088用於CLI調用RPC進行備份和還原操作
firewall-cmd --zone=public --add-port=8086/tcp --permanent
firewall-cmd --zone=public --add-port=8088/tcp --permanent
firewall-cmd --reload
# 連接到influxdb並創建用戶
fluxdb
> CREATE USER admin WITH PASSWORD '<password>' WITH ALL PRIVILEGES
# 啟用http用戶驗證,修改influxdb.conf中http section中auth-enabled = true
vim /etc/influxdb/influxdb.conf
systemctl restart influxdb
# 創建用於存儲監控數據的資料庫,保存6個月的數據
influx -username 'admin' -password '<password>'
> CREATE DATABASE telegraf
> CREATE RETENTION POLICY telegraf_6m ON telegraf DURATION 180d REPLICATION 1 DEFAULT
> SHOW DATABASES
-
安裝和配置Grafana
# 下載並安裝Grafana
wget https://dl.grafana.com/oss/release/grafana-7.0.1-1.x86_64.rpm
chmod 775 grafana-7.0.1-1.x86_64.rpm
yum localinstall grafana-7.0.1-1.x86_64.rpm
# 設置自啟動
systemctl start grafana-server.service
systemctl enable grafana-server.service
# 允許Grafana預設的埠3000
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --reload
然後在Browser中訪問http://
-
在客戶端主機安裝和配置Telegraf
所謂客戶端,就是SQL所在主機
Telegraf連接到SQL,需要一個login,具有 VIEW SERVER STATE and VIEW ANY DEFINITION的許可權,所以在每個被監控的實例上都需要創建之。
USE master;
GO
CREATE LOGIN [telegraf] WITH PASSWORD = N'1qaz@WSX';
GO
GRANT VIEW SERVER STATE TO [telegraf];
GO
GRANT VIEW ANY DEFINITION TO [telegraf];
GO
-
Telegraf on Linux
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.14.3-1.x86_64.rpm
sudo yum localinstall telegraf-1.14.3-1.x86_64.rpm
安裝完成後,先要修改Telegraf的配置文件,再啟動。在配置文件中主要配置兩個部分:inputs和outputs。 inputs表示監控數據從哪裡來,outputs表示監控要發送到哪裡去。
打開/etc/telegraf/telegraf.conf,找到[[outputs.influxdb]]部分,所有配置項預設都被註釋了。我們需要刪除註釋並配置一些項。主要是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"]
urls = ["http://172.17.2.4:8086"]
## The target database for metrics; will be created as needed.
## For UDP url endpoint database needs to be configured on server side.
database = "telegraf"
## The value of this tag will be used to determine the database. If this
## tag is not set the 'database' option is used as the default.
# database_tag = ""
## If true, the 'database_tag' will not be included in the written metric.
# exclude_database_tag = false
## If true, no CREATE DATABASE queries will be sent. Set to true when using
## Telegraf with a user without permissions to create databases or when the
## database already exists.
skip_database_creation = true
## Name of existing retention policy to write to. Empty string writes to
## the default retention policy. Only takes effect when using HTTP.
retention_policy = ""
## The value of this tag will be used to determine the retention policy. If this
## tag is not set the 'retention_policy' option is used as the default.
# retention_policy_tag = ""
## If true, the 'retention_policy_tag' will not be included in the written metric.
# exclude_retention_policy_tag = false
## Write consistency (clusters only), can be: "any", "one", "quorum", "all".
## Only takes effect when using HTTP.
write_consistency = "any"
## Timeout for HTTP messages.
timeout = "5s"
## HTTP Basic Auth
username = "admin"
password = "<password>"
- 找到[[inputs.sqlserver]]部分,取消相關配置項的註釋,servers部分連接到本地實例。
Telegraf預設的Plugin中包括了對SQL Server的實現, 這個Plugin還包括了對Azure SQL PaaS的實現
# # Read metrics from Microsoft SQL Server
[[inputs.sqlserver]]
# ## Specify instances to monitor with a list of connection strings.
# ## All connection parameters are optional.
# ## By default, the host is localhost, listening on default port, TCP 1433.
# ## for Windows, the user is the currently running AD user (SSO).
# ## See https://github.com/denisenkom/go-mssqldb for detailed connection
# ## parameters, in particular, tls connections can be created like so:
# ## "encrypt=true;certificate=<cert>;hostNameInCertificate=<SqlServer host fqdn>"
servers = [
"Server=localhost;Port=1433;User Id=telegraf;Password=<yourPassword>;app name=telegraf;log=1;"
]
#
# ## Optional parameter, setting this to 2 will use a new version
# ## of the collection queries that break compatibility with the original
# ## dashboards.
query_version = 2
#
# ## If you are using AzureDB, setting this to true will gather resource utilization metrics
# # azuredb = false
#
# ## Possible queries:
# ## - PerformanceCounters
# ## - WaitStatsCategorized
# ## - DatabaseIO
# ## - DatabaseProperties
# ## - CPUHistory
# ## - DatabaseSize
# ## - DatabaseStats
# ## - MemoryClerk
# ## - VolumeSpace
# ## - PerformanceMetrics
# ## - Schedulers
# ## - AzureDBResourceStats
# ## - AzureDBResourceGovernance
# ## - SqlRequests
# ## - ServerProperties
# ## A list of queries to include. If not specified, all the above listed queries are used.
# # include_query = []
#
# ## A list of queries to explicitly ignore.
# exclude_query = [ 'Schedulers' , 'SqlRequests']
啟動Telegraf之後,可以看到時已經載入的inputs和收集間隔
[root@SQL19N1 log]# systemctl status telegraf
● telegraf.service - The plugin-driven server agent for reporting metrics into InfluxDB
Loaded: loaded (/usr/lib/systemd/system/telegraf.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-05-26 14:19:07 UTC; 19min ago
Docs: https://github.com/influxdata/telegraf
Main PID: 12359 (telegraf)
CGroup: /system.slice/telegraf.service
└─12359 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d
May 26 14:19:07 SQL19N1 systemd[1]: Started The plugin-driven server agent for reporting metrics into InfluxDB.
May 26 14:19:07 SQL19N1 telegraf[12359]: 2020-05-26T14:19:07Z I! Starting Telegraf 1.14.3
May 26 14:19:07 SQL19N1 telegraf[12359]: 2020-05-26T14:19:07Z I! Loaded inputs: system cpu disk diskio kernel mem processes swap sqlserver
May 26 14:19:07 SQL19N1 telegraf[12359]: 2020-05-26T14:19:07Z I! Loaded aggregators:
May 26 14:19:07 SQL19N1 telegraf[12359]: 2020-05-26T14:19:07Z I! Loaded processors:
May 26 14:19:07 SQL19N1 telegraf[12359]: 2020-05-26T14:19:07Z I! Loaded outputs: influxdb
May 26 14:19:07 SQL19N1 telegraf[12359]: 2020-05-26T14:19:07Z I! Tags enabled: host=SQL19N1
May 26 14:19:07 SQL19N1 telegraf[12359]: 2020-05-26T14:19:07Z I! [agent] Config: Interval:20s, Quiet:false, Hostname:"SQL19N1", Flush Interval:10s
-
Telegraf on Windows
以管理員身份執行如下PowerShell命令
# 下載軟體
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.14.3_windows_amd64.zip ·
-OutFile "c:\temp\telegraf-1.14.3_windows_amd64.zip"
# 解壓縮到C:\Program Files\Telegraf
Expand-Archive "c:\temp\telegraf-1.14.3_windows_amd64.zip", "C:\Program Files"
# 將telegraf安裝為windows服務
C:\"Program Files"\Telegraf\telegraf.exe --service install
修改telegraf.conf中outputs.influxdb和添加inputs.sqlserver部分,這些內容和在Linux上的配置一樣,就不贅述了。
conf修改完成後,可以先測試一下telegraf是否能正常啟動,沒問題的話就啟動telegraf服務。
# 測試
C:\"Program Files"\Telegraf\telegraf.exe --config C:\"Program Files"\Telegraf\telegraf.conf --test
# 啟動服務
C:\"Program Files"\Telegraf\telegraf.exe --service start
-
配置Grafana的數據源和Dashboard
登錄Grafana後,在左側的Configuration->Data Source中配置InfluxDB數據源,填寫地址、賬號、密碼並設置為預設數據源,如下圖
Dashboard,可以自己創建,也可以在採用公開社區的(感謝熱心無私的大佬們)。這裡,我採用SQL Servers by Jonathan Rioux。這個Dashboard中使用的Piechart不是Grafana預置的,所以還需要安裝:
# Grafana所在Host安裝,重啟服務生效
grafana-cli plugins install grafana-piechart-panel
systemctl restart grafana-server.service
然後在Grafana界面,選擇左側的Dashboard->Import->填入Dashboard ID->Import,如下圖:
配置完成後的,可以看這個Dashboard提供的信息還比較豐富的,您也可以根據自己的需要修改和添加相關內容.
總結
-
實際情況中,自帶的數據收集和報表不能完全滿足業務需求,自定義的數據收集和自定義的Dashboard,也是非常容易實現的,下次再寫
-
如果已經在使用Zabbix了,Grafana可以直接對接到Zabbix的數據輸出。
-
Telegraf能非常好的支持Cloud環境,下次說說對Azure SQL PaaS的監控
-
本文內容僅代表個人觀點,與任何公司和組織無關