快速搭建grafana+influxdb+telegraf,以及常見問題

梅芬發表於2019-01-11

1.Install

1.1. influxdb

sudo yum install influxdb
sudo systemctl start influxdb
複製程式碼

判斷已經安裝完成,輸入下面的命令,可以進入influxDB的介面

> influx
複製程式碼

1.2. Telegraf 修改版本

wget https://dl.influxdata.com/telegraf/releases/telegraf-1.9.1_linux_amd64.tar.gz
        tar xf telegraf-1.9.1_linux_amd64.tar.gz
telegraf -version
複製程式碼

1.2.3 啟動服務

config: /etc/telegraf/telegraf.conf

sudo systemctl start telegraf
sudo systemctl status telegraf 
sudo systemctl enable telegraf
複製程式碼

1.3. Grafana 修改版本

1.3.1 安裝grafana

wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.1.2-1.x86_64.rpm 
複製程式碼

1.3.2 啟動服務、新增開機啟動:

systemctl enable grafana-server
systemctl start grafana-server
複製程式碼

1.3.3 配置說明

# 配置檔案 /etc/grafana/grafana.ini
# systemd服務名 grafana-server.service
# 預設日誌檔案 /var/log/grafana/grafana.log
# 預設資料庫檔案 /var/lib/grafana/grafana.db
複製程式碼

1.3.4 add plugin

新增外掛

sudo grafana-cli plugins install grafana-clock-panel
sudo systemctl restart grafana-server
複製程式碼

2. grafana 需要關注的幾個屬性

2.1 Metric

For Mode there are three options:

  1. The default option is Time and means the x-axis represents time and that the data is grouped by time (for example, by hour or by minute).

  2. The Series option means that the data is grouped by series and not by time. The y-axis still represents the value.

  3. The Histogram option converts the graph into a histogram. A Histogram is a kind of bar chart that groups numbers into ranges, often called buckets or bins.

2.2 Variables

Variables are shown as dropdown select boxes at the top of the dashboard. These dropdowns make it easy to change the data being displayed in your dashboard.

2.2.1 時間間隔

  1. 選擇 New 按鈕新建一個模板變數
  2. 選擇 Interval 變數型別,我們可以用這種變數表達時間間隔,同時設定 Name 和 Label,Name 是變數名稱,實際引用的時候用$變數名稱進行引用;Label 本身無實際作用,主要是用來展示在介面,讓使用者更加容易理解的。
  3. 可以看到在 Values 中,已經有大量預置的時間間隔,我們可以在其中增加,諸如 1m(1分鐘),1h(1小時),1d(1天)等時間間隔變數 在介面,我們可以見到已經生成了名為時間間隔的下拉框列表,列表中包括了我們設定的時間間隔預設值
  4. 將時序查詢的 interval 設定為 $t (t 為我們設定的變數Name)。此時在下拉框裡選擇不同的時間間隔,圖表將隨之進行切換。

2.2.2 基於查詢結果的下拉選單

前置步驟請參考時間間隔變數設定

1. 選擇Query 型別
2. Data source 選擇你查詢的目標資料來源
3. Query 是查詢所有可能值的查詢語句,ES/Logdb 的查詢方式是{"find": "terms", "field": "status"},其中status是我們查詢的目標欄位,在這裡可以替換成你需要的欄位。更深入的語法請參考 ES 官方文件。
4. Regex 可以選擇對於返回的狀態值進行正規表示式過濾
5. Sort 選擇排序方式
6. Multi-value 控制下拉框是否可以支援多選,如果不選中則只能單選
7. Include all value 8. 控制是否可以支援All選項,支援全選所有的值,只在多選的模式下有效果
Preview of values 可以預覽這個欄位的所有值
複製程式碼

nodename為例子

  1. set Custom Variables ,name as nodename
  2. add the parameter var-nodename=“test”,such as
Use  Url http://servername:3000/dashboard/db/dashboard?refresh=10s&var-nodename=“test複製程式碼

  1. In Query: use where clause as shown below:
WHERE nodename =$nodename
複製程式碼

you can see the output

select * 
from table
where nodename ="test"
複製程式碼

4. 常見問題

4.1 how to get the parameter from the url

