-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_coraid.py
executable file
·264 lines (201 loc) · 7.48 KB
/
check_coraid.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# check_coraid.py
"""
Nagios plugin, checks status of a Coraid shelf. Runs commands 'show -l'
and 'list -l' and compares output with a previously stored file.
Requirements:
* Coraid Ethernet Console (cec)
* 'pexpect' Python module
Before using the plugin for monitoring a Coraid device you must store its
output on a file to compare against. You can do it with the option '--create'.
Do it when the Coraid device is in good status.
Example:
# check_coraid.py -i eth2 --shelf 0 --create
This plugin must run 'cec' as root and, if there's a timeout, must be able to
kill it. That means you need something like this on your /etc/sudoers file.
nagios ALL= NOPASSWD: /usr/local/lib/nagios/plugins/check_coraid.py
This is giving full privileges to the script, so please check that no one can
overwrite the Nagios plugin neither the 'cec' binary.
This script is inspired on 'aoe-chk-coraid.sh' by William A. Arlofski
(http://www.revpol.com/coraid_scripts).
"""
# Copyright 2009 Jordi Funollet <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pexpect
import StringIO
import sys
import os
from optparse import OptionParser
import logging
# Full path of the 'cec' binary.
CEC = '/usr/local/bin/cec'
# How many seconds to wait before killing 'cec'.
CEC_TIMEOUT = 5
def parse_command_line ():
"""Optparse wrapper.
"""
usage = "usage: %prog <options>"
parser = OptionParser(usage=usage)
parser.add_option("-s", "--shelf", action="store", default=0,
help="number of the shelf (default: 0)")
parser.add_option("-i", "--interface", action="store", default='eth0',
help="interface to bind (default: eth0)")
parser.add_option("-b", "--basedir", action="store",
default='/var/lib/check_coraid',
help="directory for baseline files (default: /var/lib/check_coraid)")
parser.add_option("-w", "--show", action="store_true",
help="show commands on stdout and exit",)
parser.add_option("-c", "--create", action="store_true",
help="create initial baseline file",)
parser.add_option("-d", "--debug", action="store_true", default=False,
help="show debugging info")
options, args = parser.parse_args()
return (options, args)
def nagios_ok(msg):
"""Outputs OK message in Nagios format and exits.
"""
print "OK: %s" % msg
sys.exit(0)
def nagios_critical(msg):
"""Outputs CRITICAL message in Nagios format and exits.
"""
print "CRITICAL: %s" % msg
sys.exit(2)
def nagios_unknown(msg):
"""Outputs UNKNOWN message in Nagios format and exits.
"""
print "UNKNOWN: %s" % msg
sys.exit(3)
def cec_expect(shelf, interface):
"""Runs commands 'show -l' and 'list -l' on a Coraid console.
@shelf: number of the shelf
@interface: interface to bind
Uses the pexpect module to send commands and retrieve output.
"""
# Using pexpect with the 'cec' client gives a unsorted or noisy
# output. I add some extra carriage-returns before and after the
# commands and filter the output to remove lines without information.
# This workaround seems to be enough.
cec_cmd = "%s -s%s -ee %s" % (CEC, shelf, interface)
# Run with 'sudo' unless we are root.
if os.getuid() != 0:
cec_cmd = "sudo %s" % cec_cmd
logging.debug(cec_cmd)
# File-like object to write pexpect output.
output = StringIO.StringIO()
child = pexpect.spawn(cec_cmd, timeout=CEC_TIMEOUT)
try:
child.expect("Escape is Ctrl-e")
child.sendline("")
child.sendline("")
child.sendline("")
child.expect("SR shelf(.*)>")
child.logfile = output
# Run 'show -l'.
child.sendline("show -l")
child.sendline("")
child.sendline("")
child.sendline("")
child.expect("SR shelf(.*)>")
# Run 'list -l'
child.sendline("")
child.sendline("")
child.sendline("")
child.sendline("list -l")
child.sendline("")
child.sendline("")
child.sendline("")
child.expect("SR shelf(.*)>")
# Stop capturing output.
child.logfile = None
# Disconnect.
child.send("\r")
child.send("")
child.expect(">>>")
child.send("q\r")
child.expect(pexpect.EOF)
child.close()
except (pexpect.TIMEOUT, pexpect.EOF):
child.close(force=True)
return cec_normalize(output.getvalue())
def is_informative(line):
"""Returns True if the line contains significative information.
"""
not_needed = ('SR shelf', 'list -l', 'show -l',
'warning: show is deprecated')
if line == '' or line.isspace() or line.startswith(not_needed):
return False
return True
def cec_normalize(text):
"""Filter irrelevant lines.
"""
lines = text.split('\n')
goodlines = [line for line in lines if is_informative(line)]
return '\n'.join(goodlines)
def get_baseline(baseline_fname):
"""Returns the contents of a baseline file for use as reference.
@shelf: number of the shelf.
@base_dir: directory with the baseline files.
"""
try:
baseline_file = open(baseline_fname)
except IOError:
nagios_unknown(
"cannot open %s. Run the plugin with --create for initialization."
% baseline_fname)
return baseline_file.read()
def create_baseline(baseline_fname, contents):
"""Create a baseline file to compare with on next runs.
"""
try:
baseline_file = open(baseline_fname, 'w')
except IOError:
nagios_unknown("cannot open %s" % baseline_fname)
baseline_file.write(contents)
baseline_file.close()
def main():
"""Runs unless the file is imported.
"""
# Parse command-line.
opts, ___ = parse_command_line()
# Set debug level.
if opts.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
baseline_fname = os.path.join(opts.basedir,
'shelf%s.baseline' % opts.shelf)
if not opts.create and not opts.show:
baseline = get_baseline(baseline_fname)
# Test we have the 'cec' binary.
if not os.path.isfile(CEC):
nagios_unknown("%s not found" % CEC)
try:
output = cec_expect(opts.shelf, opts.interface)
except pexpect.TIMEOUT:
nagios_critical("AoE shelf%s not responding" % opts.shelf)
if opts.create:
create_baseline(baseline_fname, output)
sys.exit()
if opts.show:
print output
sys.exit()
if baseline == output:
nagios_ok("AoE shelf%s looks as usual" % opts.shelf)
else:
nagios_critical("AoE shelf%s has changes" % opts.shelf)
if __name__ == '__main__':
main()