From d6251db8d466b558088b1a8e373b8a050555433f Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 27 Feb 2021 03:08:28 +0300 Subject: merge CGI scripts into one --- README.md | 1 - cgi-bin/get.py | 131 +++++++++++++++++++++++++++++++++++++ cgi-bin/hostname.sh | 8 --- cgi-bin/systemctl_failed_system.sh | 8 --- cgi-bin/systemctl_failed_user.sh | 8 --- cgi-bin/systemctl_status_system.sh | 8 --- cgi-bin/systemctl_status_user.sh | 8 --- cgi-bin/systemctl_timers_system.sh | 8 --- cgi-bin/systemctl_timers_user.sh | 8 --- cgi-bin/top.sh | 8 --- index.html | 86 ++++++++---------------- 11 files changed, 158 insertions(+), 124 deletions(-) create mode 100755 cgi-bin/get.py delete mode 100755 cgi-bin/hostname.sh delete mode 100755 cgi-bin/systemctl_failed_system.sh delete mode 100755 cgi-bin/systemctl_failed_user.sh delete mode 100755 cgi-bin/systemctl_status_system.sh delete mode 100755 cgi-bin/systemctl_status_user.sh delete mode 100755 cgi-bin/systemctl_timers_system.sh delete mode 100755 cgi-bin/systemctl_timers_user.sh delete mode 100755 cgi-bin/top.sh diff --git a/README.md b/README.md index ed50bb7..3a69263 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,6 @@ TODO ---- * Show an error if a AJAX call fails. -* Dedupe scripts in cgi-bin. License ------- diff --git a/cgi-bin/get.py b/cgi-bin/get.py new file mode 100755 index 0000000..99c4569 --- /dev/null +++ b/cgi-bin/get.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python3 + +import cgi, cgitb +import json +import os +import socket +import subprocess +from subprocess import PIPE, STDOUT + + +def headers(): + print("Content-Type: text/html; charset=utf-8") + print() + + +def debugging(): + # TODO: figure out how to include this on the web page + #cgitb.enable() + pass + + +def setup(): + headers() + debugging() + + +def format_response(response): + return json.dumps(response, ensure_ascii=False) + + +def run(*args, **kwargs): + output = subprocess.run(args, stdout=PIPE, stderr=STDOUT, universal_newlines=True, **kwargs) + return output.stdout + + +def hostname(): + return socket.gethostname() + + +def top(): + return run('top', '-b', '-n', '1', '-w', '512') + + +systemd_env = os.environ.copy() +systemd_env['SYSTEMD_PAGER'] = '' +systemd_env['SYSTEMD_COLORS'] = 'no' + + +def run_systemd_command(*args): + return run(*args, env=systemd_env) + + +def run_systemctl(*args): + return run_systemd_command('systemctl', *args) + + +def system_status_overview(): + return run_systemctl('--system', 'status', '--full') + + +def system_status_failed(): + return run_systemctl('--system', 'list-units', '--failed', '--full') + + +def user_status_overview(): + return run_systemctl('--user', 'status', '--full') + + +def user_status_failed(): + return run_systemctl('--user', 'list-units', '--failed', '--full') + + +def system_timers(): + return run_systemctl('--system', 'list-timers', '--all', '--full') + + +def user_timers(): + return run_systemctl('--user', 'list-timers', '--all', '--full') + + +def status(): + status = { + 'hostname': hostname(), + 'top': top(), + 'system': { + 'overview': system_status_overview(), + 'failed': system_status_failed(), + 'timers': system_timers(), + }, + 'user': { + 'overview': user_status_overview(), + 'failed': user_status_failed(), + 'timers': user_timers(), + }, + } + return status + + +def timers(): + timers = { + 'system': { + 'timers': system_timers(), + }, + 'user': { + 'timers': user_timers(), + }, + } + return timers + + +def do(): + params = cgi.FieldStorage() + what = params['what'].value + if what == 'status': + response = status() + elif what == 'timers': + response = timers() + elif what == 'top': + response = top() + else: + raise RuntimeError(f'invalid parameter "what": {what}') + print(format_response(response)) + + +def main(): + setup() + do() + + +if __name__ == '__main__': + main() diff --git a/cgi-bin/hostname.sh b/cgi-bin/hostname.sh deleted file mode 100755 index 38b7700..0000000 --- a/cgi-bin/hostname.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: plain/text; charset=utf-8' -echo - -hostname diff --git a/cgi-bin/systemctl_failed_system.sh b/cgi-bin/systemctl_failed_system.sh deleted file mode 100755 index 39d3fc1..0000000 --- a/cgi-bin/systemctl_failed_system.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: text/plain; charset=utf-8' -echo - -SYSTEMD_COLORS=no systemctl list-units --failed --no-pager --full diff --git a/cgi-bin/systemctl_failed_user.sh b/cgi-bin/systemctl_failed_user.sh deleted file mode 100755 index fe9ddc0..0000000 --- a/cgi-bin/systemctl_failed_user.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: text/plain; charset=utf-8' -echo - -SYSTEMD_COLORS=no systemctl --user list-units --failed --no-pager --full diff --git a/cgi-bin/systemctl_status_system.sh b/cgi-bin/systemctl_status_system.sh deleted file mode 100755 index 119b2e2..0000000 --- a/cgi-bin/systemctl_status_system.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: text/plain; charset=utf-8' -echo - -SYSTEMD_COLORS=no systemctl --system status --no-pager --full diff --git a/cgi-bin/systemctl_status_user.sh b/cgi-bin/systemctl_status_user.sh deleted file mode 100755 index 7ba1dd3..0000000 --- a/cgi-bin/systemctl_status_user.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: text/plain; charset=utf-8' -echo - -SYSTEMD_COLORS=no systemctl --user status --no-pager --full diff --git a/cgi-bin/systemctl_timers_system.sh b/cgi-bin/systemctl_timers_system.sh deleted file mode 100755 index 3c93d8b..0000000 --- a/cgi-bin/systemctl_timers_system.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: text/plain; charset=utf-8' -echo - -SYSTEMD_COLORS=no systemctl --system list-timers --all --no-pager --full diff --git a/cgi-bin/systemctl_timers_user.sh b/cgi-bin/systemctl_timers_user.sh deleted file mode 100755 index c8b0a95..0000000 --- a/cgi-bin/systemctl_timers_user.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: text/plain; charset=utf-8' -echo - -SYSTEMD_COLORS=no systemctl --user list-timers --all --no-pager --full diff --git a/cgi-bin/top.sh b/cgi-bin/top.sh deleted file mode 100755 index 9edf850..0000000 --- a/cgi-bin/top.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail - -echo 'Content-Type: text/plain; charset=utf-8' -echo - -top -b -n 1 -w 512 diff --git a/index.html b/index.html index 759697f..2d8639a 100644 --- a/index.html +++ b/index.html @@ -79,66 +79,35 @@ function shutdown() { $.get('cgi-bin/poweroff.sh'); } -function refresh_hostname() { - $.get('cgi-bin/hostname.sh', function(data) { - $('#hostname').text(data); - $('title').text(data); - }); -} - function refresh_top() { - $.get('cgi-bin/top.sh', function(data) { - $('#top').text(data); - }); -} - -function refresh_systemctl_status_system() { - $.get('cgi-bin/systemctl_status_system.sh', function(data) { - $('#systemctl_status_system').text(data); - }); -} - -function refresh_systemctl_status_user() { - $.get('cgi-bin/systemctl_status_user.sh', function(data) { - $('#systemctl_status_user').text(data); + $.get('cgi-bin/get.py?what=top', function(data) { + $('#top').text(JSON.parse(data)); }); } -function refresh_systemctl_failed_system() { - $.get('cgi-bin/systemctl_failed_system.sh', function(data) { - $('#systemctl_failed_system').text(data); +function refresh_timers() { + $.get('cgi-bin/get.py?what=timers', function(data) { + data = JSON.parse(data); + $('#systemctl_timers_system').text(data['system']['timers']); + $('#systemctl_timers_user').text(data['user']['timers']); }); } -function refresh_systemctl_failed_user() { - $.get('cgi-bin/systemctl_failed_user.sh', function(data) { - $('#systemctl_failed_user').text(data); +function refresh_status() { + $.get('cgi-bin/get.py?what=status', function(data) { + data = JSON.parse(data); + $('#hostname').text(data['hostname']); + $('title').text(data['hostname']); + $('#top').text(data['top']); + $('#systemctl_failed_system').text(data['system']['failed']); + $('#systemctl_failed_user').text(data['user']['failed']); + $('#systemctl_status_system').text(data['system']['overview']); + $('#systemctl_status_user').text(data['user']['overview']); + $('#systemctl_timers_system').text(data['system']['timers']); + $('#systemctl_timers_user').text(data['user']['timers']); }); } -function refresh_systemctl_timers_system() { - $.get('cgi-bin/systemctl_timers_system.sh', function(data) { - $('#systemctl_timers_system').text(data); - }); -} - -function refresh_systemctl_timers_user() { - $.get('cgi-bin/systemctl_timers_user.sh', function(data) { - $('#systemctl_timers_user').text(data); - }); -} - -function refresh() { - refresh_hostname(); - refresh_top(); - refresh_systemctl_status_system(); - refresh_systemctl_status_user(); - refresh_systemctl_failed_system(); - refresh_systemctl_failed_user(); - refresh_systemctl_timers_system(); - refresh_systemctl_timers_user(); -} - var top_refresh_interval_seconds = 5; function loop_top() { @@ -146,24 +115,23 @@ function loop_top() { $('#top_refresh_interval').text(top_refresh_interval_seconds); } -var systemctl_timers_refresh_interval_seconds = 30; +var timers_refresh_interval_seconds = 30; -function loop_systemctl_timers() { +function loop_timers() { setInterval(function() { - refresh_systemctl_timers_system(); - refresh_systemctl_timers_user(); - }, systemctl_timers_refresh_interval_seconds * 1000); - $('#systemctl_timers_system_refresh_interval').text(systemctl_timers_refresh_interval_seconds); - $('#systemctl_timers_user_refresh_interval').text(systemctl_timers_refresh_interval_seconds); + refresh_timers(); + }, timers_refresh_interval_seconds * 1000); + $('#systemctl_timers_system_refresh_interval').text(timers_refresh_interval_seconds); + $('#systemctl_timers_user_refresh_interval').text(timers_refresh_interval_seconds); } function loop() { loop_top(); - loop_systemctl_timers(); + loop_timers(); } function main() { - refresh(); + refresh_status(); loop(); } -- cgit v1.2.3