Created
January 11, 2017 03:01
-
-
Save rodrickbrown/315e5c32782ef57d8da2a69969c24135 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
| requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
| import json | |
| import time | |
| from checks import AgentCheck | |
| from hashlib import md5 | |
| class OMSServiceCheck(AgentCheck): | |
| def check(self, instance): | |
| if 'url' not in instance: | |
| self.log.info('Skipping instance, url not configured') | |
| return | |
| if 'username' not in instance: | |
| self.log.info('Skipping instance, username not configured') | |
| return | |
| if 'password' not in instance: | |
| self.log.info('Skipping instance, password not configured') | |
| return | |
| url = instance['url'] | |
| username = instance['username'] | |
| password = instance['password'] | |
| default_timeout = self.init_config.get('default_timeout', 5) | |
| timeout = int(instance.get('timeout', default_timeout)) | |
| aggregation_key = md5(url).hexdigest() | |
| session = requests.Session() | |
| data = {"username" : username, "password": password } | |
| headers = {'Content-type': 'application/json', 'Accept': 'application/json'} | |
| try: | |
| r = session.post(url,data=json.dumps(data), headers=headers) | |
| except requests.exceptions.Timeout as e: | |
| self.timeout_event(url, timeout, aggregation_key) | |
| return | |
| if r.status_code != 200: | |
| self.status_code_event(url, r, aggregation_key) | |
| def timeout_event(self, url, timeout, aggregation_key): | |
| self.event({ | |
| 'timestamp': int(time.time()), | |
| 'event_type': 'oms_Service_check', | |
| 'msg_title': 'URL timeout connecting to OMS service', | |
| 'msg_text': '%s timed out after %s seconds.' % (url, timeout), | |
| 'alert_type': 'error', | |
| 'aggregation_key': aggregation_key | |
| }) | |
| def status_code_event(self, url, r, aggregation_key): | |
| self.event({ | |
| 'timestamp': int(time.time()), | |
| 'event_type': 'http_check', | |
| 'msg_title': 'Invalid reponse code for %s' % url, | |
| 'msg_text': '%s returned a status of %s' % (url, r.status_code), | |
| 'alert_type': 'error', | |
| 'aggregation_key': aggregation_key | |
| }) | |
| if __name__ == '__main__': | |
| check, instances = HTTPCheck.from_yaml('/etc/dd-agent/conf.d/oms_service_check.yaml') | |
| for instance in instances: | |
| print "\nRunning the check against url: %s" % (instance['url']) | |
| check.check(instance) | |
| if check.has_events(): | |
| print 'Events: %s' % (check.get_events()) | |
| print 'Metrics: %s' % (check.get_metrics()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment