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