Skip to content

Instantly share code, notes, and snippets.

@Izzette Izzette/calc_uptime.sh
Last active Mar 8, 2017

Embed
What would you like to do?
Calculate up time percentage using journalctl
#!/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
You can’t perform that action at this time.