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 <peter-jan@settlemint.com>
This commit is contained in:
Matthew Sevey 2021-12-06 06:49:58 -05:00 committed by GitHub
parent b2dbe4bdb4
commit 7bf366db24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 0 deletions

4
.gitignore vendored
View File

@ -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

View File

@ -0,0 +1 @@
- Add `/serverload` endpoint for CPU usage and free disk space

View File

@ -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;

View File

@ -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

55
setup-scripts/serverload.sh Executable file
View File

@ -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