Twitter上偶然看到一張很漂亮的動態監控圖,查看之後才知道是由一款名爲netdata的開源工具實現,獲取實時(秒級)數據並用動態圖表展示,數據從/proc相關文件獲取(Linux中一切皆文件,內核將硬件虛擬成文件來進行管理)。

Introduction

netdata代碼發佈在GitHub上。

以下是相關文檔

Document GitHub URL
Why netdata https://github.com/firehol/netdata/wiki
Installation https://github.com/firehol/netdata/wiki/Installation
netdata-systemd https://github.com/firehol/netdata/blob/master/system/netdata-systemd

Prerequisites

netdata依賴一些包,如makegccautoconfautogenautomakepkg-configgit等。

git可使用yum安裝,也可參考本人的Compile Install And Configure Git On CentOS7通過源碼編譯安裝。

執行如下命令安裝依賴包

1
2
3
4
sudo yum install -y libuuid libuuid-devel zlib zlib-devel gcc make autoconf automake pkgconfig

#根據個人情況使用
sudo yum install -y git

Node.js

官方稱:netdata使用node.js進行數據收集,其node.d.plugin會在系統路徑(system path)中查找node.js的可執行文件。

官方強烈建議使用node.js,並稱

Only node.js will work with netdata.

關於node.js的安裝,可參考本人的 Compile and Install Node.js on CentOS7

Netdata Build Introduction

通過git下載netdata倉庫後進行安裝,官方提供的操作步驟

1
2
3
4
5
6
# download it - the directory 'netdata.git' will be created
git clone https://github.com/firehol/netdata.git netdata.git --depth=1
cd netdata.git

# build it
./netdata-installer.sh

如果想自定義安裝目錄(如/opt),可使用如下命令

1
./netdata-installer.sh --install /opt

實際操作過程是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
./configure --prefix=/opt/netdata/usr --sysconfdir=/opt/netdata/etc --localstatedir=/opt/netdata/var --with-zlib --with-math --with-user=netdata CFLAGS=-O3
make
make install
groupadd -r netdata
useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
chown -R netdata:netdata /opt/netdata/usr/share/netdata/web
chmod 0775 /opt/netdata/usr/share/netdata/web
chown -R netdata:netdata /opt/netdata/etc/netdata
chmod 0775 /opt/netdata/etc/netdata
chown -R netdata:netdata /opt/netdata/var/cache/netdata
chmod 0775 /opt/netdata/var/cache/netdata
chown -R netdata:netdata /opt/netdata/var/log/netdata
chmod 0775 /opt/netdata/var/log/netdata
chown root /opt/netdata/usr/libexec/netdata/plugins.d/apps.plugin
chmod 4755 /opt/netdata/usr/libexec/netdata/plugins.d/apps.plugin

注意:自定義安裝路徑後,配置文件路徑對應更改

file path
the daemon /opt/netdata/usr/sbin/netdata
config files /opt/netdata/etc/netdata
web files /opt/netdata/usr/share/web/netdata
plugins /opt/netdata/usr/libexec/netdata
cache files /opt/netdata/var/cache/netdata (輪詢數據)
log files /opt/netdata/var/log/netdata

Updating

如何更新,官方提供的方案是

1
2
3
4
5
6
# update it
cd /path/to/netdata.git
git pull

# rebuild it and install it
./netdata-installer.sh

即更新git倉庫後,重新安裝即可。

Installation

以安裝到目錄/opt爲例

1
2
3
4
5
6
7
8
9
10
11
#進入/opt目錄
cd /opt

#git下載倉庫
sudo git clone https://github.com/firehol/netdata.git netdata.git --depth=1

#進入目錄
cd netdata.git

#執行安裝,並指定安裝路徑
sudo ./netdata-installer.sh --install /opt

安裝完成後提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Running command:

/opt/netdata/usr/sbin/netdata

OK. NetData Started!
Downloading default configuration from netdata...
New configuration saved for you to edit at /opt/netdata/etc/netdata/netdata.conf


