diff --git a/oblig3/oblig3.md b/oblig3/oblig3.md index 218c98546ffe0e94f656c5418e546a8df237342c..df22f66f279a585d360de480cebd9722c599b6ca 100644 --- a/oblig3/oblig3.md +++ b/oblig3/oblig3.md @@ -100,4 +100,390 @@ curl -s -g 'http://admin:admin@192.168.132.61:9090/api/v1/query?query=last_downl # OUTPUT 9.776767015457153 -``` \ No newline at end of file +``` + +We then download the frontpage_scale.sh script, and added some configuration code: + +``` +#!/bin/bash +source /home/ubuntu/DCSG2003_V23_group45-openrc.sh +source /home/ubuntu/dcsg2003/configuration/base.sh + +# Name of company +COMPANY_NAME="AETA" + +# IP address of server 1 or whoever is the docker swarm leader +SERVER_IP="192.168.134.127" + +# What is the lowest desired number of frontpage users? +FRONTPAGE_COUNT_LIMIT=100 + +# What increments do we increase in? +SCALE_UP_INCREMENT=25 + +# What increments do we decrease in? +SCALE_DOWN_INCREMENT=50 + +# What is the lower download time limit we would like to stay above? +DOWNLOAD_TIME_LOWER_THRESHOLD=1.9 + +# What is the upper download time limit we would like to stay below? +DOWNLOAD_TIME_UPPER_THRESHOLD=10.0 + +# SAFETY VALVE: Set this to "0" if you want the scaling to actually take place +SAFETY_VALVE=0 + +######################## +# SAFETY CHECK: Is bc installed? + +if ! which bc > /dev/null; then +echo "You need to install the package 'bc' first" +exit 1 +fi + +######################## +# Define function for scaling + +function scale { + # This script is assumed to run on the manager and SSH to a server + # in order to run the docker service upgrade command. + # we also assume that we need to use sudo. + + COMMAND="sudo docker service update --env-add BF_FRONTPAGE_LIMIT=$1 bf_web" + + # This could be improved, because what happens when server 1 is unavailable? + + SSH_COMMAND="ssh -t ubuntu@$SERVER_IP " + + if [ "$SAFETY_VALVE" -eq "0" ]; then + # Safety valve is off, we're running the command + $SSH_COMMAND $COMMAND + discord_log "Scaling to $1 frontpage users." + + else + # Saftey valve is on, we only print what we would do + echo "Safety valve is on, this is what would be executed: $SSH_COMMAND $COMMAND" + fi + +} + + +################################ +# Check if the site is up. Exit if it's down. + +STATUS=$( curl -s -g 'http://admin:admin@192.168.132.61:9090/api/v1/query?query=last_status{name="'$COMPANY_NAME'"}' | jq -r '.data.result[].value[1] ') + +if [ "$STATUS" -gt "0" ]; then + echo "Site is considered up" +else + echo "Site is considered down, we should stop here" + exit 1 +fi + +################################ +# The site is up, so we can proceed with checking its performance +# Get current download times: +DOWNLOAD_TIME=$( curl -s -g 'http://admin:admin@192.168.132.61:9090/api/v1/query?query=last_download_time{name="'$COMPANY_NAME'"}' | jq -r '.data.result[].value[1] ') +NUMBER_OF_FRONTPAGE_USERS=$( curl -s -g 'http://admin:admin@192.168.132.61:9090/api/v1/query?query=frontpage_count{name="'$COMPANY_NAME'"}' | jq -r '.data.result[].value[1] ') + +echo "Observed download time: $DOWNLOAD_TIME" + +# check if we are below the lower threshold. If we are, we scale up +if (( $(echo "$DOWNLOAD_TIME < $DOWNLOAD_TIME_LOWER_THRESHOLD" | bc -l) )); then + NEW_FRONTPAGE_COUNT=$( echo "$NUMBER_OF_FRONTPAGE_USERS + $SCALE_UP_INCREMENT" | bc ) + echo "Download time was lower, we have some capacity to spare. Scaling up to $NEW_FRONTPAGE_COUNT" + scale $NEW_FRONTPAGE_COUNT + +# check if we are above the higher threshold. If we are, scale down, but not lower than the limit +elif (( $(echo "$DOWNLOAD_TIME > $DOWNLOAD_TIME_UPPER_THRESHOLD" | bc -l) )); then + + # We can't go lower than the bottom + if [ "$NUMBER_OF_FRONTPAGE_USERS" -eq "$FRONTPAGE_COUNT_LIMIT" ]; then + echo "We should go lower, but we are already at the limit" + exit 0 + fi + + # Lowering the number of frontpage users + NEW_FRONTPAGE_COUNT=$( echo "$NUMBER_OF_FRONTPAGE_USERS - $SCALE_DOWN_INCREMENT" | bc ) + + if [ "$NEW_FRONTPAGE_COUNT" -lt "$FRONTPAGE_COUNT_LIMIT" ]; then + echo "We should scale down, but can't go lower then the limit, so we end up at $FRONTPAGE_COUNT_LIMIT" + NEW_FRONTPAGE_COUNT=$FRONTPAGE_COUNT_LIMIT + else + echo "Scaling down to $NEW_FRONTPAGE_COUNT as new frontpage_limit" + scale $NEW_FRONTPAGE_COUNT + fi +fi +``` + +This script scales the amount of frontpage users up and down, depending on factors such as download time, amount of frontpage users, and our own personal preferences. We add this script to crontab, and it is run every 15 minute. The way this script is configured above, will make bookface automaticly add frontpage users by 25 if the download time is 1.9 secunds or less. If the download time is more than 5.0 seconds, bookface will automaticly remove 50 frontpage users, and by this reduce the amount of frontpage users. + +## Task 3 + +We need to get the download time for bookface the last 24 hours. First, we run this command and get the data in this format(the command and output is for the last 1 hour only): +``` +curl -s -g 'http://admin:admin@192.168.132.61:9090/api/v1/query?query=last_download_time{name="AETA"}[1h]' | jq + +# OUTPUT + +{ + "status": "success", + "data": { + "resultType": "matrix", + "result": [ + { + "metric": { + "__name__": "last_download_time", + "instance": "192.168.129.65:9001", + "job": "mongodb", + "name": "AETA", + "tags": "dcsg2003_23" + }, + "values": [ + [ + 1679758369.944, + "12.337580919265747" + ], + [ + 1679758429.945, + "12.337580919265747" + ], + [ + 1679758489.944, + "10.641442060470581" + ], + [ + 1679758549.944, + "10.641442060470581" + ], + [ + 1679758609.944, + "10.641442060470581" + ], + [ + 1679758669.944, + "10.641442060470581" + ], + [ + 1679758729.944, + "9.864099025726318" + ], + [ + 1679758789.944, + "9.864099025726318" + ], + [ + 1679758849.944, + "9.864099025726318" + ], + [ + 1679758909.944, + "9.864099025726318" + ], + [ + 1679758969.944, + "9.864099025726318" + ], + [ + 1679759029.944, + "9.864099025726318" + ], + [ + 1679759089.944, + "9.864099025726318" + ], + [ + 1679759149.944, + "9.864099025726318" + ], + [ + 1679759209.944, + "11.698165893554688" + ], + [ + 1679759269.944, + "11.698165893554688" + ], + [ + 1679759329.944, + "11.698165893554688" + ], + [ + 1679759389.944, + "11.698165893554688" + ], + [ + 1679759449.944, + "9.226375102996826" + ], + [ + 1679759509.944, + "9.226375102996826" + ], + [ + 1679759569.944, + "9.226375102996826" + ], + [ + 1679759629.944, + "9.226375102996826" + ], + [ + 1679759689.944, + "9.776767015457153" + ], + [ + 1679759749.944, + "9.776767015457153" + ], + [ + 1679759809.944, + "9.776767015457153" + ], + [ + 1679759869.944, + "9.776767015457153" + ], + [ + 1679759929.944, + "9.776767015457153" + ], + [ + 1679759989.944, + "9.776767015457153" + ], + [ + 1679760049.944, + "12.38998794555664" + ], + [ + 1679760109.944, + "12.38998794555664" + ], + [ + 1679760169.944, + "12.38998794555664" + ], + [ + 1679760229.944, + "12.38998794555664" + ], + [ + 1679760289.944, + "12.38998794555664" + ], + [ + 1679760349.944, + "12.38998794555664" + ], + [ + 1679760409.944, + "11.564746141433716" + ], + [ + 1679760469.944, + "11.564746141433716" + ], + [ + 1679760529.944, + "11.564746141433716" + ], + [ + 1679760589.944, + "11.564746141433716" + ], + [ + 1679760649.944, + "11.564746141433716" + ], + [ + 1679760709.944, + "11.564746141433716" + ], + [ + 1679760769.944, + "11.28351092338562" + ], + [ + 1679760829.944, + "11.28351092338562" + ], + [ + 1679760889.944, + "8.994087934494019" + ], + [ + 1679760949.944, + "8.994087934494019" + ], + [ + 1679761009.944, + "8.994087934494019" + ], + [ + 1679761069.944, + "8.994087934494019" + ], + [ + 1679761129.944, + "8.994087934494019" + ], + [ + 1679761189.944, + "8.994087934494019" + ], + [ + 1679761249.944, + "11.062512159347534" + ], + [ + 1679761309.944, + "11.062512159347534" + ], + [ + 1679761369.944, + "11.062512159347534" + ], + [ + 1679761429.944, + "11.062512159347534" + ], + [ + 1679761489.944, + "11.062512159347534" + ], + [ + 1679761549.944, + "11.062512159347534" + ], + [ + 1679761609.944, + "8.128139019012451" + ], + [ + 1679761669.944, + "8.128139019012451" + ], + [ + 1679761729.944, + "8.128139019012451" + ], + [ + 1679761789.944, + "8.128139019012451" + ], + [ + 1679761849.944, + "8.128139019012451" + ], + [ + 1679761909.944, + "8.128139019012451" + ] + ] + } + ] + } +} +``` +