From 7bf366db24cd8edc5e899bd4a33d4a05f38c5985 Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Mon, 6 Dec 2021 06:49:58 -0500 Subject: [PATCH] Add serverload endpoint (#1410) * write usage script for cpu and free disk space tracking, testing new endpoint * Test alias nginx path * testing json * fix server load json file being served by nginx * Fix filenames * Add changelog * Add systemd file for serverload * Update setup-scripts/serverload.sh Co-authored-by: Peter-Jan Brone --- .gitignore | 4 ++ changelog/items/key-updates/serverload.md | 1 + docker/nginx/conf.d/server/server.api | 15 +++++++ setup-scripts/serverload.service | 8 ++++ setup-scripts/serverload.sh | 55 +++++++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 changelog/items/key-updates/serverload.md create mode 100644 setup-scripts/serverload.service create mode 100755 setup-scripts/serverload.sh diff --git a/.gitignore b/.gitignore index 0900fafd..fcf25b3b 100644 --- a/.gitignore +++ b/.gitignore @@ -96,3 +96,7 @@ docker/kratos/cr_certs/*.key # Oathkeeper JWKS signing token docker/kratos/oathkeeper/id_token.jwks.json /docker/kratos/config/kratos.yml + +# Setup-script log files +/setup-scripts/serverload.log +/setup-scripts/serverload.json \ No newline at end of file diff --git a/changelog/items/key-updates/serverload.md b/changelog/items/key-updates/serverload.md new file mode 100644 index 00000000..c626b753 --- /dev/null +++ b/changelog/items/key-updates/serverload.md @@ -0,0 +1 @@ +- Add `/serverload` endpoint for CPU usage and free disk space diff --git a/docker/nginx/conf.d/server/server.api b/docker/nginx/conf.d/server/server.api index 4402fc07..878569db 100644 --- a/docker/nginx/conf.d/server/server.api +++ b/docker/nginx/conf.d/server/server.api @@ -71,6 +71,21 @@ location /skynet/stats { proxy_pass http://sia:9980/skynet/stats; } +# Define path for server load endpoint +location /serverload { + # Define root directory in the nginx container to load file from + root /usr/local/share; + + # including this because of peer pressure from the other routes + include /etc/nginx/conf.d/include/cors; + + # tell nginx to expect json + default_type 'application/json'; + + # Allow for /serverload to load /serverload.json file + try_files $uri $uri.json =404; +} + location /skynet/health { include /etc/nginx/conf.d/include/cors; diff --git a/setup-scripts/serverload.service b/setup-scripts/serverload.service new file mode 100644 index 00000000..5d6a41d4 --- /dev/null +++ b/setup-scripts/serverload.service @@ -0,0 +1,8 @@ +[Unit] +Description=Ensure serverload script is running to provide serverload stats. + +[Service] +ExecStart=/bin/bash /home/user/skynet-webportal/serverload.sh + +[Install] +WantedBy=multi-user.target diff --git a/setup-scripts/serverload.sh b/setup-scripts/serverload.sh new file mode 100755 index 00000000..6945bcb0 --- /dev/null +++ b/setup-scripts/serverload.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +: ' +This script writes the CPU usage and the free disk space to a file in a loop. +The results are prepended to the file, so the most recent results are at the +top. This is so that the most recent information can easily be read from the +top of the file and the file can easily be truncated if needed. + +This script is run by the serverload.service systemd process. The +serverload.service file should be copied to +/etc/systemd/system/serverload.service. + +The systemd process can then be started with the following commands: +sudo systemctl start serverload.service + +The status of the process can be checked with: +sudo systemctl is-active serverload.service +' + +# Define Loop Interval +loop_interval=60 +webportal_repo_setup_scripts="/home/user/skynet-webportal/setup-scripts" +logfile_name="serverload.log" +logfile=$webportal_repo_setup_scripts/$logfile_name +jsonfile="serverload.json" +nginx_docker_path="/usr/local/share" + +# Create logfile if it doesn't exist +if [[ ! -e $logfile ]]; then + echo "init" > $logfile +fi + +# Write the output in an infinite loop. +while true; do + # CPU usage + cpu=$(echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')]) + sed -i "1iCPU: ${cpu}" $logfile + + # Disk Usage + disk=$(df -Ph . | tail -1 | awk '{print $4}') + sed -i "1iDISK: ${disk}" $logfile + + # Write the timestamp + timestamp=$(date) + sed -i "1iTIMESTAMP: ${timestamp}" $logfile + + # Write and copy a json file of the latest results to nginx docker container + # to serve + printf '{"cpu":"%s","disk":"%s","timestamp":"%s"}' "$cpu" "$disk" "$timestamp" > $webportal_repo_setup_scripts/$jsonfile + docker cp $webportal_repo_setup_scripts/$jsonfile nginx:$nginx_docker_path/$jsonfile + + # Sleep + sleep $loop_interval +done +