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