def test_send_event_with_no_realert_every_to_sensu(
        mock_send_event, mock_compose_monitoring_overrides_for_service):
    mock_compose_monitoring_overrides_for_service.return_value = {}
    check_chronos_jobs.send_event(
        Mock(service="myservice", instance="myinstance", soa_dir="soadir"),
        status_code=0,
        output="this is great",
    )
    mock_send_event.assert_called_once_with(
        service="myservice",
        check_name="check_chronos_jobs.myservice.myinstance",
        overrides={},
        status=0,
        output="this is great",
        soa_dir="soadir",
    )
Beispiel #2
0
def test_send_event_to_sensu(mock_send_event):
    check_chronos_jobs.send_event(
        service='myservice',
        instance='myinstance',
        monitoring_overrides={},
        soa_dir='soadir',
        status_code=0,
        message='this is great',
    )
    assert mock_send_event.called_once_with(
        'myservice',
        'check-chronos-jobs.myservice.myinstance',
        {},
        0,
        'this is great',
        'soadir',
    )
def test_send_event_to_sensu(mock_send_event):
    check_chronos_jobs.send_event(
        service='myservice',
        instance='myinstance',
        monitoring_overrides={},
        soa_dir='soadir',
        status_code=0,
        message='this is great',
    )
    assert mock_send_event.called_once_with(
        'myservice',
        'check-chronos-jobs.myservice.myinstance',
        {},
        0,
        'this is great',
        'soadir',
    )
Beispiel #4
0
def test_send_event_to_sensu(mock_send_event):
    check_chronos_jobs.send_event(
        service='myservice',
        instance='myinstance',
        monitoring_overrides={},
        soa_dir='soadir',
        status_code=0,
        message='this is great',
    )
    mock_send_event.assert_called_once_with(
        service='myservice',
        check_name='check_chronos_jobs.myservice.myinstance',
        overrides={},
        status=0,
        output='this is great',
        soa_dir='soadir',
    )
def test_send_event_to_sensu(mock_send_event):
    check_chronos_jobs.send_event(
        service='myservice',
        instance='myinstance',
        monitoring_overrides={},
        soa_dir='soadir',
        status_code=0,
        message='this is great',
    )
    mock_send_event.assert_called_once_with(
        service='myservice',
        check_name='check_chronos_jobs.myservice.myinstance',
        overrides={},
        status=0,
        output='this is great',
        soa_dir='soadir',
    )
Beispiel #6
0
def test_send_event_with_realert_every_to_sensu(
    mock_send_event,
    mock_compose_monitoring_overrides_for_service,
):
    mock_compose_monitoring_overrides_for_service.return_value = {
        'realert_every': 150
    }
    check_chronos_jobs.send_event(
        Mock(service='myservice', instance='myinstance', soa_dir='soadir'),
        status_code=0,
        output='this is great',
    )
    mock_send_event.assert_called_once_with(
        service='myservice',
        check_name='check_chronos_jobs.myservice.myinstance',
        overrides={'realert_every': 150},
        status=0,
        output='this is great\n\nThis check realerts every 2h30m.',
        soa_dir='soadir',
    )
