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