Accept a name for dl and include starter script file
[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_script() {
30 cat << EOF
31 #! /bin/bash
32 wget -c \$(< ./url)
33 EOF
34 }
35
36 dl() {
37 local -r name="$1"
38 local -r url="$2"
39
40 local -r timestamp="$(date --iso-8601=ns)"
41 local -r dir="$HOME"/dl/adhoc/"$timestamp"--"$name"
42 local -r url_file_path="${dir}/url"
43 local -r dl_file_path="${dir}/dl"
44
45 mkdir -p "$dir"
46 touch "$url_file_path"
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"
53 cd "$dir"
54 }
55
56 ## web search
57 ## ws : string -> unit
58 ws() {
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"
72 }
73
74
75 ## dictionary
76 ## d : string -> string list
77 d() {
78 local -r word=$(fzf < /usr/share/dict/words)
79 dict "$word"
80 }
81
82 ## shell_activity_report : (mon | dow) -> string list
83 shell_activity_report() {
84 # TODO: optional concrete number output
85 # TODO: optional combinations of granularities: hour, weekday, month, year
86 local group_by="$1"
87 case "$group_by" in
88 'mon') ;;
89 'dow') ;;
90 '') group_by='dow';;
91 *)
92 echo "Usage: $0 [mon|dow]" >&2
93 kill -INT $$
94 esac
95 history \
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
122 {
123 # NOTE: $2 & $3 are specific to oh-my-zsh history output
124 date = $2
125 time = $3
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
130 year = d[1] + 0
131 month = d[2] + 0
132 day = d[3] + 0
133 hour = t[1] + 0
134 dow = date2dow(year, month, day)
135 g = group_by == "mon" ? month : dow # dow is default
136 c = count[g, hour]++
137 }
138 if (c > max)
139 max = c
140 }
141
142 END {
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
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"
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;
169 for (hour=0; hour<24; hour++) {
170 c = count[gid, hour]
171 printf " %2d ", hour
172 for (i = 1; i <= (c * 100) / max; i++)
173 printf "|"
174 printf "\n"
175 }
176 }
177 }'
178 }
179
180 ## top_commands : unit -> (command:string * count:number * bar:string) list
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
215 ## Top Disk-Using directories
216 ## tdu : path-string -> (size:number * directory:path-string) list
217 tdu() {
218 local -r root_path="$1"
219
220 du -h "$root_path" \
221 | sort -r -h -k 1 \
222 | head -50 \
223 | tac
224 # A slight optimization: head can exit before traversing the full input.
225 }
226
227 ## Top Disk-Using Files
228 ## tduf : path-string list -> (size:number * file:path-string) list
229 tduf() {
230 find "$@" -type f -printf '%s\t%p\0' \
231 | sort -z -n -k 1 \
232 | tail -z -n 50 \
233 | numfmt -z --to=iec \
234 | tr '\0' '\n'
235 }
236
237 # Most-recently modified file system objects
238 ## recent : ?(path-string list) -> path-string list
239 recent() {
240 # NOTES:
241 # - %T+ is a GNU extension;
242 # - gawk is able to split records on \0, while awk cannot.
243 find "$@" -printf '%T@ %T+ %p\0' \
244 | tee >(gawk -v RS='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
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 print
253 }'
254 }
255
256 ## recent_dirs : ?(path-string list) -> path-string list
257 recent_dirs() {
258 recent "$@" -type d
259 }
260
261 ## recent_files : ?(path-string list) -> path-string list
262 recent_files() {
263 recent "$@" -type f
264 }
265
266 ## pa_def_sink : unit -> string
267 pa_def_sink() {
268 pactl info | awk '/^Default Sink:/ {print $3}'
269 }
270
271 ## void_pkgs : ?(string) -> json
272 void_pkgs() {
273 curl "https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq '.data'
274 }
275
276 ## Colorful man
277 ## man : string -> string
278 man() {
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
289 LESS_TERMCAP_md=$'\e[01;30m' \
290 LESS_TERMCAP_me=$'\e[0m' \
291 LESS_TERMCAP_so=$'\e[01;44;33m' \
292 LESS_TERMCAP_se=$'\e[0m' \
293 LESS_TERMCAP_us=$'\e[01;33m' \
294 LESS_TERMCAP_ue=$'\e[0m' \
295 command man "$@"
296 }
297
298 ## new experiment
299 ## x : string list -> unit
300 x() {
301 cd "$(~/bin/x $@)" || kill -INT $$
302 }
303
304 ## ocaml repl
305 ## hump : unit -> unit
306 hump() {
307 ledit -l "$(stty size | awk '{print $2}')" ocaml $@
308 }
309
310 ## search howtos
311 ## howto : unit -> string
312 howto() {
313 cat "$(find ~/arc/doc/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
314 }
315
316 _yt() {
317 local -r base_dir="$1"
318 local -r uri="$2"
319 local -r opts="$3"
320
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')
324 local -r dir="${base_dir}/${title}--${id}"
325
326 mkdir -p "$dir"
327 cd "$dir" || kill -INT $$
328 echo "$uri" > 'uri'
329 "$yt" $opts -c --write-all-thumbnails --write-description --write-info-json "$uri"
330 }
331
332 yt_audio() {
333 local -r uri="$1"
334 _yt "${DIR_YOUTUBE_AUDIO}/individual" "$uri" '-f 140'
335 }
336
337 yt_video() {
338 local -r uri="$1"
339 _yt "${DIR_YOUTUBE_VIDEO}/individual" "$uri"
340 }
341
342 gh_fetch_repos() {
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"
347 }
348
349 gh_clone() {
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}"
354 mkdir -p "$gh_dir"
355 cd "$gh_dir" || kill -INT $$
356 gh_fetch_repos "$gh_user_type" "$gh_user_name" \
357 | jq --raw-output '.[] | select(.fork | not) | .clone_url' \
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 }
369
370 gh_clone_repo() {
371 gh_username=$(echo "$1" | awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
372 gh_dir="${DIR_GITHUB}/${gh_username}"
373 mkdir -p "$gh_dir"
374 cd "$gh_dir" || kill -INT $$
375 git clone "$1"
376 }
377
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
386 daily_todo_file_template() {
387 cat << EOF
388 ===============================================================================
389 $(date '+%F %A')
390 ===============================================================================
391
392 -------------------------------------------------------------------------------
393 TODAY
394 -------------------------------------------------------------------------------
395
396
397 -------------------------------------------------------------------------------
398 CURRENT
399 -------------------------------------------------------------------------------
400
401
402 -------------------------------------------------------------------------------
403 BLOCKED
404 -------------------------------------------------------------------------------
405
406
407 -------------------------------------------------------------------------------
408 BACKLOG
409 -------------------------------------------------------------------------------
410 EOF
411 }
412
413 today() {
414 local date
415 date="$(date +%F)"
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
422 daily_todo_file_template > "$file"
423 fi
424 cd "$DIR_TODO" && "$EDITOR" $EDITOR_ARGS "$file"
425 }
426
427 todo() {
428 cd "$DIR_TODO" && "$EDITOR" TODO
429 }
430
431 work_log_template() {
432 cat << EOF
433 $(date '+%F %A')
434 ==========
435
436 Morning report
437 --------------
438
439 ### Prev
440
441 ### Curr
442
443 ### Next
444
445 ### Blockers
446
447 Day's notes
448 -----------
449 EOF
450 }
451
452 work_log() {
453 mkdir -p "$DIR_WORK_LOG"
454 local -r file_work_log_today="${DIR_WORK_LOG}/daily-$(date +%F).md"
455 if [ ! -f "$file_work_log_today" ]
456 then
457 work_log_template > "$file_work_log_today"
458 fi
459 vim -c 'set spell' "$file_work_log_today"
460
461 }
462
463 note() {
464 mkdir -p "$DIR_NOTES"
465 vim -c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
466 }
467
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' \
473 | awk '/^Device |^\t[A-Z][A-Za-z0-9]+: /'
474 }
475
476 bt_devs_paired() {
477 echo 'paired-devices' | bluetoothctl | _bt_devs_infos
478 }
479
480 bt_devs() {
481 echo 'devices' | bluetoothctl | _bt_devs_infos
482 }
483
484 run() {
485 local -r stderr="$(mktemp)"
486
487 local code urgency
488
489 $@ 2> >(tee "$stderr")
490 code="$?"
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 }
498
499 bar_gauge() {
500 awk "$@" '
501 BEGIN {
502 # CLI options
503 width = width ? width : 80
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 : "|"
508 num = num ? 1 : 0
509 pct = pct ? 1 : 0
510 }
511
512 {
513 cur = $1
514 max = $2
515 lab = $3
516
517 cur_scaled = num_scale(cur, max, 1, width)
518
519 printf \
520 "%s%s%s%s", \
521 lab ? lab " " : "", \
522 num ? cur "/" max " " : "", \
523 pct ? sprintf("%3.0f%% ", cur / max * 100) : "", \
524 ch_left
525 for (i=1; i<=width; i++) {
526 c = i <= cur_scaled ? ch_used : ch_blank
527 printf "%s", c
528 }
529 printf "%s\n", ch_right
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
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
547 printf "%s%s %.2f%%", sep, name, pct
548 sep = ", "
549 }
550
551 END {printf "\n"}
552 '
553 }
554
555 internet_addr() {
556 curl --silent --show-error --max-time "${1:=1}" 'https://api.ipify.org' 2>&1
557 }
558
559 status_batt() {
560 case "$(uname)" in
561 'Linux')
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
589 ;;
590 esac
591 }
592
593 indent() {
594 awk -v unit="$1" '{printf "%s%s\n", unit, $0}'
595 }
596
597 status() {
598 local -r indent_unit=' '
599
600 uname -srvmo
601 hostname | figlet
602 uptime
603
604 echo
605
606 echo 'accounting'
607
608 # TODO Bring back seesion and client listing, but per server/socket.
609 printf '%stmux\n' "$indent_unit"
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"
616 # "tmux: client tmux"
617 # "tmux: server tmux -L foo -S bar" <-- -S takes precedence
618 /^tmux:/ {
619 # XXX This of course assumes pervasive usage of -L
620 # TODO Handle -S
621 role=$2
622
623 split($0, sides_of_S, "-S")
624 split(sides_of_S[2], words_right_of_S, FS)
625
626 split($0, sides_of_L, "-L")
627 split(sides_of_L[2], words_right_of_L, FS)
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]
633 } else {
634 sock = "default"
635 }
636
637 roles[role]++
638 socks[sock]++
639 count[role, sock]++
640 }
641
642 END {
643 for (sock in socks) {
644 clients = count["client", sock]
645 printf "%s ", sock
646 if (clients) {
647 printf "<-> %d", clients
648 }
649 printf "\n"
650 }
651 printf "\n"
652 }' \
653 | sort \
654 | column -t \
655 | indent "${indent_unit}${indent_unit}"
656
657 echo
658
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'
678 (
679 free | awk '$1 == "Mem:" {print $3, $2, "mem"}'
680 df ~ | awk 'NR == 2 {print $3, $3 + $4, "disk"}'
681 status_batt
682 ) \
683 | bar_gauge -v width=60 -v pct=1 \
684 | column -t \
685 | indent "$indent_unit"
686
687 echo
688
689 printf '%smem by proc\n' "$indent_unit"
690 ps -eo rss,comm \
691 | awk -v total="$(free | awk '$1 == "Mem:" {print $2; exit}')" '
692 NR > 1 {
693 rss = $1
694 proc = $2
695 by_proc[proc] += rss
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
708 local _dir temp_input label_file label
709
710 printf '%sthermal\n' "$indent_unit"
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"
730 done \
731 | indent "${indent_unit}${indent_unit}"
732
733 echo 'net'
734 #local -r internet_addr=$(internet_addr 0.5)
735 #local -r internet_ptr=$(host -W 1 "$internet_addr" | awk 'NR == 1 {print $NF}' )
736
737 #echo "${indent_unit}internet"
738 #echo "${indent_unit}${indent_unit}$internet_addr $internet_ptr"
739 echo "${indent_unit}if"
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]
773 l = l ? sprintf("%.0f%%", l) : "--"
774 e = e ? e : "--"
775 print device, address[device], e, l
776 }
777 }
778 ' \
779 | column -t \
780 | indent "${indent_unit}${indent_unit}"
781
782 # WARN: ensure: $USER ALL=(ALL) NOPASSWD:/bin/netstat
783
784 echo "${indent_unit}-->"
785
786 # TODO Populate pid->cmd dict from `ps -eo pid,comm` and lookup progs there
787 # since netstat -p output comes out truncated.
788
789 sudo -n netstat -tulnp \
790 | awk -v indent="${indent_unit}${indent_unit}" '
791 NR > 2 && ((/^tcp/ && proc = $7) || (/^udp/ && proc = $6)) {
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 }'
819
820 echo "${indent_unit}<->"
821
822 printf '%sTCP\t' "${indent_unit}${indent_unit}"
823 sudo -n netstat -tnp \
824 | awk 'NR > 2 && $6 == "ESTABLISHED" {print $7}' \
825 | awk '{sub("^[0-9]+/", ""); print}' \
826 | sort -u \
827 | xargs \
828 | column -t
829
830 # TODO: iptables summary
831 }
832
833 ssh_invalid_by_addr() {
834 awk '
835 /: Invalid user/ && $5 ~ /^sshd/ {
836 addr=$10 == "port" ? $9 : $10
837 max++
838 by_addr[addr]++
839 }
840
841 END {
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 '
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 }
882 ' \
883 /var/log/auth.log \
884 /var/log/auth.log.1 \
885 | sort -k 3 \
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
902 }
903 ' \
904 /var/log/auth.log \
905 /var/log/auth.log.1 \
906 | sort -n -k 1 \
907 | bar_gauge -v width="$(stty size | awk '{print $2}')" -v num=1 -v ch_right=' ' -v ch_left=' ' -v ch_blank=' ' \
908 | column -t
909 }
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]
917 }' /var/log/syslog /var/log/syslog.1 \
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 }' \
930 | sort -n -k 1 \
931 | bar_gauge -v num=1 -v ch_right=' ' -v ch_left=' ' -v ch_blank=' ' \
932 | column -t
933 }
This page took 0.215611 seconds and 4 git commands to generate.