ok. NetData is installed and is running.

Hit http://localhost:19999/ from your browser.

To stop netdata, just kill it, with:

killall netdata

To start it, just run it:

/opt/netdata/usr/sbin/netdata

Enjoy!


INFORMATION:

I see you have kernel memory de-duper (called Kernel Same-page Merging,
or KSM) available, but it is not currently enabled.

To enable it run:

echo 1 >/sys/kernel/mm/ksm/run
echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs

If you enable it, you will save 20-60% of netdata memory.

目錄樹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[[email protected] netdata]$ pwd
/opt/netdata
[[email protected] netdata]$ tree -L 3
.
├── etc
│ └── netdata
│ ├── apps_groups.conf
│ ├── charts.d.conf
│ └── netdata.conf
├── usr
│ ├── libexec
│ │ └── netdata
│ ├── sbin
│ │ └── netdata
│ └── share
│ └── netdata
└── var
├── cache
│ └── netdata
└── log
└── netdata

13 directories, 4 files
[[email protected] netdata]$

Configuration

Systemd Support

使用systemd管理netdata,官方提供了netdata-systemd示例文件 鏈接

創建文件/usr/lib/systemd/system/netdata.service,寫入如下內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=netdata
After=network.target httpd.service nginx.service squid.service nfs-server.service mysqld.service named.service postfix.service

[Service]
Type=forking
WorkingDirectory=/opt/netdata
User=root
Group=root
PIDFile=/var/run/netdata/netdata.pid
ExecStart=/opt/netdata/usr/sbin/netdata
ExecStop=/bin/kill -SIGTERM $MAINPID
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target

直接執行
systemctl status|start|restart|stop|reload|enable|disable netdata.service
可能無法成功,可先執行
killall netdata,再執行systemctl命令。

  • 啓動 systemctl start netdata.service
  • 開機啓動 systemctl enable netdata.service

Plugins

官方提供了一些Shell腳本做插件擴展,可用於監控mysqlnginxapache等服務。

安裝後存放路徑爲/opt/netdata/usr/libexec/netdata/charts.d/

1
2
3
4
5
6
7
8
9
10
11
[[email protected] ~]$ cd /opt/netdata/usr/libexec/netdata/charts.d
[[email protected] charts.d]$ pwd
/opt/netdata/usr/libexec/netdata/charts.d
[[email protected] charts.d]$ ls
airsearches.chart.sh example.chart.sh opensips.chart.sh
apache.chart.sh load_average.chart.sh postfix.chart.sh
ap.chart.sh mem_apps.chart.sh README.md
cpu_apps.chart.sh mysql.chart.sh sensors.chart.sh
cpufreq.chart.sh nginx.chart.sh squid.chart.sh
crsproxy.chart.sh nut.chart.sh
[[email protected] charts.d]$

官方說明 https://github.com/firehol/netdata/blob/master/charts.d/README.md

而配置文件路徑是/opt/netdata/etc/netdata/,如果需要添加某功能插件,在該目錄下新建對應配置文件,以下是示例:

nginx.chart.sh

腳本文件: /opt/netdata/usr/libexec/netdata/charts.d/nginx.chart.sh

腳本通過http://127.0.0.1:80/stub_status獲取stub_status信息,在Nginx配置文件中配置

1
2
3
4
5
6
location /stub_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}

使用nginx -t檢查是否有於法錯誤,執行systemctl reload nginx重新載入配置文件。

執行systemctl restart netdata.service重啓netdata服務後,即可查看到已經正常監控Nginx。

apache.chart.sh

腳本文件: /opt/netdata/usr/libexec/netdata/charts.d/apache.chart.sh

腳本通過http://127.0.0.1:80/server-status?auto獲取

在目錄/etc/httpd/conf.d下創建文件status.conf,寫入如下內容

1
2
3
4
5
6
7
<location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</location>
ExtendedStatus On

