sj: Improve `allspace` output
[sunny256-utils.git] / sj
bloba5a1f46d80f2b30bb3448b211a624ed6041b8b23
1 #!/usr/bin/env bash
3 #=======================================================================
4 # sj
5 # File ID: 29dba898-4962-11df-adc3-d5e071bed206
7 # Shortcut to check various stuff.
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
13 progname=sj
14 VERSION=0.9.1
16 ARGS="$(getopt -o "\
20 " -l "\
21 help,\
22 maxtemp:,\
23 quiet,\
24 space:,\
25 verbose,\
26 version,\
27 " -n "$progname" -- "$@")"
28 test "$?" = "0" || exit 1
29 eval set -- "$ARGS"
31 std_maxtemp=94
33 opt_help=0
34 opt_maxtemp=$std_maxtemp
35 opt_quiet=0
36 opt_space='0'
37 opt_verbose=0
38 while :; do
39 case "$1" in
40 -h|--help) opt_help=1; shift ;;
41 --maxtemp) opt_maxtemp=$2; shift 2 ;;
42 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
43 --space) opt_space=$2; shift 2 ;;
44 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
45 --version) echo $progname $VERSION; exit 0 ;;
46 --) shift; break ;;
47 *) echo $progname: Internal error >&2; exit 1 ;;
48 esac
49 done
50 opt_verbose=$(($opt_verbose - $opt_quiet))
52 if test "$opt_help" = "1"; then
53 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
54 cat <<END
56 Usage: $progname [options] [command]
58 Options:
60 -h, --help
61 Show this help.
62 --maxtemp NUM
63 Define maximum acceptable temperature for "$progname temp-warn".
64 Default value: $std_maxtemp
65 -q, --quiet
66 Be more quiet. Can be repeated to increase silence.
67 --space BYTES
68 When used with "dfull":
69 Estimate time until the free disk space reaches BYTES. This value
70 is parsed by numfmt(1) from GNU coreutils, check the man page for
71 allowed values. Examples: 10M = 10000000, 10Mi = 10485760.
72 -v, --verbose
73 Increase level of verbosity. Can be repeated.
74 --version
75 Print version information.
77 allspace
78 Display free space of all local disks every 2nd second until
79 interrupted.
80 date
81 Run a query against pool.ntp.org to see how accurate the system
82 clock is.
83 dfull
84 Display estimated time until the current disk is full based on the
85 disk usage since the script was started.
87 Display free space of all local disks, sorted by free space.
88 kern
89 Follow the kernel log and display new entries immediately when they
90 occur.
91 ping
92 Check that the network is up by issuing a ping command until
93 interrupted.
94 space
95 Display free space of the current disk every second until
96 interrupted.
97 temp
98 Display current temperature.
99 temp-warn, tw
100 Loop and check CPU temperature. If it gets too high, create an
101 xmessage(1) window and send a warning to stderr.
104 exit 0
107 space_val="$(numfmt --from=auto --round=nearest -- $opt_space)"
108 test -z "$space_val" && {
109 echo "$progname: Invalid value in --space argument" >&2
110 exit 1
112 test $opt_verbose -ge 1 &&
113 echo "$progname: Using --space $space_val" >&2
115 free_space() {
116 df -B1 "$1" -P | tail -1 | tr -s ' ' | cut -f 4 -d ' ' \
117 | hum | rmspcall | tr -d '\n'
120 free_space_bytes() {
121 df -B 1 "$1" -P | tail -1 | tr -s ' ' | cut -f 4 -d ' ' | tr -d '\n'
124 all_free_space() {
125 mount | grep ^/dev/ | cut -f 3 -d ' ' | sort | while read f; do
126 [ "$f" = "/boot" -o "$f" = "/boot/efi" ] && continue
127 echo -n " ║ "
128 if test "$f" = "/"; then
129 echo -n "/ ";
130 free_space /
131 else
132 echo "$f " | rev | cut -f 1 -d / | rev | tr -d '\n'
133 free_space "$f"
135 done
136 echo
139 [ -z "$1" ] && { echo $progname: No command specified >&2; exit 1; }
140 if test "$1" = "allspace"; then
141 unset prev
142 while :; do
143 curr="$(all_free_space)"
144 if test "$curr" != "$prev"; then
145 echo
146 echo -n "$(date +"%d %H:%M:%S")$curr$(tput el)"
148 prev="$curr"
149 sleep 1
150 done
151 elif test "$1" = "date"; then
152 ntpdate -q pool.ntp.org
153 elif test "$1" = "df"; then
154 df -h --si | grep ^Filesystem
155 df -h --si --total | grep -e /dev/ -e ^total | sort -h -k4
156 elif test "$1" = "dfull"; then
157 origsec=$(date +%s)
158 origtime="$(date -u +"%Y-%m-%d %H:%M:%S.%N")"
159 origdf=$(( $(free_space_bytes .) - $space_val ))
160 prevdf=$origdf
161 ml_goalint=14
162 ml_goaltime=9
163 ml_dfdiff=1
164 while :; do
165 currtime="$(date -u +"%Y-%m-%d %H:%M:%S.%N")"
166 currsec=$(date +%s)
167 currdf=$(( $(free_space_bytes .) - $space_val ))
168 goal_output="$(
169 goal "$origtime" "$origdf" 0 "$currdf" 2>/dev/null
171 dfdiff="$(( $currdf-$origdf ))"
172 goalint=$(echo $goal_output | awk '{print $1}' | sed 's/\..*//')
173 goaldate=$(echo $goal_output | awk '{print $2}')
174 goaltime=$(echo $goal_output | awk '{print $3}' | sed 's/\..*//')
175 cl_goalint=$(echo $goalint | wc -L)
176 cl_goaltime=$(echo $goaltime | wc -L)
177 cl_dfdiff=$(echo $dfdiff | hum | wc -L)
178 seconds=$(echo $currsec-$origsec | bc)
180 test $cl_goalint -gt $ml_goalint && ml_goalint=$cl_goalint
181 test $cl_goaltime -gt $ml_goaltime && ml_goaltime=$cl_goaltime
182 test $cl_dfdiff -gt $ml_dfdiff && ml_dfdiff=$cl_dfdiff
184 if test "$(echo "$currdf < $prevdf" | bc)" = "1"; then
185 t_diskfree="$(tput bold; tput setaf 1)"
186 t_diskfree_reset="$(tput sgr0)"
187 elif test "$(echo "$currdf > $prevdf" | bc)" = "1"; then
188 t_diskfree="$(tput bold; tput setaf 2)"
189 t_diskfree_reset="$(tput sgr0)"
190 else
191 t_diskfree=""
192 t_diskfree_reset=""
195 if test -n "$goal_output"; then
196 printf \
197 "\\n"\
198 "%-${ml_goalint}s "\
199 "%s "\
200 "%-${ml_goaltime}s "\
201 "diff: %s%-${ml_dfdiff}s%s "\
202 "free: %s%s%s"\
203 "%s "\
204 "%s"\
205 "$goalint" \
206 "$goaldate" \
207 "$goaltime" \
208 "$t_diskfree" \
209 "$(echo $dfdiff | hum)" \
210 "$t_diskfree_reset" \
211 "$t_diskfree" \
212 "$(echo $currdf | hum)" \
213 "$t_diskfree_reset" \
214 "$(tput el)" \
216 if test $seconds -gt 0; then
217 printf " %s B/s" $(
218 printf 'scale=0; %d/%u\n' $dfdiff $seconds |
219 bc | hum
223 else
224 printf "\\n$progname dfull: No changes yet, %s%s%s bytes free%s" \
225 "$t_diskfree" \
226 "$(echo $currdf | hum)" \
227 "$t_diskfree_reset" \
228 "$(tput el)"
230 prevdf=$currdf
231 sleep 2
232 done
233 elif test "$1" = "kern"; then
234 tail -Fq /var/log/kern.log /var/log/syslog /var/log/auth.log \
235 | grep -v " $(hostname) CRON"
236 elif test "$1" = "space"; then
237 unset prevlast
238 while :; do
239 lastspace=$(free_space .)
240 if test "$lastspace" != "$prevlast"; then
241 echo -n " $lastspace $(tput el)"
242 prevlast=$lastspace
244 sleep 1
245 done
246 elif test "$1" = "temp"; then
247 temperature_file=/sys/devices/virtual/thermal/thermal_zone0/temp
248 if test ! -e "$temperature_file"; then
249 echo $progname: $temperature_file: File not found >&2
250 echo $progname: Cannot read temperature >&2
251 exit 1
254 echo scale=1
255 echo -n $(cat "$temperature_file")
256 echo / 1000
257 ) | bc -l
258 elif test "$1" = "temp-warn" -o "$1" = "tw"; then
259 unset prevtemp
260 dispfile="/tmp/sj_tw.$(date +%s).$$.tmp"
261 if test -e "$dispfile"; then
262 echo $progname: $dispfile: Tempfile already exists, that\'s spooky >&2
263 exit 1
265 while :; do
266 currtemp="$($progname temp)"
267 if test -z "$currtemp"; then
268 echo -n "$progname: Unable to read temperature, " >&2
269 echo \"$progname temp\" returned nothing >&2
270 exit 1
272 if test "$currtemp" != "$prevtemp"; then
273 echo $(date -u +"%Y-%m-%dT%H:%M:%SZ") $currtemp >>~/log/temp.log
274 echo -n " $currtemp $(tput el)"
275 prevtemp=$currtemp
277 if test $(echo "$currtemp > $opt_maxtemp" | bc) = "1"; then
278 grep Blimey "$dispfile" 2>/dev/null | grep -q . && rm "$dispfile"
279 if test ! -e $dispfile; then
280 warning="Oi! The temperature is $currtemp!"
281 xmessage -button Blimey -print "$warning" >"$dispfile" &
283 echo
284 tput setab 1
285 tput bold
286 tput setaf 3
287 echo -n "$progname: $warning"
288 tput sgr0
289 echo -n " "
290 tput el
291 ) >&2
294 sleep 2
295 done
296 elif test "$1" = "ping"; then
297 test -d /n900/. && sudo=sudo || unset sudo
298 while :; do
299 $sudo ping 178.79.142.16
300 sleep 1
301 echo ============================================
302 done
303 else
304 echo $progname: $1: Unknown command >&2
305 exit 1