Beispiel #7
0
def main():

    args = parse_args()
    soa_dir = args.soa_dir

    config = chronos_tools.load_chronos_config()
    client = chronos_tools.get_chronos_client(config)

    running_jobs = set(deployed_job_names(client))

    expected_service_jobs = set([chronos_tools.compose_job_id(*job) for job in
                                 chronos_tools.get_chronos_jobs_for_cluster(soa_dir=args.soa_dir)])

    all_tmp_jobs = set(filter_tmp_jobs(filter_paasta_jobs(running_jobs)))
    expired_tmp_jobs = set(filter_expired_tmp_jobs(client, all_tmp_jobs))
    valid_tmp_jobs = all_tmp_jobs - expired_tmp_jobs

    to_delete = running_jobs - expected_service_jobs - valid_tmp_jobs

    task_responses = cleanup_tasks(client, to_delete)
    task_successes = []
    task_failures = []
    for response in task_responses:
        if isinstance(response[-1], Exception):
            task_failures.append(response)
        else:
            task_successes.append(response)

    job_responses = cleanup_jobs(client, to_delete)
    job_successes = []
    job_failures = []
    for response in job_responses:
        if isinstance(response[-1], Exception):
            job_failures.append(response)
        else:
            job_successes.append(response)
            try:
                (service, instance) = chronos_tools.decompose_job_id(response[0])
                send_event(
                    service=service,
                    instance=instance,
                    monitoring_overrides={},
                    soa_dir=soa_dir,
                    status_code=pysensu_yelp.Status.OK,
                    message="This instance was removed and is no longer supposed to be scheduled.",
                )
            except InvalidJobNameError:
                # If we deleted some bogus job with a bogus jobid that could not be parsed,
                # Just move on, no need to send any kind of paasta event.
                pass

    if len(to_delete) == 0:
        print 'No Chronos Jobs to remove'
    else:
        if len(task_successes) > 0:
            print format_list_output("Successfully Removed Tasks (if any were running) for:",
                                     [job[0] for job in task_successes])

        # if there are any failures, print and exit appropriately
        if len(task_failures) > 0:
            print format_list_output("Failed to Delete Tasks for:", [job[0] for job in task_failures])

        if len(job_successes) > 0:
            print format_list_output("Successfully Removed Jobs:", [job[0] for job in job_successes])

        # if there are any failures, print and exit appropriately
        if len(job_failures) > 0:
            print format_list_output("Failed to Delete Jobs:", [job[0] for job in job_failures])

        if len(job_failures) > 0 or len(task_failures) > 0:
            sys.exit(1)
Beispiel #8
0
def main():

    args = parse_args()
    soa_dir = args.soa_dir

    config = chronos_tools.load_chronos_config()
    client = chronos_tools.get_chronos_client(config)

    running_jobs = set(deployed_job_names(client))

    expected_service_jobs = {
        chronos_tools.compose_job_id(*job)
        for job in chronos_tools.get_chronos_jobs_for_cluster(
            soa_dir=args.soa_dir)
    }

    all_tmp_jobs = set(filter_tmp_jobs(filter_paasta_jobs(running_jobs)))
    expired_tmp_jobs = set(filter_expired_tmp_jobs(client, all_tmp_jobs))
    valid_tmp_jobs = all_tmp_jobs - expired_tmp_jobs

    to_delete = running_jobs - expected_service_jobs - valid_tmp_jobs

    task_responses = cleanup_tasks(client, to_delete)
    task_successes = []
    task_failures = []
    for response in task_responses:
        if isinstance(response[-1], Exception):
            task_failures.append(response)
        else:
            task_successes.append(response)

    job_responses = cleanup_jobs(client, to_delete)
    job_successes = []
    job_failures = []
    for response in job_responses:
        if isinstance(response[-1], Exception):
            job_failures.append(response)
        else:
            job_successes.append(response)
            try:
                (service,
                 instance) = chronos_tools.decompose_job_id(response[0])
                send_event(
                    service=service,
                    instance=instance,
                    monitoring_overrides={},
                    soa_dir=soa_dir,
                    status_code=pysensu_yelp.Status.OK,
                    message=
                    "This instance was removed and is no longer supposed to be scheduled.",
                )
            except InvalidJobNameError:
                # If we deleted some bogus job with a bogus jobid that could not be parsed,
                # Just move on, no need to send any kind of paasta event.
                pass

    if len(to_delete) == 0:
        paasta_print('No Chronos Jobs to remove')
    else:
        if len(task_successes) > 0:
            paasta_print(
                format_list_output(
                    "Successfully Removed Tasks (if any were running) for:",
                    [job[0] for job in task_successes]))

        # if there are any failures, print and exit appropriately
        if len(task_failures) > 0:
            paasta_print(
                format_list_output("Failed to Delete Tasks for:",
                                   [job[0] for job in task_failures]))

        if len(job_successes) > 0:
            paasta_print(
                format_list_output("Successfully Removed Jobs:",
                                   [job[0] for job in job_successes]))

        # if there are any failures, print and exit appropriately
        if len(job_failures) > 0:
            paasta_print(
                format_list_output("Failed to Delete Jobs:",
                                   [job[0] for job in job_failures]))

        if len(job_failures) > 0 or len(task_failures) > 0:
            sys.exit(1)