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