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