Exemplo n.º 1
0
    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}')
Exemplo n.º 2
0
    def test_uri_timeout(self):
        # the web alert will have a timeout value
        definition_json = self._get_web_alert_definition()
        alert = WebAlert(definition_json, definition_json['source'], None)
        self.assertEquals(5.678, alert.connection_timeout)
        self.assertEquals("5", alert.curl_connection_timeout)

        # the metric definition will not and should default to 5.0
        definition_json = self._get_metric_alert_definition()
        alert = MetricAlert(definition_json, definition_json['source'], None)
        self.assertEquals(5.0, alert.connection_timeout)
Exemplo n.º 3
0
    def test_metric_alert_uses_refresh_processor(self, http_response_mock,
                                                 http_connection_mock):
        """
    Tests that the RefreshHeaderProcessor is correctly chained and called
    :param http_response_mock:
    :param http_connection_mock:
    :return:
    """
        http_conn = http_connection_mock.return_value
        http_conn.getresponse.return_value = MagicMock(status=200)
        http_response_mock.return_value = MagicMock(code=200)

        url_opener = urllib2.build_opener(RefreshHeaderProcessor())
        response = url_opener.open("http://foo.bar.baz/jmx")

        self.assertFalse(response is None)
        self.assertTrue(http_conn.request.called)
        self.assertTrue(http_conn.getresponse.called)
        self.assertTrue(http_response_mock.called)

        # now we know that the refresh header is intercepting, reset the mocks
        # and try with a METRIC alert
        MagicMock.reset_mock(http_response_mock)
        MagicMock.reset_mock(http_connection_mock)

        definition_json = self._get_metric_alert_definition()

        configuration = {
            'hdfs-site': {
                'dfs.datanode.http.address': 'c6401.ambari.apache.org:80'
            }
        }

        collector = AlertCollector()
        cluster_configuration = self.__get_cluster_configuration()
        self.__update_cluster_configuration(cluster_configuration,
                                            configuration)

        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")

        alert.collect()

        self.assertFalse(response is None)
        self.assertTrue(http_conn.request.called)
        self.assertTrue(http_conn.getresponse.called)
        self.assertTrue(http_response_mock.called)
Exemplo n.º 4
0
    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}')
Exemplo n.º 5
0
  def test_metric_alert_uses_refresh_processor(self, http_response_mock, http_connection_mock):
    """
    Tests that the RefreshHeaderProcessor is correctly chained and called
    :param http_response_mock:
    :param http_connection_mock:
    :return:
    """
    http_conn = http_connection_mock.return_value
    http_conn.getresponse.return_value = MagicMock(status=200)
    http_response_mock.return_value = MagicMock(code=200)

    url_opener = urllib2.build_opener(RefreshHeaderProcessor())
    response = url_opener.open("http://foo.bar.baz/jmx")

    self.assertFalse(response is None)
    self.assertTrue(http_conn.request.called)
    self.assertTrue(http_conn.getresponse.called)
    self.assertTrue(http_response_mock.called)

    # now we know that the refresh header is intercepting, reset the mocks
    # and try with a METRIC alert
    MagicMock.reset_mock(http_response_mock)
    MagicMock.reset_mock(http_connection_mock)

    definition_json = self._get_metric_alert_definition()

    configuration = {'hdfs-site' :
      { 'dfs.datanode.http.address': 'c6401.ambari.apache.org:80'}
    }

    collector = AlertCollector()
    cluster_configuration = self.__get_cluster_configuration()
    self.__update_cluster_configuration(cluster_configuration, configuration)

    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")

    alert.collect()

    self.assertFalse(response is None)
    self.assertTrue(http_conn.request.called)
    self.assertTrue(http_conn.getresponse.called)
    self.assertTrue(http_response_mock.called)
