SQL Server在不少企業中部署在多個平臺上(Windows,Linux和Container),須要一種能支持多平臺的解決方案用於收集和展現相關的監控指標。linux
我選擇企業中比較流行的監控展現工具Grafana和監控指標收集工具Telegraf進行實現。這也是爲了方便與企業中已經在存在監控平臺進行整合和對接。
git
如上圖所示,Telegraf部署在SQL所在host,收集數據發送給時序數據庫Influxdb存儲,而後Grafana用於展現數據。github
我將InfluxDB和Grafana安裝在同一臺CentOS主機上,生產環境中最好是分開。sql
# 下載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 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://
所謂客戶端,就是SQL所在主機vim
Telegraf鏈接到SQL,須要一個login,具備 VIEW SERVER STATE and VIEW ANY DEFINITION的權限,因此在每一個被監控的實例上都須要建立之。windows
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
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表示監控要發送到哪裏去。app
打開/etc/telegraf/telegraf.conf,找到[[outputs.influxdb]]部分,全部配置項默認都被註釋了。咱們須要刪除註釋並配置一些項。主要是Influxdb的地址,用戶名、密碼和數據庫名等。tcp
[[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>"
Telegraf默認的Plugin中包括了對SQL Server的實現, 這個Plugin還包括了對Azure SQL PaaS的實現ide
# # 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
以管理員身份執行以下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後,在左側的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的監控
本文內容僅表明我的觀點,與任何公司和組織無關