示例#1
0
def main(host, port):
    maybe_enable_rollbar()

    if host:
        status_client = _get_redis_client(host, port, STATUS_DATABASE)
        report_client = _get_redis_client(host, port, REPORT_DATABASE)
    else:
        status_client = get_redis_client(STATUS_DATABASE)
        report_client = get_redis_client(REPORT_DATABASE)
    status_client.flushdb()
    report_client.flushdb()
    exit(0)
示例#2
0
def set_heartbeat_report(host, port, report):
    if not report:
        return
    client = _get_redis_client(host, port, REPORT_DATABASE)
    batch_client = client.pipeline()
    for name, value in report.iteritems():
        batch_client.set(name, value)
    batch_client.execute()
示例#3
0
文件: report.py 项目: apolmig/inbox
def set_heartbeat_report(host, port, report):
    if not report:
        return
    client = _get_redis_client(host, port, REPORT_DATABASE)
    batch_client = client.pipeline()
    # flush the db to avoid stale information
    batch_client.flushdb()
    for name, value in report.iteritems():
        batch_client.set(name, value)
    batch_client.execute()
示例#4
0
def get_heartbeat_report(host, port):
    client = _get_redis_client(host, port, REPORT_DATABASE)
    batch_client = client.pipeline()
    names = []
    for name in client.scan_iter(count=100):
        if name == 'ElastiCacheMasterReplicationTimestamp':
            continue
        names.append(int(name))
        batch_client.get(name)
    values = map(lambda v: True if v == 'True' else False,
                 batch_client.execute())
    return dict(zip(names, values))
示例#5
0
def get_heartbeat_status(host=None, port=6379, account_id=None):
    if host:
        thresholds = _get_alive_thresholds()
        client = _get_redis_client(host, port, STATUS_DATABASE)
    else:
        thresholds = get_alive_thresholds()
        client = get_redis_client(STATUS_DATABASE)
    batch_client = client.pipeline()

    keys = []
    match_key = None
    if account_id:
        match_key = HeartbeatStatusKey.all_folders(account_id)
    for k in client.scan_iter(match=match_key, count=100):
        if k == 'ElastiCacheMasterReplicationTimestamp':
            continue
        batch_client.hgetall(k)
        keys.append(k)
    values = batch_client.execute()

    now = datetime.utcnow()

    accounts = {}
    for (k, v) in zip(keys, values):
        key = HeartbeatStatusKey.from_string(k)
        account_alive, provider_name, folders = accounts.get(key.account_id,
                                                             (True, '', {}))
        folder_alive, folder_name, devices = folders.get(key.folder_id,
                                                         (True, '', {}))

        for device_id in v:
            value = json.loads(v[device_id])

            provider_name = value['provider_name']
            folder_name = value['folder_name']

            heartbeat_at = datetime.strptime(value['heartbeat_at'],
                                             '%Y-%m-%d %H:%M:%S.%f')
            state = value.get('state', None)
            action = value.get('action', None)

            if key.folder_id == -1:
                # contacts
                device_alive = (now - heartbeat_at) < thresholds.contacts
            elif key.folder_id == -2:
                # events
                device_alive = (now - heartbeat_at) < thresholds.events
            elif provider_name == 'eas' and action == 'ping':
                # eas w/ ping
                device_alive = (now - heartbeat_at) < thresholds.eas
            else:
                device_alive = (now - heartbeat_at) < thresholds.base
            device_alive = device_alive and \
                (state in set([None, 'initial', 'poll']))

            devices[int(device_id)] = {'heartbeat_at': str(heartbeat_at),
                                       'state': state,
                                       'action': action,
                                       'alive': device_alive}

            # a folder is alive if and only if all the devices handling that
            # folder are alive
            folder_alive = folder_alive and device_alive

            folders[key.folder_id] = (folder_alive, folder_name, devices)

            # an account is alive if and only if all the folders of the account
            # are alive
            account_alive = account_alive and folder_alive

            accounts[key.account_id] = (account_alive, provider_name, folders)

    return accounts