-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_cluster_state_dependent
executable file
·95 lines (82 loc) · 2.68 KB
/
check_cluster_state_dependent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
import socket
import subprocess
import sys
import logging
logging.basicConfig()
log = logging.getLogger(__file__)
try:
import netifaces
except ImportError as e:
log.debug('Unable to import netifaces, disabling support for check with IP. Error: %s' % e)
STATE_OK = 0
STATE_WARN = 1
STATE_CRIT = 2
STATE_UNKN = 3
CRM_PATH = '/usr/sbin/crm_resource'
hostname = socket.gethostname()
def ip_addresses():
ip_list = []
for interface in netifaces.interfaces():
if netifaces.AF_INET in netifaces.ifaddresses(interface):
for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
ip_list.append(link["addr"])
if netifaces.AF_INET6 in netifaces.ifaddresses(interface):
for link in netifaces.ifaddresses(interface)[netifaces.AF_INET6]:
ip_list.append(link["addr"])
return ip_list
def cluster_is_active(resource):
"""Check if given resource is running on this node
@param resource: CRM Resource name
@type resource: str
@rtype: bool
"""
ret = subprocess.call("%s -W -r %s | grep %s" % (CRM_PATH, resource, hostname ), shell=True, stdout=subprocess.PIPE)
return ret == 0
def execute_plugin(plugin_params):
plugin = plugin_params[0]
if len(plugin_params) > 1:
plugin_args = plugin_params[1:]
else:
plugin_args = []
cmdline = "%s %s" % (plugin, " ".join(plugin_args))
try:
ret = subprocess.check_output(cmdline, shell=True)
print (ret.decode('utf8'))
sys.exit(STATE_OK)
except subprocess.CalledProcessError as ex:
print(ex.output.decode('utf8'))
sys.exit(ex.returncode)
def main():
argp = argparse.ArgumentParser()
argp.add_argument('-c', '--crm-resource', help='Corosync Resource name', dest='crm')
if 'netifaces' in sys.modules:
argp.add_argument('-i', '--ip', help='IP address', dest='ip')
argp.add_argument('plugin', help='Nagios Plugin Name', nargs=argparse.REMAINDER)
args = argp.parse_args()
if len(args.plugin) < 1:
print("UNKNOWN: Need at least one plugin to be called")
sys.exit(STATE_UNKN)
if args.crm is None and args.ip is None:
print("UNKNOWN: Need at least one check parameter")
sys.exit(STATE_UNKN)
elif args.crm is not None:
if os.path.exists(CRM_PATH) and os.access(CRM_PATH, os.X_OK):
if cluster_is_active(args.crm):
execute_plugin(args.plugin)
else:
print("OK: Cluster resource %s is not active on host %s" % (args.crm, hostname))
else:
print("UNKNOWN: crm resource binary %s is not callable" % CRM_PATH)
sys.exit(STATE_UNKN)
elif args.ip is not None:
if args.ip in ip_addresses():
execute_plugin(args.plugin)
else:
print("OK: Cluster IP %s is not active on host %s" % (args.ip, hostname))
sys.exit(STATE_OK)
if __name__ == '__main__':
main()