Collectd 古老的监控工具,在OpenWRT中派上用场了

背景

家里有两台设备,一台OpenWRT, 一台很小的Linux(低功耗,也是长年开机,当作网盘使用)。家外有两台VPS,一台甲骨文的永久免费VPS,一台bandwagon 上的VPS(朋友的)。想把这四个设备的基本指标监控起来,调研了很多,比较简单,容易部署的就算是 Collectd 了

方案

  • Oracle Cloud VPS 作为 Server
  • 其他设备上的 Collectd 当作 Client,定时把指标数据收集好 push 到 server 端
  • Server 端部署 GUI 工具,方便查看(虽然几百年也不会看一次,但是有和没有还是有区别的), 比较简单易用的 CGP Github开源的,不过最后更新时间也是2年前了。

安装与配置

其中OpenWRT中的安装与配置比较难搞一些,ubuntu都比较简单了,所以我就详细说明一下OpenWRT要怎么弄

OpenWRT

关于OpenWRT的定制编译请参考《软路由(OpenWRT)进阶编译,深度裁剪》,修改的配置下边会给出,编译的部分参考上述文章。

打开Utilities菜单

 1collectd ............... Lightweight system statistics collection daemon
 2[*]     Enable ability to use encrypted networking
 3<*>     collectd-mod-contextswith
 4<*>     collectd-mod-cpu
 5<*>     collectd-mod-cpufreq
 6<*>     collectd-mod-df
 7<*>     collectd-mod-ethstat
 8<*>     collectd-mod-exec
 9<*>     collectd-mod-interface
10<*>     collectd-mod-ipstatistics
11<*>     collectd-mod-iptables
12<*>     collectd-mod-irq
13<*>     collectd-mod-iwinfo
14<*>     collectd-mod-load
15<*>     collectd-mod-momory
16<*>     collectd-mod-netlink
17<*>     collectd-mod-network
18<*>     collectd-mod-openvpn
19<*>     collectd-mod-ping
20<*>     collectd-mod-processes
21<*>     collectd-mod-rrdtool
22<*>     collectd-mod-sensors
23<*>     collectd-mod-swap
24<*>     collectd-mod-thermal
25<*>     collectd-mod-users

第一个 Enable ability to use encrypted networking 需要开启,Client push数据到 Server端,最好不要明文 下面的 mod 部分可以根据自己的需要选择,选择好了,编译(本篇文章就略过了)

进入 OpenWRT

 1vim /etc/config/collect
 2
 3# 开启 对 collectd.conf 加载, 其它的注释部分不需要改动
 4config globals 'globals'
 5        option alt_config_file "/etc/collectd.conf"
 6#       option BaseDir '/var/run/collectd'
 7#       option Include '/tmp/collectd.d'
 8#       option PIDFile '/var/run/collectd.pid'
 9#       option PluginDir '/usr/lib/collectd'
10#       option TypesDB '/usr/share/collectd/types.db'
11#       option Interval '30'
12#       option ReadThreads '2'
13#       option WriteThreads '2'
14#       option WriteQueueLimitLow '0'
15#       option WriteQueueLimitHigh '0'
16#       option CollectInternalStats '0'

编译 /etc/collectd.conf 文件,如果没有新建一个

 1aseDir "/var/run/collectd"
 2Include "/etc/collectd/conf.d"
 3PIDFile "/var/run/collectd.pid"
 4PluginDir "/usr/lib/collectd"
 5TypesDB "/usr/share/collectd/types.db"
 6Interval 30
 7ReadThreads 10
 8WriteThreads 2
 9CollectInternalStats false
10Hostname "xx.xx.com"
11Include "/tmp/collectd.d"
12
13
14LoadPlugin cpu
15LoadPlugin df
16<Plugin df>
17	ValuesPercentage true
18</Plugin>
19LoadPlugin disk
20<Plugin disk>
21	Disk "sda1"
22	Disk "sda2"
23</Plugin>
24LoadPlugin interface
25<Plugin interface>
26	IgnoreSelected false
27	Interface "br-lan"
28	Interface "eth1"
29	Interface "wg0"
30	Interface "br-iptv"
31	Interface "tun0"
32	Interface "tun1"
33	Interface "tun2"
34</Plugin>
35LoadPlugin load
36LoadPlugin memory
37LoadPlugin network
38<Plugin network>
39    # server 的配置,就是把数据push到目标server的地址
40	<Server "host.com" "25826">
41            # 加密的部分,前面有一个编译选项,开启后才会有效
42        	SecurityLevel "sign"
43        	Username "xxxx"
44        	Password "xxxx"
45    	</Server>
46	TimeToLive 128
47	Forward true
48</Plugin>
49
50LoadPlugin processes
51<Plugin processes>
52	Process "dnsmasq"
53	Process "udpxy"
54	Process "xray"
55</Plugin>
56
57LoadPlugin thermal
58<Plugin thermal>
59        ForceUseProcfs false
60        Device "thermal_zone0"
61        IgnoreSelected false
62</Plugin>
63
64LoadPlugin dns
65<Plugin dns>
66	Interface "eth0"
67	SelectNumericQueryTypes true
68</Plugin>
69
70LoadPlugin sensors
71<Plugin sensors>
72	IgnoreSelected true
73</Plugin>

关于这些plugin如何配置请参与官方文档 collectd.org

ubuntu server

Server 端的配置和 Client 差不多,我简单的写一下差异的点吧

  • 接收数据
 1vim /etc/collectd/collectd.conf
 2
 3LoadPlugin network
 4<Plugin network>
 5    # Server监听配置
 6	<Listen "0.0.0.0" "25826">
 7        # 加密配置
 8		SecurityLevel Sign
 9		AuthFile "/etc/collectd/passwd"
10	</Listen>
11	Forward true
12</Plugin>

AuthFile 的内容,如下格式就可以了

1user:password
部署 CGP
  • 就是部署一个PHP环境,没有什么特别的,各位自便

最后你就可以看到如下的界面

  • Homepage

  • over view

  • network