for example, nodename

  1. set Custom Variables ,name as nodename
  2. add the parameter var-nodename=“test”,such as
Use  Url http://servername:3000/dashboard/db/dashboard?refresh=10s&var-nodename=“test複製程式碼

  1. In Query: use where clause as shown below:
WHERE nodename =$nodename
複製程式碼

you can see the output

select * 
from table
where nodename ="test"
複製程式碼

4.2 nginx 反向代理到 grafana

grafana配置nginx反向代理

將grafana配到www.myserver.com域名的/grafana/的location下

  1. nginx配置
location /grafana/ {
                proxy_pass http://grafana_server:3000/;
                proxy_set_header   Host $host;
        }
複製程式碼
  1. grafana配置檔案修改
#在/etc/grafana/grafana.ini配置檔案中修改
domain = www.myserver.com
root_url = %(protocol)s://%(domain)s/grafana
複製程式碼

4.3 provisioning

初始化匯入皮膚,具體的操作流程如下

  1. edit the config grafana.ini
# folder that contains provisioning config files that grafana will apply on startup and while running.
;provisioning = conf/provisioning
複製程式碼
  1. add the dashborads.yaml and db.yaml file, location in /etc/grafana/provisioning/dashborads and /etc/grafana/provisioning/databases

5. influxDB+telegraf

5.1 監聽多臺服務

  1. 在需要監控的機器上面安裝對應的telegraf
  2. 並且配置上報的influxdb的機器和資料庫
#Configuration for influxdb server to send metrics to 

[[outputs.influxdb]] 

