-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_couchdb
executable file
·204 lines (169 loc) · 6.87 KB
/
check_couchdb
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
#!/usr/bin/env python
#
# Nagios check couchdb 0.9.5 by FreshX Labs / Oct 08th, 2013
#
# For more info see https://github.com/FreshXOpenSource/nagios-couchdb
#
# Requirements : Unix, Python, Twisted >11.0, wallaby-backend-couchdb
#
# All required packages are prebuild for RedHat Entreprise/CentOS 6 and can be found at
# http://wallaby.freshx.de/repo/binary/Linux/RPM/
#
# Usage: check_couchdb [options]
#
# Options:
# --version show program's version number and exit
# -h, --help show this help message and exit
# --server=SERVER Servername to connect to
# -H SERVER Alias for --server
# --ssl Enable SSL
# --user=USER Authenticate as user
# --pass=PWD Authenticate with pass
# --port=PORT Connect to non default port (5984 or 6984
# --less Dump less performance values
# --debug Dump debug info's while running
#
import sys
from optparse import OptionParser
version="0.9.5"
# Please stop reading from here ;-)
# Default debug behaviour is false, will be reset by options later
debug=False
# Default exit code
exitCode=2
parser = OptionParser(version='%prog v'+version+', (C) FreshX Labs 2013')
parser.add_option("--server", type="string", action="store", help="Servername to connect to", dest="server")
parser.add_option("-H", type="string", action="store", help="Alias for --server", dest="server")
parser.add_option("--user", type="string", action="store", help="Authenticate as user", dest="user")
parser.add_option("--pass", type="string", action="store", help="Authenticate with pass", dest="pwd")
parser.add_option("--port", type="string", action="store", help="Connect to non default port", dest="port")
parser.add_option("--ssl", action="store_true", help="Enable SSL", dest="ssl")
parser.add_option("--less", action="store_true", help="Dump less performance values", dest="full")
parser.add_option("--debug", action="store_true", help="Dump debug info's while running", dest="debug")
(options,arg) = parser.parse_args()
if options.server is None:
parser.print_version()
parser.print_help()
sys.exit(2)
try:
from twisted.internet import reactor, defer
from wallaby.backends.couchdb import Database
except:
parser.print_version()
print "\nERROR : Required modules not found."
print "Please install twisted >= 11 and wallaby-backend-couchdb"
print "For more info see https://github.com/FreshXOpenSource/nagios-couchdb"
sys.exit(2)
def main(options,args):
global debug
global exitCode
if options.debug != None and options.debug ==True:
debug=True
if options.server:
proto="http://"
port = "5984"
if options.ssl != None and options.ssl == True:
proto="https://"
port = "6984"
connectionUrl=proto+options.server+':'+port
if(debug == True):
print connectionUrl
# Late import, so that the plugin runs already without prereqs for basic info
if(debug == True):
print "Starting reactor"
reactor.callWhenRunning(run, run, connectionUrl, options)
else:
d = defer.Deferred()
d.addCallback(run,connectionUrl,options)
d.addErrback(runFails)
reactor.callLater(1, d.callback, 'result')
try:
reactor.run()
except:
exitCode=2
reactor.stop()
sys.exit(exitCode)
@defer.inlineCallbacks
def runFails(failure):
global exitCode
from twisted.python import failure
f = failure.Failure()
print "CouchDB check failed: "+f.getErrorMessage()
yield reactor.stop()
@defer.inlineCallbacks
def run(self,connectionUrl,options):
global exitCode
if options.user != None:
if options.pwd == None:
print "Password required"
exitCode=2
reactor.stop()
return
if options.user == None:
db = Database('_stats', url=connectionUrl)
vdb = Database('', url=connectionUrl)
else:
db = Database('_stats', url=connectionUrl, user=options.user, password=options.pwd)
vdb = Database('', url=connectionUrl, user=options.user, password=options.pwd)
info = yield db.info(returnOnError=True)
version = yield vdb.info(returnOnError=True)
if(debug == True):
print info
if('error' in info):
print "CouchDB connection error : "+ info['reason']
reactor.stop()
return
if(debug == True):
import pprint
pprint.pprint(info)
versionStat = version['version']
couchStat = info['couchdb']
httpdStat = info['httpd_status_codes']
if type(info) != dict:
print "CouchDB returned unexpected data or no JSON structure"
reactor.stop()
return
statString='CouchDB '+versionStat+' ok|'
if couchStat['open_databases']['current'] == None:
openDBString= 'openDatabases=0'
else:
openDBString = ' openDatabases='+str(int(couchStat['open_databases']['current']))
if couchStat['open_os_files']['current'] == None:
openFileString = ' openFiles=0'
else:
openFileString = ' openFiles='+str(int(couchStat['open_os_files']['current']))
if couchStat['request_time']['mean'] == None:
requestTimeString = ' requestTimeAvg=0'
else:
requestTimeString = ' requestTimeAvg='+str(int(couchStat['request_time']['mean']))
if httpdStat['403']['mean'] != None:
err403StringA = ' notAllowedRequestsAvg='+str(httpdStat['403']['mean'])
else:
err403StringA =' notAllowedRequestsAvg=0'
if httpdStat['403']['sum'] != None:
err403String = ' notAllowedRequests='+str(int(httpdStat['403']['sum']))
else:
err403String=' notAllowedRequests=0'
if httpdStat['400']['mean'] != None:
err400StringA = ' badResponseRequestsAvg='+str(httpdStat['400']['mean'])
else:
err400StringA = ' badResponseRequestsAvg=0'
if httpdStat['400']['sum'] != None:
err400String = ' badResponseRequests='+str(int(httpdStat['400']['sum']))
else:
err400String = ' badResponseRequests=0'
if httpdStat['200']['mean'] != None:
err200StringA = ' okRequestsAvg='+str(httpdStat['200']['mean'])
else:
err200StringA = ' okRequestsAvg=0'
if httpdStat['200']['sum'] != None:
err200String = ' okRequests='+str(int(httpdStat['200']['sum']))
else:
err200String = ' okRequests=0'
if(options.full != None and options.full == True):
print statString+openDBString+requestTimeString+err403String
else:
print statString+openDBString+openFileString+requestTimeString+err403String+err400String+err200String+err403StringA+err400StringA+err200StringA
exitCode=0
reactor.stop()
if __name__ == '__main__': main(options,arg)