目的
- 理解influxDB的數據收集原理和方法
- 爲使用grafana分析數據及展示結作好準備
介紹
- [收集數據] Telegraf 是一個用 Go 編寫的代理程序,可收集系統和服務的統計數據,並寫入到 InfluxDB 數據庫。Telegraf 具有內存佔用小的特點,通過插件系統開發人員可輕鬆添加支持其他服務的擴展。
- [存儲數據] InfluxDB 是 Go 語言開發的一個開源分佈式時序數據庫,非常適合存儲指標、事件、分析等數據
- [展示數據] Grafana 是純 Javascript 開發的前端工具,用於訪問InfluxDB,自定義報表、顯示圖表等。
telegraf安裝
- 下載
- 安裝
- yum localinstall telegraf-0.11.1-1.x86_64.rpm -y
- 啓動服務、添加開機啓動
- systemctl start telegraf.service
- service telegraf status
- systemctl enable telegraf.service
- 查看版本
- telegraf --version
- Telegraf - Version 0.11.1
- 配置
- 路徑:/etc/telegraf/telegraf.conf
- 示例:安裝完成後會有一個示列配置文件,請根據所需仔細閱讀文件。
influxDB安裝
-
安裝部署,添加yum 源
cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo [influxdb] name = InfluxDB Repository - RHEL \$releasever baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable enabled = 1 gpgcheck = 1 gpgkey = https://repos.influxdata.com/influxdb.key EOF
-
安裝: yum install influxdb -y
-
啓動服務、添加開機啓動
service influxdb start systemctl enable influxdb service influxdb status
-
服務默認使用端口:
Networking By default, InfluxDB uses the following network ports: TCP port 8083 is used for InfluxDB’s Admin panel TCP port 8086 is used for client-server communication over InfluxDB’s HTTP API TCP ports 8088 and 8091 are required for clustered InfluxDB instances
-
服務驗證 -輸入 influx 進入數據庫
[root@ctn-7-12 ~]# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 0.11.0
InfluxDB shell 0.11.0
- 創建一個查詢用戶
CREATE USER "ptquery" WITH PASSWORD 'ptquery'
> show users;
user admin
ptquery false
ptdb1 fals
7.也可以在頁面創建查詢用戶 CREATE USER "ptquery" WITH PASSWORD 'ptquery'
- 查看服務端口
[root@ctn-7-12 ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::8083 :::* LISTEN 22124/influxd
tcp6 0 0 :::8086 :::* LISTEN 22124/influxd
- 瀏覽器訪問數據庫管理平臺:
- 參考信息:
grafana安裝
- 手動安裝:
wget https://grafanarel.s3.amazonaws.com/builds/grafana-latest-1.x86_64.rpm
yum install grafana-latest-1.x86_64.rpm
- 安裝包詳情
二進制文件 /usr/sbin/grafana-server
啓動腳本 /etc/init.d/grafana-server
環境變量 /etc/sysconfig/grafana-server
配置文件 /etc/grafana/grafana.ini
systemd服務 grafana-server.service
日誌 /var/log/grafana/grafana.log
- 服務詳情
啓動用戶 grafana
服務名稱 grafana-server
默認端口 3000
賬號 admin
密碼 admin
- 啓動服務、添加開機啓動
systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
systemctl enable grafana-server.service
- 訪問
- http://server IP :3000
將數據寫入influxDB
influxDB line寫入協議介紹
- 格式: [measurements],[tag]=[tagvalue]空格 [field1]=[field1value],[field1]=[field1value]
- 說明:
- 命名規則:measurements、tag、field的命名不能含空間,逗號,如必須有,需有\轉義;
- 行格式有嚴格的要求,“=”左右及","分隔不能有空格
- measurements,從統計學角度上看,這叫做樣本集。相當於關係數據庫的Table
- tag,樣本集中個體的標識符,相當於關係數據庫的primary key,
- filed,個體屬性的度量值,可以爲整型,浮點型,字符型,布爾型 ,相當於關係數據庫的一般字段
- 根據上面的寫入協議,編寫sh腳本,然後通過telegraf調度執行,就可以把數據寫入到influxDB,具體步驟如下:
- 編寫sh腳本,例子如下(bash shell腳本也可以調用python腳本,只要滿足line寫入協議輸出即可):
#! /bin/env bash echo 'employee,empname=kk age=20,salary=3000'
- 修改telegraf的配置文件/etc/telegraf/telegraf.conf,具體如下:
[[outputs.influxdb]] urls = ["http://192.168.18.118:8086"] #infulxdb地址 database = "telegraf" #數據庫 precision = "s" timeout = "5s" username = "admin" #帳號 password = "admin" #密碼 [[inputs.exec]] # Shell/commands array commands = ["/tmp/qq.sh"] # Data format to consume. This can be "json", "influx" or "graphite" (line-protocol) # NOTE json only reads numerical measurements, strings and booleans are ignored. data_format = "influx" interval = "60s" #調度間隔 timeout = "15s" #超時控制
- 檢驗數據,登錄到http://192.168.18.118:8086 數據已經被寫入到influxDB中
- 編寫sh腳本,例子如下(bash shell腳本也可以調用python腳本,只要滿足line寫入協議輸出即可):