阿里云自定义监控

时间:2023-01-27 09:34:15

1、文件md5校验
hashlib模块 hashlib.md5()

2、文件比对模块
filecmp模块 filecmp.cmp('md5File', 'md5File_new'): ##文件一直返回True 不一致返回False

3、打开文件操作
with open ..... as f: ##不用close关闭文件

4、格式化字符串输出
tring = "%s %s\n" % (md5.hexdigest(),line.strip())

脚本示例:

#!/usr/bin/python
#
-*- coding: utf-8 -*-
import hashlib
import os
import filecmp
import sys
import cms_post

def usagemd5():
md5
= hashlib.md5() ##定义MD5校验方法
with open('/data/tool/scripts/md5check/md5File_new', 'wb+') as f:
f.truncate()
##清空文件
with open('/data/tool/scripts/md5check/pathfile', 'r') as file:
for line in file.readlines(): ##按行读取file文件并传给line
md5.update(line) ##md5校验line
string = "%s %s\n" % (md5.hexdigest(),line.strip()) ##格式化输出字符串
with open('/data/tool/scripts/md5check/md5File_new', 'a') as f:
f.writelines(string)
##将string写入f
if filecmp.cmp('md5File', 'md5File_new'): ##比对MD5Flie和MD5File_new文件,
return 0 ##一致返回True
else:
return 1
md5_usage
=usagemd5()

if __name__ == '__main__':
cms_post.post(
"1576486466921525","MD5Check",md5_usage,"Count","md5check1576486466921525=1") ##上报信息值

 

以上是阿里云自定义监控的系统文件MD5校验监控,我这边适应阿里云cms脚本调试了挺长时间,上面有些数据库需要按照控制台创建的监控项自定义填写,另外阿里云的自定义监控上报脚本需要从阿里云官网下载,可作为参考。