mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Merge pull request #4 from mtakaki/mtakaki_push_metrics
Creating push_metrics() method to start pushing the URL latency, if it the metric id is configured
This commit is contained in:
@@ -3,7 +3,7 @@ import abc
|
||||
import logging
|
||||
import re
|
||||
import requests
|
||||
import timeit
|
||||
import time
|
||||
from yaml import load
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ class Configuration(object):
|
||||
in self.data['endpoint']['expectation']]
|
||||
for expectation in self.expectations:
|
||||
self.logger.info('Registered expectation: %s' % (expectation,))
|
||||
self.headers = {'X-Cachet-Token': self.data['cachet']['token']}
|
||||
|
||||
def evaluate(self):
|
||||
"""Sends the request to the URL set in the configuration and executes
|
||||
@@ -32,6 +33,7 @@ class Configuration(object):
|
||||
self.request = requests.request(self.data['endpoint']['method'],
|
||||
self.data['endpoint']['url'],
|
||||
timeout=self.data['endpoint']['timeout'])
|
||||
self.current_timestamp = int(time.time())
|
||||
except requests.ConnectionError:
|
||||
self.logger.warning('The URL is unreachable: %s %s' %
|
||||
(self.data['endpoint']['method'],
|
||||
@@ -56,15 +58,14 @@ class Configuration(object):
|
||||
if status > self.status:
|
||||
self.status = status
|
||||
|
||||
def push_status_and_metrics(self):
|
||||
def push_status(self):
|
||||
params = {'id': self.data['cachet']['component_id'], 'status':
|
||||
self.status}
|
||||
headers = {'X-Cachet-Token': self.data['cachet']['token']}
|
||||
component_request = requests.put('%s/components/%d' %
|
||||
(self.data['cachet']['api_url'],
|
||||
self.data['cachet']['component_id']),
|
||||
params=params, headers=headers)
|
||||
if component_request.status_code == 200:
|
||||
params=params, headers=self.headers)
|
||||
if component_request.ok:
|
||||
# Successful update
|
||||
self.logger.info('Component update: status [%d]' % (self.status,))
|
||||
else:
|
||||
@@ -72,6 +73,25 @@ class Configuration(object):
|
||||
self.logger.warning('Component update failed with status [%d]: API'
|
||||
' status: [%d]' % (component_request.status_code, self.status))
|
||||
|
||||
def push_metrics(self):
|
||||
if 'metric_id' in self.data['cachet'] and hasattr(self, 'request'):
|
||||
params = {'id': self.data['cachet']['metric_id'], 'value':
|
||||
self.request.elapsed.total_seconds(), 'timestamp':
|
||||
self.current_timestamp}
|
||||
metrics_request = requests.post('%s/metrics/%d/points' %
|
||||
(self.data['cachet']['api_url'],
|
||||
self.data['cachet']['metric_id']), params=params,
|
||||
headers=self.headers)
|
||||
|
||||
if metrics_request.ok:
|
||||
# Successful metrics upload
|
||||
self.logger.info('Metric uploaded: %.6f seconds' %
|
||||
(self.request.elapsed.total_seconds(),))
|
||||
else:
|
||||
self.logger.warning('Metric upload failed with status [%d]' %
|
||||
(metrics_request.status_code,))
|
||||
|
||||
|
||||
class Expectaction(object):
|
||||
"""Base class for URL result expectations. Any new excpectation should extend
|
||||
this class and the name added to create() method.
|
||||
|
||||
@@ -18,7 +18,8 @@ class Agent(object):
|
||||
cachet server.
|
||||
"""
|
||||
self.configuration.evaluate()
|
||||
self.configuration.push_status_and_metrics()
|
||||
self.configuration.push_status()
|
||||
self.configuration.push_metrics()
|
||||
|
||||
def start(self):
|
||||
"""Sets up the schedule based on the configuration file."""
|
||||
|
||||
@@ -8,10 +8,10 @@ endpoint:
|
||||
- type: LATENCY
|
||||
threshold: 1
|
||||
- type: REGEX
|
||||
regex: ".*<body>.*"
|
||||
regex: '.*<body>.*'
|
||||
cachet:
|
||||
api_url: http://status.cachethq.io/api/v1/
|
||||
api_url: https://demo.cachethq.io/api/v1
|
||||
token: my_token
|
||||
component_id: 1
|
||||
metric_id: 1
|
||||
#metric_id: 1
|
||||
frequency: 30
|
||||
|
||||
@@ -100,9 +100,9 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
|
||||
'response'))
|
||||
|
||||
def test_push_status_and_metrics(self):
|
||||
def test_push_status(self):
|
||||
def put(url, params=None, headers=None):
|
||||
assert url == 'http://status.cachethq.io/api/v1//components/1'
|
||||
assert url == 'https://demo.cachethq.io/api/v1/components/1'
|
||||
assert params == {'id': 1, 'status': 1}
|
||||
assert headers == {'X-Cachet-Token': 'my_token'}
|
||||
|
||||
@@ -112,11 +112,11 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
sys.modules['requests'].put = put
|
||||
self.configuration.status = 1
|
||||
self.configuration.push_status_and_metrics()
|
||||
self.configuration.push_status()
|
||||
|
||||
def test_push_status_and_metrics_with_failure(self):
|
||||
def test_push_status_with_failure(self):
|
||||
def put(url, params=None, headers=None):
|
||||
assert url == 'http://status.cachethq.io/api/v1//components/1'
|
||||
assert url == 'https://demo.cachethq.io/api/v1/components/1'
|
||||
assert params == {'id': 1, 'status': 1}
|
||||
assert headers == {'X-Cachet-Token': 'my_token'}
|
||||
|
||||
@@ -126,4 +126,4 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
sys.modules['requests'].put = put
|
||||
self.configuration.status = 1
|
||||
self.configuration.push_status_and_metrics()
|
||||
self.configuration.push_status()
|
||||
|
||||
@@ -17,11 +17,11 @@ class AgentTest(unittest.TestCase):
|
||||
|
||||
def test_execute(self):
|
||||
evaluate = self.configuration.evaluate
|
||||
push_status_and_metrics = self.configuration.push_status_and_metrics
|
||||
push_status = self.configuration.push_status
|
||||
self.agent.execute()
|
||||
|
||||
evaluate.assert_called_once()
|
||||
push_status_and_metrics.assert_called_once()
|
||||
push_status.assert_called_once()
|
||||
|
||||
def test_start(self):
|
||||
every = sys.modules['schedule'].every
|
||||
|
||||
Reference in New Issue
Block a user