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