Optimizations
Chris committed
Mar 1, 2018 at 11:08 UTC
7aad2f844947c97509d7f0d47b64e9fe58a5ad8a
3 files changed
+25
-11
conf.d/python.d/httpcheck.conf
+16
@@ -79,5 +79,21 @@ chart_cleanup: 0
79
# # regex (if defined). Be aware that you may need to escape the regex string. If redirect is enabled,
80
# # the regex will be matched to the redirected page, not the initial 3xx response.
81
82
+# Simple example:
83
+#
84
+# jira:
85
+# url: 'https://jira.localdomain/'
86
+
87
+
88
+# Complex example:
89
+#
90
+# cool_website:
91
+# url: 'http://cool.website:8080/home'
92
+# status_accepted:
93
+# - 200
94
+# - 204
95
+# regex: <title>My cool website!<\/title>
96
+# timeout: 2
97
+
98
# This plugin is intended for simple cases. Currently, the accuracy of the response time is low and should be used as reference only.
99
python.d/httpcheck.chart.py
+3
-6
@@ -56,16 +56,13 @@ CHARTS = {
56
class Service(UrlService):
57
def __init__(self, configuration=None, name=None):
58
UrlService.__init__(self, configuration=configuration, name=name)
59
- self.regex = self._compile(self.configuration.get('regex'))
59
+ pattern = self.configuration.get('regex')
60
+ self.regex = re.compile(pattern) if pattern else None
61
self.status_codes_accepted = self.configuration.get('status_accepted', [200])
62
self.follow_redirect = self.configuration.get('redirect', True)
63
self.order = ORDER
64
self.definitions = CHARTS
65
65
- @staticmethod
66
- def _compile(pattern=None):
67
- return None if pattern is None else re.compile(pattern)
68
-
66
def _get_data(self):
67
"""
68
Format data received from http request
@@ -111,7 +108,7 @@ class Service(UrlService):
108
data[HTTP_RESPONSE_LENGTH] = len(content)
109
self.debug('Content: \n\n{content}\n'.format(content=content))
110
if status in self.status_codes_accepted:
114
- if self.regex is not None and self.regex.search(content) is None:
111
+ if self.regex and self.regex.search(content) is None:
112
self.debug("No match for regex '{regex}' found".format(regex=self.regex.pattern))
113
data[HTTP_BAD_CONTENT] = 1
114
else:
python.d/python_modules/bases/FrameworkServices/UrlService.py
+6
-5
@@ -76,15 +76,16 @@ class UrlService(SimpleService):
76
"""
77
try:
78
status, data = self._get_raw_data_with_status(url, manager)
79
- if status == 200:
80
- return data
81
- else:
82
- self.debug('Url: {url}. Http response status code: {code}'.format(url=url, code=status))
83
- return None
79
except (urllib3.exceptions.HTTPError, TypeError, AttributeError) as error:
80
self.error('Url: {url}. Error: {error}'.format(url=url, error=error))
81
return None
82
83
+ if status == 200:
84
+ return data
85
+ else:
86
+ self.debug('Url: {url}. Http response status code: {code}'.format(url=url, code=status))
87
+ return None
88
+
89
def _get_raw_data_with_status(self, url=None, manager=None, retries=1, redirect=True):
90
"""
91
Get status and response body content from http request. Does not catch exceptions