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'] })
def record_that_service_stopped(service_id): cassandra.cursor().execute( "UPDATE services SET running = :running WHERE service_id = :service_id", { 'running': 'false', 'service_id': service_id })
def record_that_service_started(name, address, port, username, pid, monitor_pid): service_id = uuid.uuid1() cassandra.cursor().execute(""" UPDATE services SET name = :name, address = :address, port = :port, running = :running, username = :username, pid = :pid, monitor_pid = :monitor_pid WHERE service_id = :service_id""", {'service_id':service_id, 'name':name, 'address':address, 'running':'true', 'port':port, 'username':username, 'pid':pid, 'monitor_pid':monitor_pid}) return service_id
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)
def running_services(): """ Return list of the currently running services. """ cur = cassandra.cursor() cur.execute("SELECT * FROM services WHERE running = 'true'") r = cur.fetchall() return [dict([(c,t[i]) for i,c in enumerate(service_columns)]) for t in r]
def record_that_service_started(name, address, port, username, pid, monitor_pid): service_id = uuid.uuid1() cassandra.cursor().execute( """ UPDATE services SET name = :name, address = :address, port = :port, running = :running, username = :username, pid = :pid, monitor_pid = :monitor_pid WHERE service_id = :service_id""", { 'service_id': service_id, 'name': name, 'address': address, 'running': 'true', 'port': port, 'username': username, 'pid': pid, 'monitor_pid': monitor_pid }) return service_id
def running_services(): """ Return list of the currently running services. """ cur = cassandra.cursor() cur.execute("SELECT * FROM services WHERE running = 'true'") r = cur.fetchall() return [ dict([(c, t[i]) for i, c in enumerate(service_columns)]) for t in r ]
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']})
def latest_status(service_id): """ Return latest status information about service with given id, or None if there is no known status information. """ cur = cassandra.cursor() cur.execute('SELECT * FROM status WHERE service_id = :service_id ORDER BY time DESC LIMIT 1', {'service_id':service_id}) r = cur.fetchone() if r is None: return None else: return dict([(c, r[i]) for i, c in enumerate(status_columns)])
def latest_status(service_id): """ Return latest status information about service with given id, or None if there is no known status information. """ cur = cassandra.cursor() cur.execute( 'SELECT * FROM status WHERE service_id = :service_id ORDER BY time DESC LIMIT 1', {'service_id': service_id}) r = cur.fetchone() if r is None: return None else: return dict([(c, r[i]) for i, c in enumerate(status_columns)])
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)
def lifetime_status(service_id): cur = cassandra.cursor() cur.execute( 'SELECT * FROM status WHERE service_id = :service_id ORDER BY time ASC', {'service_id': service_id}) return cur.fetchall()
def record_that_service_stopped(service_id): cassandra.cursor().execute("UPDATE services SET running = :running WHERE service_id = :service_id", {'running':'false', 'service_id':service_id})
def lifetime_status(service_id): cur = cassandra.cursor() cur.execute('SELECT * FROM status WHERE service_id = :service_id ORDER BY time ASC', {'service_id':service_id}) return cur.fetchall()