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