def test_thermal(self):
        self.warn = 80
        self.crit = 90

        f = 'thermal.xml'
        check = check_pa.modules.thermal.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 0
            assert check.state == ServiceState(code=0, text='ok')
            assert check.summary_str == 'Temperature @ Test1 is 30 degrees' \
                                        ' Celsius, Temperature @ Test2 is 34' \
                                        ' degrees Celsius, Temperature @' \
                                        ' Test3 is 37 degrees Celsius,' \
                                        ' Temperature @ Test4' \
                                        ' is 25 degrees Celsius'
    def test_sessinfo_warning2(self):
        f = 'mock_result.xml'
        self.warn = 4000
        self.crit = 5000

        check = check_pa.modules.sessioninfo.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)

            nummax = 262142
            numactive = 5002
            kbps = 24266

            with mock.patch('check_pa.xml_reader.Finder.find_item',
                            side_effect=[nummax, numactive, kbps]):
                with pytest.raises(SystemExit):
                    check.main(verbose=3)

            assert check.exitcode == 2
            assert check.state == ServiceState(code=2, text='critical')
            assert check.summary_str == 'session is 5002 (outside range 0:5000)'
    def test_with_different_ips(self, statefile):

        pa_1 = self.__class__()
        pa_1.host = "192.168.0.1"
        pa_1.interface = "ethernet1/1"
        pa_1.warn = 8000000000
        pa_1.crit = 9000000000

        pa_2 = self.__class__()
        pa_2.host = "192.168.0.2"
        pa_2.interface = "ethernet1/1"
        pa_2.warn = 8000000000
        pa_2.crit = 9000000000

        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'
    def test_new_output_less_than_old(self, statefile):
        file1 = 'throughput1.xml'

        self.warn = 8000000000
        self.crit = 9000000000
        self.interface = 'ethernet1/1'
        interfaces = []
        for interface in self.interface.split(','):
            interfaces.append(interface)

        check = check_pa.modules.throughput.create_check(self)
        objects = []
        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)

            from nagiosplugin import Cookie

            with Cookie(statefile) as cookie:
                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
            xml_ibytes = 11
            xml_obytes = 9

            with mock.patch('check_pa.modules.throughput.get_time',
                            return_value=now):
                with mock.patch('check_pa.xml_reader.Finder.find_item',
                                side_effect=[xml_ibytes, xml_obytes]):
                    with pytest.raises(SystemExit):
                        check.main(verbose=3)

            with Cookie(statefile) as cookie:
                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
                assert time == now

        assert check.exitcode == 3
        assert check.state == ServiceState(code=3, text='unknown')
        assert check.summary_str == 'Couldn\'t get a valid value: Found throughput less then old!'
    def test_same_time(self, statefile):
        file1 = 'throughput1.xml'

        self.warn = 8000000000
        self.crit = 9000000000
        self.interface = 'ethernet1/1'
        interfaces = []
        for interface in self.interface.split(','):
            interfaces.append(interface)

        check = check_pa.modules.throughput.create_check(self)
        objects = []
        for resource in check.resources:
            objects.append(resource)

        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)

            from nagiosplugin import Cookie

            with Cookie(statefile) as cookie:
                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

            xml_ibytes = 11
            xml_obytes = 10

            with mock.patch('check_pa.modules.throughput.get_time',
                            return_value=now):
                with mock.patch('check_pa.xml_reader.Finder.find_item',
                                side_effect=[xml_ibytes, xml_obytes]):
                    with pytest.raises(SystemExit):
                        check.main(verbose=3)

            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!'
    def test_with_one_interface(self, statefile):
        file1 = 'throughput1.xml'

        self.warn = 8000000000
        self.crit = 9000000000
        self.interface = 'ethernet1/1'
        interfaces = []
        for interface in self.interface.split(','):
            interfaces.append(interface)

        check = check_pa.modules.throughput.create_check(self)
        objects = []
        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)

            from nagiosplugin import Cookie

            with Cookie(statefile) as cookie:
                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

            xml_ibytes = 1000000  # 1000000 Byte = 1 MByte
            xml_obytes = 1000000  # 1000000 Byte = 1 MByte
            with mock.patch('check_pa.modules.throughput.get_time',
                            return_value=now):
                with mock.patch('check_pa.xml_reader.Finder.find_item',
                                side_effect=[xml_ibytes, xml_obytes]):
                    with pytest.raises(SystemExit):
                        check.main(verbose=3)

            assert check.exitcode == 0
            assert check.state == ServiceState(code=0, text='ok')
            assert check.summary_str == 'Input is 0.8 Mb/s - Output is 0.8 ' \
                                        'Mb/s'
    def test_alarm(self):
        f = 'environmentals_ok.xml'
        check = environmental.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 0
            assert check.state == ServiceState(code=0, text='ok')
            assert check.summary_str == 'No alarms found.'
    def test_useragent_changed_format(self):
        self.warn = 2
        self.crit = 30

        f = 'useragent_changed_format.xml'
        check = check_pa.modules.useragent.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 3
            assert check.state == ServiceState(code=3, text='unknown')
    def test_diskspace_warning(self):
        self.warn = 50
        self.crit = 90

        f = 'diskspace.xml'
        check = check_pa.modules.diskspace.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 1
            assert check.state == ServiceState(code=1, text='warning')
            assert check.summary_str == 'sda2 is 57% (outside range 0:50)'
    def test_useragent_critical_last_heared(self):
        self.warn = 2
        self.crit = 30

        f = 'useragent_last_heared.xml'
        check = check_pa.modules.useragent.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 2
            assert check.state == ServiceState(code=2, text='critical')
            assert check.summary_str == 'Agent: Agent1 - Name1(vsys: vsys1) Host: 10.10.10.10(10.10.10.10):5007 last heared: 61 seconds ago'
    def test_useragent(self):
        self.warn = 60
        self.crit = 240

        f = 'useragent_ok.xml'
        check = check_pa.modules.useragent.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 0
            assert check.state == ServiceState(code=0, text='ok')
            assert check.summary_str == 'All agents are connected and responding.'
    def test_load_critical(self):
        self.warn = 90
        self.crit = 5

        f = 'load.xml'
        check = check_pa.modules.load.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)

            with pytest.raises(SystemExit):
                check.main(verbose=3)

        assert check.exitcode == 2
        assert check.state == ServiceState(code=2, text='critical')
        assert check.summary_str == 'CPU5 is 6% (outside range 0:5)'
