check_db 分布式检测

1.基础

准备环境
[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-279.el6.x86_64 #1 SMP Wed Jun 13 18:24:36 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# yum install http -y #安装http 从机安装的

2.相关脚本

check_db从机
#!/usr/bin/env python
import os, time , sys,multiprocessing,commands
import json
print 'Content-Type: text/html\n'
list_a=os.environ['QUERY_STRING'].split('&')
def helloworld(name):
    xxx=commands.getstatusoutput('%s'%(name))
    return json.dumps(xxx)
check_db="""java -classpath /usr/local/nagios/libexec/.:/usr/local/nagios/libexec/lib/JSAP-2.0a.jar:/usr/local/nagios/libexec/lib/classes12.jar DbCheck %s"""%(' '.join(list_a))
check_echo="""echo %s"""%(list_a[1:])
check_tcp="""/usr/local/nagios/libexec/check_tcp  %s -t 1""" %(' '.join(list_a[1:5]))
if commands.getstatusoutput('%s'%(check_tcp))[0] == 0:
    print helloworld(check_db)
else:
    print helloworld(check_tcp)
check_db服务端脚本
#!/usr/bin/env python
import sys,urllib2
import commands
from optparse import OptionParser, OptionGroup
import json
import re
def parse_args():
    usage = ''
    parser = OptionParser(usage=usage)
    required = OptionGroup(parser, "Usage: DbCheck [-h|--help] [-H <host>] [-p <port>] -s <sid> -l <login> -x <password> -f <sqlfile> -r <regex> [-w <warning>] [-c <critical>] -L <label> [-u <unit>] [-v <chkvalue>]")
    required.add_option('-H' , '--host', help='Hostname/ip address of database server. (default: )', default=None)
    required.add_option('-p' , '--port', help='Listener port, default is 1521. (default: )', default=1521)
    required.add_option('-s' , '--sid', help='Oracle database SID. (default: )', default=None)
    required.add_option('-l' , '--login', help='Oracle database user to connect.', default=None)
    required.add_option('-x' , '--password', help='Oracle database user password.', default=None)
    required.add_option('-f', '--sqlfile', help='Full path of sql file to be executed.', default=None)
    required.add_option('-r' , '--regex', help="""PERL style Regular Expression to match in SQL output. Check value must
            be enclosed between (), should match only one field and must be enclosed
            between single quotes. Example 'SYSTEM.*,.*,(.*)'""", default=None)
    required.add_option('-w' , '--warning', help="""Warning value for matched expression. Normally warning < critical value.
            If your check want to alert when result < warning , set critical less
            than warning (default: 0)""", default=None)
    required.add_option('-c' , '--critical', help="""Critical value for matched expression. Should be more than Warning
            value. If not check behaviour is reversed, see warning help text above.
            (default: 0)""", default=None)
    required.add_option('-L', '--label', help='Label for matched regex value i.e FieldName.', default=None)
    required.add_option('-u' , '--unit', help='Value unit i.e MB % (default: )', default='')
    required.add_option('-v' , '--chkvalue', help="""Check Value,if not return critical (default: )""", default=None)
    options, _ = parser.parse_args()
    return options
def test(web_url,web_argv):
    web_url='http://'+web_url+'?'+web_argv
    response = urllib2.urlopen(web_url).read()
    data=json.loads(response)
    return data
def web_aa_a(arg1,arg):
    if arg:
        return "&%s&'%s'" %(arg1,arg)
    else:
        return
def nagios_check_print(data):
    print data[1]
    if re.match('^CRITICAL', data[1], re.IGNORECASE) or \
       re.match('Connection refused', data[1], re.IGNORECASE) or \
       re.match('java.sql.SQLException', data[1], re.IGNORECASE) :
        sys.exit(2)
    elif re.match('^WARNING', data[1], re.IGNORECASE):
        sys.exit(1)
    else:
        sys.exit(0)
if __name__ == '__main__':
    web_url=['*/cgi-bin/check_db2']#从机提供的http地址
    options = parse_args()
    contacts=['10.199.20.11;1']#检测细分
    benji = 0
    fuzai=0
    for i in contacts:
        try:
            #print
            if options.host == i.split(';')[0]:
                fuzai=int(i.split(';')[1])-1
                break
        except:
            fuzai=int(1)
            benji=0
    if options.chkvalue == None:
        web_argv="""%s%s%s%s%s%s%s%s%s%s%s"""%(web_aa_a('-H',options.host),web_aa_a('-p',options.port),
            web_aa_a('-s',options.sid),web_aa_a('-l',options.login),web_aa_a('-x',options.password),
            web_aa_a('-r',options.regex),web_aa_a('-w',options.warning),web_aa_a('-c',options.critical),
            web_aa_a('-L',options.label),web_aa_a('-u',options.unit),web_aa_a('-f',options.sqlfile))
        check_db="""java -classpath /usr/local/nagios/libexec/.:/usr/local/nagios/libexec/lib/JSAP-2.0a.jar:/usr/local/nagios/libexec/lib/classes12.jar DbCheck -H '%s' -p '%s' -s '%s' -l '%s' -x '%s' -r '%s' -w '%s' -c '%s' -L '%s' -u '%s' -f '%s' """%(
            options.host,options.port,
            options.sid,options.login,options.password,
            options.regex,options.warning,options.critical,
            options.label,options.unit,options.sqlfile)
    else:
        web_argv="""%s%s%s%s%s%s%s%s%s%s%s%s"""%(web_aa_a('-H',options.host),web_aa_a('-p',options.port),
            web_aa_a('-s',options.sid),web_aa_a('-l',options.login),web_aa_a('-x',options.password),
            web_aa_a('-r',options.regex),web_aa_a('-w',options.warning),web_aa_a('-c',options.critical),
            web_aa_a('-L',options.label),web_aa_a('-u',options.unit),web_aa_a('-f',options.sqlfile),web_aa_a('-f',options.chkvalue))
        check_db="""java -classpath /usr/local/nagios/libexec/.:/usr/local/nagios/libexec/lib/JSAP-2.0a.jar:/usr/local/nagios/libexec/lib/classes12.jar DbCheck -H '%s' -p '%s' -s '%s' -l '%s' -x '%s' -r '%s' -w '%s' -c '%s' -L '%s' -u '%s' -f '%s' -v '%s' """%(
            options.host,options.port,
            options.sid,options.login,options.password,
            options.regex,options.warning,options.critical,
            options.label,options.unit,options.sqlfile,options.chkvalue)
    check_tcp="""/usr/local/nagios/libexec/check_tcp -H %s -p %s -t 1""" %(options.host,options.port)
    data=test(web_url[fuzai],web_argv)
    nagios_check_print(data)
 pythonlogo


发表评论

登录 后发表评论.