#!/usr/bin/python from __future__ import division import sys,time,pyes,argparse ###CONSTANTS### OK=0 WARNING=1 CRITICAL=2 UNKNOWN=3 ###END CONSTANTS### 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'): self.warn = warn self.crit = crit self.my_elasticsearcher = Elasticsearcher(address=myaddress) self.my_disker = Disker(file=myfile) 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) def getPrevious(self): '''Gets the previously recorded number of documents and UNIX time''' try: previous = self.my_disker.getPrevious() except: #-2 is an error code. In case shit goes wrong while accessing the file return (-2,0) #return the result and time return (previous[0],previous[1]) def getCurrent(self): '''Gets current number of documents and current UNIX time''' try: current_result = self.my_elasticsearcher.getCurrent() except: #-1 is an error code. In case shit goes wrong while interrogating Elasticsearch return (-1,0) current_time = timer() 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