-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_keepalived_by_ssh.py
executable file
·101 lines (91 loc) · 3.47 KB
/
check_keepalived_by_ssh.py
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
96
97
98
99
100
101
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
This script is a check of for known the role which have
a instance of keepalived - Leandro Lemos 20161122
https://github.com/ceso/check_keepalived_by_ssh
'''
import os
import sys
import optparse
# try load the directory where iam for load the plugin utils
myDir = os.path.dirname(__file__)
sys.path.insert(0, myDir)
try:
import schecks
except ImportError:
print("ERROR: This plugin needs the local schecks.py lib. Please install it")
sys.exit(2)
version = "0.1"
# There is no Warning, only critical
defaultWarning = None
defaultCritical = "backup"
def getRoleKa(client):
# We're looking for a line similar to this:
# KEEPALIVED-MIB::vrrpInstanceState.1 = INTEGER: master(2)
cmdGetRole = r""" /usr/bin/snmpget -v2c -cpublic localhost KEEPALIVED-MIB::vrrpInstanceState.1 """
# we'll try to get the status of the instance of KA
try:
stdin, stdout, stderr = client.exec_command(cmdGetRole)
# parse the stoutput, and store the current role of ka
currentRole = [l for l in stdout][0].strip().split(' ')[-1].rstrip('\n').split('(')[0]
return currentRole
except Exception as e:
print(e)
finally:
# before return, close the connection
client.close()
parser = optparse.OptionParser(
"%prog [options]", version="%prog " + version)
parser.add_option("-H", "--hostname",
dest="hostname",
metavar="hostname",
help="Hostname to connect to.")
parser.add_option("-p", "--port",
dest="port", type="int", default=22,
metavar="port",
help="SSH port to connect to. Default: 22")
parser.add_option("-i", "--ssh-key",
dest="sshKeyFile",
metavar="sshKeyFile",
help="SSH key file to use. By default will take ~/.ssh/id_rsa.")
parser.add_option("-u", "--user",
dest="user",
metavar="user",
help="Remote user to use. By default shinken")
parser.add_option("-P", "--passphrase",
dest="passPhrase",
metavar="passPhrase",
help="SSH key passphrase. By default will use void.")
parser.add_option("-e", "--expected-state",
dest="kaExpectedState",
metavar="kaExpectedState",
help="The expected state which Keepalived shall return. By default will be master")
parser.add_option("-c", "--critical",
dest="critical",
metavar="critical",
help="Critical value for the instance of keepalived. By default will be backup")
if __name__ == '__main__':
# first parse arguments
opts, args = parser.parse_args()
if args:
parser.error("Does not accept any argument.")
hostname = opts.hostname or ""
port = opts.port
sshKeyFile = opts.sshKeyFile or os.path.expanduser("~/.ssh/id_rsa")
user = opts.user or "shinken"
passPhrase = opts.passPhrase or ""
kaExpectedState = opts.kaExpectedState or "master"
critical = opts.critical or defaultCritical
# now, connect to the target and try to get the values
client = schecks.connect(hostname, port, sshKeyFile, passPhrase, user)
kaCurrentRole = getRoleKa(client)
if kaCurrentRole == critical:
print("Critical: expected role was {}, but the current is {}.".format(kaExpectedState, kaCurrentRole))
sys.exit(2)
elif kaCurrentRole == kaExpectedState:
print("OK: the expected role was {}, and the current is {}.".format(kaExpectedState, kaCurrentRole))
sys.exit(0)
else:
print("Unknown: SNMP or KA isn't running and I can't get the current role. Or maybe it is in fault state.")
sys.exit(3)