def test_disabled_definitions(self): test_file_path = os.path.join('ambari_agent', 'dummy_files') test_stack_path = os.path.join('ambari_agent', 'dummy_files') test_common_services_path = os.path.join('ambari_agent', 'dummy_files') test_host_scripts_path = os.path.join('ambari_agent', 'dummy_files') ash = AlertSchedulerHandler(test_file_path, test_stack_path, test_common_services_path, test_host_scripts_path, None) ash.start() self.assertEquals(1, ash.get_job_count()) json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "PORT", "uri": "{{hdfs-site/my-key}}", "default_port": 50070, "reporting": { "ok": { "text": "(Unit Tests) TCP OK - {0:.4f} response time on port {1}" }, "critical": { "text": "(Unit Tests) Could not load process info: {0}" } } } } pa = PortAlert(json, json['source']) ash.schedule_definition(pa) self.assertEquals(2, ash.get_job_count()) json['enabled'] = False pa = PortAlert(json, json['source']) ash.schedule_definition(pa) # verify disabled alert not scheduled self.assertEquals(2, ash.get_job_count()) json['enabled'] = True pa = PortAlert(json, json['source']) ash.schedule_definition(pa) # verify enabled alert was scheduled self.assertEquals(3, ash.get_job_count())
def test_default_reporting_text(self): definition_json = self._get_script_alert_definition() alert = ScriptAlert(definition_json, definition_json['source'], None) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}') definition_json['source']['type'] = 'PORT' alert = PortAlert(definition_json, definition_json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed: {0} to {1}:{2}') definition_json['source']['type'] = 'WEB' alert = WebAlert(definition_json, definition_json['source'], None) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed to {1}') definition_json['source']['type'] = 'METRIC' alert = MetricAlert(definition_json, definition_json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}')
def test_alert_collector_purge(self): definition_json = self._get_port_alert_definition() configuration = {'hdfs-site': {'my-key': 'value1'}} collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) alert = PortAlert(definition_json, definition_json['source']) alert.set_helpers(collector, cluster_configuration) alert.set_cluster("c1", "c6401.ambari.apache.org") self.assertEquals(6, alert.interval()) res = alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertTrue(alerts[0] is not None) self.assertEquals('CRITICAL', alerts[0]['state']) collector.remove_by_uuid('c1f73191-4481-4435-8dae-fd380e4c0be1') self.assertEquals(0, len(collector.alerts()))
def test_port_alert_no_sub(self): definition_json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "PORT", "uri": "http://c6401.ambari.apache.org", "default_port": 50070, "reporting": { "ok": { "text": "(Unit Tests) TCP OK - {0:.4f} response time on port {1}" }, "critical": { "text": "(Unit Tests) Could not load process info: {0}" } } } } cluster_configuration = self.__get_cluster_configuration() alert = PortAlert(definition_json, definition_json['source']) alert.set_helpers(AlertCollector(), cluster_configuration) alert.set_cluster("c1", "c6401.ambari.apache.org") self.assertEquals('http://c6401.ambari.apache.org', alert.uri) alert.collect()
def test_port_alert_complex_uri(self, socket_connect_mock): definition_json = self._get_port_alert_definition() configuration = { 'hdfs-site': { 'my-key': 'c6401.ambari.apache.org:2181,c6402.ambari.apache.org:2181,c6403.ambari.apache.org:2181' } } collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) alert = PortAlert(definition_json, definition_json['source']) alert.set_helpers(collector, cluster_configuration) alert.set_cluster("c1", "c6402.ambari.apache.org") # use a URI that has commas to verify that we properly parse it alert.set_helpers(collector, cluster_configuration) alert.set_cluster("c1", "c6401.ambari.apache.org") self.assertEquals(6, alert.interval()) alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('OK', alerts[0]['state']) self.assertTrue('(Unit Tests)' in alerts[0]['text']) self.assertTrue('response time on port 2181' in alerts[0]['text'])
def test_port_alert_no_sub(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "PORT", "uri": "http://c6401.ambari.apache.org", "default_port": 50070, "reporting": { "ok": { "text": "TCP OK - {0:.4f} response time on port {1}" }, "critical": { "text": "Could not load process info: {0}" } } } } pa = PortAlert(json, json['source']) pa.set_helpers(AlertCollector(), '') self.assertEquals('http://c6401.ambari.apache.org', pa.uri) res = pa.collect()
def test_port_alert(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "PORT", "uri": "{{hdfs-site/my-key}}", "default_port": 50070, "reporting": { "ok": { "text": "TCP OK - {0:.4f} response time on port {1}" }, "critical": { "text": "Could not load process info: {0}" } } } } collector = AlertCollector() pa = PortAlert(json, json['source']) pa.set_helpers(collector, {'hdfs-site/my-key': 'value1'}) self.assertEquals(6, pa.interval()) res = pa.collect()
def test_disabled_definitions(self): test_file_path = os.path.join('ambari_agent', 'dummy_files') test_stack_path = os.path.join('ambari_agent', 'dummy_files') test_common_services_path = os.path.join('ambari_agent', 'dummy_files') test_host_scripts_path = os.path.join('ambari_agent', 'dummy_files') cluster_configuration = self.__get_cluster_configuration() ash = AlertSchedulerHandler(test_file_path, test_stack_path, test_common_services_path, test_host_scripts_path, cluster_configuration, None) ash.start() self.assertEquals(1, ash.get_job_count()) definition_json = self._get_port_alert_definition() alert = PortAlert(definition_json, definition_json['source']) ash.schedule_definition(alert) self.assertEquals(2, ash.get_job_count()) definition_json['enabled'] = False alert = PortAlert(definition_json, definition_json['source']) ash.schedule_definition(alert) # verify disabled alert not scheduled self.assertEquals(2, ash.get_job_count()) definition_json['enabled'] = True pa = PortAlert(definition_json, definition_json['source']) ash.schedule_definition(pa) # verify enabled alert was scheduled self.assertEquals(3, ash.get_job_count())
def test_default_reporting_text(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "SCRIPT", "path": "test_script.py", } } alert = ScriptAlert(json, json['source'], None) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}') json['source']['type'] = 'PORT' alert = PortAlert(json, json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed: {0} to {1}:{2}') json['source']['type'] = 'WEB' alert = WebAlert(json, json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed to {1}') json['source']['type'] = 'METRIC' alert = MetricAlert(json, json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}')
def test_port_alert_complex_uri(self, socket_connect_mock): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "PORT", "uri": "{{hdfs-site/my-key}}", "default_port": 50070, "reporting": { "ok": { "text": "(Unit Tests) TCP OK - {0:.4f} response time on port {1}" }, "critical": { "text": "(Unit Tests) Could not load process info: {0}" } } } } collector = AlertCollector() pa = PortAlert(json, json['source']) # use a URI that has commas to verify that we properly parse it pa.set_helpers( collector, { 'hdfs-site/my-key': 'c6401.ambari.apache.org:2181,c6402.ambari.apache.org:2181,c6403.ambari.apache.org:2181' }) pa.host_name = 'c6402.ambari.apache.org' self.assertEquals(6, pa.interval()) pa.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('OK', alerts[0]['state']) self.assertTrue('(Unit Tests)' in alerts[0]['text']) self.assertTrue('response time on port 2181' in alerts[0]['text'])
def test_alert_collector_purge(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "PORT", "uri": "{{hdfs-site/my-key}}", "default_port": 50070, "reporting": { "ok": { "text": "(Unit Tests) TCP OK - {0:.4f} response time on port {1}" }, "critical": { "text": "(Unit Tests) Could not load process info: {0}" } } } } collector = AlertCollector() pa = PortAlert(json, json['source']) pa.set_helpers(collector, {'hdfs-site/my-key': 'value1'}) self.assertEquals(6, pa.interval()) res = pa.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertTrue(alerts[0] is not None) self.assertEquals('CRITICAL', alerts[0]['state']) collector.remove_by_uuid('c1f73191-4481-4435-8dae-fd380e4c0be1') self.assertEquals(0, len(collector.alerts()))
def test_port_alert(self, socket_connect_mock, time_mock): definition_json = self._get_port_alert_definition() configuration = {'hdfs-site': {'my-key': 'value1'}} collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) # called 3x with 3 calls per alert # - 900ms and then a time.time() for the date from base_alert # - 2000ms and then a time.time() for the date from base_alert # - socket.timeout to simulate a timeout and then a time.time() for the date from base_alert time_mock.side_effect = [ 0, 900, 336283000000, 0, 2000, 336283100000, socket.timeout, 336283200000 ] alert = PortAlert(definition_json, definition_json['source']) alert.set_helpers(collector, cluster_configuration) alert.set_cluster("c1", "c6401.ambari.apache.org") self.assertEquals(6, alert.interval()) # 900ms is OK alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('OK', alerts[0]['state']) # 2000ms is WARNING alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) # throws a socket.timeout exception, causes a CRITICAL alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('CRITICAL', alerts[0]['state'])
def test_port_alert(self, socket_connect_mock, time_mock): # called 3x with 3 calls per alert # - 900ms and then a time.time() for the date from base_alert # - 2000ms and then a time.time() for the date from base_alert # - socket.timeout to simulate a timeout and then a time.time() for the date from base_alert time_mock.side_effect = [ 0, 900, 336283200000, 0, 2000, 336283200000, socket.timeout, 336283200000 ] json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "PORT", "uri": "{{hdfs-site/my-key}}", "default_port": 50070, "reporting": { "ok": { "text": "(Unit Tests) TCP OK - {0:.4f} response time on port {1}" }, "warning": { "text": "(Unit Tests) TCP WARN - {0:.4f} response time on port {1}", "value": 1.5 }, "critical": { "text": "(Unit Tests) Could not load process info: {0}", "value": 5.0 } } } } collector = AlertCollector() pa = PortAlert(json, json['source']) pa.set_helpers(collector, {'hdfs-site/my-key': 'value1'}) self.assertEquals(6, pa.interval()) # 900ms is OK pa.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('OK', alerts[0]['state']) # 2000ms is WARNING pa.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) # throws a socket.timeout exception, causes a CRITICAL pa.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('CRITICAL', alerts[0]['state'])