示例#13
0
    def test_bgp_routes_ok(self):
        self.warn = 10
        self.crit = 5
        self.mode = 'routes'

        f = 'bgp_routes.xml'
        check = check_pa.modules.bgp.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)

            with pytest.raises(SystemExit):
                check.main(verbose=3)

        assert check.exitcode == 0
        assert check.state == ServiceState(code=0, text='ok')
        assert check.summary_str == 'bgp-routes-count: 12'
    def test_thermal_critical(self):
        self.warn = 20
        self.crit = 30

        f = 'thermal.xml'
        check = check_pa.modules.thermal.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 2
            assert check.state == ServiceState(code=2, text='critical')
            assert check.summary_str == 'Too high temperature: Temperature @ ' \
                                        'Test1 is 30.6 (outside range 0:30) ' \
                                        'degrees Celsius'
    def test_load_ok(self):
        self.warn = 80
        self.crit = 90

        f = 'load.xml'
        check = check_pa.modules.load.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)

            with pytest.raises(SystemExit):
                check.main(verbose=3)

        assert check.exitcode == 0
        assert check.state == ServiceState(code=0, text='ok')
        assert check.summary_str == 'CPU0: 0.0%, CPU1: 2.0%, CPU2: 4.0%, ' \
                                    'CPU3: 4.0%, CPU4: 5.0%, CPU5: 6.0%'
