Telegraf和Grafana监控多平台上的SQL Server
問題
SQL Server在很多企業中部署在多個平臺上(Windows,Linux和Container),需要一種能支持多平臺的解決方案用于收集和展示相關的監控指標。
我選擇企業中比較流行的監控展示工具Grafana和監控指標收集工具Telegraf進行實現。這也是為了方便與企業中已經在存在監控平臺進行整合和對接。
如上圖所示,Telegraf部署在SQL所在host,收集數據發送給時序數據庫Influxdb存儲,然后Grafana用于展示數據。
解決方案
安裝和配置InfluxDB
安裝和配置Grafana
然后在Browser中訪問http://:3000,第一次訪問時默登錄認賬號和密碼都為admin,登錄后會提示修改密碼。
在在被監控的數據庫主機安裝和配置Telegraf
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]; GOTelegraf on Linux
安裝完成后,先要修改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 Authusername = "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 InfluxDBLoaded: loaded (/usr/lib/systemd/system/telegraf.service; enabled; vendor preset: disabled)Active: active (running) since Tue 2020-05-26 14:19:07 UTC; 19min agoDocs: https://github.com/influxdata/telegrafMain PID: 12359 (telegraf)CGroup: /system.slice/telegraf.service└─12359 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.dMay 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:10sTelegraf 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的監控
轉載自:https://www.cnblogs.com/Joe-T/p/12973362.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Telegraf和Grafana监控多平台上的SQL Server的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法专题——第一题 Bitma
- 下一篇: 研发协同平台数据库死锁处理及改进