如何搭建視覺化應用監控伺服器效能(ubuntu下安裝influxdb+telegraf+grafana)
看完本文,你就可以部署一個監控伺服器的視覺化應用,並且可以自己匯入資料來源進行展示。
參考:https://blog.csdn.net/TTchengcheng/article/details/79742574
參考:https://docs.influxdata.com/influxdb/v1.6/introduction/installation/
1 InfluxDB
InfluxDB是一個開源的時序資料庫,使用GO語言開發,特別適合用於處理和分析資源監控資料這種時序相關資料。
1.1 安裝
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add - source /etc/lsb-release echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
下載key的過程會較慢,可以先直接通過瀏覽器下載下來,然後使用sudo apt-key add influxdb.key
新增
1.2 啟動server
influx
InfluxDB
的server地址為http://localhost:8086
1.3 基本命令
Connected to http://localhost:8086 version 1.2.4 InfluxDB shell version: 1.2.4 # 新建資料庫 > create database mydb # 顯示所有資料庫 > show databases name: databases name ---- _internal mydb # 進入資料庫 > use mydb Using database mydb # 插入資料(下面的cpu一般稱為measurement,host和region我們稱為tags) > INSERT cpu,host=serverA,region=us_west value=0.64 # 查詢資料 > select * from cpu name: cpu time host region value ---- ---- ------ ----- 1541153053182009062 serverA us_west 0.64
從上面的操作過程中可以看到,基本語句與mysql差別不大
1.4 啟動admin後臺
修改配置檔案
vim /etc/influxdb/influxdb.conf
按照下面的內容進行修改
[admin] # Determines whether the admin service is enabled. enabled = true # 將這一句取消註釋 # The default bind address used by the admin service. # bind-address = ":8083" # Whether the admin service should use HTTPS. # https-enabled = false # The SSL certificate used when HTTPS is enabled. # https-certificate = "/etc/ssl/influxdb.pem"
訪問http://127.0.0.1:8083/
即可看到如下頁面
在Query
輸入框中輸入select * from cpu
即可看到資料
右上角可以切換db
2 telegraf
Telegraf是一個用Go編寫的代理程式,可收集系統和服務的統計資料,並寫入到多種資料庫。
官網文件
2.1 安裝
點選連結下載telegraf指定版本:下載安裝包
執行命令安裝
sudo dpkg -i telegraf_1.5.3-1_amd64.deb
2.2 修改配置檔案
修改配置檔案用於向infludb寫入資料
sudo vim /etc/telegraf/telegraf.conf
按照下面的內容修改
[[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://127.0.0.1:8086"] # 取消註釋
## The target database for metrics; will be created as needed.
database = "telegraf" # 取消註釋
現在telegraf會默默的向influxdb裡面寫入cpu、記憶體、硬碟等硬體的時序引數。
3 grafana
Grafana是一個開源的度量分析與視覺化套件。經常被用作基礎設施的時間序列資料和應用程式分析的視覺化,支援多種資料來源。
官網
3.1 安裝
安裝並啟動服務
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.0.3_amd64.deb
sudo dpkg -i grafana_5.0.3_amd64.deb
sudo service grafana-server start
訪問http://127.0.0.1:3000
即可以進入視覺化介面
3.2 配置資料來源
點選Data Sources
點選Add data source
新增資料來源
按照下圖配置資料來源的相關引數,主要是選擇資料庫型別和資料庫
儲存後,就可以開始製作面板了
3.3 面板
建立新Dashboard
作以下配置,顯示CPU的執行曲線
點選Last 6 hours
按鈕,修改refreshing every
引數為10s,面板會每十秒鐘重新整理一次資料
點選儲存即可
檢視效果(這裡使用了Zabbix外掛)