Last active
March 8, 2017 06:28
-
-
Save Izzette/db43e6a3687584a6b1c35607495c08bf to your computer and use it in GitHub Desktop.
Calculate up time percentage using journalctl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function get_stime() { | |
head -n 2 <<< "$1" | tail -n 1 | cut -d ' ' -f 1-3 | date --date="$(cat /dev/fd/0)" +%s | |
} | |
function get_etime() { | |
tail -n 1 <<< "$1" | cut -d ' ' -f 1-3 | date --date="$(cat /dev/fd/0)" +%s | |
} | |
declare -i boot=0 | |
declare -i up_time=0 | |
declare -i last_etime first_stime total_time stime etime | |
declare juornal | |
while :; do | |
journal="$(journalctl --boot $boot 2> /dev/null)" || break | |
stime=$(get_stime "$journal") | |
etime=$(get_etime "$journal") | |
if [[ -z "$last_etime" ]]; then | |
last_etime=$etime | |
fi | |
: $[up_time += $(get_etime "$journal") - stime] | |
: $[boot -= 1] | |
done | |
first_stime=$stime | |
total_time=$((last_etime - first_stime)) | |
echo "$(bc -l <<< "100.0 * ($up_time / $total_time)" | sed 's/\(\.....\).*/\1/')%" | |
# vim: set ts=2 sw=2 et syn=sh: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment