Commit | Line | Data |
---|---|---|
9492242e | 1 | #! /bin/bash |
e0dbdb36 | 2 | |
5671a1ee SK |
3 | ## open : string -> unit |
4 | ## | |
5 | ## Fork xdg-open so we don't block current terminal session when opening | |
6 | ## things like pdf files. For example: | |
7 | ## | |
8 | ## open book.pdf | |
9 | ## | |
10 | open() { | |
11 | (xdg-open "$1" &) & | |
12 | } | |
13 | ||
58223ee2 SK |
14 | ## notify_done : unit -> unit |
15 | notify_done() { | |
16 | local -r _status_code="$?" | |
17 | local -r _program="$1" | |
18 | local _timestamp | |
19 | _timestamp="$(timestamp)" | |
20 | local -r _msg="$_timestamp [$_program] done " | |
21 | if [[ "$_status_code" -eq 0 ]] | |
22 | then | |
23 | notify-send -u normal "$_msg OK: $_status_code" | |
24 | else | |
25 | notify-send -u critical "$_msg ERROR: $_status_code" | |
26 | fi | |
27 | } | |
28 | ||
658fd8a9 SK |
29 | _dl_script() { |
30 | cat << EOF | |
31 | #! /bin/bash | |
32 | wget -c \$(< ./url) | |
33 | EOF | |
34 | } | |
35 | ||
7af085ce | 36 | dl() { |
658fd8a9 SK |
37 | local -r name="$1" |
38 | local -r url="$2" | |
39 | ||
7af085ce | 40 | local -r timestamp="$(date --iso-8601=ns)" |
658fd8a9 | 41 | local -r dir="$HOME"/dl/adhoc/"$timestamp"--"$name" |
7af085ce | 42 | local -r url_file_path="${dir}/url" |
658fd8a9 | 43 | local -r dl_file_path="${dir}/dl" |
7af085ce SK |
44 | |
45 | mkdir -p "$dir" | |
46 | touch "$url_file_path" | |
658fd8a9 SK |
47 | if [ "$url" != '' ] |
48 | then | |
49 | echo "$url" > "$url_file_path" | |
50 | fi | |
51 | _dl_script > "$dl_file_path" | |
52 | chmod +x "$dl_file_path" | |
7af085ce SK |
53 | cd "$dir" |
54 | } | |
55 | ||
e899b7a4 SK |
56 | ## web search |
57 | ## ws : string -> unit | |
8c2bec63 | 58 | ws() { |
56b5d8cf SK |
59 | local line search_string0 search_string |
60 | ||
61 | search_string0="$*" | |
62 | case "$search_string0" in | |
63 | '') | |
64 | while read -r line; do | |
65 | search_string="${search_string} ${line}" | |
66 | done;; | |
67 | *) | |
68 | search_string="$search_string0";; | |
69 | esac | |
70 | ||
71 | firefox --search "$search_string" | |
8c2bec63 SK |
72 | } |
73 | ||
e899b7a4 SK |
74 | |
75 | ## dictionary | |
76 | ## d : string -> string list | |
e0dbdb36 SK |
77 | d() { |
78 | local -r word=$(fzf < /usr/share/dict/words) | |
79 | dict "$word" | |
80 | } | |
81 | ||
e899b7a4 | 82 | ## shell_activity_report : (mon | dow) -> string list |
94df2def | 83 | shell_activity_report() { |
ec81fc0a | 84 | # TODO: optional concrete number output |
ec81fc0a | 85 | # TODO: optional combinations of granularities: hour, weekday, month, year |
d72bfd8f SK |
86 | local group_by="$1" |
87 | case "$group_by" in | |
88 | 'mon') ;; | |
89 | 'dow') ;; | |
90 | '') group_by='dow';; | |
91 | *) | |
957b0d4f | 92 | echo "Usage: $0 [mon|dow]" >&2 |
4b5a2977 | 93 | kill -INT $$ |
d72bfd8f | 94 | esac |
94df2def | 95 | history \ |
d72bfd8f SK |
96 | | awk -v group_by="$group_by" ' |
97 | function date2dow(y, m, d, _t, _i) { | |
98 | # Contract: | |
99 | # y > 1752, 1 <= m <= 12. | |
100 | # Source: | |
101 | # Sakamoto`s methods | |
102 | # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods | |
103 | _t[ 0] = 0 | |
104 | _t[ 1] = 3 | |
105 | _t[ 2] = 2 | |
106 | _t[ 3] = 5 | |
107 | _t[ 4] = 0 | |
108 | _t[ 5] = 3 | |
109 | _t[ 6] = 5 | |
110 | _t[ 7] = 1 | |
111 | _t[ 8] = 4 | |
112 | _t[ 9] = 6 | |
113 | _t[10] = 2 | |
114 | _t[11] = 4 | |
115 | y -= m < 3 | |
116 | _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7 | |
117 | _i = _i == 0 ? 7 : _i # Make Sunday last | |
118 | return _i | |
119 | ||
120 | } | |
121 | ||
94df2def | 122 | { |
ec81fc0a SK |
123 | # NOTE: $2 & $3 are specific to oh-my-zsh history output |
124 | date = $2 | |
94df2def | 125 | time = $3 |
ec81fc0a SK |
126 | d_fields = split(date, d, "-") |
127 | t_fields = split(time, t, ":") | |
128 | if (t_fields && d_fields) { | |
129 | # +0 to coerce number from string | |
d72bfd8f | 130 | year = d[1] + 0 |
ec81fc0a | 131 | month = d[2] + 0 |
d72bfd8f | 132 | day = d[3] + 0 |
ec81fc0a | 133 | hour = t[1] + 0 |
d72bfd8f SK |
134 | dow = date2dow(year, month, day) |
135 | g = group_by == "mon" ? month : dow # dow is default | |
136 | c = count[g, hour]++ | |
94df2def | 137 | } |
ec81fc0a SK |
138 | if (c > max) |
139 | max = c | |
94df2def SK |
140 | } |
141 | ||
ec81fc0a | 142 | END { |
d72bfd8f SK |
143 | w[1] = "Monday" |
144 | w[2] = "Tuesday" | |
145 | w[3] = "Wednesday" | |
146 | w[4] = "Thursday" | |
147 | w[5] = "Friday" | |
148 | w[6] = "Saturday" | |
149 | w[7] = "Sunday" | |
150 | ||
ec81fc0a SK |
151 | m[ 1] = "January" |
152 | m[ 2] = "February" | |
153 | m[ 3] = "March" | |
154 | m[ 4] = "April" | |
155 | m[ 5] = "May" | |
156 | m[ 6] = "June" | |
157 | m[ 7] = "July" | |
158 | m[ 8] = "August" | |
159 | m[ 9] = "September" | |
160 | m[10] = "October" | |
161 | m[11] = "November" | |
162 | m[12] = "December" | |
d72bfd8f SK |
163 | |
164 | n = group_by == "mon" ? 12 : 7 # dow is default | |
165 | ||
166 | for (gid = 1; gid <= n; gid++) { | |
167 | group = group_by == "mon" ? m[gid] : w[gid] | |
168 | printf "%s\n", group; | |
94df2def | 169 | for (hour=0; hour<24; hour++) { |
d72bfd8f | 170 | c = count[gid, hour] |
ec81fc0a SK |
171 | printf " %2d ", hour |
172 | for (i = 1; i <= (c * 100) / max; i++) | |
94df2def SK |
173 | printf "|" |
174 | printf "\n" | |
175 | } | |
ec81fc0a SK |
176 | } |
177 | }' | |
94df2def SK |
178 | } |
179 | ||
e899b7a4 | 180 | ## top_commands : unit -> (command:string * count:number * bar:string) list |
d265cd11 SK |
181 | top_commands() { |
182 | history \ | |
183 | | awk ' | |
184 | { | |
185 | count[$4]++ | |
186 | } | |
187 | ||
188 | END { | |
189 | for (cmd in count) | |
190 | print count[cmd], cmd | |
191 | }' \ | |
192 | | sort -n -r -k 1 \ | |
193 | | head -50 \ | |
194 | | awk ' | |
195 | { | |
196 | cmd[NR] = $2 | |
197 | c = count[NR] = $1 + 0 # + 0 to coerce number from string | |
198 | if (c > max) | |
199 | max = c | |
200 | } | |
201 | ||
202 | END { | |
203 | for (i = 1; i <= NR; i++) { | |
204 | c = count[i] | |
205 | printf "%s %d ", cmd[i], c | |
206 | scaled = (c * 100) / max | |
207 | for (j = 1; j <= scaled; j++) | |
208 | printf "|" | |
209 | printf "\n" | |
210 | } | |
211 | }' \ | |
212 | | column -t | |
213 | } | |
214 | ||
e899b7a4 | 215 | ## Top Disk-Using directories |
e899b7a4 | 216 | ## tdu : path-string -> (size:number * directory:path-string) list |
c7de24d9 | 217 | tdu() { |
e899b7a4 SK |
218 | local -r root_path="$1" |
219 | ||
216c3ebd SK |
220 | du -h "$root_path" \ |
221 | | sort -r -h -k 1 \ | |
502dff1e SK |
222 | | head -50 \ |
223 | | tac | |
224 | # A slight optimization: head can exit before traversing the full input. | |
c7de24d9 SK |
225 | } |
226 | ||
e899b7a4 SK |
227 | ## Top Disk-Using Files |
228 | ## tduf : path-string list -> (size:number * file:path-string) list | |
27456eb6 | 229 | tduf() { |
e899b7a4 | 230 | find "$@" -type f -printf '%s\t%p\0' \ |
858aa230 SK |
231 | | sort -z -n -k 1 \ |
232 | | tail -z -n 50 \ | |
216c3ebd SK |
233 | | numfmt -z --to=iec \ |
234 | | tr '\0' '\n' | |
27456eb6 SK |
235 | } |
236 | ||
909ece30 | 237 | # Most-recently modified file system objects |
e899b7a4 | 238 | ## recent : ?(path-string list) -> path-string list |
909ece30 | 239 | recent() { |
68992a1d | 240 | # NOTES: |
68992a1d SK |
241 | # - %T+ is a GNU extension; |
242 | # - gawk is able to split records on \0, while awk cannot. | |
e899b7a4 | 243 | find "$@" -printf '%T@ %T+ %p\0' \ |
68992a1d | 244 | | tee >(gawk -v RS='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \ |
909ece30 SK |
245 | | sort -z -k 1 -n -r \ |
246 | | head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \ | |
247 | | gawk -v RS='\0' ' | |
248 | { | |
249 | sub("^" $1 " +", "") # Remove epoch time | |
250 | sub("+", " ") # Blank-out the default separator | |
251 | sub("\\.[0-9]+", "") # Remove fractional seconds | |
252 | ||
253 | }' | |
909ece30 SK |
254 | } |
255 | ||
e899b7a4 | 256 | ## recent_dirs : ?(path-string list) -> path-string list |
909ece30 | 257 | recent_dirs() { |
e899b7a4 | 258 | recent "$@" -type d |
909ece30 SK |
259 | } |
260 | ||
e899b7a4 | 261 | ## recent_files : ?(path-string list) -> path-string list |
909ece30 | 262 | recent_files() { |
e899b7a4 | 263 | recent "$@" -type f |
909ece30 SK |
264 | } |
265 | ||
e899b7a4 | 266 | ## pa_def_sink : unit -> string |
c7de24d9 SK |
267 | pa_def_sink() { |
268 | pactl info | awk '/^Default Sink:/ {print $3}' | |
269 | } | |
270 | ||
e899b7a4 | 271 | ## void_pkgs : ?(string) -> json |
c7de24d9 SK |
272 | void_pkgs() { |
273 | curl "https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq '.data' | |
274 | } | |
275 | ||
e899b7a4 SK |
276 | ## Colorful man |
277 | ## man : string -> string | |
c7de24d9 | 278 | man() { |
0c296cad SK |
279 | # mb: begin blink |
280 | # md: begin bold | |
281 | # me: end bold, blink and underline | |
282 | # | |
283 | # so: begin standout (reverse video) | |
284 | # se: end standout | |
285 | # | |
286 | # us: begin underline | |
287 | # ue: end underline | |
288 | ||
d93397a6 | 289 | LESS_TERMCAP_md=$'\e[01;30m' \ |
c7de24d9 | 290 | LESS_TERMCAP_me=$'\e[0m' \ |
c7de24d9 | 291 | LESS_TERMCAP_so=$'\e[01;44;33m' \ |
fc2ae05b | 292 | LESS_TERMCAP_se=$'\e[0m' \ |
d93397a6 | 293 | LESS_TERMCAP_us=$'\e[01;33m' \ |
fc2ae05b | 294 | LESS_TERMCAP_ue=$'\e[0m' \ |
c7de24d9 SK |
295 | command man "$@" |
296 | } | |
64ec9f23 | 297 | |
e899b7a4 SK |
298 | ## new experiment |
299 | ## x : string list -> unit | |
1be8e74b SK |
300 | x() { |
301 | cd "$(~/bin/x $@)" || kill -INT $$ | |
64ec9f23 | 302 | } |
801dd7bd | 303 | |
e899b7a4 SK |
304 | ## ocaml repl |
305 | ## hump : unit -> unit | |
801dd7bd | 306 | hump() { |
2e8cf226 | 307 | ledit -l "$(stty size | awk '{print $2}')" ocaml $@ |
801dd7bd | 308 | } |
632b7c4a | 309 | |
e899b7a4 SK |
310 | ## search howtos |
311 | ## howto : unit -> string | |
632b7c4a | 312 | howto() { |
a42aa7f8 | 313 | cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)" |
632b7c4a | 314 | } |
f4e0bb58 | 315 | |
86b74662 SK |
316 | _yt() { |
317 | local -r base_dir="$1" | |
e5eff223 SK |
318 | local -r uri="$2" |
319 | local -r opts="$3" | |
86b74662 | 320 | |
71c4bf5f SK |
321 | local -r yt=youtube-dl |
322 | local -r id=$("$yt" --get-id "$uri") | |
323 | local -r title=$("$yt" --get-title "$uri" | sed 's/[^А-Яа-яA-Za-z0-9._-]/_/g') | |
86b74662 SK |
324 | local -r dir="${base_dir}/${title}--${id}" |
325 | ||
326 | mkdir -p "$dir" | |
327 | cd "$dir" || kill -INT $$ | |
328 | echo "$uri" > 'uri' | |
71c4bf5f | 329 | "$yt" $opts -c --write-all-thumbnails --write-description --write-info-json "$uri" |
86b74662 SK |
330 | } |
331 | ||
332 | yt_audio() { | |
333 | local -r uri="$1" | |
e5eff223 | 334 | _yt "${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140' |
86b74662 SK |
335 | } |
336 | ||
337 | yt_video() { | |
338 | local -r uri="$1" | |
339 | _yt "${DIR_YOUTUBE_VIDEO}/individual" "$uri" | |
60e43329 SK |
340 | } |
341 | ||
f4e0bb58 | 342 | gh_fetch_repos() { |
84a0359e SK |
343 | local -r user_type="$1" |
344 | local -r user_name="$2" | |
345 | ||
346 | curl "https://api.github.com/$user_type/$user_name/repos?page=1&per_page=10000" | |
f4e0bb58 SK |
347 | } |
348 | ||
349 | gh_clone() { | |
1f80896b SK |
350 | local -r gh_user_type="$1" |
351 | local -r gh_user_name="$2" | |
352 | ||
353 | local -r gh_dir="${DIR_GITHUB}/${gh_user_name}" | |
8aa18398 | 354 | mkdir -p "$gh_dir" |
4b5a2977 | 355 | cd "$gh_dir" || kill -INT $$ |
8aa18398 | 356 | gh_fetch_repos "$gh_user_type" "$gh_user_name" \ |
08cb8095 | 357 | | jq --raw-output '.[] | select(.fork | not) | .clone_url' \ |
f4e0bb58 SK |
358 | | parallel -j 25 \ |
359 | git clone {} | |
360 | } | |
361 | ||
362 | gh_clone_user() { | |
363 | gh_clone 'users' "$1" | |
364 | } | |
365 | ||
366 | gh_clone_org() { | |
367 | gh_clone 'orgs' "$1" | |
368 | } | |
e09a8d5a | 369 | |
610785ef SK |
370 | gh_clone_repo() { |
371 | gh_username=$(echo "$1" | awk -F / '"$1 == "https" && $3 == github.com" {print $4}') | |
8aa18398 | 372 | gh_dir="${DIR_GITHUB}/${gh_username}" |
610785ef | 373 | mkdir -p "$gh_dir" |
4b5a2977 | 374 | cd "$gh_dir" || kill -INT $$ |
610785ef | 375 | git clone "$1" |
610785ef SK |
376 | } |
377 | ||
52045fbd SK |
378 | bar() { |
379 | local -r len="${1:-79}" # 1st arg or 79. | |
380 | local -r char="${2:--}" # 2nd arg or a dash. | |
381 | for _ in {1.."$len"}; do | |
382 | printf '%c' "$char"; | |
383 | done | |
384 | } | |
385 | ||
917869f8 SK |
386 | daily_todo_file_template() { |
387 | cat << EOF | |
388 | =============================================================================== | |
389 | $(date '+%F %A') | |
390 | =============================================================================== | |
391 | ||
ec83da20 | 392 | ------------------------------------------------------------------------------- |
89c7c21f | 393 | TODAY |
ec83da20 SK |
394 | ------------------------------------------------------------------------------- |
395 | ||
396 | ||
397 | ------------------------------------------------------------------------------- | |
89c7c21f | 398 | CURRENT |
ec83da20 | 399 | ------------------------------------------------------------------------------- |
917869f8 | 400 | |
ec83da20 SK |
401 | |
402 | ------------------------------------------------------------------------------- | |
403 | BLOCKED | |
404 | ------------------------------------------------------------------------------- | |
405 | ||
406 | ||
407 | ------------------------------------------------------------------------------- | |
408 | BACKLOG | |
917869f8 SK |
409 | ------------------------------------------------------------------------------- |
410 | EOF | |
411 | } | |
412 | ||
3251b589 | 413 | today() { |
e2b9f534 | 414 | local date |
917869f8 | 415 | date="$(date +%F)" |
e2b9f534 SK |
416 | local -r dir="$DIR_TODO/daily" |
417 | local -r file="$dir/$date.txt" | |
418 | ||
419 | mkdir -p "$dir" | |
420 | if [ ! -f "$file" ] | |
421 | then | |
917869f8 | 422 | daily_todo_file_template > "$file" |
e2b9f534 | 423 | fi |
a8c4d370 | 424 | cd "$DIR_TODO" && "$EDITOR" $EDITOR_ARGS "$file" |
e2b9f534 SK |
425 | } |
426 | ||
3251b589 SK |
427 | todo() { |
428 | cd "$DIR_TODO" && "$EDITOR" TODO | |
429 | } | |
430 | ||
c45bdb58 SK |
431 | work_log_template() { |
432 | cat << EOF | |
d8da04c5 | 433 | $(date '+%F %A') |
c45bdb58 SK |
434 | ========== |
435 | ||
436 | Morning report | |
437 | -------------- | |
438 | ||
a744f575 | 439 | ### Prev |
c45bdb58 | 440 | |
a744f575 SK |
441 | ### Curr |
442 | ||
443 | ### Next | |
c45bdb58 SK |
444 | |
445 | ### Blockers | |
446 | ||
447 | Day's notes | |
448 | ----------- | |
449 | EOF | |
450 | } | |
451 | ||
452 | work_log() { | |
453 | mkdir -p "$DIR_WORK_LOG" | |
773a0061 | 454 | local -r file_work_log_today="${DIR_WORK_LOG}/daily-$(date +%F).md" |
c45bdb58 SK |
455 | if [ ! -f "$file_work_log_today" ] |
456 | then | |
457 | work_log_template > "$file_work_log_today" | |
458 | fi | |
3008cd51 | 459 | vim -c 'set spell' "$file_work_log_today" |
c45bdb58 SK |
460 | |
461 | } | |
462 | ||
065977fd SK |
463 | note() { |
464 | mkdir -p "$DIR_NOTES" | |
3008cd51 | 465 | vim -c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md" |
065977fd SK |
466 | } |
467 | ||
d0a246c9 SK |
468 | _bt_devs_infos() { |
469 | # grep's defintion of a line does not include \r, wile awk's does and | |
470 | # which bluetoothctl outputs | |
471 | awk '/^Device +/ {print $2}' \ | |
472 | | xargs -I% sh -c 'echo info % | bluetoothctl' \ | |
311f355a | 473 | | awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /' |
d0a246c9 SK |
474 | } |
475 | ||
2c0865d1 | 476 | bt_devs_paired() { |
d0a246c9 | 477 | echo 'paired-devices' | bluetoothctl | _bt_devs_infos |
2c0865d1 SK |
478 | } |
479 | ||
480 | bt_devs() { | |
d0a246c9 | 481 | echo 'devices' | bluetoothctl | _bt_devs_infos |
2c0865d1 | 482 | } |
dfbaafa4 SK |
483 | |
484 | run() { | |
1f80896b SK |
485 | local -r stderr="$(mktemp)" |
486 | ||
487 | local code urgency | |
488 | ||
dfbaafa4 SK |
489 | $@ 2> >(tee "$stderr") |
490 | code="$?" | |
dfbaafa4 SK |
491 | case "$code" in |
492 | 0) urgency='normal';; | |
493 | *) urgency='critical' | |
494 | esac | |
495 | notify-send -u "$urgency" "Job done: $code" "$(cat $stderr)" | |
496 | rm "$stderr" | |
497 | } | |
3f673776 SK |
498 | |
499 | bar_gauge() { | |
948953b2 SK |
500 | awk "$@" ' |
501 | BEGIN { | |
325a679e | 502 | # CLI options |
f865d284 | 503 | width = width ? width : 80 |
948953b2 SK |
504 | ch_left = ch_left ? ch_left : "[" |
505 | ch_right = ch_right ? ch_right : "]" | |
506 | ch_blank = ch_blank ? ch_blank : "-" | |
507 | ch_used = ch_used ? ch_used : "|" | |
325a679e SK |
508 | num = num ? 1 : 0 |
509 | pct = pct ? 1 : 0 | |
948953b2 | 510 | } |
3f673776 | 511 | |
3f673776 | 512 | { |
e89edca7 SK |
513 | cur = $1 |
514 | max = $2 | |
515 | lab = $3 | |
3f673776 | 516 | |
948953b2 | 517 | cur_scaled = num_scale(cur, max, 1, width) |
3f673776 | 518 | |
948953b2 | 519 | printf \ |
48c23173 | 520 | "%s%s%s%s", \ |
948953b2 SK |
521 | lab ? lab " " : "", \ |
522 | num ? cur "/" max " " : "", \ | |
48c23173 | 523 | pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \ |
948953b2 | 524 | ch_left |
3f673776 | 525 | for (i=1; i<=width; i++) { |
948953b2 | 526 | c = i <= cur_scaled ? ch_used : ch_blank |
bcc030dc | 527 | printf "%s", c |
3f673776 | 528 | } |
948953b2 | 529 | printf "%s\n", ch_right |
3f673776 SK |
530 | } |
531 | ||
532 | function num_scale(src_cur, src_max, dst_min, dst_max) { | |
533 | return dst_min + ((src_cur * (dst_max - dst_min)) / src_max) | |
534 | } | |
535 | ' | |
536 | } | |
537 | ||
9699e21d SK |
538 | flat_top_5() { |
539 | sort -n -k 1 -r \ | |
540 | | head -5 \ | |
541 | | awk ' | |
542 | { | |
543 | cur = $1 | |
544 | max = $2 | |
545 | name = $3 | |
546 | pct = cur / max * 100 | |
2cb32f21 SK |
547 | printf "%s%s %.2f%%", sep, name, pct |
548 | sep = ", " | |
9699e21d SK |
549 | } |
550 | ||
551 | END {printf "\n"} | |
2cb32f21 | 552 | ' |
9699e21d SK |
553 | } |
554 | ||
ab62e6ac SK |
555 | internet_addr() { |
556 | curl --silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1 | |
557 | } | |
558 | ||
3914b6b1 | 559 | status_batt() { |
3f673776 SK |
560 | case "$(uname)" in |
561 | 'Linux') | |
db8ff593 SK |
562 | if which upower > /dev/null |
563 | then | |
564 | upower --dump \ | |
565 | | awk ' | |
566 | /^Device:[ \t]+/ { | |
567 | device["path"] = $2 | |
568 | next | |
569 | } | |
570 | ||
571 | / battery/ && device["path"] { | |
572 | device["is_battery"] = 1 | |
573 | next | |
574 | } | |
575 | ||
576 | / percentage:/ && device["is_battery"] { | |
577 | device["battery_percentage"] = $2 | |
578 | sub("%$", "", device["battery_percentage"]) | |
579 | next | |
580 | } | |
581 | ||
582 | /^$/ { | |
583 | if (device["is_battery"] && device["path"] == "/org/freedesktop/UPower/devices/DisplayDevice") | |
584 | print device["battery_percentage"], 100, "batt" | |
585 | delete device | |
586 | } | |
587 | ' | |
588 | fi | |
3f673776 SK |
589 | ;; |
590 | esac | |
54031225 SK |
591 | } |
592 | ||
593 | indent() { | |
594 | awk -v unit="$1" '{printf "%s%s\n", unit, $0}' | |
595 | } | |
596 | ||
3914b6b1 | 597 | status() { |
54031225 SK |
598 | local -r indent_unit=' ' |
599 | ||
600 | uname -srvmo | |
601 | hostname | figlet | |
602 | uptime | |
3f673776 | 603 | |
291e586a SK |
604 | echo |
605 | ||
2cb32f21 SK |
606 | echo 'accounting' |
607 | ||
cd113e38 | 608 | # TODO Bring back seesion and client listing, but per server/socket. |
549b689c | 609 | printf '%stmux\n' "$indent_unit" |
921bf326 SK |
610 | ps -eo comm,cmd \ |
611 | | awk ' | |
612 | # Expecting lines like: | |
613 | # "tmux: server tmux -L pistactl new-session -d -s pistactl" | |
614 | # "tmux: client tmux -L foo" | |
615 | # "tmux: client tmux -Lbar" | |
8fff953b SK |
616 | # "tmux: client tmux" |
617 | # "tmux: server tmux -L foo -S bar" <-- -S takes precedence | |
921bf326 SK |
618 | /^tmux:/ { |
619 | # XXX This of course assumes pervasive usage of -L | |
620 | # TODO Handle -S | |
621 | role=$2 | |
8fff953b SK |
622 | |
623 | split($0, sides_of_S, "-S") | |
624 | split(sides_of_S[2], words_right_of_S, FS) | |
625 | ||
921bf326 SK |
626 | split($0, sides_of_L, "-L") |
627 | split(sides_of_L[2], words_right_of_L, FS) | |
8fff953b SK |
628 | |
629 | if (words_right_of_S[1]) { | |
630 | sock = "path." words_right_of_S[1] | |
631 | } else if (words_right_of_L[1]) { | |
632 | sock = "name." words_right_of_L[1] | |
921bf326 | 633 | } else { |
8fff953b | 634 | sock = "default" |
921bf326 | 635 | } |
8fff953b | 636 | |
921bf326 SK |
637 | roles[role]++ |
638 | socks[sock]++ | |
639 | count[role, sock]++ | |
640 | } | |
641 | ||
642 | END { | |
921bf326 | 643 | for (sock in socks) { |
549b689c SK |
644 | clients = count["client", sock] |
645 | printf "%s ", sock | |
646 | if (clients) { | |
647 | printf "<-> %d", clients | |
921bf326 | 648 | } |
549b689c | 649 | printf "\n" |
921bf326 SK |
650 | } |
651 | printf "\n" | |
652 | }' \ | |
653 | | sort \ | |
654 | | column -t \ | |
655 | | indent "${indent_unit}${indent_unit}" | |
54031225 SK |
656 | |
657 | echo | |
658 | ||
2cb32f21 SK |
659 | printf '%sprocs by user\n' "${indent_unit}" |
660 | ps -eo user \ | |
661 | | awk ' | |
662 | NR > 1 { | |
663 | count_by_user[$1]++ | |
664 | total++ | |
665 | } | |
666 | ||
667 | END { | |
668 | for (user in count_by_user) | |
669 | print count_by_user[user], total, user | |
670 | } | |
671 | ' \ | |
672 | | flat_top_5 \ | |
673 | | indent "${indent_unit}${indent_unit}" | |
674 | ||
675 | echo | |
676 | ||
677 | echo 'resources' | |
54031225 | 678 | ( |
e89edca7 SK |
679 | free | awk '$1 == "Mem:" {print $3, $2, "mem"}' |
680 | df ~ | awk 'NR == 2 {print $3, $3 + $4, "disk"}' | |
3914b6b1 | 681 | status_batt |
54031225 | 682 | ) \ |
f865d284 | 683 | | bar_gauge -v width=60 -v pct=1 \ |
54031225 SK |
684 | | column -t \ |
685 | | indent "$indent_unit" | |
686 | ||
687 | echo | |
688 | ||
2cb32f21 | 689 | printf '%smem by proc\n' "$indent_unit" |
fbcc6a55 | 690 | ps -eo rss,comm \ |
c5f3db43 | 691 | | awk -v total="$(free | awk '$1 == "Mem:" {print $2; exit}')" ' |
2cb32f21 SK |
692 | NR > 1 { |
693 | rss = $1 | |
fbcc6a55 | 694 | proc = $2 |
02f10c8b | 695 | by_proc[proc] += rss |
2cb32f21 SK |
696 | } |
697 | ||
698 | END { | |
699 | for (proc in by_proc) | |
700 | print by_proc[proc], total, proc | |
701 | } | |
702 | ' \ | |
703 | | flat_top_5 \ | |
704 | | indent "${indent_unit}${indent_unit}" | |
705 | ||
706 | echo | |
707 | ||
d87138ae SK |
708 | local _dir temp_input label_file label |
709 | ||
b7ea4f20 | 710 | printf '%sthermal\n' "$indent_unit" |
d87138ae SK |
711 | for _dir in /sys/class/hwmon/hwmon*; do |
712 | cat "$_dir"/name | |
713 | find "$_dir"/ -name 'temp*_input' \ | |
714 | | while read -r temp_input; do | |
715 | label_file=${temp_input//_input/_label} | |
716 | if [ -f "$label_file" ]; then | |
717 | label=$(< "$label_file") | |
718 | else | |
719 | label='' | |
720 | fi | |
721 | awk -v label="$label" '{ | |
722 | if (label) | |
723 | label = sprintf(" (%s)", label) | |
724 | printf("%.2f°C%s\n", $1 / 1000, label) | |
725 | }' \ | |
726 | "$temp_input" | |
727 | done \ | |
728 | | sort \ | |
729 | | indent "$indent_unit" | |
b7ea4f20 | 730 | done \ |
b7ea4f20 SK |
731 | | indent "${indent_unit}${indent_unit}" |
732 | ||
733 | echo 'net' | |
713423d0 SK |
734 | #local -r internet_addr=$(internet_addr 0.5) |
735 | #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' ) | |
b8651fc6 | 736 | |
713423d0 SK |
737 | #echo "${indent_unit}internet" |
738 | #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr" | |
857e80ac | 739 | echo "${indent_unit}if" |
291e586a SK |
740 | (ifconfig; iwconfig) 2> /dev/null \ |
741 | | awk ' | |
742 | /^[^ ]/ { | |
743 | device = $1 | |
744 | sub(":$", "", device) | |
745 | if ($4 ~ "ESSID:") { | |
746 | _essid = $4 | |
747 | sub("^ESSID:\"", "", _essid) | |
748 | sub("\"$", "", _essid) | |
749 | essid[device] = _essid | |
750 | } | |
751 | next | |
752 | } | |
753 | ||
754 | /^ / && $1 == "inet" { | |
755 | address[device] = $2 | |
756 | next | |
757 | } | |
758 | ||
759 | /^ +Link Quality=[0-9]+\/[0-9]+ +Signal level=/ { | |
760 | split($2, lq_parts_eq, "=") | |
761 | split(lq_parts_eq[2], lq_parts_slash, "/") | |
762 | cur = lq_parts_slash[1] | |
763 | max = lq_parts_slash[2] | |
764 | link[device] = cur / max * 100 | |
765 | next | |
766 | } | |
767 | ||
768 | END { | |
769 | for (device in address) | |
770 | if (device != "lo") { | |
771 | l = link[device] | |
772 | e = essid[device] | |
164f3674 | 773 | l = l ? sprintf("%.0f%%", l) : "--" |
291e586a SK |
774 | e = e ? e : "--" |
775 | print device, address[device], e, l | |
776 | } | |
777 | } | |
778 | ' \ | |
54031225 SK |
779 | | column -t \ |
780 | | indent "${indent_unit}${indent_unit}" | |
291e586a | 781 | |
3f673776 | 782 | # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat |
857e80ac SK |
783 | |
784 | echo "${indent_unit}-->" | |
785 | ||
a76a676b SK |
786 | # TODO Populate pid->cmd dict from `ps -eo pid,comm` and lookup progs there |
787 | # since netstat -p output comes out truncated. | |
788 | ||
4eb99bdc SK |
789 | sudo -n netstat -tulnp \ |
790 | | awk -v indent="${indent_unit}${indent_unit}" ' | |
791 | NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) { | |
c28f8c74 SK |
792 | protocol = $1 |
793 | addr = $4 | |
794 | port = a[split(addr, a, ":")] | |
795 | name = p[split(proc, p, "/")] | |
796 | names[name] = 1 | |
797 | protocols[protocol] = 1 | |
798 | if (!seen[protocol, name, port]++) | |
799 | ports[protocol, name, ++seen[protocol, name]] = port | |
800 | } | |
801 | ||
802 | END { | |
803 | for (protocol in protocols) { | |
804 | printf "%s%s\t", indent, toupper(protocol) | |
805 | for (name in names) { | |
806 | if (n = seen[protocol, name]) { | |
807 | sep = "" | |
808 | printf "%s:", name | |
809 | for (i = 1; i <= n; i++) { | |
810 | printf "%s%d", sep, ports[protocol, name, i] | |
811 | sep = "," | |
812 | } | |
813 | printf " " | |
814 | } | |
815 | } | |
816 | printf "\n" | |
817 | } | |
818 | }' | |
857e80ac SK |
819 | |
820 | echo "${indent_unit}<->" | |
821 | ||
0f8aaa1b | 822 | printf '%sTCP\t' "${indent_unit}${indent_unit}" |
857e80ac SK |
823 | sudo -n netstat -tnp \ |
824 | | awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \ | |
e99563b8 | 825 | | awk '{sub("^[0-9]+/", ""); print}' \ |
857e80ac SK |
826 | | sort -u \ |
827 | | xargs \ | |
828 | | column -t | |
5dfe845a SK |
829 | |
830 | # TODO: iptables summary | |
3f673776 | 831 | } |
ae23e5e3 | 832 | |
b635bb83 | 833 | ssh_invalid_by_addr() { |
ae23e5e3 SK |
834 | awk ' |
835 | /: Invalid user/ && $5 ~ /^sshd/ { | |
ae23e5e3 SK |
836 | addr=$10 == "port" ? $9 : $10 |
837 | max++ | |
b635bb83 | 838 | by_addr[addr]++ |
ae23e5e3 SK |
839 | } |
840 | ||
841 | END { | |
b635bb83 SK |
842 | for (addr in by_addr) |
843 | if ((c = by_addr[addr]) > 1) | |
844 | printf "%d %d %s\n", c, max, addr | |
845 | } | |
846 | ' \ | |
847 | /var/log/auth.log \ | |
848 | /var/log/auth.log.1 \ | |
849 | | sort -n -k 1 \ | |
850 | | bar_gauge -v width="$(stty size | awk '{print $2}')" -v num=1 -v ch_right=' ' -v ch_left=' ' -v ch_blank=' ' \ | |
851 | | column -t | |
852 | } | |
853 | ||
854 | ssh_invalid_by_day() { | |
855 | awk ' | |
c28f8c74 SK |
856 | BEGIN { |
857 | m["Jan"] = "01" | |
858 | m["Feb"] = "02" | |
859 | m["Mar"] = "03" | |
860 | m["Apr"] = "04" | |
861 | m["May"] = "05" | |
862 | m["Jun"] = "06" | |
863 | m["Jul"] = "07" | |
864 | m["Aug"] = "08" | |
865 | m["Sep"] = "09" | |
866 | m["Oct"] = "10" | |
867 | m["Nov"] = "11" | |
868 | m["Dec"] = "12" | |
869 | } | |
870 | ||
871 | /: Invalid user/ && $5 ~ /^sshd/ { | |
872 | day = m[$1] "-" $2 | |
873 | max++ | |
874 | by_day[day]++ | |
875 | } | |
876 | ||
877 | END { | |
878 | for (day in by_day) | |
879 | if ((c = by_day[day]) > 1) | |
880 | printf "%d %d %s\n", c, max, day | |
881 | } | |
b635bb83 SK |
882 | ' \ |
883 | /var/log/auth.log \ | |
884 | /var/log/auth.log.1 \ | |
b2402792 | 885 | | sort -k 3 \ |
b635bb83 SK |
886 | | bar_gauge -v width="$(stty size | awk '{print $2}')" -v num=1 -v ch_right=' ' -v ch_left=' ' -v ch_blank=' ' \ |
887 | | column -t | |
888 | } | |
889 | ||
890 | ssh_invalid_by_user() { | |
891 | awk ' | |
892 | /: Invalid user/ && $5 ~ /^sshd/ { | |
893 | user=$8 | |
894 | max++ | |
895 | by_user[user]++ | |
896 | } | |
897 | ||
898 | END { | |
899 | for (user in by_user) | |
900 | if ((c = by_user[user]) > 1) | |
901 | printf "%d %d %s\n", c, max, user | |
ae23e5e3 SK |
902 | } |
903 | ' \ | |
904 | /var/log/auth.log \ | |
905 | /var/log/auth.log.1 \ | |
906 | | sort -n -k 1 \ | |
ea9d43c9 | 907 | | bar_gauge -v width="$(stty size | awk '{print $2}')" -v num=1 -v ch_right=' ' -v ch_left=' ' -v ch_blank=' ' \ |
ae23e5e3 SK |
908 | | column -t |
909 | } | |
fecd2781 SK |
910 | |
911 | loggers() { | |
912 | awk ' | |
913 | { | |
914 | split($5, prog, "[") | |
915 | sub(":$", "", prog[1]) # if there were no [], than : will is left behind | |
916 | print prog[1] | |
abbf7a2a | 917 | }' /var/log/syslog /var/log/syslog.1 \ |
fecd2781 SK |
918 | | awk ' |
919 | { | |
920 | n = split($1, path, "/") # prog may be in path form | |
921 | prog = path[n] | |
922 | total++ | |
923 | count[prog]++ | |
924 | } | |
925 | ||
926 | END { | |
927 | for (prog in count) | |
928 | print count[prog], total, prog | |
929 | }' \ | |
edc3863d | 930 | | sort -n -k 1 \ |
fecd2781 SK |
931 | | bar_gauge -v num=1 -v ch_right=' ' -v ch_left=' ' -v ch_blank=' ' \ |
932 | | column -t | |
933 | } |