MAJOR: first commit after migrate
This commit is contained in:
parent
83e36c2c0a
commit
2ffb635ff6
64
raspberry-pi-web-info/api.py
Normal file
64
raspberry-pi-web-info/api.py
Normal file
@ -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)
|
||||||
62
raspberry-pi-web-info/pie.py
Normal file
62
raspberry-pi-web-info/pie.py
Normal file
@ -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')
|
||||||
|
|
||||||
7
raspberry-pi-web-info/static/styles/style.css
Normal file
7
raspberry-pi-web-info/static/styles/style.css
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
.footer {
|
||||||
|
bottom: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 60px;
|
||||||
|
line-height: 60px;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
114
raspberry-pi-web-info/templates/pie.html
Normal file
114
raspberry-pi-web-info/templates/pie.html
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
<!-- @author andrealmeida -->
|
||||||
|
|
||||||
|
<!doctype html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
||||||
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
|
||||||
|
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='styles/style.css') }}">
|
||||||
|
</head>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<div class="jumbotron text-center">
|
||||||
|
<div align="right">
|
||||||
|
<a class="btn btn-danger" href="/shutdown" onclick="return confirm('Are you sure?');">Shutdown</a>
|
||||||
|
</div>
|
||||||
|
<h1>Raspberry | Pie</h1>
|
||||||
|
<br>
|
||||||
|
<small>Latest read: </small><small id="timestamp">{{ data['timestamp'] }} </small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<h3 align="center">Info</h3>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<h3 align="center">Disk and CPU</h3>
|
||||||
|
<table class="table" id="set_values_table">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td id="interface_key">Disk used</td>
|
||||||
|
<td id="interface_value">{{ data['disk']['used_disk_space'] }} MB</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="ip_key">Disk free</td>
|
||||||
|
<td id="ip_value">{{ data['disk']['free_disk_space'] }} MB</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="ip_key">Disk total</td>
|
||||||
|
<td id="ip_value">{{ data['disk']['total_disk_space'] }} MB</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="mac_key">CPU temperature</td>
|
||||||
|
<td id="mac_value">{{ data['cpu_temp'] }}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<h3 align="center">Memory</h3>
|
||||||
|
<table class="table" id="set_values_table">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td id="mac_key">Used</td>
|
||||||
|
<td id="mac_value">{{ data['memory']['used'] }} MB</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="ip_key">Free</td>
|
||||||
|
<td id="ip_value">{{ data['memory']['free'] }} MB</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="ip_key">Total</td>
|
||||||
|
<td id="ip_value">{{ data['memory']['total'] }} MB</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<h3 align="center">Interfaces</h3>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<table class="table" id="set_values_table">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td id="interface_key">Interface</td>
|
||||||
|
<td id="interface_value">{{ data['interfaces'][0]['interface'] }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="ip_key">IP</td>
|
||||||
|
<td id="ip_value">{{ data['interfaces'][0]['IP'] }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="mac_key">Mac Address</td>
|
||||||
|
<td id="mac_value">{{ data['interfaces'][0]['mac_address'][0] }}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<table class="table" id="extended_data_table">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td id="interface_key">Interface</td>
|
||||||
|
<td id="interface_value">{{ data['interfaces'][1]['interface'] }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="ip_key">IP</td>
|
||||||
|
<td id="ip_value">{{ data['interfaces'][1]['IP'] }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="mac_key">Mac Address</td>
|
||||||
|
<td id="mac_value">{{ data['interfaces'][1]['mac_address'][0] }}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<footer class="footer">
|
||||||
|
<div class="container_footer">
|
||||||
|
<span class="text-muted">Developed by <a href="https://andrealmeida.net">André Almeida</a>.</span>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue
Block a user