Exemplo n.º 6
0
    def test_alert_uri_structure(self, ma_load_jmx_mock):
        definition_json = self._get_metric_alert_definition()

        ma_load_jmx_mock.return_value = [0, 0]

        # run the alert without specifying any keys; an exception should be thrown
        # indicating that there was no URI and the result is UNKNOWN
        collector = AlertCollector()
        cluster_configuration = self.__get_cluster_configuration()
        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")
        alert.collect()

        self.assertEquals('UNKNOWN', collector.alerts()[0]['state'])

        # set properties that make no sense wihtout the main URI properties
        configuration = {'hdfs-site': {'dfs.http.policy': 'HTTP_ONLY'}}

        collector = AlertCollector()
        cluster_configuration = self.__get_cluster_configuration()
        self.__update_cluster_configuration(cluster_configuration,
                                            configuration)

        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")
        alert.collect()

        self.assertEquals('UNKNOWN', collector.alerts()[0]['state'])

        # set an actual property key (http)
        configuration = {
            'hdfs-site': {
                'dfs.http.policy': 'HTTP_ONLY',
                'dfs.datanode.http.address': 'c6401.ambari.apache.org:80'
            }
        }

        self.__update_cluster_configuration(cluster_configuration,
                                            configuration)

        collector = AlertCollector()
        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")
        alert.collect()

        self.assertEquals('OK', collector.alerts()[0]['state'])

        # set an actual property key (https)
        configuration = {
            'hdfs-site': {
                'dfs.http.policy': 'HTTP_ONLY',
                'dfs.datanode.https.address': 'c6401.ambari.apache.org:443'
            }
        }

        self.__update_cluster_configuration(cluster_configuration,
                                            configuration)

        collector = AlertCollector()
        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")
        alert.collect()

        self.assertEquals('OK', collector.alerts()[0]['state'])

        # set both (http and https)
        configuration = {
            'hdfs-site': {
                'dfs.http.policy': 'HTTP_ONLY',
                'dfs.datanode.http.address': 'c6401.ambari.apache.org:80',
                'dfs.datanode.https.address': 'c6401.ambari.apache.org:443'
            }
        }

        self.__update_cluster_configuration(cluster_configuration,
                                            configuration)

        collector = AlertCollector()
        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")
        alert.collect()

        self.assertEquals('OK', collector.alerts()[0]['state'])
Exemplo n.º 7
0
    def test_metric_alert(self, ma_load_jmx_mock):
        definition_json = self._get_metric_alert_definition()
        configuration = {
            'hdfs-site': {
                'dfs.datanode.http.address': 'c6401.ambari.apache.org:80'
            }
        }

        collector = AlertCollector()
        cluster_configuration = self.__get_cluster_configuration()
        self.__update_cluster_configuration(cluster_configuration,
                                            configuration)

        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")

        # trip an OK
        ma_load_jmx_mock.return_value = [1, 25]

        alert.collect()
        alerts = collector.alerts()
        self.assertEquals(0, len(collector.alerts()))
        self.assertEquals('OK', alerts[0]['state'])
        self.assertEquals('(Unit Tests) OK: 1 25 125', alerts[0]['text'])

        # trip a warning
        ma_load_jmx_mock.return_value = [1, 75]

        alert.collect()
        alerts = collector.alerts()
        self.assertEquals(0, len(collector.alerts()))
        self.assertEquals('WARNING', alerts[0]['state'])
        self.assertEquals('(Unit Tests) Warning: 1 75 175', alerts[0]['text'])

        # trip a critical now
        ma_load_jmx_mock.return_value = [1, 150]

        alert.collect()
        alerts = collector.alerts()
        self.assertEquals(0, len(collector.alerts()))
        self.assertEquals('CRITICAL', alerts[0]['state'])
        self.assertEquals('(Unit Tests) Critical: 1 150 250',
                          alerts[0]['text'])

        del definition_json['source']['jmx']['value']
        collector = AlertCollector()

        alert = MetricAlert(definition_json, definition_json['source'])
        alert.set_helpers(collector, cluster_configuration)
        alert.set_cluster("c1", "c6401.ambari.apache.org")

        # now try without any jmx value to compare to
        ma_load_jmx_mock.return_value = [1, 25]

        alert.collect()
        alerts = collector.alerts()
        self.assertEquals(0, len(collector.alerts()))
        self.assertEquals('OK', alerts[0]['state'])
        self.assertEquals('(Unit Tests) OK: 1 25 None', alerts[0]['text'])
