-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcheck_mdadm_by_ssh.py
executable file
·110 lines (89 loc) · 3.67 KB
/
check_mdadm_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
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python2
# Copyright (C) 2013:
# Gabes Jean, [email protected]
# Pasche Sebastien, [email protected]
# Benjamin Moran, [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
"""This script checks Linux RAID status via /proc/mdstat. """
import os
import sys
# Ok try to load our directory to load the plugin utils.
my_dir = os.path.dirname(__file__)
sys.path.insert(0, my_dir)
try:
import schecks
except ImportError:
print "ERROR : this plugin needs the local schecks.py lib. Please install it"
sys.exit(2)
VERSION = "0.3"
def get_raid_status(client):
# Default values
mdraid_healthy = True
mdraid_recover = '100'
mdraid_check = '0'
mdraid_sync = '0'
# Try to read in the mdstat file contents.
cat_mdstat = 'cat /proc/mdstat'
stdin, stdout, stderr = client.exec_command('export LC_LANG=C && unset LANG && %s' % cat_mdstat)
if stderr.read():
print 'No MDRAID arrays found'
sys.exit(0)
else:
mdadm_lines = [line.strip() for line in stdout]
# Sometimes a /proc/mdstat will exist, even when there are no active arrays.
# Check to see if any md* arrays exist.
if not any('md' in line for line in mdadm_lines):
print 'No active MDRAID arrays found'
sys.exit(0)
# Check if there are any missing RAID devices. If so, array must be degraded.
if any('_' in line for line in mdadm_lines):
mdraid_healthy = False
# Check the RAID scrub, sync, and recover status.
for line in mdadm_lines:
if 'check' in line:
mdraid_check = line.split()[3][:-1]
elif 'sync' in line:
mdraid_sync = line.split()[3][:-1]
elif 'recover' in line:
mdraid_recover = line.split()[3][:-1]
# Before return, close the client
client.close()
return mdraid_healthy, mdraid_recover, mdraid_check, mdraid_sync
###############################################################################
parser = schecks.get_parser()
if __name__ == '__main__':
# Ok first job : parse args
opts, args = parser.parse_args()
# Ok now got an object that link to our destination
client = schecks.get_client(opts)
# Scrape /proc/mdstat and get result and perf data
healthy, recover, scrub, sync = get_raid_status(client)
# Format perf data
perf = "|Recover={0}%;0%;100% Scrub={1}%;0%;100% Sync={2}%;0%;100%".format(recover, scrub, sync)
if not float(recover) == 100:
print("WARNING: RAID is recovering " + perf)
sys.exit(1)
elif healthy:
print "OK: RAID is healthy " + perf
sys.exit(0)
else:
print "CRITICAL: RAID is degraded " + perf
sys.exit(2)