天天看點

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

1:如果同一台伺服器上安裝有多個MongoDB執行個體,telegraf.conf 中關于 MongoDB 如何配置?配置資料在【INPUT PLUGINS的[[inputs.mongodb]]】部分。

單個執行個體配置

servers = ["mongodb://UID:[email protected]:27218"]      

錯誤的多執行個體配置(例如兩個執行個體);

servers = ["mongodb://UID:[email protected]:27218"]
   servers = ["mongodb://UID:[email protected]:27213"]      

重新開機服務,檢視服務狀态,提示錯誤資訊如下;

Failed to start The plugin-driven server agent for reporting metrics into InfluxDB.      
搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

正确的配置應該為;

servers = ["mongodb://UID:[email protected]:27213","mongodb://UID:[email protected]:27218"]      

2.配置Grafana 告警規則後,發現隻是告警一次,後面恢複後再報警一次。即異常持續期間沒有一直告警。

解決辦法,這個設定其實在【Alterting】--》【Notification channels】-->【Send reminders】

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

 例如以下的設定可以了解為,每5分鐘觸發一下告警資訊。

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

3.告警檢查顯示沒有資料。

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

這個時候有兩種原因

(1)收集監控項的代理程式有問題 ;

(2)或者是代理程式沒問題,是彙報資料不及時的問題。

針對第二問題,我們可以調整代理程式執行頻率;如果實時性要求不是很高,還可以調整告警規則檢查資料的時間範圍。

例如,我們可以從檢查 過去5分鐘到過去1分鐘内的資料,調整為過去10分鐘到過去5分鐘内的資料。對應的設定如下:

調整前;

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

調整後

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

4.随着需要監控的子項的增多,收集時間必然增多,需要調整運作周期。

否則,報錯資訊如下;

telegraf[2908]: 2019-03-01T02:40:46Z E! Error in plugin [inputs.mysql]: took longer to collect than collection interval (10s)      

解決方案:調整 telegraf.conf 檔案中 [agent] 部分的interval參數。

5.InfluxDB 既有的規則不建議删除,删除後查詢寫入都報錯。

例如我們建立了如下一個規則:

CREATE RETENTION POLICY "48_hours" ON "telegraf" DURATION 48h REPLICATION 1 DEFAULT;      

檢視規則的指令:

SHOW RETENTION POLICIES;      

然後執行删除指令

drop retention POLICY "48_hours" ON "telegraf";      

 查詢資料,提示以下錯誤;

ERR: retention policy not found: 48_hours      
搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

檢視各個telegraf收集器,也開始報錯了。

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

 基礎知識

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題
字段 解釋說明
name 名稱, 此示例名稱為autogen
duration 持續時間, 0代表無限制
shardGroupDuration shardGroup的存儲時間, shardGroup是InfluxDB的一個基本存儲結構, 應該大于這個時間的資料在查詢效率上應該有所降低
replicaN 全稱是REPLICATION, 副本個數
default 是否是預設政策

解決方案;

 建立的政策為預設政策,删除後沒有了預設政策,要将一個政策設定為預設政策。

本例是将原來的autogen政策恢複為true,下面是完整的測試過程。

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

6.InfluxDB設定登入賬号後,如何auth驗證。

登入後,需要進行Auth驗證,否則指令無法正常執行,報錯如下:

ERR: unable to parse authentication credentials      

需要輸入,auth 指令,再分别輸入賬号命名即可。

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

7. Grafana 關于報警郵件的配置

(1)我們明明配置了SMTP,但是提示錯誤:

"Failed to send alert notification email" logger=alerting.notifier.email error="SMTP not configured, check your grafana.ini config file's [smtp] section"      

原因是:grafana.ini檔案中,很多行的注釋符是(;)

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

(2)與配置Linux系統的郵件服務不同,需要添加端口。(例如,添加25);否則報錯:

"Failed to send alert notification email" logger=alerting.notifier.email error="address ygmail.yiguo.com: missing port in address"      

(3)如無特别需要,請将skip_verify 設定為true。否則報錯:

"Failed to send alert notification email" logger=alerting.notifier.email error="x509: certificate is valid for XXXXXX"      

是以,grafana.ini中關于郵件部分的配置格式如下;

#################################### SMTP / Emailing ##########################
[smtp]
enabled = true
host = 郵件服務(位址):port
user = 使用者名
# If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;"""
password = XXXXXXX
;cert_file =
;key_file =
skip_verify = true
from_address = 告警郵件的位址
from_name = Grafana

[emails]
;welcome_email_on_sign_up = false      

8.我們在搭建收集log的系統時,下載下傳logstatsh,驗證報錯

驗證代碼:

bin/logstash -e 'input { stdin { } } output { stdout {} }'      
搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題
which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
could not find java; set JAVA_HOME or ensure java is in PATH      

解決方案:

yum install java      

再次驗證:

