httpcheck.chart.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. # Description: http check netdata python.d module
  3. # Original Author: ccremer (github.com/ccremer)
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import re
  6. import urllib3
  7. try:
  8. from time import monotonic as time
  9. except ImportError:
  10. from time import time
  11. from bases.FrameworkServices.UrlService import UrlService
  12. # default module values (can be overridden per job in `config`)
  13. update_every = 3
  14. priority = 60000
  15. # Response
  16. HTTP_RESPONSE_TIME = 'time'
  17. HTTP_RESPONSE_LENGTH = 'length'
  18. # Status dimensions
  19. HTTP_SUCCESS = 'success'
  20. HTTP_BAD_CONTENT = 'bad_content'
  21. HTTP_BAD_STATUS = 'bad_status'
  22. HTTP_TIMEOUT = 'timeout'
  23. HTTP_NO_CONNECTION = 'no_connection'
  24. ORDER = [
  25. 'response_time',
  26. 'response_length',
  27. 'status',
  28. ]
  29. CHARTS = {
  30. 'response_time': {
  31. 'options': [None, 'HTTP response time', 'milliseconds', 'response', 'httpcheck.responsetime', 'line'],
  32. 'lines': [
  33. [HTTP_RESPONSE_TIME, 'time', 'absolute', 100, 1000]
  34. ]
  35. },
  36. 'response_length': {
  37. 'options': [None, 'HTTP response body length', 'characters', 'response', 'httpcheck.responselength', 'line'],
  38. 'lines': [
  39. [HTTP_RESPONSE_LENGTH, 'length', 'absolute']
  40. ]
  41. },
  42. 'status': {
  43. 'options': [None, 'HTTP status', 'boolean', 'status', 'httpcheck.status', 'line'],
  44. 'lines': [
  45. [HTTP_SUCCESS, 'success', 'absolute'],
  46. [HTTP_BAD_CONTENT, 'bad content', 'absolute'],
  47. [HTTP_BAD_STATUS, 'bad status', 'absolute'],
  48. [HTTP_TIMEOUT, 'timeout', 'absolute'],
  49. [HTTP_NO_CONNECTION, 'no connection', 'absolute']
  50. ]
  51. }
  52. }
  53. class Service(UrlService):
  54. def __init__(self, configuration=None, name=None):
  55. UrlService.__init__(self, configuration=configuration, name=name)
  56. self.order = ORDER
  57. self.definitions = CHARTS
  58. pattern = self.configuration.get('regex')
  59. self.regex = re.compile(pattern) if pattern else None
  60. self.status_codes_accepted = self.configuration.get('status_accepted', [200])
  61. self.follow_redirect = self.configuration.get('redirect', True)
  62. def _get_data(self):
  63. """
  64. Format data received from http request
  65. :return: dict
  66. """
  67. data = dict()
  68. data[HTTP_SUCCESS] = 0
  69. data[HTTP_BAD_CONTENT] = 0
  70. data[HTTP_BAD_STATUS] = 0
  71. data[HTTP_TIMEOUT] = 0
  72. data[HTTP_NO_CONNECTION] = 0
  73. url = self.url
  74. try:
  75. start = time()
  76. status, content = self._get_raw_data_with_status(retries=1 if self.follow_redirect else False,
  77. redirect=self.follow_redirect)
  78. diff = time() - start
  79. data[HTTP_RESPONSE_TIME] = max(round(diff * 10000), 0)
  80. self.debug('Url: {url}. Host responded with status code {code} in {diff} s'.format(
  81. url=url, code=status, diff=diff
  82. ))
  83. self.process_response(content, data, status)
  84. except urllib3.exceptions.NewConnectionError as error:
  85. self.debug('Connection failed: {url}. Error: {error}'.format(url=url, error=error))
  86. data[HTTP_NO_CONNECTION] = 1
  87. except (urllib3.exceptions.TimeoutError, urllib3.exceptions.PoolError) as error:
  88. self.debug('Connection timed out: {url}. Error: {error}'.format(url=url, error=error))
  89. data[HTTP_TIMEOUT] = 1
  90. except urllib3.exceptions.HTTPError as error:
  91. self.debug('Connection failed: {url}. Error: {error}'.format(url=url, error=error))
  92. data[HTTP_NO_CONNECTION] = 1
  93. except (TypeError, AttributeError) as error:
  94. self.error('Url: {url}. Error: {error}'.format(url=url, error=error))
  95. return None
  96. return data
  97. def process_response(self, content, data, status):
  98. data[HTTP_RESPONSE_LENGTH] = len(content)
  99. self.debug('Content: \n\n{content}\n'.format(content=content))
  100. if status in self.status_codes_accepted:
  101. if self.regex and self.regex.search(content) is None:
  102. self.debug('No match for regex "{regex}" found'.format(regex=self.regex.pattern))
  103. data[HTTP_BAD_CONTENT] = 1
  104. else:
  105. data[HTTP_SUCCESS] = 1
  106. else:
  107. data[HTTP_BAD_STATUS] = 1