|
|
@@ -4,12 +4,12 @@ import errno
|
|
|
import json
|
|
|
import os
|
|
|
from os import path
|
|
|
-import struct
|
|
|
import warnings
|
|
|
|
|
|
warnings.filterwarnings('ignore', 'libevent')
|
|
|
import gevent.pywsgi
|
|
|
|
|
|
+import fileio
|
|
|
import reloader
|
|
|
|
|
|
DATA_DIR = path.expanduser('~/sysvitals_data')
|
|
|
@@ -37,47 +37,13 @@ def application(environ, start_response):
|
|
|
else:
|
|
|
print 'split was', split
|
|
|
|
|
|
-formats = {
|
|
|
- 'cpu': 'f',
|
|
|
- 'mem': 'q',
|
|
|
- 'net': 'q',
|
|
|
- 'disk': 'q',
|
|
|
-}
|
|
|
-
|
|
|
def get_data(split, environ):
|
|
|
group = int(split[1])
|
|
|
client_id = int(split[3])
|
|
|
data_path = path.join(DATA_DIR, str(group), str(client_id))
|
|
|
with open(data_path, 'r') as f:
|
|
|
- buf = f.read()
|
|
|
- rval = {}
|
|
|
- index = 0
|
|
|
- while index < len(buf):
|
|
|
- # read the key
|
|
|
- key_size = ord(buf[index])
|
|
|
- fmt = '%dp' % (key_size + 1)
|
|
|
- data = struct.unpack(fmt, buf[index:index+key_size+1])
|
|
|
- key = data[0]
|
|
|
-
|
|
|
- # get the format_code
|
|
|
- split = key.split('.')
|
|
|
- stat_group = split[0]
|
|
|
- format_code = formats[stat_group]
|
|
|
-
|
|
|
- # read the data
|
|
|
- fmt = '%dp 1440%s' % (key_size + 1, format_code)
|
|
|
- size = struct.calcsize(fmt)
|
|
|
- data = struct.unpack(fmt, buf[index:index+size])
|
|
|
- dict_insert(rval, split, data[1:])
|
|
|
- index += size
|
|
|
- return json.dumps(rval)
|
|
|
-
|
|
|
-def dict_insert(d, split, value):
|
|
|
- if len(split) > 1:
|
|
|
- d.setdefault(split[0], {})
|
|
|
- dict_insert(d[split[0]], split[1:], value)
|
|
|
- else:
|
|
|
- d[split[0]] = value
|
|
|
+ stats = fileio.read_stats(f)
|
|
|
+ return json.dumps(stats)
|
|
|
|
|
|
def post_datum(split, environ):
|
|
|
group = int(split[1])
|
|
|
@@ -90,19 +56,7 @@ def post_datum(split, environ):
|
|
|
if e.errno != errno.EEXIST:
|
|
|
raise
|
|
|
with open(path.join(group_dir, str(client_id)), 'w') as f:
|
|
|
- for stat_group, stats in body.items():
|
|
|
- if stat_group == 'client_id':
|
|
|
- continue
|
|
|
- format_code = formats[stat_group]
|
|
|
- for stat, data in stats.items():
|
|
|
- key = '%s.%s' % (stat_group, stat)
|
|
|
- write_stat(f, key.encode('utf-8'), format_code, data)
|
|
|
+ fileio.write_datum(f, body)
|
|
|
return '{"status": "ok"}'
|
|
|
|
|
|
-def write_stat(f, key, format_code, data):
|
|
|
- fmt = '%dp 1440%s' % (len(key) + 1, format_code)
|
|
|
- array = [-1] * 1440
|
|
|
- array[0] = data
|
|
|
- f.write(struct.pack(fmt, key, *array))
|
|
|
-
|
|
|
main()
|