採集器categraf採用golang編寫,所以只要支持golang的os,理論上都可以運行。

當前categraf使用golang 1.21版本編譯。

  • Windows: Windows10 及以上版本, Windows Server2008以上版本
  • Linux: 內核2.6.32以上的OS
  • macOS: 10.15及以上版本

文檔:https://flashcat.cloud/docs/content/flashcat-monitor/categraf/2-installation/

目錄:

  • 夜鶯集羣部署之序幕篇
  • 夜鶯集羣部署之mysql主從篇
  • 夜鶯集羣部署之redis主從篇
  • 夜鶯集羣部署之後端時序數據庫VictoriaMetrics篇
  • 夜鶯集羣部署之Nightingale篇
  • 夜鶯集羣部署之categraf篇

安裝

  • 國內下載地址:https://flashcat.cloud/download/categraf/?version=v0.4.3    
  • 國外訪問github.com下載。

在目標機器部署,只需要 categraf 二進制、 conf 目錄。

conf 下有一個主配置文件:

  1. config.toml,定義機器名、全局採集頻率、全局附加標籤、remote write backend地址等;
  2. 另外就是各種採集插件的配置目錄,以input.打頭,categraf 會遍歷這些文件下的插件配置執行採集任務,如果某個採集器 xx 不想啓用,把 input.xx 改個其他前綴,比如 bak.input.xx,categraf 就會忽略這個採集器。


配置連接n9e服務端

配置文件介紹:https://flashcat.cloud/docs/content/flashcat-monitor/categraf/3-configuration/

[root@Rocky3 conf]# cat config.toml 
[global]
# whether print configs
print_configs = false

# add label(agent_hostname) to series
# "" -> auto detect hostname
# "xx" -> use specified string xx
# "$hostname" -> auto detect hostname
# "$ip" -> auto detect ip
# "$sn" -> auto detect bios serial number
# "$hostname-$ip" -> auto detect hostname and ip to replace the vars
hostname = "$ip"

# will not add label(agent_hostname) if true
omit_hostname = false

# global collect interval, unit: second
# 採集頻率,默認15s採集一次
interval = 15

# input provider settings; optional: local / http
providers = ["local"]

# The concurrency setting controls the number of concurrent tasks spawned for each input. 
# By default, it is set to runtime.NumCPU() * 10. This setting is particularly useful when dealing
# with configurations that involve extensive instances of input like ping, net_response, or http_response.
# As multiple goroutines run simultaneously, the "ResponseTime" metric might appear larger than expected. 
# However, utilizing the concurrency setting can help mitigate this issue and optimize the response time.
concurrency = -1

# Setting http.ignore_global_labels = true if disabled report custom labels
[global.labels]
region = "dongguan"
# env = "localhost"
# sn = "$sn"

[log]
# file_name is the file to write logs to
file_name = "stdout"

# options below will not be work when file_name is stdout or stderr
# max_size is the maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes.
# 如果是寫入文件,最大寫入大小,單位是MB
max_size = 100
# max_age is the maximum number of days to retain old log files based on the timestamp encoded in their filename.  
# 保留多少天的日誌文件
max_age = 1
# max_backups is the maximum number of old log files to retain.  
# 保留多少個日誌文件
max_backups = 1
# local_time determines if the time used for formatting the timestamps in backup files is the computer's local time.  
# 是否使用本地時間
local_time = true
# Compress determines if the rotated log files should be compressed using gzip. 
# 是否將老文件壓縮(gzip格式)
compress = false

[writer_opt]
# 發給後端的時序數據,會先被扔到 categraf 內存隊列裏,每個採集插件一個隊列
# chan_size 定義了隊列最大長度
# batch 是每次從隊列中取多少條,發送給後端backend
batch = 1000
chan_size = 1000000

[[writers]]
# n9e 服務端地址,訪問用户和密碼
url = "http://192.168.68.131:17001/prometheus/v1/write"

