def __json_to_callable(self, clusterName, hostName, json_definition):
    """
    converts the json that represents all aspects of a definition
    and makes an object that extends BaseAlert that is used for individual
    """
    alert = None

    try:
      source = json_definition['source']
      source_type = source.get('type', '')

      if logger.isEnabledFor(logging.DEBUG):
        logger.debug("[AlertScheduler] Creating job type {0} with {1}".format(source_type, str(json_definition)))


      if source_type == AlertSchedulerHandler.TYPE_METRIC:
        alert = MetricAlert(json_definition, source, self.config)
      elif source_type == AlertSchedulerHandler.TYPE_PORT:
        alert = PortAlert(json_definition, source)
      elif source_type == AlertSchedulerHandler.TYPE_SCRIPT:
        source['stacks_directory'] = self.stacks_dir
        source['common_services_directory'] = self.common_services_dir
        source['host_scripts_directory'] = self.host_scripts_dir
        alert = ScriptAlert(json_definition, source, self.config)
      elif source_type == AlertSchedulerHandler.TYPE_WEB:
        alert = WebAlert(json_definition, source, self.config)
      elif source_type == AlertSchedulerHandler.TYPE_RECOVERY:
        alert = RecoveryAlert(json_definition, source, self.recovery_manger)

      if alert is not None:
        alert.set_cluster(clusterName, hostName)

    except Exception,exception:
      logger.exception("[AlertScheduler] Unable to load an invalid alert definition. It will be skipped.")
Beispiel #2
0
  def test_collect(self, urllib):
    alert_meta = {
      'name': 'alert1',
      'label': 'label1',
      'serviceName': 'service1',
      'componentName': 'component1',
      'uuid': '123',
      'enabled': 'true'
    }
    alert_source_meta = {
      'jmx': {
        'property_list': [
          'x/y'
        ]
      },
      'uri': {
        'http': '192.168.0.10:8080',
        'https_property': '{{hdfs-site/dfs.http.policy}}',
        'https_property_value': 'HTTPS_ONLY'
      },
      "reporting": {
        "ok": {
          "text": "OK: {0}"
        },
        "warning": {
          "text": "Warn: {0}",
          "value": 2
        },
        "critical": {
          "text": "Crit: {0}",
          "value": 5
        }
      }
    }
    cluster = 'c1'
    host = 'host1'
    expected_text = 'Warn: 4'

    def collector_side_effect(clus, data):
      self.assertEquals(data['name'], alert_meta['name'])
      self.assertEquals(data['label'], alert_meta['label'])
      self.assertEquals(data['text'], expected_text)
      self.assertEquals(data['service'], alert_meta['serviceName'])
      self.assertEquals(data['component'], alert_meta['componentName'])
      self.assertEquals(data['uuid'], alert_meta['uuid'])
      self.assertEquals(data['enabled'], alert_meta['enabled'])
      self.assertEquals(data['cluster'], cluster)
      self.assertEquals(clus, cluster)

    response = Mock()
    urllib.return_value = response
    response.read = Mock(return_value='{"beans": [{"y": 4}]}')
    mock_collector = MagicMock()
    mock_collector.put = Mock(side_effect=collector_side_effect)

    alert = MetricAlert(alert_meta, alert_source_meta)
    alert.set_helpers(mock_collector, {'foo-site/bar': 12, 'foo-site/baz': 'asd'})
    alert.set_cluster(cluster, host)

    alert.collect()
  def test_collect(self, urllib):
    alert_meta = {
      'name': 'alert1',
      'label': 'label1',
      'serviceName': 'service1',
      'componentName': 'component1',
      'uuid': '123',
      'enabled': 'true'
    }
    alert_source_meta = {
      'jmx': {
        'property_list': [
          'x/y'
        ]
      },
      'uri': {
        'http': '192.168.0.10:8080',
        'https_property': '{{hdfs-site/dfs.http.policy}}',
        'https_property_value': 'HTTPS_ONLY'
      },
      "reporting": {
        "ok": {
          "text": "OK: {0}"
        },
        "warning": {
          "text": "Warn: {0}",
          "value": 2
        },
        "critical": {
          "text": "Crit: {0}",
          "value": 5
        }
      }
    }
    cluster = 'c1'
    host = 'host1'
    expected_text = 'Warn: 4'

    def collector_side_effect(clus, data):
      self.assertEquals(data['name'], alert_meta['name'])
      self.assertEquals(data['label'], alert_meta['label'])
      self.assertEquals(data['text'], expected_text)
      self.assertEquals(data['service'], alert_meta['serviceName'])
      self.assertEquals(data['component'], alert_meta['componentName'])
      self.assertEquals(data['uuid'], alert_meta['uuid'])
      self.assertEquals(data['enabled'], alert_meta['enabled'])
      self.assertEquals(data['cluster'], cluster)
      self.assertEquals(clus, cluster)

    response = Mock()
    urllib.return_value = response
    response.read = Mock(return_value='{"beans": [{"y": 4}]}')
    mock_collector = MagicMock()
    mock_collector.put = Mock(side_effect=collector_side_effect)

    alert = MetricAlert(alert_meta, alert_source_meta)
    alert.set_helpers(mock_collector, {'foo-site/bar': 12, 'foo-site/baz': 'asd'})
    alert.set_cluster(cluster, host)

    alert.collect()
    def __json_to_callable(self, clusterName, hostName, json_definition):
        '''
    converts the json that represents all aspects of a definition
    and makes an object that extends BaseAlert that is used for individual
    '''
        source = json_definition['source']
        source_type = source.get('type', '')

        if logger.isEnabledFor(logging.DEBUG):
            logger.debug("Creating job type {0} with {1}".format(
                source_type, str(json_definition)))

        alert = None

        if source_type == AlertSchedulerHandler.TYPE_METRIC:
            alert = MetricAlert(json_definition, source)
        elif source_type == AlertSchedulerHandler.TYPE_PORT:
            alert = PortAlert(json_definition, source)
        elif source_type == AlertSchedulerHandler.TYPE_SCRIPT:
            source['stacks_dir'] = self.stacks_dir
            alert = ScriptAlert(json_definition, source)

        if alert is not None:
            alert.set_cluster(clusterName, hostName)

        return alert