40 lines
959 B
Python
40 lines
959 B
Python
from flask import Flask, render_template, jsonify
|
|
import subprocess
|
|
import platform
|
|
import json
|
|
import threading
|
|
import time
|
|
|
|
app = Flask(__name__)
|
|
|
|
def ping(host):
|
|
param = '-n' if platform.system().lower() == 'windows' else '-c'
|
|
command = ['ping', param, '1', host]
|
|
return subprocess.call(command, stdout=subprocess.DEVNULL) == 0
|
|
|
|
def load_config():
|
|
with open('config.json') as f:
|
|
return json.load(f)
|
|
|
|
device_status = {}
|
|
|
|
def update_status():
|
|
while True:
|
|
config = load_config()
|
|
for device in config['devices']:
|
|
device_status[device['name']] = ping(device['ip'])
|
|
time.sleep(config['check_interval'])
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/api/status')
|
|
def status():
|
|
return jsonify(device_status)
|
|
|
|
if __name__ == '__main__':
|
|
status_thread = threading.Thread(target=update_status, daemon=True)
|
|
status_thread.start()
|
|
app.run(debug=True)
|