Exemplo n.º 8
0
    def test_metric_alert(self, ma_load_jmx_mock):
        json = {
            "name": "cpu_check",
            "service": "HDFS",
            "component": "NAMENODE",
            "label": "NameNode process",
            "interval": 6,
            "scope": "host",
            "enabled": True,
            "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1",
            "source": {
                "type": "METRIC",
                "uri": "http://myurl:8633",
                "jmx": {
                    "property_list":
                    ["someJmxObject/value", "someOtherJmxObject/value"],
                    "value":
                    "{0} * 100 + 123"
                },
                "reporting": {
                    "ok": {
                        "text": "ok_arr: {0} {1} {2}",
                    },
                    "warning": {
                        "text": "",
                        "value": 13
                    },
                    "critical": {
                        "text": "crit_arr: {0} {1} {2}",
                        "value": 72
                    }
                }
            }
        }

        ma_load_jmx_mock.return_value = [1, 3]

        collector = AlertCollector()
        ma = MetricAlert(json, json['source'])
        ma.set_helpers(collector, '')
        ma.collect()

        self.assertEquals('CRITICAL', collector.alerts()[0]['state'])
        self.assertEquals('crit_arr: 1 3 223', collector.alerts()[0]['text'])

        del json['source']['jmx']['value']
        collector = AlertCollector()
        ma = MetricAlert(json, json['source'])
        ma.set_helpers(collector, '')
        ma.collect()

        self.assertEquals('OK', collector.alerts()[0]['state'])
        self.assertEquals('ok_arr: 1 3 None', collector.alerts()[0]['text'])
Exemplo n.º 9
0
  def test_alert_uri_structure(self, ma_load_jmx_mock):
    definition_json = self._get_metric_alert_definition()

    ma_load_jmx_mock.return_value = [0,0]
    
    # run the alert without specifying any keys; an exception should be thrown
    # indicating that there was no URI and the result is UNKNOWN
    collector = AlertCollector()
    cluster_configuration = self.__get_cluster_configuration()
    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")
    alert.collect()

    self.assertEquals('UNKNOWN', collector.alerts()[0]['state'])

    # set properties that make no sense wihtout the main URI properties
    configuration = {'hdfs-site' :
      { 'dfs.http.policy' : 'HTTP_ONLY'}
    }

    collector = AlertCollector()
    cluster_configuration = self.__get_cluster_configuration()
    self.__update_cluster_configuration(cluster_configuration, configuration)

    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")
    alert.collect()
    
    self.assertEquals('UNKNOWN', collector.alerts()[0]['state'])
    
    # set an actual property key (http)
    configuration = {'hdfs-site' :
      { 'dfs.http.policy' : 'HTTP_ONLY',
        'dfs.datanode.http.address' : 'c6401.ambari.apache.org:80' }
    }

    self.__update_cluster_configuration(cluster_configuration, configuration)

    collector = AlertCollector()
    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")
    alert.collect()
    
    self.assertEquals('OK', collector.alerts()[0]['state'])
    
    # set an actual property key (https)
    configuration = {'hdfs-site' :
      { 'dfs.http.policy' : 'HTTP_ONLY',
        'dfs.datanode.https.address' : 'c6401.ambari.apache.org:443' }
    }

    self.__update_cluster_configuration(cluster_configuration, configuration)

    collector = AlertCollector()
    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")
    alert.collect()
    
    self.assertEquals('OK', collector.alerts()[0]['state'])

    # set both (http and https)
    configuration = {'hdfs-site' :
      { 'dfs.http.policy' : 'HTTP_ONLY',
        'dfs.datanode.http.address' : 'c6401.ambari.apache.org:80',
        'dfs.datanode.https.address' : 'c6401.ambari.apache.org:443' }
    }

    self.__update_cluster_configuration(cluster_configuration, configuration)

    collector = AlertCollector()
    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")
    alert.collect()
    
    self.assertEquals('OK', collector.alerts()[0]['state'])    