urls = [“http://1x.xxx:8086”] #influxdb地址 

database = “telegraf_ali” # required #influxdb資料庫 

retention_policy = “”#資料保留策略 

write_consistency = “any” #資料寫入策略,僅適用於叢集模式 

timeout = “5s” #寫入超時策略 

username = “telegraf_ali” #資料庫使用者名稱 

password = “gPHhbeh” #資料庫密碼 

#user_agent = “telegraf” #採集器代理名稱
複製程式碼

5.1.1 TRY

  1. A機器部署influxdb+telegraf
> influx
> use telegraf;
> SHOW TAG VALUES FROM system WITH KEY=host
# 可以看到一臺主機的資訊
複製程式碼
  1. B機器部署telegraf
  2. 在B機器,修改telegraf influxdb地址,使用預設telegraf
  3. 重啟機器B 的telegraf機器 進入A機器
> influx
> use telegraf;
> SHOW TAG VALUES FROM system WITH KEY=host

複製程式碼
  • 上面查詢主機的資訊1條--> 兩條主機資訊,說明操作成功

image

POLICY

> CREATE RETENTION POLICY "2h0m0s" ON "telegraf" DURATION 2h REPLICATION 1 DEFAULT
> SHOW RETENTION POLICIES ON telegraf
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        false
2h0m0s  2h0m0s   1h0m0s             1        true
複製程式碼
SELECT time,host,usage_system FROM "autogen".cpu limit 2
name: cpu
time                host             usage_system
----                ----             ------------
1526008670000000000 VM_42_233_centos 1.7262947210419817
1526008670000000000 VM_42_233_centos 1.30130130130254
複製程式碼
SELECT 100 - usage_idel FROM "autogen"."cpu" WHERE time > now() - 1m and "cpu"='cpu0'
複製程式碼

5.2 COMMAND

SHOW MEASUREMENTS  --查詢當前資料庫中含有的表
SHOW FIELD KEYS --檢視當前資料庫所有表的欄位
SHOW series from pay --檢視key資料
SHOW TAG KEYS FROM "pay" --檢視key中tag key值
SHOW TAG VALUES FROM "pay" WITH KEY = "merId" --檢視key中tag 指定key值對應的值
SHOW TAG VALUES FROM cpu WITH KEY IN ("region", "host") WHERE service = 'redis'
DROP SERIES FROM <measurement_name[,measurement_name]> WHERE <tag_key>='<tag_value>' --刪除key
SHOW CONTINUOUS QUERIES   --檢視連續執行命令
SHOW QUERIES  --檢視最後執行命令
KILL QUERY <qid> --結束命令
SHOW RETENTION POLICIES ON mydb  --檢視保留資料
查詢資料
SELECT * FROM /.*/ LIMIT 1  --查詢當前資料庫下所有表的第一行記錄
select * from pay  order by time desc limit 2
select * from  db_name."POLICIES name".measurement_name --指定查詢資料庫下資料保留中的表資料 POLICIES name資料保留
刪除資料
delete from "query" --刪除表所有資料,則表就不存在了
drop MEASUREMENT "query"   --刪除表(注意會把資料保留刪除使用delete不會)
DELETE FROM cpu
DELETE FROM cpu WHERE time < '2000-01-01T00:00:00Z'
DELETE WHERE time < '2000-01-01T00:00:00Z'
DROP DATABASE “testDB” --刪除資料庫
DROP RETENTION POLICY "dbbak" ON mydb --刪除保留資料為dbbak資料
DROP SERIES from pay where tag_key='' --刪除key中的tag

SHOW SHARDS  --檢視資料儲存檔案
DROP SHARD 1
SHOW SHARD GROUPS
SHOW SUBSCRIPTIONS

複製程式碼

5.3 grafana tools

- [Puppet](https://forge.puppet.com/puppet/grafana)
- [Ansible](https://github.com/cloudalchemy/ansible-grafana)
- [Chef](https://github.com/JonathanTron/chef-grafana)
- [Saltstack](https://github.com/salt-formulas/salt-formula-grafana)
- [Jsonnet](https://github.com/grafana/grafonnet-lib/)
- [quick install](https://github.com/samuelebistoletti/docker-statsd-influxdb-grafana)

複製程式碼

5.3.1 quick install

Centos 7 docker 啟動grafana容器報"iptables No chain/target/match by that name"

docker run -d -p 3000:3000  grafana/grafana:5.1.0  
Error response from daemon: Cannot start container 565c06efde6cd4411e2596ef3d726817c58dd777bc5fd13762e0c34d86076b9e: iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3888 -j DNAT --to-destination 192.168.42.11:3888 ! -i docker0: iptables: No chain/target/match by that name
複製程式碼

5.3.2 解決方法:

vim /etc/sysconfig/iptables

*nat
:PREROUTING ACCEPT [27:11935]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [598:57368]
:POSTROUTING ACCEPT [591:57092]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
COMMIT
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:DOCKER - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s 10.0.0.0/8 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
-A INPUT -s 172.16.0.0/12 -j ACCEPT
-A INPUT -s 192.168.0.0/16 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3000 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 36091 -j ACCEPT
-A INPUT -j DROP
-A FORWARD -j DROP
-A OUTPUT -j ACCEPT
COMMIT
複製程式碼

6. remove

6.1 remove influxdb

解除安裝命令:

[root@localhost shared]# rpm -q influxdb 
influxdb-0.8.7-1.x86_64 
[root@localhost shared]# rpm -e influxdb 
[root@localhost shared]# rpm -q influxdb 
package influxdb is not installed
複製程式碼

引數說明:

首先通過 rpm -q <關鍵字> 可以查詢到rpm包的名字 然後 呼叫 rpm -e <包的名字> 刪除特定rpm包 如果遇到依賴,無法刪除,使用 rpm -e --nodeps <包的名字> 不檢查依賴,直接刪除rpm包 如果恰好有多個包叫同樣的名字,使用 rpm -e --allmatches --nodeps <包的名字> 刪除所有相同名字的包, 並忽略依賴 刪除完後,清除已有檔案:

[root@localhost opt]# ls 
influxdb 
[root@localhost opt]# rm -rf influxdb 
[root@localhost opt]# ls
複製程式碼

處理埠占用

name=$(lsof -i:8086|tail -1|awk '"$1"!=""{print $2}')
if [ -z $name ]
then
	echo "No process can be used to killed!"
	exit 0
fi
id=$(lsof -i:8086|tail -1|awk '"$1"!=""{print $2}')
kill -9 $id
 
echo "Process name=$name($id) kill!"
exit 0
複製程式碼

6.2 remove grafana

移除命令

sudo yum remove grafana
複製程式碼

6.3 remove telegraf

sudo yum remove telegraf
複製程式碼

相關文章