Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add threshold #2

Merged
merged 2 commits into from

2 participants

@urg

add ability to change check from a max check to min check (primarily for docs - we want a min number of docs in es)

@radu-gheorghe radu-gheorghe merged commit f8b8adb into radu-gheorghe:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 26, 2013
  1. @urg

    Add article option

    urg authored
Commits on Feb 27, 2013
  1. @urg

    add threshold

    urg authored
This page is out of date. Refresh to see the latest.
View
4 check_es_docs.py 100644 → 100755
@@ -13,7 +13,7 @@ def main():
#get my command-line arguments
cmdline = check_es_insert.getArgs('Nagios plugin for checking the total number of documents stored in Elasticsearch')
#make a calculator
- my_calc = check_es_insert.Calculator(warn=cmdline['warning'],crit=cmdline['critical'],myfile=cmdline['file'],myaddress=cmdline['address'])
+ my_calc = check_es_insert.Calculator(warn=cmdline['warning'],crit=cmdline['critical'],myfile=cmdline['file'],myaddress=cmdline['address'],threshold=cmdline['threshold'],index=cmdline['index'])
#get the current number of documents from Elasticsearch
(result,time) = my_calc.getCurrent()
#if there's an error, exit with UNKNOWN
@@ -22,7 +22,7 @@ def main():
check_es_insert.exiter(UNKNOWN)
else:
#otherwise, thown in some nicely formatted text
- check_es_insert.printer("Total number of documents in Elasticsearch is %d | 'es_docs'=%d;%d;%d;;" % (result,result,cmdline['warning'],cmdline['critical']))
+ check_es_insert.printer("Total number of documents in Elasticsearch (index: %s) is %d | 'es_docs'=%d;%d;%d;;" % (cmdline['index'] if cmdline['index'] != '' else 'all', result,result,cmdline['warning'],cmdline['critical']))
#and exit with the code returned by Calculator
(text,exitcode)=my_calc.printandexit(result)
check_es_insert.exiter(exitcode)
View
10 check_es_docs.test.py 100644 → 100755
@@ -13,11 +13,11 @@ def test_main_returnsUnknown_ifCalculator_returnsMinus1(self):
#expect to be called properly and return some command-line options
check_es_insert.getArgs(\
'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
- .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
+ .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla', 'threshold' : 'lt', 'index' : '' })
#mock a Calculator instance
m.StubOutClassWithMocks(check_es_insert,'Calculator')
#mock a dummy Calculator
- dummy_calculator=check_es_insert.Calculator(warn=5,crit=7,myaddress='myhost:1234', myfile='/tmp/bla')
+ dummy_calculator=check_es_insert.Calculator(warn=5,crit=7,myaddress='myhost:1234', myfile='/tmp/bla', threshold='lt', index='')
#make getCurrent return -1
dummy_calculator.getCurrent().AndReturn((-1,1338558185.54))
#mock printer()
@@ -45,11 +45,11 @@ def test_main_doesWhatPrintAndExitSays_inNormalConditions(self):
#expect to be called properly and return some command-line options
check_es_insert.getArgs(\
'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
- .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
+ .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla', 'threshold' : 'lt', 'index' : '' })
#mock a Calculator instance
m.StubOutClassWithMocks(check_es_insert,'Calculator')
#mock a dummy Calculator
- dummy_calculator=check_es_insert.Calculator(warn=5,crit=7,myaddress='myhost:1234', myfile='/tmp/bla')
+ dummy_calculator=check_es_insert.Calculator(warn=5,crit=7,myaddress='myhost:1234', myfile='/tmp/bla', threshold='lt', index='')
#make getCurrent return -1
dummy_calculator.getCurrent().AndReturn((3,1338558185.54))
#make printandexit expect to be called with the result and return something
@@ -57,7 +57,7 @@ def test_main_doesWhatPrintAndExitSays_inNormalConditions(self):
#mock printer()
m.StubOutWithMock(check_es_insert,"printer")
#expect to print "Can't get number of documents from Elasticsearch"
- check_es_insert.printer("Total number of documents in Elasticsearch is %d | 'es_docs'=%d;%d;%d;;" % (3,3,5,7))
+ check_es_insert.printer("Total number of documents in Elasticsearch (index: %s) is %d | 'es_docs'=%d;%d;%d;;" % ('all',3,3,5,7))
#mock exiter()
m.StubOutWithMock(check_es_insert,"exiter")
#expect to exit with 2
View
42 check_es_insert.py 100644 → 100755
@@ -13,11 +13,13 @@
class Calculator():
'''This is our main class. It takes results from Elasticsearch and from what was previously recorded
and can give the text to be printed and the exit code'''
- def __init__(self,warn,crit,myfile='/tmp/check_es_insert',myaddress='localhost:9200'):
+ def __init__(self,warn,crit,myfile='/tmp/check_es_insert',myaddress='localhost:9200',threshold='lt',index=''):
self.warn = warn
self.crit = crit
self.my_elasticsearcher = Elasticsearcher(address=myaddress)
self.my_disker = Disker(file=myfile)
+ self.threshold = threshold
+ self.index = index
def calculate(self,old_value,new_value,old_time,new_time):
'''Calculates the number of inserts per second since the last recording'''
return (new_value - old_value)/(new_time - old_time)
@@ -33,7 +35,7 @@ def getPrevious(self):
def getCurrent(self):
'''Gets current number of documents and current UNIX time'''
try:
- current_result = self.my_elasticsearcher.getCurrent()
+ current_result = self.my_elasticsearcher.getCurrent(self.index)
except:
#-1 is an error code. In case shit goes wrong while interrogating Elasticsearch
return (-1,0)
@@ -41,13 +43,24 @@ def getCurrent(self):
return (current_result,current_time)
def printandexit(self,result):
'''Given the number of inserts per second, it gives the formatted text and the exit code'''
- text="Number of documents inserted per second is %f | 'es_insert'=%f;%d;%d;;" % (result,result,self.warn,self.crit)
- if result<self.warn:
- return (text,OK)
- if result<self.crit:
- return (text,WARNING)
+ text="Number of documents inserted per second (index: %s) is %f | 'es_insert'=%f;%d;%d;;" % (self.index if self.index != '' else 'all', result,result,self.warn,self.crit)
+ if self.threshold == 'lt':
+ if result<self.warn:
+ return (text,OK)
+ if result<self.crit:
+ return (text,WARNING)
+ else:
+ return (text,CRITICAL)
+ elif self.threshold == 'gt':
+ if result<self.crit:
+ return (text,CRITICAL)
+ if result<self.warn:
+ return (text,WARNING)
+ else:
+ return (text,OK)
else:
- return (text,CRITICAL)
+ return ('Unknown threshold value',UNKNOWN)
+
def run(self):
'''This does everything, and returns the text to be printed and the exit code'''
#get the current number of documents and time
@@ -106,11 +119,12 @@ class Elasticsearcher():
def __init__(self,address='localhost:9200'):
self.address = address
self.mysum = 0
- def getCurrent(self):
+ def getCurrent(self, index=''):
conn = pyes.ES([self.address])
status = conn.status()
- for index in status['indices'].iterkeys():
- self.mysum = self.mysum + status['indices'][index]['docs']['num_docs']
+ for es_index in status['indices'].iterkeys():
+ if index == es_index or index == "":
+ self.mysum = self.mysum + status['indices'][es_index]['docs']['num_docs']
return self.mysum
def getArgs(helptext):
@@ -118,16 +132,20 @@ def getArgs(helptext):
parser = argparse.ArgumentParser(description=helptext)
parser.add_argument('-c','--critical', type=int, help='Critical value', action='store',required=True)
parser.add_argument('-w','--warning', type=int, help='Warning value', action='store',required=True)
+ parser.add_argument('-t','--threshold', choices=['lt','gt'], type=str, help='Check result less than (lt) or greater than (gt) the warning/critcal values', action='store',default='lt')
parser.add_argument('-a','--address', type=str, help='Elasticsearch address', action='store',default='localhost:9200')
+ parser.add_argument('-i','--index', type=str, help='Elasticsearch index', action='store',default='')
parser.add_argument('-f','--file', type=str, help='Where to store gathered data', action='store',default='/tmp/check_es_insert')
return vars(parser.parse_args())
def main():
'''The main function'''
cmdline = getArgs('Nagios plugin for checking the number of inserts per second in Elasticsearch')
+ if cmdline['file'] == '/tmp/check_es_insert' and cmdline['index'] != '':
+ cmdline['file'] = '/tmp/check_es_insert_' + cmdline['index']
#print cmdline
#exit()
- my_calculator = Calculator(warn=cmdline['warning'],crit=cmdline['critical'],myfile=cmdline['file'],myaddress=cmdline['address'])
+ my_calculator = Calculator(warn=cmdline['warning'],crit=cmdline['critical'],myfile=cmdline['file'],myaddress=cmdline['address'],threshold=cmdline['threshold'],index=cmdline['index'])
(text,exitcode) = my_calculator.run()
printer(text)
exiter(exitcode)
View
22 check_es_insert.test.py 100644 → 100755
@@ -17,10 +17,10 @@ def test_calculate_returns_documentspersecond(self):
my_calculator=check_es_insert.Calculator(warn=5,crit=7)
result=my_calculator.calculate(old_value=7,new_value=37,old_time=1338558183.7,new_time=1338558193.7)
self.assertEqual(result,3.0)
- def printandexit_runner(self,value,expected_code):
+ def printandexit_runner(self,value,expected_code,index='all'):
my_calculator=check_es_insert.Calculator(warn=7,crit=20)
(text,exit_code)=my_calculator.printandexit(result=value)
- self.assertEqual(text,"Number of documents inserted per second is %f | 'es_insert'=%f;7;20;;" % (value,value))
+ self.assertEqual(text,"Number of documents inserted per second (index: %s) is %f | 'es_insert'=%f;7;20;;" % (index,value,value))
self.assertEqual(exit_code,expected_code)
def test_printandexit_withOK_returns_expectedTextAndExpectedCode(self):
'''Given a set of values, it should return the correct exit codes for OK,WARNING,CRITICAL,UNKNOWN, and also the correct text'''
@@ -65,7 +65,7 @@ def test_getCurrent_shouldReturnWhateverElasticsearcherReturns(self):
self.mox.StubOutClassWithMocks(check_es_insert,'Elasticsearcher')
dummy_elasticsearcher = check_es_insert.Elasticsearcher(address='localhost:9200')
#make it return a value
- dummy_elasticsearcher.getCurrent().AndReturn(7)
+ dummy_elasticsearcher.getCurrent('').AndReturn(7)
#mock timer()
self.mox.StubOutWithMock(check_es_insert,"timer")
check_es_insert.timer().AndReturn(1338558185.54)
@@ -82,7 +82,7 @@ def test_getCurrent_ElasticsearcherRaisesException_returnsminus1(self):
#mock an Elasticsearcher
dummy_elasticsearcher = check_es_insert.Elasticsearcher(address='localhost:9200')
#make it raise an exception
- dummy_elasticsearcher.getCurrent().AndRaise(BaseException("Something nasty happened"))
+ dummy_elasticsearcher.getCurrent('').AndRaise(BaseException("Something nasty happened"))
#replay the mocks
self.mox.ReplayAll()
#now let's test
@@ -96,7 +96,7 @@ def test_run_shoulddoeverything(self):
#mock an Elasticsearcher
dummy_elasticsearcher = check_es_insert.Elasticsearcher(address='localhost:9200')
#make it return a value
- dummy_elasticsearcher.getCurrent().AndReturn(7)
+ dummy_elasticsearcher.getCurrent('').AndReturn(7)
#mock timer()
self.mox.StubOutWithMock(check_es_insert,"timer")
check_es_insert.timer().AndReturn(1338558185.54)
@@ -113,7 +113,7 @@ def test_run_shoulddoeverything(self):
my_calculator=check_es_insert.Calculator(warn=2,crit=3)
result = my_calculator.run()
self.assertEqual(result[1],0)
- self.assertEqual(result[0], "Number of documents inserted per second is %f | 'es_insert'=%f;%d;%d;;" % (0.787402,0.787402,2,3))
+ self.assertEqual(result[0], "Number of documents inserted per second (index: %s) is %f | 'es_insert'=%f;%d;%d;;" % ('all',0.787402,0.787402,2,3))
def test_run_writeCurrentThrowsException_shouldExitWithUnknown(self):
'''If it can't write the current results to the file, it should return UNKNOWN to alert the user'''
@@ -121,7 +121,7 @@ def test_run_writeCurrentThrowsException_shouldExitWithUnknown(self):
#mock an Elasticsearcher
dummy_elasticsearcher = check_es_insert.Elasticsearcher(address='localhost:9200')
#make it return a value
- dummy_elasticsearcher.getCurrent().AndReturn(7)
+ dummy_elasticsearcher.getCurrent('').AndReturn(7)
#mock timer()
self.mox.StubOutWithMock(check_es_insert,"timer")
check_es_insert.timer().AndReturn(1338558185.54)
@@ -146,7 +146,7 @@ def test_run_getCurrentReturnsMinus1_shouldntWriteCurrentValuesToFile(self):
#mock an Elasticsearcher
dummy_elasticsearcher = check_es_insert.Elasticsearcher(address='localhost:9200')
#make it return a value
- dummy_elasticsearcher.getCurrent().AndRaise(BaseException("Something bad happened"))
+ dummy_elasticsearcher.getCurrent('').AndRaise(BaseException("Something bad happened"))
#replay all mocks
self.mox.ReplayAll()
#now let's test
@@ -161,7 +161,7 @@ def test_run_getPreviousReturnsMinus2_shouldReturnUNKNOWNbutWriteCurrentDataIfIt
#mock an Elasticsearcher
dummy_elasticsearcher = check_es_insert.Elasticsearcher(address='localhost:9200')
#make it return a value
- dummy_elasticsearcher.getCurrent().AndReturn(7)
+ dummy_elasticsearcher.getCurrent('').AndReturn(7)
#mock timer()
self.mox.StubOutWithMock(check_es_insert,"timer")
check_es_insert.timer().AndReturn(1338558185.54)
@@ -249,11 +249,11 @@ def test_main_returnsWhateverRunReturns(self):
#mock the argument parser
m.StubOutWithMock(check_es_insert,"getArgs")
#expect to return the needed stuff
- check_es_insert.getArgs('Nagios plugin for checking the number of inserts per second in Elasticsearch').AndReturn({ 'critical' : 3, 'warning' : 2, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
+ check_es_insert.getArgs('Nagios plugin for checking the number of inserts per second in Elasticsearch').AndReturn({ 'critical' : 3, 'warning' : 2, 'address' : 'myhost:1234', 'file' : '/tmp/bla', 'threshold' : 'lt', 'index' : 'articles' })
#mock a calculator
m.StubOutClassWithMocks(check_es_insert,'Calculator')
#mock a Calculator
- dummy_calculator = check_es_insert.Calculator(warn=2, crit=3,myfile='/tmp/bla',myaddress='myhost:1234')
+ dummy_calculator = check_es_insert.Calculator(warn=2, crit=3,myfile='/tmp/bla',myaddress='myhost:1234',threshold='lt',index='articles')
#make run() return foo and 3
dummy_calculator.run().AndReturn(("foo",3))
#mock printer()
Something went wrong with that request. Please try again.