Skip to content

Instantly share code, notes, and snippets.

@themegabyte
Last active April 25, 2020 17:00
Show Gist options
  • Save themegabyte/92a4ced4a98cd369a1a6b5a006d9a35f to your computer and use it in GitHub Desktop.
Save themegabyte/92a4ced4a98cd369a1a6b5a006d9a35f to your computer and use it in GitHub Desktop.
#!/bin/sh
RED='\033[1;31m'
BLUE='\033[1;34m'
GREEN='\033[1;32m'
NC='\033[0m'
YELLOW='\033[1;33m'
# make sure $VAPIKEY has been exported either through your bash prompt
# or in your .bashrc file inside your home directory. Like so:
# export VAPIKEY="YOUR API KEY HERE"
# To check do an echo $VAPIKEY
# and see if it prints on the command line
#make the required API calls and save the JSON into vars so we can reuse data without calling the API
j_sv_list="$(curl -sH "API-Key: $VAPIKEY" https://api.vultr.com/v1/server/list)"
j_a_info="$(curl -sH "API-Key: $VAPIKEY" https://api.vultr.com/v1/account/info)"
#j_sv_list=$(cat j_sv_list)
#j_a_info=$(cat j_a_info)
cost_per_month="$(echo $j_sv_list | jq -r '.[].cost_per_month')"
balance="$(echo $j_a_info | jq -r '.balance')"
pending_charges="$(echo $j_a_info | jq -r '.pending_charges')"
credit_remain="$(awk -v bal="$balance" -v pcharge="$pending_charges" 'BEGIN {print bal+pcharge; exit}' | sed 's/-//g' )"
hours_left="$(awk -v cr="$credit_remain" -v cpm="$cost_per_month" 'BEGIN {runtime=(cr*672)/cpm; print runtime; exit}')"
if [ ${hours_left%%.*} -gt 24 ]
then
awk -v hl="$hours_left" 'BEGIN {print "\033[1;33mYou have", int (hl/24), "days and", int(((hl/24)-(int(hl/24)))*24), "hours left \033[0m"; exit}'
else
printf "\033[1;31mYou have %.f hours left\n\033[0m" $hours_left
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment