一、了解Monit工具
Monit工具是基于Linux系统的一款开源的监控工具,它可以监控系统总体资源利用,对硬件资源和操作系统资源——例如物理设备、虚拟设备、文件系统、网络连接、用户、进程等进行监测和持续跟踪。另外,它可以调度自定义脚本去实现使用者自定义的检查。Monit工具对宿主Linux的CPU和内存占量很小,虽然它本质上是个监控工具,但因为它开放式的特点,故特别适合用于嵌入式系统的测试。
本文中提供了一些应用示例,示例取自于模块O-RAN无线通信产品的集成测试过程。

二、Monit工具的安装方法
在官方网站
https://mmonit.com/wiki/Monit/Installation下载软件包,建议使用Pre-built binaries的方式执行安装。基本过程为:
$ tar zxvf monit-x.y.z-linux-x64.tar.gz (x.y.z denotes version numbers)
$ cd monit-x.y.z
$ cp bin/monit /usr/local/bin/
$ cp conf/monitrc /etc/
安装步骤示例。
1)在系统中创建一个home/monit目录,将软件包上传到该目录中。
root@k8s-master-0:/home/monit# ll
total 2252
drwxr-xr-x 2 root root 4096 Dec 5 10:29 ./
drwxr-xr-x 15 root root 4096 Dec 5 10:29 ../
-rw-r--r-- 1 root root 2293992 Dec 5 10:29 monit-5.34.0-linux-x64.tar.gz
2)将软件包解压缩于/home/monit目录中。
root@k8s-master-0:/home/monit# tar zxvf monit-5.34.0-linux-x64.tar.gz
monit-5.34.0/
monit-5.34.0/man/
monit-5.34.0/man/man1/
monit-5.34.0/man/man1/monit.1
monit-5.34.0/conf/
monit-5.34.0/conf/monitrc
monit-5.34.0/CHANGES
monit-5.34.0/bin/
monit-5.34.0/bin/monit
monit-5.34.0/COPYING
root@k8s-master-0:/home/monit# cd monit-5.34.0/
3)设置环境变量。
root@k8s-master-0:/home/monit/monit-5.34.0# cp bin/monit /usr/local/bin/
root@k8s-master-0:/home/monit/monit-5.34.0# cp conf/monitrc /etc/
4)运行monit,检验安装效果。
root@k8s-master-0:/home/monit/monit-5.34.0# monit
New Monit id: 07b1ed309a9a41cab9df8e7a2e387dad
Stored in '/root/.monit.id'
Starting Monit 5.34.0 daemon with http interface at [localhost]:2812
或者执行monit -h若能正常显示出帮助信息也说明已安装正确。
root@k8s-master-0:/home/monit/monit-5.34.0# monit -h
Usage: monit [options]+ [command]
Options are as follows:
-c file Use this control file
-d n Run as a daemon once per n seconds
-g name Set group name for monit commands
-l logfile Print log information to this file
-p pidfile Use this lock file in daemon mode
-s statefile Set the file monit should write state information to
-I Do not run in background (needed when run from init)
--id Print Monit's unique ID
--resetid Reset Monit's unique ID. Use with caution
-B Batch command line mode (do not output tables or colors)
-t Run syntax check for the control file
-v Verbose mode, work noisy (diagnostic output)
-vv Very verbose mode, same as -v plus log stacktrace on error
-H [filename] Print SHA1 and MD5 hashes of the file or of stdin if the
filename is omitted; monit will exit afterwards
-V Print version number and patchlevel
-h Print this text
Optional commands are as follows:
start all - Start all services
start - Only start the named service
stop all - Stop all services
stop - Stop the named service
restart all - Stop and start all services
restart - Only restart the named service
monitor all - Enable monitoring of all services
monitor - Only enable monitoring of the named service
unmonitor all - Disable monitoring of all services
unmonitor - Only disable monitoring of the named service
reload - Reinitialize monit
status [name] - Print full status information for service(s)
summary [name] - Print short status information for service(s)
report [up|down|..] - Report state of services. See manual for options
quit - Kill the monit daemon process
validate - Check all services and start if not running
procmatch - Test process matching pattern
root@k8s-master-0:/home/monit/monit-5.34.0#
三、 Monit工具的基本配置
在上一节安装示例的步骤4中,我们可以看到monit进程运行后会打开localhost的2812端口,我们可以修改工具的配置文件允许使用者远程访问工具页面。
root@k8s-master-0:/home/monit/monit-5.34.0# vi /etc/monitrc
将默认配置中包含的段落
set httpd port 2812 and
use address localhost # only accept connection from localhost (drop if you use M/Monit)
allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
修改为
set httpd port 2812 and
use address localhost # only accept connection from localhost (drop if you use M/Monit)
use address 192.168.1.172
# allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
重新启动monit。
root@k8s-master-0:/home/monit/monit-5.34.0# monit quit
Monit daemon with pid [2757148] killed
root@k8s-master-0:/home/monit/monit-5.34.0# monit
Starting Monit 5.34.0 daemon with http interface at [192.168.1.72]:2812
而后在测试PC的浏览器中输入192.168.1.72:2812即可看到默认配置下的检查结果,如下图所示。

