Пример #1
0
def send_log_to_database(service_id, logfile, filename):
    global lastmod
    cur = cassandra.cursor()
    c = unicode(open(logfile).read(),
                errors='ignore')  # ignore non-unicode characters in log file
    if len(c) == 0:
        print "logfile is empty"
        return
    now = cassandra.time_to_timestamp(time.time())
    for r in c.splitlines():
        print {
            'logfile': logfile,
            'message': r,
            'service_id': service_id,
            'time': now
        }
        cur.execute(
            "UPDATE log SET logfile = :logfile, message = :message WHERE service_id = :service_id AND time = :time",
            {
                'logfile': os.path.split(logfile)[-1],
                'message': r,
                'service_id': service_id,
                'time': now
            })

    # potential race condition situation below
    if mtime(logfile) != lastmod:
        # file appended to during db send, so delete the part of file we sent (but not the rest)
        open(logfile, 'w').write(open(logfile).read()[len(c):])
    else:
        # just clear file
        open(logfile, 'w').close()
    lastmod = mtime(logfile)
Пример #2
0
def update_status(service_id, pid):
    global last_status
    fields = ['pcpu', 'pmem', 'pid', 'cputime', 'rss', 'vsize']
    v = subprocess.Popen(
        ['ps', '-p', str(int(pid)), '-o', ' '.join(fields)],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE).stdout.read().splitlines()
    if len(v) <= 1:
        return  # process not running -- no status

    d = dict(zip(fields, v[-1].split()))
    if d != last_status:
        last_status = d
        now = cassandra.time_to_timestamp(time.time())
        cputime = cputime_to_float(d['cputime'])
        cassandra.cursor().execute(
            """UPDATE status SET
                                      pmem = :pmem, pcpu = :pcpu, cputime = :cputime, vsize = :vsize, rss = :rss
                                      WHERE service_id = :service_id AND time = :time""",
            {
                'service_id': service_id,
                'time': now,
                'pmem': d['pmem'],
                'pcpu': d['pcpu'],
                'cputime': cputime,
                'vsize': d['vsize'],
                'rss': d['rss']
            })
Пример #3
0
def update_status(service_id, pid):
    global last_status
    fields = ['pcpu', 'pmem', 'pid', 'cputime', 'rss', 'vsize']
    v = subprocess.Popen(['ps', '-p', str(int(pid)), '-o', ' '.join(fields)],
                         stdin=subprocess.PIPE, stdout = subprocess.PIPE,
                         stderr=subprocess.PIPE).stdout.read().splitlines()
    if len(v) <= 1:
        return    # process not running -- no status

    d = dict(zip(fields, v[-1].split()))
    if d != last_status:
        last_status = d
        now = cassandra.time_to_timestamp(time.time())
        cputime = cputime_to_float(d['cputime'])
        cassandra.cursor().execute("""UPDATE status SET
                                      pmem = :pmem, pcpu = :pcpu, cputime = :cputime, vsize = :vsize, rss = :rss
                                      WHERE service_id = :service_id AND time = :time""",
                    {'service_id':service_id, 'time':now, 'pmem':d['pmem'], 'pcpu':d['pcpu'],
                     'cputime':cputime, 'vsize':d['vsize'], 'rss':d['rss']})
Пример #4
0
def send_log_to_database(service_id, logfile, filename):
    global lastmod
    cur = cassandra.cursor()
    c = unicode(open(logfile).read(), errors='ignore')  # ignore non-unicode characters in log file
    if len(c) == 0:
        print "logfile is empty"
        return
    now = cassandra.time_to_timestamp(time.time())
    for r in c.splitlines():
        print {'logfile':logfile, 'message':r, 'service_id':service_id, 'time':now}
        cur.execute("UPDATE log SET logfile = :logfile, message = :message WHERE service_id = :service_id AND time = :time",
                    {'logfile':os.path.split(logfile)[-1], 'message':r, 'service_id':service_id, 'time':now})

    # potential race condition situation below
    if mtime(logfile) != lastmod:
        # file appended to during db send, so delete the part of file we sent (but not the rest)
        open(logfile,'w').write(open(logfile).read()[len(c):])
    else:
        # just clear file
        open(logfile,'w').close()
    lastmod = mtime(logfile)