示例#16
0
    def test_certificate_ok(self):
        check = check_pa.modules.certificate.create_check(self)
        obj = check.resources[0]

        from datetime import datetime
        now = datetime(2011, 9, 1)
        with mock.patch('check_pa.modules.certificate.get_now',
                        return_value=now):
            with responses.RequestsMock() as rsps:
                rsps.add(responses.GET,
                         obj.xml_obj.build_request_url(),
                         body=utils.read_xml('certificates.xml'),
                         status=200,
                         content_type='document',
                         match_querystring=True)

                with pytest.raises(SystemExit):
                    check.main(verbose=3)

        assert check.exitcode == 0
        assert check.state == ServiceState(code=0, text='ok')
        assert check.summary_str == 'The next certificate will expire ' \
                                    'in 31 days.'
    def test_useragent_critical_noconn(self):
        self.warn = 60
        self.crit = 240

        f = 'useragent_no_connection.xml'
        check = check_pa.modules.useragent.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 2
            assert check.state == ServiceState(code=2, text='critical')
            assert check.summary_str == 'Agent: Agent2 - Name2(vsys: vsys1) Host: 192.168.0.1(192.168.0.1):5007 ' \
                                        'connection status is error, ' \
                                        'Agent: Agent3 - Name3(vsys: vsys1) Host:11.11.11.11(11.11.11.11):5007 ' \
                                        'connection status is non-conn'
    def test_diskspace(self):
        self.warn = 80
        self.crit = 90

        f = 'diskspace.xml'
        check = check_pa.modules.diskspace.create_check(self)
        obj = check.resources[0]

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     obj.xml_obj.build_request_url(),
                     body=utils.read_xml(f),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            with pytest.raises(SystemExit):
                check.main(verbose=3)

            assert check.exitcode == 0
            assert check.state == ServiceState(code=0, text='ok')
            assert check.summary_str == 'sda2: 57%, ' \
                                        'sda5: 43%, ' \
                                        'sda6: 30%, ' \
                                        'sda8: 47%'
示例#19
0
    def test_with_three_interfaces(self, statefile):
        self.interface = 'ethernet1/1, ethernet1/2, ethernet1/3'
        interfaces = []
        for interface in self.interface.split(','):
            interfaces.append(interface)

        file1 = 'throughput1.xml'
        file2 = 'throughput2.xml'
        file3 = 'throughput3.xml'

        check = check_pa.modules.throughput.create_check(self)
        objects = []
        for resource in check.resources:
            objects.append(resource)

        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)
            rsps.add(responses.GET,
                     objects[1].xml_obj.build_request_url(),
                     body=utils.read_xml(file2),
                     status=200,
                     content_type='document',
                     match_querystring=True)
            rsps.add(responses.GET,
                     objects[2].xml_obj.build_request_url(),
                     body=utils.read_xml(file3),
                     status=200,
                     content_type='document',
                     match_querystring=True)

            from nagiosplugin import Cookie

            # Resetting cookies
            with Cookie(statefile) as cookie:
                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
            xml_ibytes = 1000000
            xml_obytes = 1000000

            with mock.patch('check_pa.modules.throughput.get_time',
                            return_value=now):
                with mock.patch('check_pa.xml_reader.Finder.find_item',
                                side_effect=[
                                    xml_ibytes, xml_obytes, xml_ibytes,
                                    xml_ibytes, xml_ibytes, xml_ibytes
                                ]):
                    with pytest.raises(SystemExit):
                        check.main(verbose=3)

            assert check.exitcode == 0
            assert check.state == ServiceState(code=0, text='ok')

            # 3000000 Byte = 3 MByte = 24 Mbit in 1 second = 24.0 Mb/s
            assert check.summary_str == 'Input is 24.0 Mb/s - Output is 24.0 ' \
                                        'Mb/s'