Kapacitor學習

目錄

TICK技術棧

TICK流程

安裝Kapacitor

啓動Kapacitor

docker運行kapacitor

kapacitor報錯

 原因

kapacitor客戶端連遠程

根據tick腳本定義task

從流數據觸發告警

查看當前的告警任務列表

查看告警任務詳情

錯誤排查

檢查日誌

任務測試通過後啓用該任務

TICK

tick語法

單引號和雙引號的區別



TICK技術棧

包括Telegraf, InfluxDB, Chronograf, Kapacitor

TICK流程

1.安裝influxdb和telefraf

2.啓動influxdb,telegraf,並從telegraf發送數據到Influxdb

telegraf一旦安裝成功並啓動好,就會按照默認配置信息,發送系統指標到influxdb,會自動創建"telegraf"數據庫。

3.安裝kapacitor

4.啓動kapacitor

5.定義並運行流任務觸發cpu告警

6.定義並運行批處理任務觸發cpu告警 

telegraf配置文件:/etc/telegraf/telegraf.conf

 

[agent].interval   // 聲明瞭發送系統指標到influxdb的頻率
[[outputs.influxd]]  // 聲明瞭怎麼連接到influxdb以及目標數據庫,默認是telegraf數據庫
[[inputs.cpu]]  // 聲明瞭怎樣採集系統cpu指標

// telegraf配置文件例子
[agent]
  ## Default data collection interval for all inputs
  interval = "10s"

...
[[outputs.influxdb]]
  ## The HTTP or UDP URL for your InfluxDB instance.  Each item should be
  ## of the form:
  ##   scheme "://" host [ ":" port]
  ##
  ## Multiple urls can be specified as part of the same cluster,
  ## this means that only ONE of the urls will be written to each interval.
  # urls = ["udp://localhost:8089"] # UDP endpoint example
  urls = ["http://localhost:8086"] # required
  ## The target database for metrics (telegraf will create it if not exists).
  database = "telegraf" # required
...
[[inputs.cpu]]
  ## Whether to report per-cpu stats or not
  percpu = true
  ## Whether to report total system cpu stats or not
  totalcpu = true
  ## If true, collect raw CPU time metrics.
  collect_cpu_time = false

安裝Kapacitor

wget https://dl.influxdata.com/kapacitor/releases/kapacitor-1.5.0.x86_64.rpm

啓動Kapacitor

sudo systemctl start kapacitor

當Kapacitor啓動時就會發現influxdb運行在http://localhost:8086,會在influxdb上創建一些訂閱信息,這些訂閱信息告訴influxdb,influxdb把收集到的所有數據發送給Kapacitor還是第三方服務。

檢查Kapacitor服務狀態

sudo systemctl status kapacitor

docker運行kapacitor

事先準備好kapacitor.conf

docker run -d --name kapacitor --network host --restart always -v /root/kapacitor:/etc/kapacitor:ro -v /var/lib/cloudtogo/data/kapacitor:/var/lib/kapacitor:rw registry.local/cloudtogo.cn/official/kapacitor:1.5.3

kapacitor報錯

[run] 2020/03/17 08:48:59 E! create server: host cannot be empty. To generate a valid configuration file run `kapacitord config > kapacitor.generated.conf`.
run: create server: host cannot be empty. To generate a valid configuration file run `kapacitord config > kapacitor.generated.conf`.

 原因

influxdb沒有使用host網絡模式,而是默認的bridge。kapacitor.conf裏面的[[influxdb]] urls使用的是默認的配置localhost,即

[[influxdb]]
  enabled = true
  name = "default"
  default = false
  urls = ["http://localhost:8086"]
  username = ""
  password = ""
  ssl-ca = ""
  ssl-cert = ""
  ssl-key = ""

當前kapacitor容器裏面肯定是沒有influxdb的,由於啓用了influxdb,即enabled=true,所以kapacitor會去連接默認的localhost:8086,導致連不通。

 

kapacitor客戶端連遠程

kapacitor -url http://10.10.13.5:9092 list tasks

