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