## Optional TLS Config
# tls_min_version = "1.2"
# tls_ca = "/etc/categraf/ca.pem"
# tls_cert = "/etc/categraf/cert.pem"
# tls_key = "/etc/categraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = true

# Basic auth username
basic_auth_user = "api01"

# Basic auth password
basic_auth_pass = "api01"

## Optional headers
# headers = ["X-From", "categraf", "X-Xyz", "abc"]

# timeout settings, unit: ms
timeout = 5000
dial_timeout = 2500
max_idle_conns_per_host = 100

[http]
enable = false
address = ":9100"
print_access = false
run_mode = "release"
ignore_hostname = false
agent_host_tag = ""
ignore_global_labels = false

[ibex]
# 是否啓用告警自愈agent
enable = true
## ibex flush interval
interval = "1000ms"
## n9e ibex server rpc address 不是每個categraf都要打開遠程執行腳本,按需打開。
servers = ["192.168.68.131:20091"]
## temp script dir
meta_dir = "./meta"

[heartbeat]
# 心跳上報(附帶資源信息,對象列表中使用),適用於夜鶯v6+
# 如果是v5版本,這裏不需要保留
enable = true

# report os version cpu.util mem.util metadata
url = "http://192.168.68.131:17001/v1/n9e/heartbeat"

# interval, unit: s
interval = 10

# Basic auth username 賬號密碼寫和上面推送的用户密碼一樣
basic_auth_user = "api01"

# Basic auth password
basic_auth_pass = "api01"

## Optional headers
# headers = ["X-From", "categraf", "X-Xyz", "abc"]

# timeout settings, unit: ms
timeout = 5000
dial_timeout = 2500
max_idle_conns_per_host = 100

[prometheus]
enable = false
scrape_config_file = "/path/to/in_cluster_scrape.yaml"
## log level, debug warn info error
log_level = "debug"
## wal file storage path ,default ./data-agent
# wal_storage_path = "/path/to/storage"
## wal reserve time duration, default value is 2 hour
# wal_min_duration = 2

測試

我們經常會需要測試某個採集器的行為,臨時看一下這個採集器輸出哪些監控指標,比如配置好了 conf/input.mysql/mysql.toml 想要看看採集了哪些 mysql 指標,可以執行命令:./categraf --test --inputs mysql

查看多個插件的輸出,可以執行命令:./categraf --test --inputs mysql:cpu:mem, 注意使用冒號來分割多個插件。

這個命令會去連接你配置的 mysql 實例,執行SQL收集輸出,將輸出的內容做格式轉換,最終打印到 stdout,如果我們在 stdout 正常看到了 mysql 相關監控指標,則説明一切正常,否則就是哪裏出了問題,大概率是 conf/input.mysql/mysql.toml 配置的有問題。


做成systemd,啓動服務

命令有參數可以自動生成systemd文件

# 以service方式安裝, 相當於添加service文件+systemctl daemon-reload,然後自己在service文件自己加一些限制內存和cpu參數。
sudo ./categraf  --install
# 以service方式卸載, 相當於systemctl stop categraf + 刪除service文件
sudo ./categraf  --remove

# 以service方式啓動categraf ,相當於systemctl start categraf
# 如果之前有nohup啓動的categraf進程,需要先人工停掉原來的categraf進程
sudo ./categraf  --start

# 以service方式停止categraf,相當於systemctl stop categraf
sudo ./categraf  --stop

# 以service方式查看categraf,相當於systemctl status categraf
sudo ./categraf  --status

配置參數

categraf啓動時 可以通過-configs參數指定配置目錄,如果不指定,會默認讀取工作目錄下的 conf。

