LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 脚本编程 >

检查Linux系统与mysql错误日志脚本

时间:2013-01-06 10:24来源:51CTO 编辑:lover007 点击:
对系统日志的检查和数据库日志的检查很重要,出现问题及时的通知系统管理员更为重要,本脚本用python写的监控脚本,主要是为zabbix监控自定义的key而准备的,当然大家也可以在返回
对系统日志的检查和数据库日志的检查很重要,出现问题及时的通知系统管理员更为重要,本脚本用python写的监控脚本,主要是为zabbix监控自定义的key而准备的,当然大家也可以在返回值方面做修改,可以在写个发邮件的模块,做个定时,有问题自动发邮件(在之前写过一个发邮件的类,大家可以做参考:http://wangwei007.blog.51cto.com/68019/978743)。在zabbix中自定义key来检测系统日志和数据库日志:
UnsafeUserParameters=1
UserParameter=check.sys_error,/usr/local/zabbix/bin/chk_err_log.py syslog
UserParameter=check.mysql_error,/usr/local/zabbix/bin/chk_err_log.py mysqllog

本脚本适合一台服务器多实例的mysql错误日志检测,也适用于单个示例的检测,根据自己的需求做修改。
    #!/usr/bin/env python 
    #encoding=utf-8 
    import os, sys 
     
    def chk_err(log_file,tmp_file,type,print_list,port=""): 
        cur_num = int(os.popen("sudo grep '' %s | wc -l" % log_file).read().strip()) 
        old_num = 0 
        if os.path.exists(tmp_file): 
            old_num = int(open(tmp_file).read().strip()) 
            if cur_num < old_num: 
                os.popen("echo 0 > %s" % tmp_file) 
                old_num = 0 
        else: 
            os.popen("echo 0 > %s" % tmp_file) 
        err_log = os.popen("sudo grep -ni 'error' %s" % log_file).readlines() 
        if err_log: 
            err_list = [] 
            for err in err_log: 
                if int(err.split(":")[0]) > old_num: 
                    err_list.append(err[len(err.split(":")[0])+1:]) 
            if err_list: 
                os.popen("echo %s > %s" % (err_log[-1].split(":")[0], tmp_file)) 
                if type == "syslog": 
                    print "0" 
                elif type == "mysqllog": 
                    print_list.append(port) 
        elif not err_log and type == "syslog": 
            print "0" 
     
    def chk_err_log(type): 
        try: 
            print_list = [] 
            homedir = "/home/zabbix" 
            if not os.path.exists(homedir): 
                os.mkdir(homedir) 
            if type == "syslog": 
                log_file = "/var/log/messages" 
                tmp_file = "%s/.syslog_num"%homedir 
                chk_err(log_file,tmp_file,type,print_list) 
            elif type == "mysqllog": 
                psinfo = os.popen("ps -auxww|grep mysqld|grep -v root|grep -v grep").readlines() 
                if not psinfo: 
                    print "0" 
                for i in psinfo: 
                    for j in i.split("--"): 
                        if j.find("datadir") != -1: 
                            datadir = j.split("=")[1].strip() 
                        elif j.find("port") != -1: 
                            port = j.split("=")[1].strip() 
                    if port == "3306": 
                        log_file = "%s/$(hostname).err" % datadir 
                    else: 
                        log_file = "%s/mysql.err" % datadir 
                    tmp_file = "%s/.mysqllog_%s" % (homedir,port) 
                    chk_err(log_file,tmp_file,type,print_list,port) 
                if len(print_list)==0: 
                    print "0" 
                else: 
                    if type == "syslog": 
                        print "0" 
                    elif type == "mysqllog": 
                        print print_list 
        except Exception, e: 
            print e 
     
    if __name__ == "__main__": 
        chk_err_log(sys.argv[1]) 

转载请保留固定链接: https://linuxeye.com/program/1135.html

------分隔线----------------------------
标签:错误日志
栏目列表
推荐内容