[root@QQWeiXin—0081 logstash-6.2.4]# bin/logstash -e 'input { stdin { } } output { stdout {} }'
Sending Logstash's logs to /data/logstash/logstash-6.2.4/logs which is now configured via log4j2.properties
[2018-09-23T17:29:46,228][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"fb_apache", :directory=>"/data/logstash/logstash-6.2.4/modules/fb_apache/configuration"}
[2018-09-23T17:29:46,243][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"netflow", :directory=>"/data/logstash/logstash-6.2.4/modules/netflow/configuration"}
[2018-09-23T17:29:46,335][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.queue", :path=>"/data/logstash/logstash-6.2.4/data/queue"}
[2018-09-23T17:29:46,342][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.dead_letter_queue", :path=>"/data/logstash/logstash-6.2.4/data/dead_letter_queue"}
[2018-09-23T17:29:46,661][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
[2018-09-23T17:29:46,702][INFO ][logstash.agent           ] No persistent UUID file found. Generating new UUID {:uuid=>"c9e6fd92-0171-4a2b-87e5-36b98c21db16", :path=>"/data/logstash/logstash-6.2.4/data/uuid"}
[2018-09-23T17:29:47,274][INFO ][logstash.runner          ] Starting Logstash {"logstash.version"=>"6.2.4"}
[2018-09-23T17:29:47,607][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}
[2018-09-23T17:29:49,568][INFO ][logstash.pipeline        ] Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>40, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50}
[2018-09-23T17:29:49,739][INFO ][logstash.pipeline        ] Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x718a7b78 sleep>"}
The stdin plugin is now waiting for input:
[2018-09-23T17:29:49,815][INFO ][logstash.agent           ] Pipelines running {:count=>1, :pipelines=>["main"]}


{
       "message" => "",
      "@version" => "1",
    "@timestamp" => 2018-09-23T09:30:24.535Z,
          "host" => "QQWeiXin—0081"
}
{
       "message" => "",
      "@version" => "1",
    "@timestamp" => 2018-09-23T09:30:24.969Z,
          "host" => "QQWeiXin—0081"
}

{
       "message" => "",
      "@version" => "1",
    "@timestamp" => 2018-09-23T09:30:25.189Z,
          "host" => "QQWeiXin—0081"
}      

 9.InfluxDB WHERE 子句

(1)在WHERE子句中,支援在fields, tags, and timestamps上進行條件表達式的運算。不支援使用 OR 來指定不同的time區間。

(2) 在WHERE子句中,支援對string, boolean, float 和 integer類型的field values進行比較。

在WHERE子句中,如果是string類型的field value,一定要用單引号括起來。如果不适用引号括起來,或者使用的是雙引号,将不會傳回任何資料,有時甚至都不報錯!

10.grafana 啟動域賬号登入

Step 1 修改 grafana.ini 配置檔案,啟動調整以下參數

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

Step 2 重新開機下 grafana-server.service 服務

systemctl stop grafana-server.service
systemctl start grafana-server.service
systemctl status grafana-server.service      

如果沒有 ldap.toml 檔案,重新開機會生成

step 3 修改ldap.toml檔案

[[servers]]
host = "xxx.xxx.xxx.xxx"
start_tls = true
ssl_skip_verify = true
bind_dn = "CORP\\%s" #bind_dn = "CORP\\testuser" 雙斜杠一定要有,%s使用具體的AD賬戶
bind_password = 'xxxxxxx'
search_filter = "(sAMAccountName=%s)"
search_base_dns = ["dc=corp,dc=local"]
bind_password = '********'

[servers.attributes]
name = "givenName"
surname = "sn"
username = "XXXXX"
member_of = "memberOf"
email = "mail"


[[servers.group_mappings]]
group_dn = "cn=admins,dc=grafana,dc=org"
org_role = "Admin"


[[servers.group_mappings]]
group_dn = "cn=users,dc=grafana,dc=org"
org_role = "Editor"      

step 4 重新開機下 grafana-server.service 服務

11:監控MongoDB 副本集節點,telegraf賬号的權限設定

設定不當,報錯如下:

Jan 21 17:06:00 XXXX-mongodb02 telegraf[77945]: 2019-01-21T09:06:00Z E! Error in plugin [inputs.mongodb]: not authorized on admin to execute command { serverStatus: 1, recordStats: 0, $readPreference: { mode: "secondaryPreferred" }, $db: "admin" }
Jan 21 17:06:00 XXXX-mongodb02 telegraf[77945]: 2019-01-21T09:06:00Z E! Error in plugin [inputs.mongodb]: not authorized on admin to execute command { serverStatus: 1, recordStats: 0, $readPreference: { mode: "secondaryPreferred" }, $db: "admin" }      

解決方案:

db.createUser({user:"uid_XXXXX", pwd:"PWD_??????",roles: [ {role: "clusterMonitor",db: "admin"}] })      
搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

12.Grafana 圖形最大值、最小值、平均值的設定

在編輯-->Legend 處設定,要記得儲存。

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

設定後的顯示

搭建 Telegraf + InfluxDB + Grafana 監控遇到幾個小問題

13 CPU的IOWait 名額不準确的問題 

IOWait 是Sever 性能的一個關鍵名額,可以用來參考磁盤IO性能、是否有僵屍程序等。

但有的同學配置的時候,會發現grafana 顯示的iowait 與真實值(或者Zabbix)比較放大了好幾倍。

原因是檢視Telegraf 收集到的CPU資料需要指定cpu 的資料類型。

轉換成SQL來看,有問題的SQL如下:

select last("usage_iowait") From "cpu" WHERE ("host" =~/^$host$/ ) and $timeFilter GROUP BY time(10s)fill(null)      

調整後的SQL

select last("usage_iowait") From "cpu" WHERE ("host" =~/^$host$/  AND "cpu" ='cpu-total') and $timeFilter GROUP BY time(10s)fill(null)      

調整後,顯示的資料OK了。

即,顯示IOWait資料,在條件中一定要指定  "cpu" ='cpu-total'  。

參考資料:

https://grafana.com/docs/grafana/latest/auth/ldap/

繼續閱讀