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