3个版本 (破坏性更新)
使用旧的Rust 2015
0.3.0 | 2019年1月28日 |
---|---|
0.2.0 | 2019年1月27日 |
0.1.0 | 2018年9月29日 |
#2 在 #icinga 中
49KB
888 行
check_timed_logs_fast
这是一个使用Rust(原始版本是Perl)重写的非常快速的nagios插件check_timed_logs。API保持不变,因此只需将原始的Perl脚本替换为二进制文件即可。
插件的目的在于监控日志文件,如果过去Y分钟内正则表达式发生超过X次,则发出警报(例如,过去一分钟内超过一个异常或过去两分钟内超过五次警告)。
这次重写是由处理非常大的冗长日志文件时出现的问题触发的。原始插件解析时间很长,而Nagios在几秒钟内未收到检查的响应后就会超时——这会在监控中错误地显示为一个关键事件。
性能
日志文件大小 | 1.2M | 37M | 5.7G |
---|---|---|---|
原始 | 0.878秒 | 20.287秒 | >30分钟 |
Rust 重写 | 0.031秒 | 0.676秒 | 83.088秒 |
改进 | 96.4 % | 96.6 % |
这些指标提供了一个粗略的概念,我还没有仔细查看确切的内存使用差异(但似乎没有增加)。性能也取决于正则表达式的复杂性。
我在高性能服务器上使用了以下命令进行基准测试(实际上这不应该很重要,因为只有一个核心被使用,内存指纹也很低)。
perf stat
-r 10
-d ./check_timed_logs_fast -pattern '.*nonExistentPattern.*' -i 9999999 -c 1 -logfile ./log
该命令执行检查十次并解析整个文件,上面的表格中的平均执行时间是持续时间。
疯狂的改进速度来自Rust和使用memmap
向后读取文件。目前实现相当简单——一个进程通过I/O操作和解析进行阻塞。我怀疑还有改进的空间,并希望实现两种额外的策略
- 将工作拆分到工作线程中
- 异步处理
此外,我知道(因为我也进行了基准测试),fancy-regex
crate 是一个减速因素。regex
crate 具有更好的性能,但不支持诸如向前查看等高级正则表达式功能。原始的check_timed_logs
脚本支持这些功能,因为我希望保持兼容性,我必须使用支持这些功能的(较慢的)crate。
安装/使用
cargo install check_timed_logs_fast
# a warning should be issued if there is >= 1 occurrence of either the string
# "timeout" or "closed" in the last ten minutes. if there are >= 5 matches
# issue a critical incident.
check_timed_logs_fast -logfile /var/log/app.log -pattern "timeout|closed" -interval 10 -w 1 -c 5
您可以使用MUSL编译适用于某些未知Linux的通用静态二进制文件。
rustup target add x86_64-unknown-linux-musl
cargo build --release --target x86_64-unknown-linux-musl
构建Debian软件包
按照以下步骤构建软件包
使用以下命令克隆和编译:cargo build --release --target=x86_64-unknown-linux-musl
。生成的可执行文件位于target/x86_64-unknown-linux-musl/release/check_timed_logs_fast
。如果您想使用此fpm
命令构建Debian软件包,请先进入cd target/x86_64-unknown-linux-musl/release/
目录。
fpm -s dir \
-t deb \
--iteration 1plugins1 \
--architecture all \
--deb-ignore-iteration-in-dependencies \
--maintainer "Michael Mueller <[email protected]>" \
--name check_timed_logs_fast \
--verbose \
--version 0.0.8 \
./check_timed_logs_fast=/usr/lib/nagios/plugins/check_timed_logs_fast
许可证
Copyright (c)
2018 Michael Mueller, http://micha.elmueller.net/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
依赖项
~7–16MB
~226K SLoC