def main():
    if len(sys.argv) == 1:
        print('usage: %s metric_name count' % sys.argv[0], file=sys.stderr)
        return 1

    mon_client = utils.create_mon_client()

    metric_start_time = time.time()
    metric_name = sys.argv[1]
    num_metrics_to_send = int(sys.argv[2])
    dimensions = {'Test_Send': 'Number_1'}  # Should be arg
    start_time = time.time()
    fields = {'name': metric_name}
    fields['dimensions'] = dimensions
    for val in range(0, num_metrics_to_send):
        fields['value'] = str(val)
        fields['timestamp'] = time.time()
        call_mon_api(mon_client.metrics.create, fields)
        # time.sleep(1)

    print("Took %d seconds to send %d measurements" %
          ((time.time() - start_time), num_metrics_to_send))
    metric_end_time = time.time()
    # API requires end time to be greater than start time
    if (metric_end_time - metric_start_time) < 1:
        metric_end_time = metric_start_time + 1
    start_timestamp = create_timestamp(metric_start_time)
    end_timestamp = create_timestamp(metric_end_time)
    fields = {'name': metric_name}
    fields['dimensions'] = dimensions
    fields['start_time'] = start_timestamp
    fields['end_time'] = end_timestamp
    for i in range(0, 30):
        result = call_mon_api(mon_client.metrics.list_measurements, fields)
        if len(result) > 0:
            measurements = result[0]['measurements']
            if len(measurements) >= num_metrics_to_send:
                break
            print('Found %d of %d metrics so far' %
                  (len(measurements), num_metrics_to_send))
        time.sleep(1)

    if len(result) == 0:
        print('Did not receive any metrics in %d seconds' % i, file=sys.stderr)
        return 1

    if len(measurements) != num_metrics_to_send:
        print('Expected %d measurements but found %d' %
              (num_metrics_to_send, len(measurements)), file=sys.stderr)
        return 1
    print('Took %d seconds for metrics to fully arrive' % i)
    expected = num_metrics_to_send - 1
    result = 0
    for index in range(num_metrics_to_send, 0):
        value = measurements[index]
        if value[2] != expected:
            print('Expected %d but found %d for %d' %
                  (expected, value[2], index), file=sys.stderr)
        expected = expected - 1
    return result
def main():
    if len(sys.argv) == 1:
        print('usage: %s count [alarm-id]' % sys.argv[0], file=sys.stderr)
        return 1

    if not utils.ensure_has_notification_engine():
        return 1

    mon_client = utils.create_mon_client()
    num_cycles = int(sys.argv[1])

    alarm_name = 'notification_cycleTest'
    alarm_json = alarm.find_alarm_byname(mon_client, alarm_name)
    if alarm_json is not None:
        alarm_id = alarm_json['id']
    else:
        existing = notification.find_by_name(mon_client, alarm_name)
        if existing is not None:
            notification_id = existing['id']
        else:
            notification_id = notification.create(mon_client, alarm_name,
                                                  "root@localhost")
        alarm_id = alarm.create(mon_client, alarm_name, None, 'max(cc) > 100',
                                notification_id, notification_id,
                                notification_id)

    user = '******'
    start_time = time.time()
    initial_state = alarm.get_state(mon_client, alarm_id)
    state = initial_state

    existing_notifications = utils.find_notifications(alarm_id, user)
    notifications_sent = num_cycles * 2
    for _ in range(0, notifications_sent):
        if state == 'OK':
            state = 'ALARM'
        else:
            state = 'OK'
        if not alarm.set_state(mon_client, alarm_id, state):
            return 1

    print("Took %d seconds to send %d alarm state changes" %
          ((time.time() - start_time), num_cycles * 2))

    for i in range(0, 30):
        notifications = utils.find_notifications(alarm_id, user)
        notifications_found = len(notifications) - len(existing_notifications)
        if notifications_found >= notifications_sent:
            break
        print('Found %d of %d expected notifications so far' %
              (notifications_found, notifications_sent))
        time.sleep(1)

    if notifications_found < notifications_sent:
        print('Expected %d notifications but found %d' %
              (notifications_sent, notifications_found), file=sys.stderr)
        return 1

    print('Took %d seconds for notifications to fully arrive' % i)
    result = 0
    return result
def main():
    if not utils.ensure_has_notification_engine():
        return 1

    # Delete notification for OK.Cycle OK, ALARM, UNDETERMINED
    # Ensure proper notifications got written for ALARM, UNDETERMINED

    states = ['OK', 'ALARM', 'UNDETERMINED']
    mon_client = utils.create_mon_client()

    try:
        # Create 3 notifications with different emails, root, kafka,
        # and monasca-agent
        email1 = "root"
        email2 = "kafka"
        email3 = "monasca-agent"
        notification_id_1 = find_or_create_notification(mon_client, email1,
                                                        email1 + "@localhost")
        notification_id_2 = find_or_create_notification(mon_client, email2,
                                                        email2 + "@localhost")
        notification_id_3 = find_or_create_notification(mon_client, email3,
                                                        email3 + "@localhost")

        # Create an alarm. Cycle OK, ALARM, UNDETERMINED,
        alarm_name = "Test Notifications-" + str(os.getpid())
        expr = 'max(not_real_metric{}) > 10'
        alarm_id = alarm.create(mon_client, alarm_name, None, expr,
                                notification_id_1, notification_id_2,
                                notification_id_3)
        print('Created Alarm %s' % alarm_id)
        print_notification_setup(mon_client, alarm_id)
        print('Test initial cycle of Alarms')
        cycle_states(mon_client, alarm_id, states)

        # Ensure proper notifications got written to each
        if not check_notifications(alarm_id, email1, email2, email3,
                                   states[0], states[1], states[2], 0):
            return 1

        # Disable alarm. Cycle OK, ALARM, UNDETERMINED,
        print('Disable Alarm')
        alarm.disable(mon_client, alarm_id)
        cycle_states(mon_client, alarm_id, states)

        # Ensure no new notifications
        if not check_notifications(alarm_id, email1, email2, email3,
                                   states[0], states[1], states[2], 0):
            return 1

        # Enable alarm. Cycle OK, ALARM, UNDETERMINED
        print('Enable Alarm')
        alarm.enable(mon_client, alarm_id)
        cycle_states(mon_client, alarm_id, states)

        # Ensure proper notifications got written to each
        if not check_notifications(alarm_id, email1, email2, email3,
                                   states[0], states[1], states[2], 1):
            return 1

        # Switch Alarm notifications around. Cycle OK, ALARM, UNDETERMINED,
        print("Switch around Alarm notifications")
        alarm.patch(mon_client, alarm_id,
                    {'ok_actions': [notification_id_2],
                     'alarm_actions': [notification_id_3],
                     'undetermined_actions': [notification_id_1]})
        print_notification_setup(mon_client, alarm_id)
        cycle_states(mon_client, alarm_id, states)

        # Ensure proper notifications got written to each
        if not check_notifications(alarm_id, email2, email3, email1,
                                   states[0], states[1], states[2], 2):
            return 1

        # Switch the email addresses around. Cycle OK, ALARM, UNDETERMINED,
        # Ensure proper notifications got written to each
        return 0
    except exc.HTTPException as he:
        print(he.code)
        print(he.message)
        return 1