diff --git a/init.sh b/init.sh new file mode 100644 index 0000000..6b74bfc --- /dev/null +++ b/init.sh @@ -0,0 +1 @@ +python raspberry-pi-web-info/api.py & \ No newline at end of file diff --git a/raspberry-pi-web-info/api.py b/raspberry-pi-web-info/api.py new file mode 100644 index 0000000..87e5758 --- /dev/null +++ b/raspberry-pi-web-info/api.py @@ -0,0 +1,64 @@ +# @author andrealmeida + +from pie import network_info, measure_temp_info, disk_info, memory_info, shutdown_pie + +#!flask/bin/python +from flask import Flask, request, Response, redirect, render_template, url_for, jsonify +from functools import wraps + +import json + +app = Flask(__name__) +data = {} + +def check_auth(username, password): + return username == 'admin' and password == 'admin' + +def authenticate(): + return Response( + 'Could not verify your access level for that URL.\n' + 'You have to login with proper credentials', 401, + {'WWW-Authenticate': 'Basic realm="Login Required"'}) + +def requires_auth(f): + @wraps(f) + def decorated(*args, **kwargs): + auth = request.authorization + if not auth or not check_auth(auth.username, auth.password): + return authenticate() + return f(*args, **kwargs) + return decorated + +@app.route('/') +@requires_auth +def index(): + data = network_info() + data = measure_temp_info() + data = disk_info("/") + data = memory_info() + return render_template('pie.html', data = data) + +@app.route('/logout') +@requires_auth +def logout(): + return authenticate() + +@app.route("/readpietojson") +@requires_auth +def readpietojson(): + data = network_info() + data = measure_temp_info() + data = disk_info("/") + data = memory_info() + return jsonify(data); + +@app.route("/shutdown") +@requires_auth +def shutdown(): + shutdown_pie() + return ; + +# __main__ # + +if __name__ == '__main__': + app.run(host="0.0.0.0", port="5000", debug=False) diff --git a/raspberry-pi-web-info/pie.py b/raspberry-pi-web-info/pie.py new file mode 100644 index 0000000..bd393a1 --- /dev/null +++ b/raspberry-pi-web-info/pie.py @@ -0,0 +1,62 @@ +# @author andrealmeida + +import netifaces, time, json, os, psutil + +data = {} + +def network_info (): + interfaces = netifaces.interfaces() + tmp_data = {} + + interface = "" + mac_address = "" + ip = "" + i = 0 + + for interface in interfaces: + if interface != 'lo': + mac_address = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr'], + + try: + ip = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'] + + except KeyError: + continue + + tmp_data[i] = { + "interface": interface, + "mac_address": mac_address, + "IP": ip + } + + i += 1 + data['interfaces'] = tmp_data + data['timestamp'] = time.time() + data['machine_status'] = True + + return data; + +def measure_temp_info (): + temp = os.popen("vcgencmd measure_temp").readline() + cpu_temp = temp.replace("temp=","") + data['cpu_temp'] = cpu_temp + + return data; + +def disk_info(dirname): + st = os.statvfs(dirname) + tmp_data = {} + tmp_data['used_disk_space'] = psutil.disk_usage("/").used / 1024 / 1024 + tmp_data['free_disk_space'] = psutil.disk_usage("/").free / 1024 / 1024 + tmp_data['total_disk_space'] = psutil.disk_usage(".").total / 1024 / 1024 + + data['disk'] = tmp_data + return data; + +def memory_info (): + data['memory'] = psutil.virtual_memory() + return data; + +def shutdown_pie (): + os.system('sudo shutdown now') + diff --git a/raspberry-pi-web-info/static/styles/style.css b/raspberry-pi-web-info/static/styles/style.css new file mode 100644 index 0000000..5375a77 --- /dev/null +++ b/raspberry-pi-web-info/static/styles/style.css @@ -0,0 +1,7 @@ +.footer { + bottom: 0; + width: 100%; + height: 60px; + line-height: 60px; + background-color: #f5f5f5; +} diff --git a/raspberry-pi-web-info/templates/pie.html b/raspberry-pi-web-info/templates/pie.html new file mode 100644 index 0000000..16a2689 --- /dev/null +++ b/raspberry-pi-web-info/templates/pie.html @@ -0,0 +1,114 @@ + + + + + + + + + + + + +
+
+ Shutdown +
+

Raspberry | Pie

+
+ Latest read: {{ data['timestamp'] }} +
+ +
+

Info

+
+
+

Disk and CPU

+ + + + + + + + + + + + + + + + + + + +
Disk used{{ data['disk']['used_disk_space'] }} MB
Disk free{{ data['disk']['free_disk_space'] }} MB
Disk total{{ data['disk']['total_disk_space'] }} MB
CPU temperature{{ data['cpu_temp'] }}
+
+
+

Memory

+ + + + + + + + + + + + + + + +
Used{{ data['memory']['used'] }} MB
Free{{ data['memory']['free'] }} MB
Total{{ data['memory']['total'] }} MB
+
+
+

Interfaces

+
+
+ + + + + + + + + + + + + + + +
Interface{{ data['interfaces'][0]['interface'] }}
IP{{ data['interfaces'][0]['IP'] }}
Mac Address{{ data['interfaces'][0]['mac_address'][0] }}
+
+
+ + + + + + + + + + + + + + + +
Interface{{ data['interfaces'][1]['interface'] }}
IP{{ data['interfaces'][1]['IP'] }}
Mac Address{{ data['interfaces'][1]['mac_address'][0] }}
+
+
+
+ + + \ No newline at end of file