[Tool Development] keepalived uses the nagios monitoring script, keepalivednagios
Recently, keepalived and keepalived are used in the development and office environments. Based on the LVS supported by the Linux kernel, keepalived achieves high availability and load balancing, which is very practical.
Keepalived can be used to monitor service status in multiple ways, such as HTTP_GET | SSL_GET | TCP_CHECK | SMTP_CHECK | MISC_CHECK (Custom Script Mode). To precisely monitor service status, we need to write a custom monitoring script.
At present, many companies deploy nagios as a service monitoring platform. nagios also needs monitoring scripts or programs. If the keepalived monitoring script can be applied to naigios, it can save a lot of trouble. However, the nagios monitoring script or program is different from keepalived:
Returned values of nagios scripts or programs: 0-OK, 1-Warn, 2-Critical, 3-Unknown
Returned values of the keepalived Script: 0-OK, 1-Fail, 2 ~ 255-OK and exit code-2
It can be seen that you only need to change the returned values of the nagios script or program to keepalived. If it is a text nagios script, you can directly change the return value. If it is a binary program, you cannot directly change it. The method I use is to put the nagios Monitoring Program in python and modify its return value, so that it can be used for keepalived.
The following example uses python to change the check_dns binary Monitoring Program of nagios to a script that can be used by keepalived. Note that the commands in commands. getstatusoutput are called the nagios binary Monitoring Program:
(Because my service does not need to modify the exit status code, only 0 or 1 is returned)
#! /Usr/bin/python # coding: UTF-8 # Return 0: Health Check OK, weight keep # return 1: Health Check failed, weight set to 0 # Return 2-255: the health check is OK, and the weight is set to: Exit status code-2 import commandsimport sys, getoptimport OS. pathdef check_dns (server = '', timeout ='', name = '', address =''): status, output = commands. getstatusoutput ("/usr/lib64/nagios/plugins/check_dns-s" + server + "-t" + timeout + "-H" + name + "-a" + address) return statusif _ name _ = '_ main _': server, timeout, name, address = '', ''script = OS. path. split (OS. path. realpath (_ file _) [1] try: opts, args = getopt. getopt (sys. argv [1:], "s: n: a: t:", ["server =", "name =", "address =", "timeout ="]) counter t getopt. getoptError, err: # print str (err) sys. exit (3) for o, a in opts: if o in ("-s", "-- server"): server = a elif o in ("-n ", "-- name"): name = a elif o in ("-a", "-- address"): address = a elif o in ("-t ", "-- timeout"): timeout = a else: # print "usage:" + script + "-s server-n name-a address-t timeout" sys. exit (3) if server = ''or name ='' or address = ''or timeout ='': # print "usage: "+ script +"-s server-n name-a address-t timeout "sys. exit (3) else: status = check_dns (server, timeout, name, address) if status: # print status sys. exit (1) else: # print status sys. exit (0)