summaryrefslogtreecommitdiffstats
path: root/lib/panel/df-uptime
diff options
context:
space:
mode:
Diffstat (limited to 'lib/panel/df-uptime')
-rwxr-xr-xlib/panel/df-uptime133
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/panel/df-uptime b/lib/panel/df-uptime
new file mode 100755
index 0000000..0597cc5
--- /dev/null
+++ b/lib/panel/df-uptime
@@ -0,0 +1,133 @@
+#!/bin/sh
+#
+# df-uptime -- Panel applet to monitor file system free space and system uptime
+#
+# Copyright (C) 2018 Patrick "P. J." McDermott
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+set -eu
+
+indent=''
+le="$(printf '\n.')"; le="${le%.}"
+
+err()
+{
+ local fmt="${1}"
+ shift 1
+
+ printf "Error: ${fmt}\n" "${@}" 1>&2
+ return 0
+}
+
+gen_indent()
+{
+ local l=${1}
+ shift 1
+ local i=
+
+ i=0
+ while [ ${i} -lt ${l} ]; do
+ indent="${indent} "
+ i=$((${i} + 1))
+ done
+
+ return 0
+}
+
+do_df()
+{
+ local sed_bre=
+
+ sed_bre='^..* *..* *..* *..* *\([0-9][0-9]*%\) \(..*\)$'
+ df -x devtmpfs -x tmpfs | \
+ sed "1d; s/${sed_bre}/${indent}\\1 \\2/;" | tr '\n' "${le}"
+
+ return 0
+}
+
+do_uptime()
+{
+ local up=
+ local idle=
+ local min=
+ local hr=
+ local day=
+
+ read -r up idle 0</proc/uptime
+
+ up="${up%.*}"
+ min=$((${up} / 60))
+ hr=$((${min} / 60))
+ day=$((${hr} / 24))
+
+ min=$((${min} % 60))
+ hr=$((${hr} % 24))
+
+ printf '%sUp %d d %2d:%02d%c' "${indent}" ${day} ${hr} ${min} "${le}"
+
+ return 0
+}
+
+usage()
+{
+ printf 'Usage: %s [<options>]\n' "${0}"
+ printf '\n'
+ printf 'Options:\n'
+ printf ' -i <num> Indent lines with <num> spaces\n'
+ printf ' -r Print carriage returns instead of line feeds\n'
+
+ return 0
+}
+
+main()
+{
+ local opt=
+
+ while getopts 'i:r' opt "${@}"; do
+ case "${opt}" in
+ 'i')
+ case "${OPTARG}" in *[!0-9]* | '')
+ usage 1>&2
+ return 1
+ esac
+ gen_indent ${OPTARG}
+ ;;
+ 'r') le="$(printf '\r.')"; le="${le%.}";;
+ '?')
+ usage 1>&2
+ return 1
+ ;;
+ esac
+ done
+ shift $((${OPTIND} - 1))
+
+ if [ ${#} -ne 0 ]; then
+ usage 1>&2
+ return 1
+ fi
+
+ do_df
+ do_uptime
+
+ return 0
+}
+
+main "${@}"