Exemplo n.º 1
0
    def test_task_calls_backend(self, mock_task, mock_backend):
        # Given
        itservice = factories.ITServiceFactory(is_main=True,
                                               backend_id='VALID')

        min_dt = datetime.date.today().replace(day=10) - relativedelta(
            months=2)
        max_dt = datetime.date.today().replace(day=10) - relativedelta(
            months=1)
        mock_backend().get_sla_range.return_value = min_dt, max_dt

        # When
        pull_sla(itservice.host.uuid)

        # Then
        mock_backend().get_sla_range.assert_called_once_with(
            itservice.backend_id)
        month1_beginning = min_dt.replace(day=1)
        month2_beginning = min_dt.replace(day=1) + relativedelta(months=+1)
        mock_task.delay.assert_has_calls([
            mock.call(itservice.pk, format_period(min_dt),
                      datetime_to_timestamp(month1_beginning),
                      datetime_to_timestamp(month2_beginning)),
            mock.call(itservice.pk, format_period(max_dt),
                      datetime_to_timestamp(month2_beginning),
                      datetime_to_timestamp(max_dt))
        ])
Exemplo n.º 2
0
    def setUp(self):
        self.staff = structure_factories.UserFactory(is_staff=True)
        self.client.force_authenticate(self.staff)
        self.itservice = factories.ITServiceFactory()

        today = datetime.date.today()
        period = format_period(today)
        self.timestamp = datetime_to_timestamp(today)

        next_month = datetime.date.today() + relativedelta(months=1)
        self.next_month = format_period(next_month)

        self.history = models.SlaHistory.objects.create(
            itservice=self.itservice, period=period, value=100.0)
        self.events = models.SlaHistoryEvent.objects.create(
            history=self.history, timestamp=self.timestamp, state='U')
Exemplo n.º 3
0
def pull_sla(host_uuid):
    """
    Pull SLAs for given Zabbix host for all time of its existence in Zabbix
    """
    try:
        host = Host.objects.get(uuid=host_uuid)
    except Host.DoesNotExist:
        logger.warning(
            'Unable to pull SLA for host with UUID %s, because it is gone',
            host_uuid)
        return

    try:
        itservice = ITService.objects.get(host=host, is_main=True)
    except ITService.DoesNotExist:
        logger.warning(
            'Unable to pull SLA for host with UUID %s, because IT service does not exist',
            host_uuid,
        )
        return

    backend = itservice.get_backend()

    try:
        # Get dates of first and last service alarm
        min_dt, max_dt = backend.get_sla_range(itservice.backend_id)
    except ZabbixBackendError as e:
        logger.warning(
            'Unable to pull SLA for host with with UUID %s because of database error: %s',
            host_uuid,
            e,
        )
        return

    # Shift date to beginning of the month
    current_point = min_dt.replace(day=1)
    while current_point <= max_dt:
        period = format_period(current_point)
        start_time = core_utils.datetime_to_timestamp(current_point)
        current_point += relativedelta(months=+1)
        end_time = core_utils.datetime_to_timestamp(min(max_dt, current_point))
        update_itservice_sla.delay(itservice.pk, period, start_time, end_time)

    logger.debug('Successfully pulled SLA for host with with UUID %s',
                 host_uuid)
Exemplo n.º 4
0
def update_sla(sla_type):
    if sla_type not in ('yearly', 'monthly'):
        logger.error('Requested unknown SLA type: %s' % sla_type)
        return

    dt = datetime.datetime.now()

    if sla_type == 'yearly':
        period = dt.year
        start_time = int(
            datetime.datetime.strptime('01/01/%s' % dt.year,
                                       '%d/%m/%Y').strftime("%s"))
    else:  # it's a monthly SLA update
        period = format_period(dt)
        month_start = datetime.datetime.strptime(
            '01/%s/%s' % (dt.month, dt.year), '%d/%m/%Y')
        start_time = int(month_start.strftime("%s"))

    end_time = int(dt.strftime("%s"))

    for itservice in ITService.objects.all():
        update_itservice_sla.delay(itservice.pk, period, start_time, end_time)