From 39558916b84a717bfed3b8027fcff764335018a8 Mon Sep 17 00:00:00 2001 From: Ralph Offinger Date: Tue, 16 May 2017 17:05:12 +0200 Subject: [PATCH] Release 0.3.2 --- HISTORY.rst | 6 +- README.rst | 4 +- check_pa/__init__.py | 2 +- check_pa/check_paloalto.py | 2 +- check_pa/modules/throughput.py | 12 +-- setup.py | 2 +- tests/test_throughput.py | 141 ++++++++++++++++++++++++--------- 7 files changed, 121 insertions(+), 48 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 9c8bf80..0b68b3a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,10 @@ History ------- +0.3.2 (2017-16-05) +------------------ +* Fixed issue #8: Measuring throughput on multiple identically configured PA's fails + 0.3.1 (2017-10-03) ------------------ @@ -10,7 +14,7 @@ History * Removed the the unit of measurement (UOM) for throughput command 0.3 (2017-27-02) ------------------- +---------------- * Support for Python 3.5 added * Minor code improvements diff --git a/README.rst b/README.rst index 0470256..cc67a59 100644 --- a/README.rst +++ b/README.rst @@ -7,11 +7,11 @@ It is written in Python and based on the PA REST API. Tested on: - PA-500 v6.0.1 - v6.0.9 -- PA-3050 v6.0.9 - 7.0.9 +- PA-3050 v6.0.9 - 7.1.9 .. image:: https://travis-ci.org/ralph-hm/nagios_check_paloalto.svg?branch=master :target: https://travis-ci.org/ralph-hm/nagios_check_paloalto?branch=master - + .. image:: https://coveralls.io/repos/github/ralph-hm/nagios_check_paloalto/badge.svg?branch=master :target: https://coveralls.io/github/ralph-hm/nagios_check_paloalto?branch=master diff --git a/check_pa/__init__.py b/check_pa/__init__.py index 4174c78..dc490fd 100755 --- a/check_pa/__init__.py +++ b/check_pa/__init__.py @@ -2,4 +2,4 @@ __author__ = 'Ralph Offinger' __email__ = 'ralph.offinger@gmail.com' -__version__ = '0.3.1' +__version__ = '0.3.2' diff --git a/check_pa/check_paloalto.py b/check_pa/check_paloalto.py index 48da931..70022b9 100755 --- a/check_pa/check_paloalto.py +++ b/check_pa/check_paloalto.py @@ -38,7 +38,7 @@ def parse_args(args): info = parser.add_argument_group('Info') info.add_argument('--version', action='version', - version='%(prog)s 0.3.1') + version='%(prog)s 0.3.2') subparsers = parser.add_subparsers(dest='command') subparsers.required = True diff --git a/check_pa/modules/throughput.py b/check_pa/modules/throughput.py index 8896d34..baa4711 100644 --- a/check_pa/modules/throughput.py +++ b/check_pa/modules/throughput.py @@ -86,16 +86,16 @@ def probe(self): _log.debug('Path to statefile: %r' % get_statefile_path()) with np.Cookie(get_statefile_path()) as cookie: - old_inbytes = cookie.get(self.interface_name + 'i', api_inbytes) - old_outbytes = cookie.get(self.interface_name + 'o', api_outbytes) - old_time = cookie.get(self.interface_name + 't', current_time) + old_inbytes = cookie.get(self.host + self.interface_name + 'i', api_inbytes) + old_outbytes = cookie.get(self.host + self.interface_name + 'o', api_outbytes) + old_time = cookie.get(self.host + self.interface_name + 't', current_time) if not api_inbytes or not api_outbytes or float(api_inbytes) < 0 or float(api_outbytes) < 0: raise np.CheckError('Couldn\'t get a valid value!') - cookie[self.interface_name + 'i'] = api_inbytes - cookie[self.interface_name + 'o'] = api_outbytes - cookie[self.interface_name + 't'] = current_time + cookie[self.host + self.interface_name + 'i'] = api_inbytes + cookie[self.host + self.interface_name + 'o'] = api_outbytes + cookie[self.host + self.interface_name + 't'] = current_time if float(api_inbytes) < float(old_inbytes) or float(api_outbytes) < float(old_outbytes): raise np.CheckError('Couldn\'t get a valid value: Found throughput less then old!') diff --git a/setup.py b/setup.py index dc52930..205151f 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ ] setup(name='check_paloalto', - version='0.3.1', + version='0.3.2', description="check_paloalto is a Nagios/Icinga plugin for Palo Alto Next Generation Firewalls. It is written in " "Python and based on the PA REST API.", long_description=readme + '\n\n' + history, diff --git a/tests/test_throughput.py b/tests/test_throughput.py index 38ef573..f3d5d7d 100644 --- a/tests/test_throughput.py +++ b/tests/test_throughput.py @@ -65,15 +65,15 @@ def test_with_three_interfaces(self, statefile): # Resetting cookies with Cookie(statefile) as cookie: - cookie[interfaces[0] + 'i'] = 0 - cookie[interfaces[0] + 'o'] = 0 - cookie[interfaces[0] + 't'] = 1441324800 - cookie[interfaces[1] + 'i'] = 0 - cookie[interfaces[1] + 'o'] = 0 - cookie[interfaces[1] + 't'] = 1441324800 - cookie[interfaces[2] + 'i'] = 0 - cookie[interfaces[2] + 'o'] = 0 - cookie[interfaces[2] + 't'] = 1441324800 + cookie[self.host + interfaces[0] + 'i'] = 0 + cookie[self.host + interfaces[0] + 'o'] = 0 + cookie[self.host + interfaces[0] + 't'] = 1441324800 + cookie[self.host + interfaces[1] + 'i'] = 0 + cookie[self.host + interfaces[1] + 'o'] = 0 + cookie[self.host + interfaces[1] + 't'] = 1441324800 + cookie[self.host + interfaces[2] + 'i'] = 0 + cookie[self.host + interfaces[2] + 'o'] = 0 + cookie[self.host + interfaces[2] + 't'] = 1441324800 # Check will be executed exactly one second later now = 1441324801 @@ -107,8 +107,8 @@ def test_with_one_interface(self, statefile): check = check_pa.modules.throughput.create_check(self) objects = [] - for ressource in check.resources: - objects.append(ressource) + for res in check.resources: + objects.append(res) with responses.RequestsMock() as rsps: @@ -122,9 +122,9 @@ def test_with_one_interface(self, statefile): from nagiosplugin import Cookie with Cookie(statefile) as cookie: - cookie[interfaces[0] + 'i'] = 0 - cookie[interfaces[0] + 'o'] = 0 - cookie[interfaces[0] + 't'] = 1441324800 + cookie[self.host + interfaces[0] + 'i'] = 0 + cookie[self.host + interfaces[0] + 'o'] = 0 + cookie[self.host + interfaces[0] + 't'] = 1441324800 # Check will be executed exactly ten seconds later now = 1441324810 @@ -143,6 +143,76 @@ def test_with_one_interface(self, statefile): assert check.summary_str == 'Input is 0.8 Mb/s - Output is 0.8 ' \ 'Mb/s' + def check_pa(self, time, ibytes, obytes, filename): + objects = [] + file1 = filename + + check = check_pa.modules.throughput.create_check(self) + + for res in check.resources: + objects.append(res) + + with responses.RequestsMock() as rsps: + rsps.add(responses.GET, + objects[0].xml_obj.build_request_url(), + body=utils.read_xml(file1), + status=200, + content_type='document', + match_querystring=True) + + with mock.patch('check_pa.modules.throughput.get_time', + return_value=time): + with mock.patch('check_pa.xml_reader.Finder.find_item', + side_effect=[ibytes, obytes]): + with pytest.raises(SystemExit): + check.main(verbose=3) + + return check + + @responses.activate + def test_with_different_ips(self, statefile): + + pa_1 = self.__class__() + pa_1.host = "192.168.0.1" + pa_1.interface = "ethernet1/1" + + pa_2 = self.__class__() + pa_2.host = "192.168.0.2" + pa_2.interface = "ethernet1/1" + + from nagiosplugin import Cookie + + with Cookie(statefile) as cookie: + cookie[pa_1.host + pa_1.interface + 'i'] = 0 + cookie[pa_1.host + pa_1.interface + 'o'] = 0 + cookie[pa_1.host + pa_1.interface + 't'] = 1441324800 + + check = pa_1.check_pa(1441324800, 10, 10, "throughput1.xml") + + assert check.exitcode == 3 + assert check.state == ServiceState(code=3, text='unknown') + assert check.summary_str == 'Difference between old timestamp and new timestamp is less or equal 0: If it is the first time you run the script, please execute it again!' + + check = pa_2.check_pa(1441324810, 110, 110, "throughput1.xml") + + assert check.exitcode == 3 + assert check.state == ServiceState(code=3, text='unknown') + assert check.summary_str == 'Difference between old timestamp and new timestamp is less or equal 0: If it is the first time you run the script, please execute it again!' + + check = pa_1.check_pa(1441324801, 1000000, 1000000, "throughput1.xml") + assert check.exitcode == 0 + assert check.state == ServiceState(code=0, text='ok') + # 1000000 Byte = 1 MByte = 8 Mbit in 1 second = 8.0 Mb/s + assert check.summary_str == 'Input is 8.0 Mb/s - Output is 8.0 ' \ + 'Mb/s' + + check = pa_2.check_pa(1441324811, 1000000, 1000000, "throughput1.xml") + assert check.exitcode == 0 + assert check.state == ServiceState(code=0, text='ok') + # 1000000 Byte = 1 MByte = 8 Mbit in 1 second = 8.0 Mb/s + assert check.summary_str == 'Input is 8.0 Mb/s - Output is 8.0 ' \ + 'Mb/s' + @responses.activate def test_new_input_less_than_old(self, statefile): file1 = 'throughput1.xml' @@ -154,8 +224,8 @@ def test_new_input_less_than_old(self, statefile): check = check_pa.modules.throughput.create_check(self) objects = [] - for ressource in check.resources: - objects.append(ressource) + for res in check.resources: + objects.append(res) with responses.RequestsMock() as rsps: @@ -169,9 +239,9 @@ def test_new_input_less_than_old(self, statefile): from nagiosplugin import Cookie with Cookie(statefile) as cookie: - cookie[interfaces[0] + 'i'] = 10 - cookie[interfaces[0] + 'o'] = 10 - cookie[interfaces[0] + 't'] = 1441324800 + cookie[self.host + interfaces[0] + 'i'] = 10 + cookie[self.host + interfaces[0] + 'o'] = 10 + cookie[self.host + interfaces[0] + 't'] = 1441324800 # Check will be executed exactly ten seconds later now = 1441324810 @@ -200,8 +270,8 @@ def test_new_output_less_than_old(self, statefile): check = check_pa.modules.throughput.create_check(self) objects = [] - for ressource in check.resources: - objects.append(ressource) + for res in check.resources: + objects.append(res) with responses.RequestsMock() as rsps: @@ -215,9 +285,9 @@ def test_new_output_less_than_old(self, statefile): from nagiosplugin import Cookie with Cookie(statefile) as cookie: - cookie[interfaces[0] + 'i'] = 10 - cookie[interfaces[0] + 'o'] = 10 - cookie[interfaces[0] + 't'] = 1441324800 + cookie[self.host + interfaces[0] + 'i'] = 10 + cookie[self.host + interfaces[0] + 'o'] = 10 + cookie[self.host + interfaces[0] + 't'] = 1441324800 # Check will be executed exactly ten seconds later now = 1441324810 @@ -232,9 +302,9 @@ def test_new_output_less_than_old(self, statefile): check.main(verbose=3) with Cookie(statefile) as cookie: - input = cookie.get(interfaces[0] + 'i') - output = cookie.get(interfaces[0] + 'o') - time = cookie.get(interfaces[0] + 't') + input = cookie.get(self.host + interfaces[0] + 'i') + output = cookie.get(self.host + interfaces[0] + 'o') + time = cookie.get(self.host + interfaces[0] + 't') assert input == xml_ibytes assert output == xml_obytes @@ -244,7 +314,6 @@ def test_new_output_less_than_old(self, statefile): assert check.state == ServiceState(code=3, text='unknown') assert check.summary_str == 'Couldn\'t get a valid value: Found throughput less then old!' - @responses.activate def test_same_time(self, statefile): file1 = 'throughput1.xml' @@ -270,9 +339,9 @@ def test_same_time(self, statefile): from nagiosplugin import Cookie with Cookie(statefile) as cookie: - cookie[interfaces[0] + 'i'] = 10 - cookie[interfaces[0] + 'o'] = 10 - cookie[interfaces[0] + 't'] = 1441324800 + cookie[self.host + interfaces[0] + 'i'] = 10 + cookie[self.host + interfaces[0] + 'o'] = 10 + cookie[self.host + interfaces[0] + 't'] = 1441324800 # Check will be executed exactly at the same time now = 1441324800 @@ -305,8 +374,8 @@ def test_api_failed(self, statefile): check = check_pa.modules.throughput.create_check(self) objects = [] - for ressource in check.resources: - objects.append(ressource) + for res in check.resources: + objects.append(res) with responses.RequestsMock() as rsps: @@ -320,9 +389,9 @@ def test_api_failed(self, statefile): from nagiosplugin import Cookie with Cookie(statefile) as cookie: - cookie[interfaces[0] + 'i'] = 10 - cookie[interfaces[0] + 'o'] = 10 - cookie[interfaces[0] + 't'] = 1441324800 + cookie[self.host + interfaces[0] + 'i'] = 10 + cookie[self.host + interfaces[0] + 'o'] = 10 + cookie[self.host + interfaces[0] + 't'] = 1441324800 # Check will be executed exactly ten seconds later now = 1441324810