Shell脚本日志关键字监控+告警

最近小张的爬虫程序越来越多,可当爬虫程序报错,不能及时的发现,从而造成某些重要信息不能及时获取的问题,更有甚者,遭到领导的批评。于是就在想有没有一种方法,当爬取信息报错的时候,可以通过邮件或者短信的方式及时通知小张呢,于是我进行相关搜索,还真让我找到了^_^。 为了避免大家踩坑,以下为自己亲测,其中出现了一些bug的相关解决方法我已做说明,文章最后有我的源代码。

该程序使用场景说明:主要用于Linux服务器爬虫程序日志监控,如出现关键字异常则触发相应的动作或告警操作,通知到邮件联系人,让你及时了解到爬虫程序是否正常爬取,也可用于服务器重要日志的监控

一、安装邮件服务

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:d1d26dec-0f66-4b6b-ad4e-8b0aa1279003

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:aca71b20-4eec-4870-ab13-eea3a8e1818b

python;gutter:true; systemctl stop sendmail systemctl disable sendmail systemctl stop postfix systemctl disbale postfix</p> <pre><code> 然后下载mailx,下载地址: ![Shell脚本日志关键字监控+告警](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230602/1479908-20200422152147234-2028180796.jpg) </code></pre> <p>http://www.linuxfromscratch.org/blfs/view/7.6/basicnet/mailx.html</p> <pre><code> 1、解压 ;gutter:true;
tar -jxf mailx-12.4.tar.bz2 -C /usr/src/ && cd /usr/src/mailx-12.4/

如果报错 tar (child): lbzip2: Cannot exec: No such file or directory

解决方法:

yum -y install bzip2

2、编译

python;gutter:true; make install UCBINSTALL=/usr/bin/install</p> <pre><code> 此时若报"make:***[openssl.o] 错误 1"则进行以下异常报错解决方法: 下载mailx补丁文件"mailx-12.4-openssl_1.0.0_build_fix-1.patch",将补丁文件和mailx解压缩目录放置同一路径. ;gutter:true;
wget http://www.linuxfromscratch.org/patches/blfs/7.6/mailx-12.4-openssl_1.0.0_build_fix-1.patch

执行打补丁命令:

python;gutter:true; patch -p0 < mailx-12.4-openssl_1.0.0_build_fix-1.patch</p> <pre><code> 进入mailx-12.4重新执行编译命令: ;gutter:true;
make install UCBINSTALL=/usr/bin/install

检测mailx是否安装成功:mailx -V

3、配置

vim /etc/nail.rc,在文件的最后添加

python;gutter:true; set from= set smtp= set smtp-auth-user=</p> <h1>不是qq密码。而是授权码</h1> <p>set smtp-auth-password= set smtp-auth=login</p> <pre><code> 如下图所示,修改配置完后保存.注意邮件地址,qq或者其他邮箱请开启SMTP,不知如何开启的请找度娘。 ![Shell脚本日志关键字监控+告警](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230602/1479908-20200422153541208-1802163629.jpg) 4、测试发送邮件 echo '邮件正文'|mailx -v -s "邮件标题" 邮箱地址 [TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:ff2b9acb-330c-4f7e-9633-80875f31816c<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:27cfb44f-1980-4be9-bc6b-e2f2c4f93a17</font>*</details> ## 二、关键字监控脚本开发 CheckLogs.sh 日志监控程序 sendmail.sh 发邮件脚本,该脚本可自定义 conf 配置文件目录,chklist为配置文件 接收到的邮件如下图所示: ![Shell脚本日志关键字监控+告警](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230602/1479908-20200422152310731-140168272.jpg) [详细代码或者文章](https://mp.weixin.qq.com/s/2iyj2I17QzyZbQlzIo9ARg),可扫描右上角二维码,关注公众号回复" **日志监控**" 进行获取,附件截图如下,包括软件、补丁、shell脚本等。 ![Shell脚本日志关键字监控+告警](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230602/1479908-20200422152439564-293120776.jpg) ;gutter:true;
作者:小张学Python
本文链接: https://mp.weixin.qq.com/s/2iyj2I17QzyZbQlzIo9ARg
转载请注明来源!!

创作不易,期待你的打赏!!

Shell脚本日志关键字监控+告警

Original: https://www.cnblogs.com/xiaozhangpython/p/12752474.html
Author: 小张学Python
Title: Shell脚本日志关键字监控+告警

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/563687/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球