def run():
    global past_client_list
    global timeline_stack
    data = tsquery.query()
    client_ids = []
    for client in data['connected_users_list']:
        regex_string = "Unknown from \d*.\d*.\d*.\d*:.\d*"
        name = client['client_nickname']
        real = None == re.search(regex_string, name)
        if real:
            client_ids.append(client['client_database_id'])
            log_new_presence_if_necessary(client['client_database_id'], client['client_nickname'])

    add_list = []
    for client in client_ids:
        if not client in past_client_list:
            add_list.append(client)

    drop_list = []
    for client in past_client_list:
        if not client in client_ids:
            drop_list.append(client)

    past_client_list = client_ids

    for client in add_list:
        timeline_stack.append([client, get_time(), None])
        print convert_uid_to_name(client) + " connected!"

    for client in drop_list:
        print convert_uid_to_name(client) + " disconnected!"
        for line in timeline_stack:
            if line[0] == client:
                line[2] = get_time()
                log_connection(line)
                timeline_stack.remove(line)

    # Log the current users in a different system under the same format
    cur_stack = []
    for client in client_ids:
        for line in timeline_stack:
            new_line = line[:]
            if client == line[0]:
                new_line[2] = get_time()
                cur_stack.append(new_line)
    log_current_users(cur_stack)

    merge_ts_connection_files()
def run():
    while True:
        timestamp = str(datetime.datetime.now().replace(microsecond=0))
        try:
            google_delay = ping.ping('www.google.com', timeout=2000)
            google_raw = 1000 * google_delay
            google_processed = str(int(math.floor(google_raw)))
            router_delay = ping.ping('192.168.1.1', timeout=2000)
            router_raw = 1000 * router_delay
            router_processed = str(int(math.floor(router_raw)))
            localhost_delay = ping.ping('www.andrewdickinson.us', timeout=2000)
            localhost_raw = localhost_delay * 1000
            localhost_processed = str(int(math.floor(localhost_raw)))
            cpu_use = psutil.cpu_percent(interval=1)
            ram_use = psutil.virtual_memory().percent

            ts_data = tsquery.query()
            out = timestamp + " Google: " + google_processed + " Local Router: " + router_processed + " localhost: " + \
                              localhost_processed + " CPU: " + str(cpu_use) + "%" + " RAM: " + str(ram_use) + "%" \
                              " TS Users: " + ts_data['connected_users'] + " Avg TS ping: " + ts_data['avg_ping'] + \
                              " TS Bandwidth Out: " + ts_data['bandwidth_up'] + " TS Bandwidth In: " + \
                              ts_data['bandwidth_down'] + " </br>" + '\n '

            out = out.replace(' 0 ', ' <1 ')
            log(out)

            csv_store([
                timestamp, google_raw, router_raw, localhost_raw, cpu_use,
                ram_use, ts_data['avg_ping'], ts_data['bandwidth_down'],
                ts_data['bandwidth_up'], ts_data['connected_users']
            ])

        # except TypeError:
        #     print "Error"
        #     log(str(datetime.datetime.now().replace(microsecond=0)) + " Error2</br>" + "\n")
        #     csv_store([timestamp, 0, 0, 0, 0, 0, 0, 0, 0, 0])
        except socket.error, e:
            print "Ping Error:" + str(e)
            log(
                str(datetime.datetime.now().replace(microsecond=0)) +
                " Error" + "</br>" + '\n')
            csv_store([timestamp, 0, 0, 0, 0, 0, 0, 0, 0, 0])
        except EOFError:
            print "TS Connection Issue"
            log(
                str(datetime.datetime.now().replace(microsecond=0)) +
                " Error3" + "</br>" + '\n')
            csv_store([timestamp, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Example #3
0
#-*- coding:UTF-8 -*-
import sys,json
import tsquery as ts

line=' '
while line:
    line = sys.stdin.readline()

    try:
        # 結束時,由於line不是正常的資料,會發生錯誤,似乎不需要處理
        message = json.loads(line)
        if message.get('func'):
            print(ts.query(message))
    except Exception as e:
        pass #print(e)

# 用python -u 來執行,故不需要手工flush()
#sys.stdout.flush()