今天我們要聊的是NodeExporter,一款可擴展的系統監控組件。NodeExporter是Prometheus生態系統中的一員,作為Prometheus的一個客戶端,NodeExporter主要負責收集服務器的各種指標,諸如CPU使用率、內存使用率、磁盤使用情況、網絡流量等。
一、NodeExporter源碼
NodeExporter是開源軟件,其源碼託管在GitHub上,感興趣的讀者可以前往查看。
二、NodeExporter指標
作為Prometheus客戶端,NodeExporter的最主要的職責就是提供系統各種指標,以下是一些NodeExporter支持的指標:
node_boot_time_seconds:系統啟動時間
node_cpu_seconds_total:CPU使用時間、CPU使用率
node_filesystem_avail_bytes:文件系統的可用空間大小
node_filesystem_size_bytes:文件系統的總空間大小
node_load1:1分鐘平均負載
node_memory_MemAvailable_bytes:系統內存可用空間大小
node_vmstat_pgfault:發生缺頁錯誤的次數
以上指標只是冰山一角,NodeExporter提供的指標非常多,完整列表可以在Prometheus的官方網站上查看。
三、NodeExporter採集原理
NodeExporter實現了Prometheus的/(metrics)API,提供了最基礎的metric輸出方式。當Prometheus Server查詢NodeExporter的metric API時,NodeExporter返回系統信息。這樣Prometheus就可以通過拉取的方式獲得數據。
當然,NodeExporter也支持通過PushGateway的方式,將數據主動推送到Prometheus Server上,PushGateway在我們後面的文章中會介紹。
四、NodeExporter源碼解讀
下面我們來看一下NodeExporter源碼的分層設計:
│ prometheus.go
│ README.md
│ main.go
│
├─collectors
│ │ collectors.go
│ │ cpu.go
│ │ cpufreq_linux.go
│ │ diskstats_linux.go
│ │ exports.go
│ │ filesystem_linux.go
│ │ ioctl_linux.go
│ │ loadavg_linux.go
│ │ meminfo_linux.go
│ │ netdev_linux.go
│ │ netstat_linux.go
│ │ processes_linux.go
│ │ procstat_linux.go
│ │ stat_linux.go
│ │ textfile.go
│ │ textfile_linux.go
│ │
│ ├─collector_structs
│ │ cpufreq_linux.go
│ │ diskstat.go
│ │ filesystem_linux.go
│ │ interrupts.go
│ │ meminfo.go
│ │ netstat.go
│ │ stat_linux.go
│ │ tcpstat.go
│ │ uptime.go
│ │
│ └─systemd
│ networkd_linux.go
│ systemd_linux.go
│ systemd_units.go
│ timedate_linux.go
│ udev_linux.go
│ units.go
│
├─collector/check_test_data
│ test-linux-proc-diskstats
│ test2-linux-proc-diskstats
│ test2-linux-proc-net-dev_snmp6
│ test-linux-device-mapper_multipath
│ test2-linux-device-mapper_multipath
│ test-linux-proc-stat
│ test2-linux-proc-stat
│ test2-textfile-time
│ test-textfile-go
│
├─config
│ config.go
│
├─exporter
│ node_exporter.go
│
├─textfile
│ textfile.go
│
├─util
│ helpers.go
│ md5sum.go
│ version_info.go
│
└─vendor
NodeExporter的源碼分以collectors、config、exporter、textfile、util等為主要模塊。
其中collectors是最核心的模塊,其中包括collectors.go、cpu.go、filesystem_linux.go等 Collected部分指標。
NodeExporter的代碼非常清晰、模塊化並且易於理解,這也為其提供了較好的可擴展性。如果有興趣深入了解NodeExporter的源碼實現,建議閱讀源碼。
五、Node-export選取
Node-export選取是NodeExporter中的一個子系統,該子系統負責自動發現節點的機制和配置。
下面是一個簡單的Node-export選取配置的例子:
bearer_token_file: /path/to/token/file
disable_exporter_metrics: true
disable_collectors:
- netdev
- sockstat
以上的配置文件意味着NodeExporter會讀取/path/to/token/file中的token,同時禁用netdev和sockstat的Collector。
六、總結
綜上所述,NodeExporter可以讓我們輕鬆地收集各種系統指標,以便我們更好地監控我們的系統。NodeExporter源碼清晰、模塊化,易於理解,而Node-export選取則為NodeExporter提供了自動化的配置機制。
使用NodeExporter和Prometheus可以非常方便地搭建起一個優秀的監控系統,同時,我們也可以通過深入了解其源碼實現,進一步擴展其功能。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/184397.html