可以看到工具默认会对系统的CPU、内存等负载情况进行展示。
四、使用工具的语法编写check段落进行测试
可以在/etc/monitrc文件中编写check段落,设计检查项。
vi /etc/monitrc
在文件中增加
###监控系统
check system $HOST
if loadavg (5min) per core > 15 for 10 cycles then alert
if cpu usage > 70% for 10 cycles then alert
if memory usage > 70% for 10 cycles then alert
if swap usage > 25% then alert
##监控5G进程
check process gnbCuMcuNode matching "gnbCuMcuNode"
if changed pid then alert
if totalmem > 150.0 MB for 5 cycles then alert
mode passive
##监控网络接口
check network br_gnbOamNode with interface br_gnbOamNode
if link down then alert
##监控磁盘
check filesystem os with path /var/log
if space usage > 95% for 5 cycles then alert
check system段落用于对系统进行检查。本示例中对loadavg、cpu usage、memory usage、swap usage的检查是“且”的关系,若其中一项不符比如内存使用率超出系统最大内存的70%,则判定Status failed。(注:自定义此段落后即覆盖掉上一节的默认检查。)
For xx cycles的意思是检查多轮,避免突发的数据异常影响到结果判定。
可以通过工具web页面看到执行效果,如下图所示的System、Process、Filesystem和Net部分。

check process段落用于检查系统中运行的进程。本示例中检查了进程ID是否变化过,进程的内存占用是否超出了设计标准。mode passive表示不进行fail状态的后处理(一些监控类任务有做后处理的需求,测试类任务不该有后处理)。
check network段落用于检查系统中运行的网络Device的状态。
check filesystem段落用于检查系统中的文件系统,示例中对缓存文件较频繁的路径/var/log进行容量检查。
除了上述示例的检查外,还可以设计对许多其他资源进行检查,比如单个文件的checksum、文件权限,又比如单个进程的CPU利用率、内存占用率、服务端口连通性、磁盘I/O速率,再比如本地系统与远端host的之间的ping通率、http/dns/mysql等应用层访问的成功率、TCP/UDP下载速率等等,更完善的方法可以在其官网的wiki中寻找到。不过,手册繁琐,建议初始阶段先照着monitrc配置文件中注释的实例依葫芦画瓢地去用,即已能掌握到最基本的方法。
在ORAN无线通信产品中,进程数、网络设备数通常会达到数十个甚至上百个,测试员依据对全部系统资源或依据对系统设计的了解选择对部分关键资源去进行上摆个测试用例设计,经验证这些测试设计能很好的映射到monit工具的check段落中,通过工具去调度执行,能有效提升测试效率和节省人力。
五、编写自定义脚本进行测试
可以在/etc/monitrc文件中增加check program的段落,调用使用者的自定义脚本。可以增加多个段落。
##监控自定义脚本
check program checklog_bsp_bbu_init with path "/home/monit/customScripts/checklog_bsp_bbu_init.sh"
if status !=0 then alert
check program checkrunlog_gnbCuMcuNode with path "/home/monit/customScripts/checkRunlog_gnbCuMcuNode.sh"
if status !=0 then alert
check program checkN2connection with path "/home/monit/customScripts/checkN2connection.sh"
if status !=0 then alert
#
需要将待调用的自定义脚本放置在对应目录下,并设置可执行权限。
root@k8s-master-0:/home/monit/customScripts# chmod +x *
root@k8s-master-0:/home/monit/customScripts# ll
total 20
drwxr-xr-x 2 root root 4096 Dec 5 14:55 ./
drwxr-xr-x 4 root root 4096 Dec 5 13:58 ../
-rwxr-xr-x 1 root root 642 Dec 5 14:37 checklog_bsp_bbu_init.sh*
-rwxr-xr-x 1 root root 471 Dec 5 14:55 checkN2connection.sh*
-rwxr-xr-x 1 root root 520 Dec 5 14:50 checkRunlog_gnbCuMcuNode.sh*
运行效果如下图中的Program部分。

示例中的checkN2connection执行结果为失败,可以点击它查阅详细信息。通常shell脚本返回0表示成功,返回1表示失败。

