class testPlugin(unittest.TestCase): def setUp(self): self.argv_store = sys.argv from pynag.Plugins import simple as Plugin self.np = Plugin() sys.stdout = StringIO() sys.stderr = StringIO() def tearDown(self): sys.argv = self.argv_store sys.stdout = original_stdout sys.stderr = original_stderr def run_expect(self, case, expected_exit, value): sys.argv = [sys.argv[0]] + case.split() self.np.activate() try: self.np.add_perfdata('fake', value, uom='fakes', warn=10, crit=20, minimum=-100, maximum=100) perfdata_string = self.np.perfdata_string() print perfdata_string self.assertEquals(perfdata_string, "| '%s'=%s%s;%s;%s;%s;%s" % ( 'fake', value, 'fakes', 10, 20, -100, 100)) self.np.add_message('OK', 'Some message') self.assertEquals(self.np.data['messages'][0], ['Some message']) self.np.check_range(value) except SystemExit, e: self.assertEquals(type(e), type(SystemExit())) self.assertEquals(e.code, expected_exit) except Exception, e: import traceback print traceback.format_exc() self.fail('unexpected exception: %s' % e)
import os, sys ## Import plugin from nagios Module from pynag.Plugins import simple as Plugin ## Create the plugin option np = Plugin() ## Add a command line argument np.add_arg("l", "load-file", "Enter a load average file", required=None) ## This starts the actual plugin activation np.activate() ## Use a custom load average file, if specified to if np['load-file']: load_file = np['load-file'] else: load_file = "/proc/loadavg" if not os.path.isfile(load_file): np.nagios_exit("UNKNOWN", "Missing Load average file %s" % load_file) ## Get the check value current_load = open(load_file).readline().split()[0] ## Add the perdata np.add_perfdata("1min", current_load) np.check_range(current_load)
## This is for the custom nagios module sys.path.insert(1, '../') from pynag.Plugins import simple as Plugin ## Create the plugin option np = Plugin() ## Add a command line argument np.add_arg("l","load-file", "Enter a load average file", required=None) ## This starts the actual plugin activation np.activate() ## Use a custom load average file, if specified to if np['load-file']: load_file = np['load-file'] else: load_file = "/proc/loadavg" if not os.path.isfile(load_file): np.nagios_exit("UNKNOWN", "Missing Load average file %s" % load_file) ## Get the check value current_load = os.popen("cat %s" % load_file).readline().split()[0] ## Add the perdata np.add_perfdata("1min", current_load) np.check_range(current_load)
line.strip() (l_type, l_id, l_data) = line.split(" ", 3) if mtr_output.has_key(l_id) == False: mtr_output[l_id] = { 'p': [] } if l_type == "p": mtr_output[l_id][l_type].append(int(l_data)) else: mtr_output[l_id][l_type] = l_data for k in range(0, len(mtr_output)): obj = str(k) try: avg = sum(mtr_output[obj]['p']) / len(mtr_output[obj]['p']) np.add_perfdata('hop%s' % (obj), avg) np.add_message(OK, "%s average %imsec" % (mtr_output[obj]['h'], float(avg / 1000))) except: pass if np['long-output']: (code, message) = np.check_messages(joinallstr = "\n") else: (code, message) = np.check_messages() # Exit with the relevant code np.nagios_exit( code, message ) # ##h 5 92.43.192.110 ##d 5 neumann.mbl.is ##p 5 35334
for k, v in self.__info.iteritems(): self.__dict__[k] = v disque = Disque() info_properties = [ "used_memory_rate", "connected_clients", "client_longest_output_list", "client_biggest_input_buf", "client_biggest_input_buf", "rejected_connections", "total_commands_processed", "total_connections_received", "used_memory_human", "used_memory_peak_human", "mem_fragmentation_ratio", "instantaneous_ops_per_sec", ] for info_property in info_properties: np.add_perfdata(info_propert, getattr(disque, info_property)) code, messages = np.check_messages() np.nagios_exit( code, messages, )
def main(): global np global tmpdir # new pynag.Plugin np = Plugin(must_threshold=False) # Arguments np.add_arg('f', 'file', 'Remote file, space seperate multiple files', required=False) np.add_arg('w', 'warning', 'Warn if tftp downloads take longer', required=False) np.add_arg('c', 'critical', 'Critical if tftp downloads take longer', required=False) np.add_arg('l', 'longoutput', 'Each file broken up into a new line for readability', required=False, action="store_true") # Activate np.activate() if np['host'] == "": np.nagios_exit(UNKNOWN, "Hostname is required") tmpdir = mktmpdir() end_time = time.time() + int(np['timeout'] or 20) # data.txt add manually contient list of file that we will check # dirname = os.path.join('data', 'tftplist.txt') dir = os.path.dirname(os.path.realpath(__file__)) with open(dir + '/data.txt', 'r') as myfile: files = myfile.read().replace('\n', '') # creat list of ips #files = np['file'].split() with open(dir + '/ips.txt', 'r') as myips: ips = myips.read().replace('\n', '') # Loop through the files for ip in ips: for file in files: file_start_time = time.time() try: size = tftp_get(ip, file, timeout=(end_time - time.time())) file_end_time = time.time() run_time = time.time() - file_start_time if size is not False: if np['critical'] and run_time >= int(np['critical']): stat = CRITICAL elif np['warning'] and run_time >= int(np['warning']): stat = WARNING else: stat = OK np.add_message( stat, "tftp://%s/%s got %iB in %.2f secs" % (np['host'], file, size, (file_end_time - file_start_time))) np.add_perfdata("%s_size" % (file), size) np.add_perfdata("%s_fetch" % (file), (file_end_time - file_start_time)) except Exception, e: np.nagios_exit(UNKNOWN, e)
try: process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None) output = process.communicate()[0] except OSError as ex: plugin.nagios_exit(Plugins.UNKNOWN, str(ex)) if process.returncode != 0: plugin.nagios_exit(Plugins.CRITICAL, output.rstrip()) matcher = re.search(r'^Leap status\s*:\s*(.*)$', output, flags=re.MULTILINE) leap_status = matcher.group(1) if leap_status == 'Not synchronised': plugin.nagios_exit(Plugins.CRITICAL, 'Server is not synchronised') if leap_status == 'Unknown': plugin.nagios_exit(Plugins.UNKNOWN, 'Server status is unknown') matcher = re.search(r'^System time\s*:\s*([0-9]+\.[0-9]*) seconds (slow|fast)', output, flags=re.MULTILINE) offset = float(matcher.group(1)) status = Plugins.check_threshold(abs(offset), warning=plugin['warning'], critical=plugin['critical']) plugin.add_perfdata('offset', offset, uom='s', warn=plugin['warning'], crit=plugin['critical']) plugin.nagios_exit(status, 'Offset {} seconds'.format(offset))
number_of_instance = len(instances_health) number_of_running_instance = 0 for instance_health in instances_health: if instance_health.state == 'InService': number_of_running_instance += 1 if np["numbers"] == None: desired_number = number_of_instance / 2 else: desired_number = int(np["numbers"]) # Performance Data warn_perfdata = desired_number * 1.25 if warn_perfdata > desired_number: warn_perfdata = desired_number np.add_perfdata("running", number_of_running_instance, None, warn_perfdata, desired_number, 0, number_of_instance) np.add_perfdata("all", number_of_instance) # Return value if desired_number > number_of_running_instance: np.nagios_exit( CRITICAL, "Only %d instance running, %d desired" % (number_of_running_instance, desired_number)) else: np.nagios_exit( OK, "%d instance run, %d desired" % (number_of_running_instance, desired_number))
class Plugin(unittest.TestCase): def setUp(self): self.argv_store = sys.argv from pynag.Plugins import simple as Plugin self.np = Plugin() sys.stdout = StringIO() sys.stderr = StringIO() def tearDown(self): sys.argv = self.argv_store sys.stdout = original_stdout sys.stderr = original_stderr def run_expect(self, case, expected_exit, value): sys.argv = [sys.argv[0]] + case.split() self.np.activate() try: self.np.add_perfdata('fake', value, uom='fakes', warn=10, crit=20, minimum=-100, maximum=100) perfdata_string = self.np.perfdata_string() print(perfdata_string) self.assertEquals(perfdata_string, "| '%s'=%s%s;%s;%s;%s;%s" % ( 'fake', value, 'fakes', 10, 20, -100, 100)) self.np.add_message('OK', 'Some message') self.assertEquals(self.np.data['messages'][0], ['Some message']) self.np.check_range(value) except SystemExit as e: self.assertEquals(type(e), type(SystemExit())) self.assertEquals(e.code, expected_exit) except Exception as e: import traceback print(traceback.format_exc()) self.fail('unexpected exception: %s' % e) else: self.fail('SystemExit exception expected') # Throws SystemExit, required parameter not set when activating def test_add_arg_req_missing(self): self.np.add_arg('F', 'fakedata', 'fake data to test thresholds', required=True) self.assertRaises(SystemExit, self.np.activate) def test_add_arg_req(self): self.np.add_arg('F', 'fakedata', 'fake data to test thresholds', required=True) sys.argv = [sys.argv[0]] + '-F 100 -w 1 -c 2'.split() self.np.activate() def test_add_arg(self): self.np.add_arg('F', 'fakedata', 'fake data to test thresholds', required=False) sys.argv = [sys.argv[0]] + '-w 1 -c 2'.split() self.np.activate() def test_codestring_to_int(self): code = self.np.code_string2int('OK') self.assertEquals(code, 0, "OK did not map to 0") code = self.np.code_string2int('WARNING') self.assertEquals(code, 1, "WARNING did not map to 1") code = self.np.code_string2int('CRITICAL') self.assertEquals(code, 2, "CRITICAL did not map to 2") code = self.np.code_string2int('UNKNOWN') self.assertEquals(code, 3, "UNKNOWN did not map to 3") # Critical if "stuff" is over 20, else warn if over 10 # (will be critical if "stuff" is less than 0) def test_number_1(self): case = '-w 10 -c 20' self.run_expect(case, 2, -23) def test_number_2(self): case = '-w 10 -c 20' self.run_expect(case, 0, 3) def test_number_3(self): case = '-w 10 -c 20' self.run_expect(case, 1, 13) def test_number_4(self): case = '-w 10 -c 20' self.run_expect(case, 2, 23) # Same as above. Negative "stuff" is OK def test_number_5(self): case = '-w ~:10 -c ~:20' self.run_expect(case, 0, -23) def test_number_6(self): case = '-w ~:10 -c ~:20' self.run_expect(case, 0, 3) def test_number_7(self): case = '-w ~:10 -c ~:20' self.run_expect(case, 1, 13) def test_number_8(self): case = '-w ~:10 -c ~:20' self.run_expect(case, 2, 23) # Critical if "stuff" is over 20, else warn if "stuff" is below 10 # (will be critical if "stuff" is less than 0) def test_number_9(self): case = '-w 10: -c 20' self.run_expect(case, 2, -23) def test_number_10(self): case = '-w 10: -c 20' self.run_expect(case, 1, 3) def test_number_11(self): case = '-w 10: -c 20' self.run_expect(case, 0, 13) def test_number_12(self): case = '-w 10: -c 20' self.run_expect(case, 2, 23) # Critical if "stuff" is less than 1 def test_number_13(self): case = '-c 1:' self.run_expect(case, 2, -23) def test_number_14(self): case = '-c 1:' self.run_expect(case, 2, 0) def test_number_15(self): case = '-c 1:' self.run_expect(case, 0, 13) def test_number_16(self): case = '-c 1:' self.run_expect(case, 0, 23) # 1-9 is warning, negative or above 10 is critical def test_number_17(self): case = '-w ~:0 -c 10' self.run_expect(case, 2, -23) def test_number_18(self): case = '-w ~:0 -c 10' self.run_expect(case, 0, 0) def test_number_19(self): case = '-w ~:0 -c 10' self.run_expect(case, 1, 7) def test_number_20(self): case = '-w ~:0 -c 10' self.run_expect(case, 2, 23) # The only noncritical range is 5:6 def test_number_21(self): case = '-c 5:6' self.run_expect(case, 2, -23) def test_number_22(self): case = '-c 5:6' self.run_expect(case, 2, 0) def test_number_23(self): case = '-c 5:6' self.run_expect(case, 2, 2) def test_number_24(self): case = '-c 5:6' self.run_expect(case, 0, 5) def test_number_25(self): case = '-c 5:6' self.run_expect(case, 0, 6) # Critical if "stuff" is 10 to 20 def test_number_26(self): case = '-c @10:20' self.run_expect(case, 0, -23) def test_number_27(self): case = '-c @10:20' self.run_expect(case, 0, 0) def test_number_28(self): case = '-c @10:20' self.run_expect(case, 0, 2) def test_number_29(self): case = '-c @10:20' self.run_expect(case, 2, 10) def test_number_30(self): case = '-c @10:20' self.run_expect(case, 2, 15) def test_number_31(self): case = '-c @10:20' self.run_expect(case, 2, 20) def test_number_32(self): case = '-c @10:20' self.run_expect(case, 0, 23)
np.nagios_exit(UNKNOWN, "Unable to get elb list. Is network up ? Is region configured ? (Region %s)" % ( conn.DefaultRegionName)) number_of_instance=len(instances_health) number_of_running_instance=0 for instance_health in instances_health: if instance_health.state == 'InService': number_of_running_instance += 1 if np["numbers"] == None: desired_number = number_of_instance/2 else: desired_number = int(np["numbers"]) # Performance Data warn_perfdata = desired_number*1.25 if warn_perfdata > desired_number: warn_perfdata = desired_number np.add_perfdata("running", number_of_running_instance, None, warn_perfdata, desired_number, 0, number_of_instance) np.add_perfdata("all", number_of_instance) # Return value if desired_number > number_of_running_instance: np.nagios_exit(CRITICAL, "Only %d instance running, %d desired" % ( number_of_running_instance, desired_number )) else: np.nagios_exit(OK, "%d instance run, %d desired" % ( number_of_running_instance, desired_number ))