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