对应的checkN2connection.sh脚本参考内容如下图。该脚本调用了上层应用(即gnbCuMcuNode模块)的命令接口去检查于其非常重要的N2连接是否已初始化成功。
#!/usr/bin/sh
#检查NR基站是否发现了核心网AMF网元
result=$(kubectl exec -n oran cu-pod -- odc -n gnbCuMcuNode amf-show | grep sctpAssociationUp)
if [[ "$result" == *"false"* ]]; then
echo "找到'false'字符串,输出为$result"
exit 0 # 返回状态0,表示成功找到"false"
else
echo "未找到'false'字符串,输出为$result"
exit 1 # 返回状态1,表示未找到"false"
fi
在对嵌入式系统的OS&BSP模块测试时,通常要考察平台层与应用层的适配情况,则可利用上述方法把上层应用的脚本(或利用它们的接口自己写脚本)集成过来,完善测试覆盖。
六、更多说明
Monit工具可以很轻松的调度上百、上千个检查段落,掌握一些公共参数的用法能使工具更有效率地运行。
1)monit进程默认以daemon进程的方式运行在系统中,可以设置其工作间隔。修改/etc/monitrc。
## Start Monit in the background (run as a daemon):
#
set daemon 300 # check services at 30 seconds intervals
# with start delay 240 # optional: delay the first check by 4-minutes (by
# # default Monit check immediately after Monit start)
在上述示例中,工具每300秒会对所有的check段落进行一次调度。还可以设置在工具启动后延迟一段时间再进行全部或部分的检查。
2) 设置工具的log路径,修改/etc/monitrc。monitLog能记录工具的启动与停止过程,以及各检查执行轮次的检查结果等。
#
set log /home/monit/monitLog
#
3)设置与脚本调度相关的公共参数。如下参数通常是为了保障工具运行的有效性和效率。
默认配置为
# set limits {
# programOutput: 512 B, # check program's output truncate limit
# sendExpectBuffer: 256 B, # limit for send/expect protocol test
# fileContentBuffer: 512 B, # limit for file content test
# httpContentBuffer: 1 MB, # limit for HTTP content test
# networkTimeout: 5 seconds # timeout for network I/O
# programTimeout: 300 seconds # timeout for check program
# stopTimeout: 30 seconds # timeout for service stop
# startTimeout: 30 seconds # timeout for service start
# restartTimeout: 30 seconds # timeout for service restart
# execTimeout: 0 seconds # timeout for test action exec
# }
修改/etc/monitrc
set limits {
programOutput: 10240 B, # check program's output truncate limit
sendExpectBuffer: 256 B, # limit for send/expect protocol test
fileContentBuffer: 10240 B, # limit for file content test
httpContentBuffer: 1 MB, # limit for HTTP content test
networkTimeout: 5 seconds # timeout for network I/O
programTimeout: 300 seconds # timeout for check program
stopTimeout: 30 seconds # timeout for service stop
startTimeout: 30 seconds # timeout for service start
restartTimeout: 30 seconds # timeout for service restart
execTimeout: 0 seconds # timeout for test action exec
}
以programOutput参数为例介绍,该参数的含义是允许单个用户自定义脚本返回结果的最大长度限制。若实际测试中需要用到类似第五章示例的
checkRunlog_gnbCuMcuNode.sh脚本(其是功能是返回所有的ERROR级别Log),则应当意识到该脚本是有可能返回比较长的log文本的,所以512B的默认值不够用,需要斟酌改大一些。
4) 使用monit工具做测试,你会意识到你的测试输入触发和测试结果检查是分离的(或者通信领域爱说开环而非闭环),但可以通过设计工具的运行形式去更好的过程匹配,这些方法包含:
A)为各个check段落设置group,控制不同group有不同的周期和时延;
B)在同一个系统中运行多个monit进程;
C)如果你的测试对象是分布式的系统,可以将各monit(每个系统至少一个)的输出重定向到M/Monit(而非示例中的本地WEB页面)。
以上属于进阶的用法,建议在掌握基本的用法后再行研究。
七、总结
·Monit工具本身原用于做系统监控,但是特别适合于嵌入式系统测试,特别是OS&BSP模块测试。
· Monit工具的通过monitrc配置文件进行集中配置,既允许使用工具原生语法设计标准的check段落,也允许调用使用者自定义的脚本,统一的测试结果可以输出到web和log文件中。
· 测试人员依据自己对被测系统的深入理解,设计出有效的脚本并通过Monit工具统一调度执行。小则可以优化个人的测试环境,增加个人发现问题的效率。大则可以将工具与CI/CD环境去做进一步的集成。可以把不同验证阶段的脚本都聚合到一起,测试用例越多、检查项越丰富则工具的效果越明显。
文末了,我邀请你进入我们的软件测试学习交流群,大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,了解测试行业的最新趋势,助你快速进阶Python自动化测试/测试开发,稳住当前职位同时走向高薪之路。
最后:
1)关注+私信回复:“测试”,可以免费领取一份10G软件测试工程师面试宝典文档资料。以及相对应的视频学习教程免费分享!
2)关注+私信回复:"入群" 就可以邀请你进入软件测试群学习交流~~