一般情況下,我們自定義的一些監控項都是通過push到pushgateway上,再由prometheus從pushgateway pull。
但是通過pushgateway推送數據時,如果推送過程中有重復的數據,prometheus從pushgateway pull數據時,會出現獲取不到數據的情況。
還有一種自定義監控項的方式是將監控采集到的數據寫入本地文件中,然后由node_exporter讀取該數據文件,這樣自定義的監控項就可以被prometheus從node_exporter上pull到了。
具體實現:
1、node_exporter啟動時需要添加參數:–collector.textfile.directory=xxx/。該參數定義一個數據文件目錄,啟動后默認會讀取該目錄下的數據文件
2、node_exporter能讀取到的數據文件是以.prom結尾的文件。
數據格式:
# HELP node_cpu_seconds_total Seconds the cpus spent in each mode. # metric說明 # TYPE node_cpu_seconds_total counter # metric value類型 node_cpu_seconds_total{cpu="0",mode="idle"} 77699.77 # metric
注:如果沒有寫HELP和TYPE的話,系統會幫助生成一個簡單的HELP描述和TYPE類型,如下:
# HELP example_metric Metric read from /some/path/textfile/example.prom # TYPE example_metric untyped example_metric 1
但是如果有多個文件中出現相同的指標名稱(example_metric),需要保證這些指標的HELP和TYPE都一致,否則采集將出錯. 基本格式也可以參考node_exporter/metrics路徑下顯示的內容.
3、prometheus server從node_exporter端pull數據