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