kapacitor -url http://10.10.13.5:9092 define k8s-batch-pod -tick batch-pod-alert.tick -dbrp "telegraf"."autogen"

根據tick腳本定義task

kapacitor define cpu_alert -type stream -tick /root/cpu_alert.tick -dbrp telegraf.autogen

Kapacitor默認的日誌目錄:/var/log/kapacitor

從流數據觸發告警

Kapacitor的處理原理:

Kapaciror中的一個task代表要對一組數據執行的工作量。有兩種類型的task:stream和batch。

Kapacitor使用DSL,即TICKscript來定義task,每個TICKscript都定義了一個管道(pipeline),告訴Kapacitor哪些數據要處理以及怎樣被處理。

Kapacitor最常見的是被用作觸發告警。

以下例子是設置高cpu使用率的告警任務,telegraf根據cpu在空閒狀態下花費的比例向influxdb寫入cpu指標,當使用率小於70%,觸發警告。

dbrp "telegraf"."autogen"

stream
    // Select just the cpu measurement from our example database.
    |from()
        .measurement('cpu')
    |alert()
        .crit(lambda: int("usage_idle") <  70)
        // Whenever we get an alert write it to a file.
        .log('/tmp/alerts.log')

Kapacitor提供http api接口,Kapacitor客戶端通過命令行暴露這些api接口。

// 使用之前定義的TICKscript腳本定義高cpu使用率告警任務
kapacitor define cpu_alert -tick cpu_alert.tick

kapacitor不僅可以定義task,還可以指定database,以及保留策略
當Kapacitor版本從1.4開始,可以使用TICKscript中的可選項聲明數據庫和保留策略,如dbrp "telegraf"."autogen"。
如果沒有在TICKscript腳本中聲明,則必須使用在kapacitor命令後使用 -dbrp <數據庫名>.<保留策略>

查看當前的告警任務列表

kapacitor list tasks

查看告警任務詳情

// kapacitor show 任務名稱/ID
kapacitor show cpu_alert

在啓用告警任務之前,應該對該任務進行測試,以確保它不會向日志文件或通信通道發送帶有警報的垃圾郵件。

// 使用record記錄當前數據流,測試當前任務
kapacitor record stream -task cpu_alert -duration 60s

錯誤排查

如果命令行報錯,如getsockopt: connection refused(linux),onnectex: No connection could be made...(windows),請先確保Kapacitor服務已經啓動。

如果Kapacitor服務已經啓動但上述錯誤仍存在,則檢查本機的防火牆設置,確保9092端口可用(因爲kapacitor默認的端口即9092)。

也可以檢查/var/log/kapacitor/kapacitor.log日誌文件查看錯誤詳情。

如果Kapacitor沒有收到數據,依次檢查:telegraf, influxdb, kapacitor。

如果telegraf連接不上influxdb,telegraf會記錄下來;

如果influxdb無法發送數據到kapacitor,Influxdb會記錄:connection refused。

使用 SHOW SUBSCRIPTIONS,檢查influxdb將數據發送到Kapacitor的端點。

使用reply指令將數據回放到指定的task進行隔離測試

kapacitor replay -recording $rid -task cpu_alert

由於數據已經被記錄,就能夠很快被執行,而不必等着實時執行。

使用-real-clock標記,數據就會等到指定的時間來執行。無論是否使用-real-clock,執行結果都是一致的

檢查日誌

sudo cat /tmp/alerts.log

任務測試通過後啓用該任務

kapacitor enable cpu_alert

TICK

tick語法

https://docs.influxdata.com/kapacitor/v1.5/tick/syntax/#influxql-in-tickscript

tick腳本使用lambda表達式,語法

All field or tag identifiers must be double quoted.
The comparison operator for equality is == not =.
https://docs.influxdata.com/kapacitor/v1.5/tick/syntax
https://docs.influxdata.com/kapacitor/v1.5/tick/expr/#stateless-functions

單引號和雙引號的區別

https://docs.influxdata.com/kapacitor/v1.5/introduction/getting-started/#gotcha-single-versus-double-quotes

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章