使用httpd -t檢查是否有於法錯誤,執行systemctl reload httpd重新載入配置文件。

執行systemctl restart netdata.service重啓netdata服務後,即可查看到已經正常監控Apache httpd。

mysql.chart.sh

腳本文件: /opt/netdata/usr/libexec/netdata/charts.d/mysql.chart.sh

根據官方文檔提供的操作說明,始終操作不成功。直接閱讀腳本mysql.chart.sh,讓無法解決。

以下是netdata錯誤日誌中信息

1
2
3
4
5
6
7
[[email protected] netdata]$ pwd
/opt/netdata/var/log/netdata
[[email protected] netdata]$ cat error.log | grep mysql
charts.d: mysql: cannot get global status for 'mariadb'. Please set mysql_opts[mariadb]='options' to whatever needed to get connected to the mysql server, in /opt/netdata/etc/netdata/mysql.conf
charts.d: mysql: no mysql servers found. Please set mysql_opts[name]='options' to whatever needed to get connected to the mysql server, in /opt/netdata/etc/netdata/mysql.conf
charts.d: chart 'mysql' check() function reports failure.
[[email protected] netdata]$

暫時擱置

Working

啓動netdata.service後,在瀏覽器地址欄中輸入

1
2
3
http://127.0.0.1:19999/

http://localhost:19999/

即可看到動態圖表,顯示內容如http://netdata.firehol.org/中顯示。

Virtualization On-line

本人在VPS上測試成功,在瀏覽器中輸入URL,即可顯示可視化數據。

通過https://仍能正常使用SSL,但會出現一個問題,無法再使用http://(會自動跳轉到https),須清空瀏覽器緩存才可以,該問題暫未能解決。

以下圖片來自Netstat的git倉庫 地址

  • Overview

  • Disk I/O

  • Network interfaces

  • Network QoS

  • Applications

Running Behind Nginx

通過Nginx訪問netdata可參考官方文檔 Running behind nginx

此處為其配置SSL證書

As a virtual host

Nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
upstream backend {
server 127.0.0.1:19999;
keepalive 64;
}

server {
listen 80;
server_name netdata.lemptest.tech;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl default_server;
server_name netdata.lemptest.tech;

location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_pass_request_headers on;
proxy_set_header Connection "keep-alive";
proxy_store off;
}

...
...
ssl_configuation
...
...

}

截圖

Subfolder Of Existing Virtual Host

Nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
upstream netdata {
server 127.0.0.1:19999;
keepalive 64;
}

# redirect http to https
server {
listen 80;
server_name lemptest.tech;
return 301 https://$server_name$request_uri;
}


server {
listen 443 ssl default_server;
server_name lemptest.tech;
root /usr/share/nginx/html;

location /netdata {
return 301 /netdata/;
}

location ~ /netdata/(?<ndpath>.*) {
proxy_redirect off;
proxy_set_header Host $host;

proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass_request_headers on;
proxy_set_header Connection "keep-alive";
proxy_store off;
proxy_pass http://netdata/$ndpath$is_args$args;

gzip on;
gzip_proxied any;
gzip_types *;
}

...
...
ssl_configuation
...
...

}

截圖

Netdata Official Collection

偶然間到Netdata的github看了下,隨意點進了一個頁面what our users say about netdata?,意外發現自己這篇文章的截圖被ktsaou展示出來了,好激動。

此爲鏈接錨點
https://github.com/firehol/netdata/issues/148#issuecomment-206824520


Change Log

  • 2016.03.31 14:54 Thu Asia/Beijing
    • 初稿完成
  • 2016.04.16 19:26 Sat Asia/Beijing
    • 添加Netdata Official Collection
  • 2016.12.30 17:42 Fri Asia/Shanghai
    • 通過配置過SSL證書的Nginx訪問netdata

  • Note Time: 2016.03.31 14:54 Thu
  • Note Location: Asia/Beijing
  • Writer: lempstacker