检查Linux系统日志error和mysql错误日志的脚本

对系统日志的检查和数据库日志的检查很重要,出现问题及时的通知系统管理员更为重要,本脚本用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错误日志检测,也适用于单个示例的检测,根据自己的需求做修改。

  
  
  
  
  1. #!/usr/bin/env python

  2. #encoding=utf-8

  3. import os, sys


  4. def chk_err(log_file,tmp_file,type,print_list,port):

  5.    cur_num = int(os.popen("sudo grep '' %s | wc -l" % log_file).read().strip())

  6.    old_num = 0

  7. if os.path.exists(tmp_file):

  8.        old_num = int(open(tmp_file).read().strip())

  9. if cur_num < old_num:

  10.            os.popen("echo 0 > %s" % tmp_file)

  11.            old_num = 0

  12. else:

  13.        os.popen("echo 0 > %s" % tmp_file)

  14.    err_log = os.popen("sudo grep -ni 'error' %s" % log_file).readlines()

  15. if err_log:

  16.        err_list = []

  17. for err in err_log:

  18. if int(err.split(":")[0]) > old_num:

  19.                err_list.append(err[len(err.split(":")[0])+1:])

  20. if err_list:

  21.            os.popen("echo %s > %s" % (err_log[-1].split(":")[0], tmp_file))

  22.            print_list.append(port)


  23. def chk_err_log(type):

  24. try:

  25.        print_list = []

  26.        homedir = "/home/zabbix"

  27. ifnot os.path.exists(homedir):

  28.            os.mkdir(homedir)

  29. if type == "syslog":

  30.            log_file = "/var/log/messages"

  31.            tmp_file = "%s/.syslog_num"%homedir

  32.            cur_num = int(os.popen("sudo grep '' %s | wc -l" % log_file).read().strip())

  33.            old_num = 0

  34. if os.path.exists(tmp_file):

  35.                old_num = int(open(tmp_file).read().strip())

  36. if cur_num < old_num:

  37.                    os.popen("echo 0 > %s" % tmp_file)

  38.                    old_num = 0

  39. else:

  40.                os.popen("echo 0 > %s" % tmp_file)

  41.            err_log = os.popen("sudo grep -ni 'error' %s|grep -v snmpd|grep -v sftp" % log_file).readlines()

  42. ifnot err_log:

  43. return"0"

  44.            err_list = []

  45. for err in err_log:

  46. if int(err.split(":")[0]) > old_num:

  47.                    err_list.append(err[len(err.split(":")[0])+1:])

  48. ifnot err_list:

  49. return"0"

  50. else:

  51.                os.popen("echo %s > %s" % (err_log[-1].split(":")[0], tmp_file))

  52. return"1"

  53. elif type == "mysqllog":

  54.            psinfo = os.popen("ps auxww|grep mysqld|grep -v root|grep -v grep").readlines()

  55. ifnot psinfo:

  56. return"No mysqld running in this server now"

  57. for i in psinfo:

  58.                port = "0"

  59. for j in i.split("--"):

  60. if j.find("datadir") != -1:

  61.                        datadir = j.split("=")[1].strip()

  62. elif j.find("port") != -1:

  63.                        port = j.split("=")[1].strip()

  64. if port == "0":

  65. continue

  66. if port == "3306":

  67.                    log_file = "%s/$(hostname).err" % datadir

  68. else:

  69.                    log_file = "%s/mysql.err" % datadir

  70.                tmp_file = "%s/.mysqllog_%s" % (homedir,port)

  71.                chk_err(log_file,tmp_file,type,print_list,port)

  72. if len(print_list)==0:

  73. return"0"

  74. else:

  75. return print_list

  76. except Exception, e:

  77. return e


  78. if __name__ == "__main__":

  79. print chk_err_log(sys.argv[1])



你可能感兴趣的:(数据库,python,error,服务器,系统管理员)