Exemplo n.º 10
0
  def test_metric_alert(self, ma_load_jmx_mock):
    definition_json = self._get_metric_alert_definition()
    configuration = {'hdfs-site' :
      { 'dfs.datanode.http.address': 'c6401.ambari.apache.org:80'}
    }

    collector = AlertCollector()
    cluster_configuration = self.__get_cluster_configuration()
    self.__update_cluster_configuration(cluster_configuration, configuration)

    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")

    # trip an OK
    ma_load_jmx_mock.return_value = [1, 25]

    alert.collect()
    alerts = collector.alerts()
    self.assertEquals(0, len(collector.alerts()))
    self.assertEquals('OK', alerts[0]['state'])
    self.assertEquals('(Unit Tests) OK: 1 25 125', alerts[0]['text'])

    # trip a warning
    ma_load_jmx_mock.return_value = [1, 75]

    alert.collect()
    alerts = collector.alerts()
    self.assertEquals(0, len(collector.alerts()))
    self.assertEquals('WARNING', alerts[0]['state'])
    self.assertEquals('(Unit Tests) Warning: 1 75 175', alerts[0]['text'])

    # trip a critical now
    ma_load_jmx_mock.return_value = [1, 150]

    alert.collect()
    alerts = collector.alerts()
    self.assertEquals(0, len(collector.alerts()))
    self.assertEquals('CRITICAL', alerts[0]['state'])
    self.assertEquals('(Unit Tests) Critical: 1 150 250', alerts[0]['text'])

    del definition_json['source']['jmx']['value']
    collector = AlertCollector()

    alert = MetricAlert(definition_json, definition_json['source'])
    alert.set_helpers(collector, cluster_configuration)
    alert.set_cluster("c1", "c6401.ambari.apache.org")

    # now try without any jmx value to compare to
    ma_load_jmx_mock.return_value = [1, 25]

    alert.collect()
    alerts = collector.alerts()
    self.assertEquals(0, len(collector.alerts()))
    self.assertEquals('OK', alerts[0]['state'])
    self.assertEquals('(Unit Tests) OK: 1 25 None', alerts[0]['text'])
Exemplo n.º 11
0
    def test_alert_uri_structure(self, ma_load_jmx_mock):
        json = {
            "name": "cpu_check",
            "service": "HDFS",
            "component": "NAMENODE",
            "label": "NameNode process",
            "interval": 6,
            "scope": "host",
            "enabled": True,
            "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1",
            "source": {
                "type": "METRIC",
                "uri": {
                    "http": "{{hdfs-site/dfs.datanode.http.address}}",
                    "https": "{{hdfs-site/dfs.datanode.https.address}}",
                    "https_property": "{{hdfs-site/dfs.http.policy}}",
                    "https_property_value": "HTTPS_ONLY"
                },
                "jmx": {
                    "property_list":
                    ["someJmxObject/value", "someOtherJmxObject/value"],
                    "value":
                    "{0}"
                },
                "reporting": {
                    "ok": {
                        "text": "(Unit Tests) ok_arr: {0} {1} {2}",
                    },
                    "warning": {
                        "text": "",
                        "value": 10
                    },
                    "critical": {
                        "text": "(Unit Tests) crit_arr: {0} {1} {2}",
                        "value": 20
                    }
                }
            }
        }

        ma_load_jmx_mock.return_value = [1, 1]

        # run the alert without specifying any keys; an exception should be thrown
        # indicating that there was no URI and the result is UNKNOWN
        collector = AlertCollector()
        ma = MetricAlert(json, json['source'])
        ma.set_helpers(collector, '')
        ma.collect()

        self.assertEquals('UNKNOWN', collector.alerts()[0]['state'])

        # set 2 properties that make no sense wihtout the main URI properties
        collector = AlertCollector()
        ma = MetricAlert(json, json['source'])
        ma.set_helpers(collector, {'hdfs-site/dfs.http.policy': 'HTTP_ONLY'})
        ma.collect()

        self.assertEquals('UNKNOWN', collector.alerts()[0]['state'])

        # set an actual property key (http)
        collector = AlertCollector()
        ma = MetricAlert(json, json['source'])
        ma.set_helpers(
            collector, {
                'hdfs-site/dfs.datanode.http.address': '1.2.3.4:80',
                'hdfs-site/dfs.http.policy': 'HTTP_ONLY'
            })
        ma.collect()

        self.assertEquals('OK', collector.alerts()[0]['state'])

        # set an actual property key (https)
        collector = AlertCollector()
        ma = MetricAlert(json, json['source'])
        ma.set_helpers(
            collector, {
                'hdfs-site/dfs.datanode.https.address': '1.2.3.4:443',
                'hdfs-site/dfs.http.policy': 'HTTP_ONLY'
            })
        ma.collect()

        self.assertEquals('OK', collector.alerts()[0]['state'])

        # set both (http and https)
        collector = AlertCollector()
        ma = MetricAlert(json, json['source'])
        ma.set_helpers(
            collector, {
                'hdfs-site/dfs.datanode.http.address': '1.2.3.4:80',
                'hdfs-site/dfs.datanode.https.address': '1.2.3.4:443',
                'hdfs-site/dfs.http.policy': 'HTTP_ONLY'
            })
        ma.collect()

        self.assertEquals('OK', collector.alerts()[0]['state'])