使用Telegraf + Influxdb + Grafana 監控SQLserver服務器的運行狀況
前言
本文在Debian9下采用Docker的方式安裝Telegraf + Influxdb + Grafana對服務器進行監控。
首先我們需要在一台Server上安裝Debian9作為監控主機,這個監控主機可以配置為可監控多台Server。
安裝Docker
如果之前已經安裝過舊版本的Docker,先移除舊版本的安裝
sudo apt-get remove docker docker-engine docker.io containerd runc
更新apt-get
sudo apt-get update
安裝依賴項
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common
添加Docker的GPG key,命令執行成功后會顯示一個OK
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
添加Docker的apt-get源
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"
更新apt-get源
sudo apt-get update
安裝Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
驗證Docker是否安裝成功
sudo docker run hello-world
執行以上的命令將會從服務器上拉取hello-world的docker鏡像,然后再運行,如果看到Hello from Docker!的字樣代表安裝成功。
安裝Docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
修改權限
sudo chmod +x /usr/local/bin/docker-compose
檢查是否安裝成功
root@TRPG:/opt/monitoring# docker-compose --version
docker-compose version 1.24.0, build 0aa59064
安裝Docker鏡像
新增一個用於安裝的目錄
mkdir /opt/monitoring && cd /opt/monitoring
為InfluxDB和Grafana創建一個配置文件docker-compose.yml,內容如下
version: "2"
services:
grafana:
image: grafana/grafana
container_name: grafana
restart: always
ports:
- 3000:3000
networks:
- monitoring
volumes:
- grafana-volume:/var/lib/grafana
influxdb:
image: influxdb
container_name: influxdb
restart: always
ports:
- 8086:8086
networks:
- monitoring
volumes:
- influxdb-volume:/var/lib/influxdb
networks:
monitoring:
volumes:
grafana-volume:
external: true
influxdb-volume:
external: true
以上配置文件為Influxdb和Grafana配置了相同的網絡,同時分別為他們配置了外部存儲區域,外部存儲的好處是可以避免Docker重啟時數據和配置文件丟失。
創建Dokcer的網絡和外部存儲
docker network create monitoring
docker volume create grafana-volume
docker volume create influxdb-volume
使用一下命令查看是否建立成功
kevin@TRPG:/opt/monitoring# docker network ls
NETWORK ID NAME DRIVER SCOPE
21e7cf397dda bridge bridge local
130831cce642 host host local
f58bbb798bea monitoring bridge local
a285f4ff4c69 none null local
kevin@TRPG:/opt/monitoring# docker volume ls
DRIVER VOLUME NAME
local grafana-volume
local influxdb-volume
如果可以看到我們創建的網絡及存儲后,就可以開始拉取Influxdb的鏡像,當中包含一些參數及用戶名和密碼
docker run --rm \
-e INFLUXDB_DB=telegraf -e INFLUXDB_ADMIN_ENABLED=true \
-e INFLUXDB_ADMIN_USER=admin \
-e INFLUXDB_ADMIN_PASSWORD=supersecretpassword \
-e INFLUXDB_USER=telegraf -e INFLUXDB_USER_PASSWORD=secretpassword \
-v influxdb-volume:/var/lib/influxdb \
influxdb /init-influxdb.sh
使用docker-compose啟動
docker-compose up -d
檢查是否運行成功
kevin@TRPG:/opt/monitoring# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddc05619ab66 grafana/grafana "/run.sh" 47 seconds ago Up 46 seconds 0.0.0.0:3000->3000/tcp grafana
73efef324306 influxdb "/entrypoint.sh infl…" 47 seconds ago Up 45 seconds 0.0.0.0:8086->8086/tcp influxdb
以上,Influxdb和grafana都安裝和啟動成功,從上面的信息可以看到Influxdb使用端口tcp 8086,Grafana使用端口tcp 3000,在瀏覽器打開IP:3000就可以看到Grafana的登錄界面了,默認的用戶名及密碼都是admin,首次登陸后會提示修改密碼。
登錄到Grafana后,選擇Add datasource ,類型選擇InfluxDB,配置界面如下圖
URL的部分輸入的influxdb是表示之前創建的鏡像名稱,因為兩個鏡像共用了同一個網絡,因此可以使用網絡發現的功能,然后下面的Save & Test按鈕測試配置是否成功。
在需要收集數據的服務器上安裝Telegraf
到以下地址下載Telegraf的windows版本。
將下載所得文件解壓縮。
將Telegraf安裝為Windows服務
切換到Telegraf的存放目錄下,執行一下命令安裝至Windows 服務
telegraf.exe --service install
此時Telegraf的服務狀態還是出於停止的狀態
配置Telegraf收集相關數據
在數據庫中創建Telegraf的登錄用戶
USE master;
GO
CREATE LOGIN [telegraf] WITH PASSWORD = N'mystrongpassword';
GO
GRANT VIEW SERVER STATE TO [telegraf];
GO
GRANT VIEW ANY DEFINITION TO [telegraf];
GO
修改配置文件如下
[[outputs.influxdb]]
urls = ["http://192.168.0.207:8086"]
## The target database for metrics; will be created as needed.
database = "telegraf"
retention_policy = ""
write_consistency = "any"
## Timeout for HTTP messages.
timeout = "5s"
## HTTP Basic Auth
username = "telegraf"
password = "password"
# 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.
servers = [
"Server=127.0.0.1;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
## If you would like to exclude some of the metrics queries, list them here
## Possible choices:
## - PerformanceCounters
## - WaitStatsCategorized
## - DatabaseIO
## - DatabaseProperties
## - CPUHistory
## - DatabaseSize
## - DatabaseStats
## - MemoryClerk
## - VolumeSpace
exclude_query = [ 'DatabaseIO', 'PerformanceCounters', 'WaitStatsCategorized', 'DatabaseProperties', 'CPUHistory', 'DatabaseSize', 'DatabaseStats', 'MemoryClerk', 'VolumeSpace' ]
測試是否連接成功
telegraf.exe --config telegraf_sql.conf -test
另外可至GITHUB下載Grafana的配置文件和對應的telgraf配置文件
最終結果如下圖