conf 目錄結構如下:

  • config.toml # 主配置文件
  • logs.toml # logs-agent 配置文件
  • prometheus.toml # prometheus agent 配置文件
  • traces.yaml # trace-agent 配置
  • conf/input.*/*.toml # 各個插件的配置文件

interval參數

每個插件的配置中,一開始通常都是 interval 配置,表示採集頻率,如果這個配置註釋掉了,就會複用 config.toml 中的採集頻率,這個配置如果配置成數字,單位就是秒,如果配置成字符串,就要給出單位,比如:

interval = 60
interval = "60s"
interval = "1m"

上面三種寫法,都表示採集頻率是1分鐘,如果是使用字符串,可以使用的單位有:

  • 秒:s
  • 分鐘:m
  • 小時:h

注意:每個插件也只支持一個interval 配置, 如果插件目錄中的 toml 文件拆分成了多個,每個文件又包含一個 interval 配置,這個時候以第一個加載的文件內的 interval 為準(默認是字典序第一個文件)。 所以一般建議是不配置插件(input)級別的 interval (你很熟悉這個特性除外),推薦直接配置 interval_times 這個配置項,説明如下:

interval_times參數

instances 下面如果有 interval_times 配置項,則採集頻率計算如下:interval = global.interval * interval_times,即以 interval 的倍數來表示計算頻率。

比如針對 ping 監控,有些目標地址採集頻率是 15 秒,有些可能想採集的別太頻繁,設置為 30 秒,那就可以把 interval 配置成15,把不需要頻繁採集的那些 instances 的interval_times 配置成 2。

或者:把interval配置成5,需要15秒採集一次的那些 instances 的 interval_times 配置成 3,需要30秒採集一次的那些 instances 的 interval_times 配置成 6

instances參數

很多采集插件的配置中,都有 instances 配置段,用 [[]] 包住,説明是數組,即,可以出現多個 [[instances]] 配置段,比如 ping 監控的採集插件,想對 4 個 IP 做 ping 探測,可以按照下面的方式來配置:

[[instances]]
targets = [
    "www.baidu.com",
    "127.0.0.1",
    "10.4.5.6",
    "10.4.5.7"
]

也可以下面這樣子配置:

[[instances]]
targets = [
    "www.baidu.com",
    "127.0.0.1"
]

[[instances]]
targets = [
    "10.4.5.6",
    "10.4.5.7"
]

instances.labels參數

instances 下面的 labels 和 config.toml 中的 global.labels 的作用類似,只是生效範圍不同,都是為時序數據附加標籤,instances 下面的 labels 是附到對應的實例上,global.labels 是附到所有時序數據上。

關於 TLS 的配置

在很多插件中,都有 tls 相關的配置項,表示是否使用 TLS 來連接採集對象。

use_tls = false
  tls_min_version = "1.2"
  tls_ca = "/etc/categraf/ca.pem"
  tls_cert = "/etc/categraf/cert.pem"
  tls_key = "/etc/categraf/key.pem"
  ## Use TLS but skip chain & host verification
  insecure_skip_verify = true

use_tls 為 true 時,表示使用 tls 連接到採集對象。

如果證書是自簽證書,想要避免 X509: certificate signed by unknown authority 錯誤,可以設置 insecure_skip_verify = true

注意 目前的設計兩個參數是共同起作用的,單獨設置insecure_skip_verify = true無法避免 X509: certificate signed by unknown authority

tls_min_version 表示支持的最小tls版本,可選值是1.0 1.1 1.2 1.3 分別對應 TLS1.0 到TLS1.3 。 SSLv3 不支持。

tls_catls_certtls_key 分別是ca證書,客户端證書,客户端私鑰。

FAQ

報錯:heartbeat status code: 401  response

配置文件心跳地址寫的是url = "http://192.168.68.131:17001/v1/n9e/heartbeat",這裏寫的是nginx的地址,nginx轉給後端。

答:配置文件心跳地址寫對了,用户和密碼寫和推送數據的一樣。

basic_auth_user = "api01"

# Basic auth password
basic_auth_pass = "api01"


機器列表cpu和內存顯示unknown

六、企業級夜鶯集羣之categraf篇_categraf

答:心跳沒上報的原因,和錯誤“heartbeat status code: 401  response”一樣。