Docker – NetData

測試環境為 CentOS 7 x86_64 虛擬機 (CentOS 8 的 Container 管理變成 podman ,試過以下方式是無法透過 podman 使用 netdata 的).

Netdata 是一種用來監控並收集系統資訊(如 CPU , Memory , 磁碟 , 網路 …使用率)的工具.

透過 Docker (關於 Docker 的使用請參考 – http://benjr.tw/95998 )的方式來安裝 Netdata,下面先來安裝 Docker 並啟動服務.

[root@localhost ~]# yum install docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
[root@localhost ~]# systemctl status docker

透過 Docker 來安裝 Netdata – https://learn.netdata.cloud/docs/agent/packaging/docker .

[root@localhost ~]# docker run -d --name=netdata \
  -p 19999:19999 \
  -v netdataconfig:/etc/netdata \
  -v netdatalib:/var/lib/netdata \
  -v netdatacache:/var/cache/netdata \
  -v /etc/passwd:/host/etc/passwd:ro \
  -v /etc/group:/host/etc/group:ro \
  -v /proc:/host/proc:ro \
  -v /sys:/host/sys:ro \
  -v /etc/os-release:/host/etc/os-release:ro \
  --restart unless-stopped \
  --cap-add SYS_PTRACE \
  --security-opt apparmor=unconfined \
  netdata/netdata

檢視 Netdata 所使用的埠 port.

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED              STATUS                        PORTS                      NAMES
08b3a2e0bc71        netdata/netdata     "/usr/sbin/run.sh"   About a minute ago   Up About a minute (healthy)   0.0.0.0:19999->19999/tcp   netdata
[root@localhost ~]# docker port netdata
19999/tcp -> 0.0.0.0:19999

檢視一下目前 Host 的 IP .

[root@localhost ~]# ifconfig 
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:8aff:fe93:41b2  prefixlen 64  scopeid 0x20<link>
        ether 02:42:8a:93:41:b2  txqueuelen 0  (Ethernet)
        RX packets 57  bytes 13031 (12.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 54  bytes 15947 (15.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.111.29  netmask 255.255.255.0  broadcast 192.168.111.255
        inet6 fe80::cddd:a1cd:13e:ca24  prefixlen 64  scopeid 0x20<link>
        inet6 fd15:4ba5:5a2b:1008:bb5:c542:b284:aac4  prefixlen 64  scopeid 0x0<global>
        ether 00:0c:29:d1:ea:ec  txqueuelen 1000  (Ethernet)
        RX packets 96145  bytes 143646806 (136.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17174  bytes 1255524 (1.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

透過瀏覽器看 http://192.168.111.29:19999 或是 http://172.17.0.1:19999 皆可以看到 Netdata 顯示 host 系統的狀態.

Netdata REST API

除了網頁外還有什麼方式可以查看到這一些 Host 的資訊呢! 可以透過 restfull API 的方式來查看.

API 提供的資訊很多,可以透過官網建議的 Swagger Editor – https://registry.my-netdata.io/swagger/#/default/get_info 來查詢有哪一些 Restfull API 的資訊可以查詢.

Host Info 的 api 網址如下,透過 curl 文字版的瀏覽器來查,以 JSON 交換格式來顯示.

[root@localhost ~]# curl http://192.168.111.29:19999/api/v1/info
{
	"version": "v1.31.0-24-nightly",
	"uid": "b2770e94-bff4-11eb-9ef0-0242ac110002",
	"mirrored_hosts": [
		"08b3a2e0bc71"
	],
	"mirrored_hosts_status": [
		{ "guid": "b2770e94-bff4-11eb-9ef0-0242ac110002", "reachable": true, "claim_id": null }
	],
	"alarms": {
		"normal": 68,
		"warning": 0,
		"critical": 0
	},
	"os_name": "CentOS Linux",
	"os_id": "centos",
	"os_id_like": "rhel fedora",
	"os_version": "7 (Core)",
....

更多如 system.cpu 的網址如下,更多組合可以透過剛剛的 Swagger Editor 編輯.

[root@localhost ~]# curl http://192.168.111.29:19999/api/v1/data?chart=system.cpu&after=-600&before=0&points=20&group=average&gtime=0&format=json&options=seconds&options=jsonwrap
[1] 3024
[2] 3025
[3] 3026
[4] 3027
[5] 3028
[6] 3029
[7] 3030
[8] 3031
[2]   Done                    after=-600
[3]   Done                    before=0
[root@localhost ~]# {
 "labels": ["time", "guest_nice", "guest", "steal", "softirq", "irq", "user", "system", "nice", "iowait"],
    "data":
 [
      [ 1622271776, 0, 0, 0, 0, 0, 0.5, 2, 1, 0],
      [ 1622271775, 0, 0, 0, 0, 0, 0, 1.0152284, 0.5076142, 0],
      [ 1622271774, 0, 0, 0, 0, 0, 0, 1.010101, 1.010101, 0],
      [ 1622271773, 0, 0, 0, 0, 0, 0.5025126, 2.01005, 1.0050251, 0.5025126],
      [ 1622271772, 0, 0, 0, 0, 0, 0, 7.070707, 3.535354, 0],
      [ 1622271771, 0, 0, 0, 0, 0, 0, 34.5, 17.5, 0],
      [ 1622271770, 0, 0, 0, 0, 0, 0, 35.02538, 15.736041, 0],
      [ 1622271769, 0, 0, 0, 0, 0, 0, 35.5, 16, 0],
      [ 1622271768, 0, 0, 0, 0, 0, 0, 1.010101, 1.010101, 0],
      [ 1622271767, 0, 0, 0, 0, 0, 0.5050505, 1.5151515, 1.010101, 0],
      [ 1622271766, 0, 0, 0, 0, 0, 0, 1.5151515, 0.5050505, 0],
      [ 1622271765, 0, 0, 0, 0, 0, 0.5050505, 1.5151515, 0.5050505, 0],
      [ 1622271764, 0, 0, 0, 0, 0, 0.5076142, 2.030457, 1.0152284, 0],
      [ 1622271763, 0, 0, 0, 0, 0, 0.5050505, 1.5151515, 0.5050505, 0],
....
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料