Initial commit
Benjamin Renard

Benjamin Renard commited on 2013-12-03 10:00:39
Showing 3 changed files, with 255 additions and 0 deletions.

... ...
@@ -0,0 +1 @@
1
+*~
... ...
@@ -0,0 +1,47 @@
1
+Nagios plugin to check Ceph cluster usage
2
+=========================================
3
+
4
+Usage
5
+-----
6
+
7
+    Usage: check_ceph_usage [options]
8
+    
9
+    Options:
10
+      -h, --help            show this help message and exit
11
+      -d, --debug           
12
+      -b BIN, --bin=BIN     Ceph binary (default : /usr/bin/ceph)
13
+      --conf=CONF           Ceph configuration file
14
+      -m MON, --mon=MON     Ceph monitor address[:port]
15
+      -i ID, --id=ID        Ceph client id
16
+      -k KEYRING, --keyring=KEYRING
17
+                            Ceph client keyring file
18
+      -w WARNDATA, --warning-data=WARNDATA
19
+                            Warning data threshold (default : 70%)
20
+      -c CRITDATA, --critical-data=CRITDATA
21
+                            Critical data threshold (default : 85%)
22
+      -W WARNALLOC, --warning-allocated=WARNALLOC
23
+                            Warning allocated threshold (default : 80%)
24
+      -C CRITALLOC, --critical-allocated=CRITALLOC
25
+                            Critical allocated threshold (default : 90%)
26
+
27
+Copyright
28
+---------
29
+
30
+Copyright (c) 2013 Benjamin Renard <brenard@zionetrix.net>
31
+
32
+License
33
+-------
34
+
35
+This program is free software; you can redistribute it and/or
36
+modify it under the terms of the GNU General Public License version 2
37
+as published by the Free Software Foundation.
38
+
39
+This program is distributed in the hope that it will be useful,
40
+but WITHOUT ANY WARRANTY; without even the implied warranty of
41
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
+GNU General Public License for more details.
43
+
44
+You should have received a copy of the GNU General Public License
45
+along with this program; if not, write to the Free Software
46
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
47
+
... ...
@@ -0,0 +1,207 @@
1
+#!/usr/bin/python
2
+#
3
+# Nagios plugin to check Ceph cluster usage
4
+#
5
+#     Usage: check_ceph_usage [options]
6
+#     
7
+#     Options:
8
+#       -h, --help            show this help message and exit
9
+#       -d, --debug           
10
+#       -b BIN, --bin=BIN     Ceph binary (default : /usr/bin/ceph)
11
+#       --conf=CONF           Ceph configuration file
12
+#       -m MON, --mon=MON     Ceph monitor address[:port]
13
+#       -i ID, --id=ID        Ceph client id
14
+#       -k KEYRING, --keyring=KEYRING
15
+#                             Ceph client keyring file
16
+#       -w WARNDATA, --warning-data=WARNDATA
17
+#                             Warning data threshold (default : 70%)
18
+#       -c CRITDATA, --critical-data=CRITDATA
19
+#                             Critical data threshold (default : 85%)
20
+#       -W WARNALLOC, --warning-allocated=WARNALLOC
21
+#                             Warning allocated threshold (default : 80%)
22
+#       -C CRITALLOC, --critical-allocated=CRITALLOC
23
+#                             Critical allocated threshold (default : 90%)
24
+#
25
+# Copyright (c) 2013 Benjamin Renard <brenard@zionetrix.net>
26
+#
27
+# This program is free software; you can redistribute it and/or
28
+# modify it under the terms of the GNU General Public License version 2
29
+# as published by the Free Software Foundation.
30
+# 
31
+# This program is distributed in the hope that it will be useful,
32
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
33
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
+# GNU General Public License for more details.
35
+# 
36
+# You should have received a copy of the GNU General Public License
37
+# along with this program; if not, write to the Free Software
38
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
39
+#
40
+
41
+import sys,os,json,subprocess
42
+from optparse import OptionParser
43
+
44
+# default ceph values
45
+CEPH_COMMAND = '/usr/bin/ceph'
46
+WARN_DATA = 70
47
+CRIT_DATA = 85
48
+WARN_ALLOC = 80
49
+CRIT_ALLOC = 90
50
+
51
+# nagios exit code
52
+STATUS_OK = 0
53
+STATUS_WARNING = 1
54
+STATUS_ERROR = 2
55
+STATUS_UNKNOWN = 3
56
+
57
+parser = OptionParser()
58
+parser.add_option('-d',
59
+                  '--debug',
60
+                  action="store_true",
61
+                  dest="debug",
62
+                  default=False)
63
+
64
+parser.add_option('-b',
65
+                  '--bin',
66
+                  action="store",
67
+                  dest="bin",
68
+                  help="Ceph binary (default : %s)" % CEPH_COMMAND,
69
+                  type='string',
70
+                  default=CEPH_COMMAND)
71
+
72
+parser.add_option('--conf',
73
+                  action="store",
74
+                  dest="conf",
75
+                  help="Ceph configuration file",
76
+                  type='string',
77
+                  default=None)
78
+
79
+parser.add_option('-m',
80
+                  '--mon',
81
+                  action="store",
82
+                  dest="mon",
83
+                  help="Ceph monitor address[:port]",
84
+                  type='string',
85
+                  default=None)
86
+
87
+parser.add_option('-i',
88
+                  '--id',
89
+                  action="store",
90
+                  dest="id",
91
+                  help="Ceph client id",
92
+                  type='string',
93
+                  default=None)
94
+
95
+parser.add_option('-k',
96
+                  '--keyring',
97
+                  action="store",
98
+                  dest="keyring",
99
+                  help="Ceph client keyring file",
100
+                  type='string',
101
+                  default=None)
102
+
103
+parser.add_option('-w',
104
+                  '--warning-data',
105
+                  action="store",
106
+                  dest="warndata",
107
+                  help="Warning data threshold (default : %s%%)" % WARN_DATA,
108
+                  type='int',
109
+                  default=WARN_DATA)
110
+
111
+parser.add_option('-c',
112
+                  '--critical-data',
113
+                  action="store",
114
+                  dest="critdata",
115
+                  help="Critical data threshold (default : %s%%)" % CRIT_DATA,
116
+                  type='int',
117
+                  default=CRIT_DATA)
118
+
119
+parser.add_option('-W',
120
+                  '--warning-allocated',
121
+                  action="store",
122
+                  dest="warnalloc",
123
+                  help="Warning allocated threshold (default : %s%%)" % WARN_ALLOC,
124
+                  type='int',
125
+                  default=WARN_ALLOC)
126
+
127
+parser.add_option('-C',
128
+                  '--critical-allocated',
129
+                  action="store",
130
+                  dest="critalloc",
131
+                  help="Critical allocated threshold (default : %s%%)" % CRIT_ALLOC,
132
+                  type='int',
133
+                  default=CRIT_ALLOC)
134
+
135
+(options, args) = parser.parse_args()
136
+
137
+ # validate args
138
+if not os.path.exists(options.bin):
139
+    print "ERROR: ceph executable '%s' doesn't exist" % options.bin
140
+    sys.exit(STATUS_UNKNOWN)
141
+
142
+if options.conf and not os.path.exists(options.conf):
143
+    print "ERROR: ceph conf file '%s' doesn't exist" % options.conf
144
+    sys.exit(STATUS_UNKNOWN)
145
+
146
+if options.keyring and not os.path.exists(options.keyring):
147
+    print "ERROR: keyring file '%s' doesn't exist" % options.keyring
148
+    sys.exit(STATUS_UNKNOWN)
149
+
150
+# build command
151
+ceph_cmd = [options.bin]
152
+if options.mon:
153
+    ceph_cmd.append('-m')
154
+    ceph_cmd.append(options.mon)
155
+if options.conf:
156
+    ceph_cmd.append('-c')
157
+    ceph_cmd.append(options.conf)
158
+if options.id:
159
+    ceph_cmd.append('--id')
160
+    ceph_cmd.append(options.id)
161
+if options.keyring:
162
+    ceph_cmd.append('--keyring')
163
+    ceph_cmd.append(options.keyring)
164
+ceph_cmd.append('status')
165
+ceph_cmd.append('--format=json')
166
+    
167
+# exec command
168
+p = subprocess.Popen(ceph_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
169
+output, err = p.communicate()
170
+
171
+if output:
172
+	data=json.loads(output)
173
+	if 'pgmap' not in data:
174
+		print "UNKNOWN : fail to read pgmap status"
175
+		sys.exit(STATUS_UNKNOWN)
176
+
177
+	if options.debug:
178
+		print "data : %s" % data['pgmap']['data_bytes']
179
+		print "allocated : %s" % data['pgmap']['bytes_used']
180
+		print "total : %s" % data['pgmap']['bytes_total']
181
+
182
+	PER_DATA=round(int(data['pgmap']['data_bytes'])*100/int(data['pgmap']['bytes_total']),1)
183
+	DATA_WARN_T=int(int(data['pgmap']['bytes_total'])*options.warndata/100)
184
+	DATA_CRIT_T=int(int(data['pgmap']['bytes_total'])*options.critdata/100)
185
+	PER_ALLOC=round(int(data['pgmap']['bytes_used'])*100/int(data['pgmap']['bytes_total']),1)
186
+	ALLOC_WARN_T=int(int(data['pgmap']['bytes_total'])*options.warnalloc/100)
187
+	ALLOC_CRIT_T=int(int(data['pgmap']['bytes_total'])*options.critalloc/100)
188
+
189
+
190
+	if options.debug:
191
+		print "%% data : %s" % PER_DATA
192
+		print "%% allocated : %s" % PER_ALLOC
193
+
194
+	STATUS=STATUS_OK
195
+	STATUS_TXT="OK"
196
+	if PER_DATA > options.critdata or PER_ALLOC > options.critalloc:
197
+		STATUS=STATUS_CRITICAL
198
+		STATUS_TXT="CRITICAL"
199
+	elif PER_DATA > options.warndata or PER_ALLOC > options.warnalloc:
200
+		STATUS=STATUS_WARNING
201
+		STATUS_TXT="WARNING"
202
+
203
+	print "%s : %s%% allocated / %s%% really used|allocated=%sB;%s;%s;0;%s,used=%sB;%s;%s;0;%s" % (STATUS_TXT,PER_ALLOC,PER_DATA,data['pgmap']['bytes_used'],ALLOC_WARN_T,ALLOC_CRIT_T,data['pgmap']['bytes_total'],data['pgmap']['data_bytes'],DATA_WARN_T,DATA_CRIT_T,data['pgmap']['bytes_total'])
204
+	sys.exit(STATUS)
205
+else:
206
+	print "UNKNOWN : fail to execute ceph status command"
207
+	sys.exit(STATUS_UNKNOWN)
0 208