Instantly share code, notes, and snippets.

@sagax sagax/swaptop.awk
Last active Oct 28, 2017

Embed
What would you like to do?
How to see what happen with swap
#!/usr/bin/awk -f
# GNU Awk 4.1.1, API: 1.1
#
# place script to $HOME/bin/swaptop
# type chmod +x $HOME/bin/swaptop
# run!
function red(s) { return "\033[0;30;41m" s "\033[0m" }
function mag(s) { return "\033[0;30;45m" s "\033[0m" }
function orange(s) { return "\033[0;30;43m" s "\033[0m" }
function green(s) { return "\033[1;37;42m" s "\033[0m" }
function blue(s) { return "\033[1;37;44m" s "\033[0m" }
function getdot(count) {
_dots = ""
for (i=0; i < count; i++) { _dots = _dots "." }
return _dots
}
function refresh_list() {
delete _list
for (item in list) { _list[item] = list[item] }
}
function gen_list() {
delete _slist
iter = 0
for (item in list) {
_slist[iter] = item "|" list[item]
iter++
}
}
function sort_by_value(i1, v1, i2, v2) {
split(v1, _v1, "|")
split(v2, _v2, "|")
value1 = _v1[2]
value2 = _v2[2]
if (value1 < value2) { return -1 }
else if (value1 == value2) { return 0 }
else if (value1 > value2) { return 1 }
}
function sort_by_value_r(i1, v1, i2, v2) {
split(v1, _v1, "|")
split(v2, _v2, "|")
value1 = _v1[2]
value2 = _v2[2]
if (value1 < value2) { return 1 }
else if (value1 == value2) { return 0 }
else if (value1 > value2) { return -1 }
}
function sort_by_name(i1, v1, i2, v2) {
split(v1, _v1, "|")
split(v2, _v2, "|")
name1 = _v1[1]
name2 = _v2[1]
if (name1 < name2) { return -1 }
else if (name1 == name2) { return 0 }
else if (name1 > name2) { return 1 }
}
function sort_by_name_r(i1, v1, i2, v2) {
split(v1, _v1, "|")
split(v2, _v2, "|")
name1 = _v1[1]
name2 = _v2[1]
if (name1 < name2) { return 1 }
else if (name1 == name2) { return 0 }
else if (name1 > name2) { return -1 }
}
function alert_swap_used_size(percent) {
if (percent <= 30) {
return blue(" " used_swap_size_mb " MB " ) " :: " blue(" " used_swap_percent "% ")
} else if (percent <= 50) {
return green(" " used_swap_size_mb " MB ") " :: " green(" " used_swap_percent "% ")
} else if (percent > 50 && percent < 80) {
return orange(" " used_swap_size_mb " MB ") " :: " orange(" " used_swap_percent "% ")
} else if (percent >= 80) {
return mag(" " used_swap_size_mb " MB ") " :: " red(" " used_swap_percent "% ")
} else {
return " " used_swap_size_mb " MB " " :: " " " used_swap_percent "% "
}
}
function alert_swap_used_to_process(size, msg_string) {
if (swap_was_catched != "true") {
print msg_string
return
}
if (size >= (all_swap_size/2 )) { print red(msg_string) }
else if (size >= (all_swap_size/3 )) { print red(msg_string) }
else if (size >= (all_swap_size/4 )) { print red(msg_string) }
else if (size >= (all_swap_size/5 )) { print mag(msg_string) }
else if (size >= (all_swap_size/6 )) { print mag(msg_string) }
else if (size >= (all_swap_size/7 )) { print mag(msg_string) }
else if (size >= (all_swap_size/8 )) { print orange(msg_string) }
else if (size >= (all_swap_size/9 )) { print orange(msg_string) }
else if (size >= (all_swap_size/10 )) { print orange(msg_string) }
else if (size >= (all_swap_size/150)) { print blue(msg_string) }
else if (size >= (all_swap_size/500)) { print blue(msg_string) }
else { print msg_string }
}
function print_data_line(_plist) {
for (item in _plist) {
split(_plist[item], w, "|")
key = w[1]
value = w[2]
msg = key " "
l_item = length(key)
l_value = length(value)
l_dot = 38 - l_item - l_value
msg = msg getdot(l_dot)
msg = msg " " value "kb"
alert_swap_used_to_process(value, msg)
}
}
function nline() {
resg = getline
if ($1 != "VmSwap:" && resg == 1) { nline() }
else { _value = $2 }
}
BEGIN {
IGNORECASE=1
ARGWASFIND="false"
for (i=0; i < ARGC; i++) {
if (ARGV[i] == "summ") { summ = "true"; ARGWASFIND = "true"; delete ARGV[i] }
else if (ARGV[i] == "size") { size = "true"; ARGWASFIND = "true"; delete ARGV[i] }
else if (ARGV[i] == "sizer") { sizer = "true"; ARGWASFIND = "true"; delete ARGV[i] }
else if (ARGV[i] == "name") { name = "true"; ARGWASFIND = "true"; delete ARGV[i] }
else if (ARGV[i] == "namer") { namer = "true"; ARGWASFIND = "true"; delete ARGV[i] }
}
if (ARGWASFIND == "false") {
print "need arg: name namer size sizer summ"
exit 0
}
while ("ls /proc/*/status" | getline filename) {
ARGV[ARGC++] = filename
}
ARGV[ARGC++] = "/proc/swaps"
} /^name/ {
_name = $2
nline()
if (list[_name] == "") {
list[_name] = _value
} else {
list[_name] = list[_name] + _value
}
} /^\/dev/ {
# TODO test this with few swaps
if (FILENAME == "/proc/swaps") {
swap_was_catched = "true"
all_swap_size = $3
all_swap_size_mb = $3 / 1024
used_swap_size = $4
used_swap_size_mb = $4 / 1024
used_swap_percent = used_swap_size * 100 / all_swap_size
}
}
END {
refresh_list()
gen_list()
if (size == "true") {
asort(_slist, __slist, "sort_by_value")
print_data_line(__slist)
} else if (sizer == "true") {
asort(_slist, __slist, "sort_by_value_r")
print_data_line(__slist)
} else if (name == "true") {
asort(_slist, __slist, "sort_by_name")
print_data_line(__slist)
} else if (namer == "true") {
asort(_slist, __slist, "sort_by_name_r")
print_data_line(__slist)
} else if (summ == "true") {
total = 0
for (item in list) {
total += list[item]
}
print "summ: " blue(" " total " kB ") " :: " blue(" "(total/1024) " MB ")
if (swap_was_catched == "true") {
print "swap: " blue(" " all_swap_size " kB ") " :: " blue(" " all_swap_size_mb " MB ") " :: " alert_swap_used_size(used_swap_percent)
}
} else {
exit 0
}
}
@sagax

This comment has been minimized.

Copy link
Owner Author

sagax commented May 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment