From 1a4582c78d7d8883dc0aea8c832dfe88668d54b7 Mon Sep 17 00:00:00 2001 From: Max Woolf Date: Mon, 17 Oct 2016 22:26:27 -0700 Subject: [PATCH] Initial File Commit --- README.md | 2 +- flask_system.py | 63 ++++++++++ system_daskboard_flask.json | 230 ++++++++++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 flask_system.py create mode 100644 system_daskboard_flask.json diff --git a/README.md b/README.md index 3876423..9d6d40e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # system-dashboard -Minimalist Win/OSX/Linux System Dashboard using Flask and Freeboard +Minimalist Win/OSX/Linux System Dashboard using Flask and Freeboard. This tool was created extremely quickly (less than a couple hours) as a "Hello World" for both technologies. diff --git a/flask_system.py b/flask_system.py new file mode 100644 index 0000000..e7be4f6 --- /dev/null +++ b/flask_system.py @@ -0,0 +1,63 @@ +from flask import Flask +from flask_cors import CORS +from flask_cors import cross_origin +import json +import os +import psutil +import time + +app = Flask(__name__) +CORS(app) # Necessary since API is running locally + +period = 1 + +# Disable Flask console messages: http://stackoverflow.com/a/18379764 +import logging +log = logging.getLogger('werkzeug') +log.setLevel(logging.ERROR) + +@cross_origin() +@app.route('/') +def index(): + + disk_write_data_start = psutil.disk_io_counters(perdisk=False) + io_data_start = psutil.net_io_counters() + + time.sleep(period) + + cpu_data = psutil.cpu_percent(interval=None) + ram_data = psutil.virtual_memory() + disk_data = psutil.disk_usage('/') + disk_write_data = psutil.disk_io_counters(perdisk=False) + io_data = psutil.net_io_counters() + + data = { + 'cpu': { + 'percent': cpu_data + }, + 'ram': { + 'percent': ram_data[2], + 'total': ram_data[0], + 'used': ram_data[3] + }, + 'disk': { + 'total': disk_data[0], + 'used': disk_data[1], + 'percent': disk_data[3], + 'read_bytes_sec': (disk_write_data[2] - disk_write_data_start[2]) \ + / period, + 'write_bytes_sec': (disk_write_data[3] - disk_write_data_start[3]) \ + / period + }, + 'io': { + 'sent_bytes_sec': (io_data[0] - io_data_start[0]) / period, + 'received_bytes_sec': (io_data[1] - io_data_start[1]) / period + } + } + + return json.dumps(data) + +if __name__ == "__main__": + print "System API up at http://localhost:5002" + PORT = int(os.getenv('PORT', 5002)) + app.run(port=PORT, threaded=True) \ No newline at end of file diff --git a/system_daskboard_flask.json b/system_daskboard_flask.json new file mode 100644 index 0000000..be789d7 --- /dev/null +++ b/system_daskboard_flask.json @@ -0,0 +1,230 @@ +{ + "version": 1, + "allow_edit": true, + "plugins": [], + "panes": [ + { + "width": 1, + "row": { + "3": 1 + }, + "col": { + "3": 1 + }, + "col_width": 1, + "widgets": [ + { + "type": "text_widget", + "settings": { + "size": "regular", + "value": "datasources[\"Clock\"][\"time_string_value\"]", + "animate": true + } + } + ] + }, + { + "title": "Disk", + "width": 1, + "row": { + "3": 1 + }, + "col": { + "3": 3 + }, + "col_width": 1, + "widgets": [ + { + "type": "text_widget", + "settings": { + "title": "Bytes Read / Sec", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"disk\"][\"read_bytes_sec\"] / 1000000).toFixed(3)", + "sparkline": true, + "animate": false, + "units": "MB" + } + }, + { + "type": "text_widget", + "settings": { + "title": "Bytes Written / Sec", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"disk\"][\"write_bytes_sec\"] / 1000000).toFixed(3)", + "sparkline": true, + "animate": false, + "units": "MB" + } + } + ] + }, + { + "title": "I/O", + "width": 1, + "row": { + "3": 1 + }, + "col": { + "3": 2 + }, + "col_width": 1, + "widgets": [ + { + "type": "text_widget", + "settings": { + "title": "Bytes Sent / Sec", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"io\"][\"sent_bytes_sec\"] / 1000000).toFixed(3)", + "sparkline": true, + "animate": false, + "units": "MB" + } + }, + { + "type": "text_widget", + "settings": { + "title": "Bytes Received / Sec", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"io\"][\"received_bytes_sec\"] / 1000000).toFixed(3)", + "sparkline": true, + "animate": false, + "units": "MB" + } + } + ] + }, + { + "title": "CPU", + "width": 1, + "row": { + "3": 5 + }, + "col": { + "3": 1 + }, + "col_width": 1, + "widgets": [ + { + "type": "text_widget", + "settings": { + "size": "big", + "value": "datasources[\"System Dashboard Flask\"][\"cpu\"][\"percent\"].toFixed(1)", + "sparkline": true, + "animate": false, + "units": "%" + } + } + ] + }, + { + "title": "RAM", + "width": 1, + "row": { + "3": 11 + }, + "col": { + "3": 1 + }, + "col_width": 1, + "widgets": [ + { + "type": "text_widget", + "settings": { + "size": "big", + "value": "datasources[\"System Dashboard Flask\"][\"ram\"][\"percent\"].toFixed(1)", + "sparkline": true, + "animate": false, + "units": "%" + } + } + ] + }, + { + "title": "Disk Usage", + "width": 1, + "row": { + "3": 11 + }, + "col": { + "3": 3 + }, + "col_width": "1", + "widgets": [ + { + "type": "text_widget", + "settings": { + "title": "Space Used", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"disk\"][\"used\"] / (1000 * 1024 * 1024)).toFixed(2)", + "animate": true, + "units": "GB" + } + }, + { + "type": "text_widget", + "settings": { + "title": "Disk Total", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"disk\"][\"total\"] / (1000 * 1024 * 1024)).toFixed(2)", + "animate": true, + "units": "GB" + } + } + ] + }, + { + "width": 1, + "row": { + "3": 11 + }, + "col": { + "3": 2 + }, + "col_width": 1, + "widgets": [ + { + "type": "text_widget", + "settings": { + "title": "RAM Used", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"ram\"][\"used\"] / (1000 * 1024 * 1024)).toFixed(2)", + "sparkline": false, + "animate": false, + "units": "GB" + } + }, + { + "type": "text_widget", + "settings": { + "title": "RAM Total", + "size": "regular", + "value": "(datasources[\"System Dashboard Flask\"][\"ram\"][\"total\"] / (1000 * 1024 * 1024)).toFixed(2)", + "sparkline": false, + "animate": true, + "units": "GB" + } + } + ] + } + ], + "datasources": [ + { + "name": "System Dashboard Flask", + "type": "JSON", + "settings": { + "url": "http://127.0.0.1:5002", + "use_thingproxy": false, + "refresh": 1, + "method": "GET" + } + }, + { + "name": "Clock", + "type": "clock", + "settings": { + "refresh": 1 + } + } + ], + "columns": 3 +} \ No newline at end of file