def collect(self, config):
        ds0 = config.datasources[0]
        hostname = ds0.params['hostname']
        timeout = int(ds0.params['timeout'])
        port = int(ds0.params['port'])
        useSsl = ast.literal_eval(ds0.params['useSsl'])
        url = ds0.params['url']
        ipaddress = ds0.params['ipAddress']
        regex = ds0.params['regex']
        caseSensitive = ds0.params['caseSensitive']
        invert = ds0.params['invert']
        onRedirect = ds0.params['onRedirect']

        onRedirect = str(onRedirect)

        if onRedirect in ('False', ''):
            onRedirect = "fail"
        elif onRedirect == "True":
            onRedirect = "follow"

        basicAuthUser = ds0.params['basicAuthUser']
        basicAuthPass = ds0.params['basicAuthPass']
        proxyAuthUser = ds0.params['proxyAuthUser']
        proxyAuthPassword = ds0.params['proxyAuthPassword']
        log.info("HTTPMonitor collecting started for: {}".format(hostname or ipaddress or url))
        chttp = HTTPMonitor(ipAddr=ipaddress, hostname=hostname, url=url, port=port, timeout=timeout, ssl=useSsl,
                            follow=onRedirect)
        if proxyAuthUser:
            chttp.useProxy(proxyAuthUser, proxyAuthPassword)
        if basicAuthUser:
            chttp.useAuth(basicAuthUser, basicAuthPass)
        if regex:
            chttp.regex(regex, ast.literal_eval(caseSensitive), ast.literal_eval(invert))
        return chttp.connect()
 def test_regex_caseSensitive(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("hello", caseSensitive=True, invert=False)
     self.assertEqual(hm._checkRegex(body), {
         'status': 'CRITICAL',
         'msg': 'pattern not found'
     })
 def test_regex_wrong_expression(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("[]*", caseSensitive=False, invert=False)
     self.assertEqual(
         hm._checkRegex(body), {
             'status': 'CRITICAL',
             'msg': "Could not compile regular expression: '[]*'"
         })
    def collect(self, config):
        ds0 = config.datasources[0]
        hostname = ds0.params['hostname']
        timeout = int(ds0.params['timeout'])
        port = int(ds0.params['port'])
        useSsl = ast.literal_eval(ds0.params['useSsl'])
        url = ds0.params['url']
        ipaddress = ds0.params['ipAddress']
        regex = ds0.params['regex']
        caseSensitive = ds0.params['caseSensitive']
        invert = ds0.params['invert']
        onRedirect = ds0.params['onRedirect']

        onRedirect = str(onRedirect)

        if onRedirect in ('False', ''):
            onRedirect = "fail"
        elif onRedirect == "True":
            onRedirect = "follow"

        basicAuthUser = ds0.params['basicAuthUser']
        basicAuthPass = ds0.params['basicAuthPass']
        proxyAuthUser = ds0.params['proxyAuthUser']
        proxyAuthPassword = ds0.params['proxyAuthPassword']
        log.info("HTTPMonitor collecting started for: {}".format(hostname
                                                                 or ipaddress
                                                                 or url))
        chttp = HTTPMonitor(ipAddr=ipaddress,
                            hostname=hostname,
                            url=url,
                            port=port,
                            timeout=timeout,
                            ssl=useSsl,
                            follow=onRedirect)
        if proxyAuthUser:
            chttp.useProxy(proxyAuthUser, proxyAuthPassword)
        if basicAuthUser:
            chttp.useAuth(basicAuthUser, basicAuthPass)
        if regex:
            chttp.regex(regex, ast.literal_eval(caseSensitive),
                        ast.literal_eval(invert))
        return chttp.connect()
 def test_regex_caseSensitive_invert(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("hello", caseSensitive=True, invert=True)
     self.assertEqual(hm._checkRegex(body), None)
 def test_regex_found(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("Web", caseSensitive=False, invert=False)
     self.assertEqual(hm._checkRegex(body), None)
 def test_regex_wrong_expression(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("[]*", caseSensitive=False, invert=False)
     self.assertEqual(hm._checkRegex(body), {'status': 'CRITICAL',
                                             'msg': "Could not compile regular expression: '[]*'"})
 def test_regex_caseSensitive_invert(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("hello", caseSensitive=True, invert=True)
     self.assertEqual(hm._checkRegex(body), None)
 def test_regex_caseSensitive(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("hello", caseSensitive=True, invert=False)
     self.assertEqual(hm._checkRegex(body), {'status': 'CRITICAL', 'msg': 'pattern not found'})
 def test_regex_found(self):
     body = "Hello Web!"
     hm = HTTPMonitor("10.20.30.40", "tester.test", "/path")
     hm.regex("Web", caseSensitive=False, invert=False)
     self.assertEqual(hm._checkRegex(body), None)