mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Fixing log format and extracting main logic to a separate class.
This commit is contained in:
@@ -15,6 +15,7 @@ class Configuration(object):
|
|||||||
#TODO(mtakaki|2016-04-26): Needs validation if the config is correct.
|
#TODO(mtakaki|2016-04-26): Needs validation if the config is correct.
|
||||||
#TODO(mtakaki|2016-04-28): Accept overriding settings using environment
|
#TODO(mtakaki|2016-04-28): Accept overriding settings using environment
|
||||||
# variables so we have a more docker-friendly approach.
|
# variables so we have a more docker-friendly approach.
|
||||||
|
self.logger = logging.getLogger('cachet_url_monitor.configuration.Configuration')
|
||||||
self.config_file = config_file
|
self.config_file = config_file
|
||||||
self.data = load(file(self.config_file, 'r'))
|
self.data = load(file(self.config_file, 'r'))
|
||||||
self.expectations = [Expectaction.create(expectation) for expectation
|
self.expectations = [Expectaction.create(expectation) for expectation
|
||||||
@@ -30,17 +31,17 @@ class Configuration(object):
|
|||||||
self.data['endpoint']['url'],
|
self.data['endpoint']['url'],
|
||||||
timeout=self.data['endpoint']['timeout'])
|
timeout=self.data['endpoint']['timeout'])
|
||||||
except requests.ConnectionError:
|
except requests.ConnectionError:
|
||||||
logging.warning('The URL is unreachable: %s %s' %
|
self.logger.warning('The URL is unreachable: %s %s' %
|
||||||
(self.data['endpoint']['method'],
|
(self.data['endpoint']['method'],
|
||||||
self.data['endpoint']['url']))
|
self.data['endpoint']['url']))
|
||||||
self.status = 3
|
self.status = 3
|
||||||
return
|
return
|
||||||
except requests.HTTPError:
|
except requests.HTTPError:
|
||||||
logging.exception('Unexpected HTTP response')
|
self.logger.exception('Unexpected HTTP response')
|
||||||
self.status = 3
|
self.status = 3
|
||||||
return
|
return
|
||||||
except requests.Timeout:
|
except requests.Timeout:
|
||||||
logging.warning('Request timed out')
|
self.logger.warning('Request timed out')
|
||||||
self.status = 3
|
self.status = 3
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -63,10 +64,10 @@ class Configuration(object):
|
|||||||
params=params, headers=headers)
|
params=params, headers=headers)
|
||||||
if component_request.status_code == 200:
|
if component_request.status_code == 200:
|
||||||
# Successful update
|
# Successful update
|
||||||
logging.info('Component update: status [%d]' % (self.status,))
|
self.logger.info('Component update: status [%d]' % (self.status,))
|
||||||
else:
|
else:
|
||||||
# Failed to update the API status
|
# Failed to update the API status
|
||||||
logging.warning('Component update failed with status [%d]: API'
|
self.logger.warning('Component update failed with status [%d]: API'
|
||||||
' status: [%d]' % (component_request.status_code, self.status))
|
' status: [%d]' % (component_request.status_code, self.status))
|
||||||
|
|
||||||
class Expectaction(object):
|
class Expectaction(object):
|
||||||
|
|||||||
@@ -25,16 +25,27 @@ class Agent(object):
|
|||||||
schedule.every(self.configuration.data['frequency']).seconds.do(self.execute)
|
schedule.every(self.configuration.data['frequency']).seconds.do(self.execute)
|
||||||
|
|
||||||
|
|
||||||
|
class Scheduler(object):
|
||||||
|
def __init__(self, config_file):
|
||||||
|
self.logger = logging.getLogger('cachet_url_monitor.scheduler.Scheduler')
|
||||||
|
self.configuration = Configuration(config_file)
|
||||||
|
self.agent = Agent(self.configuration)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.agent.start()
|
||||||
|
self.logger.info('Starting monitor agent...')
|
||||||
|
while True:
|
||||||
|
schedule.run_pending()
|
||||||
|
time.sleep(self.configuration.data['frequency'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
FORMAT = "%(levelname)9s [%(asctime)-15s] %(name)s - %(message)s"
|
||||||
|
logging.basicConfig(format=FORMAT, level=logging.INFO)
|
||||||
|
|
||||||
if len(sys.argv) <= 1:
|
if len(sys.argv) <= 1:
|
||||||
logging.fatal('Missing configuration file argument')
|
logging.fatal('Missing configuration file argument')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
configuration = Configuration(sys.argv[1])
|
scheduler = Scheduler(sys.argv[1])
|
||||||
agent = Agent(configuration)
|
scheduler.start()
|
||||||
|
|
||||||
agent.start()
|
|
||||||
logging.info('Starting monitor agent...')
|
|
||||||
while True:
|
|
||||||
schedule.run_pending()
|
|
||||||
time.sleep(configuration.data['frequency'])
|
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ from cachet_url_monitor.configuration import Configuration
|
|||||||
|
|
||||||
class ConfigurationTest(unittest.TestCase):
|
class ConfigurationTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
def getLogger(name):
|
||||||
|
self.mock_logger = mock.Mock()
|
||||||
|
return self.mock_logger
|
||||||
|
sys.modules['logging'].getLogger = getLogger
|
||||||
|
|
||||||
self.configuration = Configuration('config.yml')
|
self.configuration = Configuration('config.yml')
|
||||||
sys.modules['requests'].Timeout = Timeout
|
sys.modules['requests'].Timeout = Timeout
|
||||||
sys.modules['requests'].ConnectionError = ConnectionError
|
sys.modules['requests'].ConnectionError = ConnectionError
|
||||||
@@ -61,7 +66,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
self.configuration.evaluate()
|
self.configuration.evaluate()
|
||||||
|
|
||||||
assert self.configuration.status == 3
|
assert self.configuration.status == 3
|
||||||
sys.modules['logging'].warning.assert_called_with('Request timed out')
|
self.mock_logger.warning.assert_called_with('Request timed out')
|
||||||
|
|
||||||
def test_evaluate_with_connection_error(self):
|
def test_evaluate_with_connection_error(self):
|
||||||
def request(method, url, timeout=None):
|
def request(method, url, timeout=None):
|
||||||
@@ -75,7 +80,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
self.configuration.evaluate()
|
self.configuration.evaluate()
|
||||||
|
|
||||||
assert self.configuration.status == 3
|
assert self.configuration.status == 3
|
||||||
sys.modules['logging'].warning.assert_called_with(('The URL is '
|
self.mock_logger.warning.assert_called_with(('The URL is '
|
||||||
'unreachable: GET http://localhost:8080/swagger'))
|
'unreachable: GET http://localhost:8080/swagger'))
|
||||||
|
|
||||||
def test_evaluate_with_http_error(self):
|
def test_evaluate_with_http_error(self):
|
||||||
@@ -90,7 +95,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
self.configuration.evaluate()
|
self.configuration.evaluate()
|
||||||
|
|
||||||
assert self.configuration.status == 3
|
assert self.configuration.status == 3
|
||||||
sys.modules['logging'].exception.assert_called_with(('Unexpected HTTP '
|
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
|
||||||
'response'))
|
'response'))
|
||||||
|
|
||||||
def test_push_status_and_metrics(self):
|
def test_push_status_and_metrics(self):
|
||||||
|
|||||||
@@ -29,3 +29,5 @@ class AgentTest(unittest.TestCase):
|
|||||||
self.agent.start()
|
self.agent.start()
|
||||||
|
|
||||||
every.assert_called_with(5)
|
every.assert_called_with(5)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user