码迷,mamicode.com
首页 > 其他好文 > 详细

阿里云自定义监控-系统文件MD5校验

时间:2017-07-03 16:17:49      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:阿里云   utf-8   下载   log   too   格式化   new   需要   阿里   

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脚本调试了挺长时间,上面有些数据库需要按照控制台创建的监控项自定义填写,另外阿里云的自定义监控上报脚本需要从阿里云官网下载,可作为参考。

阿里云自定义监控-系统文件MD5校验

标签:阿里云   utf-8   下载   log   too   格式化   new   需要   阿里   

原文地址:http://www.cnblogs.com/cuimeng/p/7111315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!