Как узнать, чем именно занят swap


Anonymous - Posted on 06 Май 2017

С помощью этого скрипта можно узнать, чем именно занят swap.

Скрипт написан на awk, более точно версия awk указана в комментарии скрипта. На данный момент тестировалось только на одной версии awk.

Текст скрипта доступен по ссылке https://gist.github.com/sagax/25495c279bb221e39813f1114547047c , я не очень понял, как тут добавить код с подсветкой awk. Как именно это выглядит в работе можно посмотреть тут https://asciinema.org/a/b1643wf79iair17p0xizracsb .

Если нашли ошибку - пишите в комментарии к этому совету или на gist. Надеюсь кому-нибудь этот совет окажется полезен.

#!/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;30;42m" s "\033[0m" }
function blue(s)   { return "\033[1;30;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 mag(msg_string) }
    else if (size > (all_swap_size/5)) { print mag(msg_string) }
    else if (size > (all_swap_size/6)) { print orange(msg_string) }
    else if (size > (all_swap_size/7)) { print orange(msg_string) }
    else if (size > (all_swap_size/8)) { print blue(msg_string) }
    else if (size > (all_swap_size/9)) { print blue(msg_string) }
    else if (size < (all_swap_size/10) && size > (all_swap_size/150)) {
        print blue(msg_string)
    }
    else if (size < (all_swap_size/150) && 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)
    }
 
    if (sizer == "true") {
        asort(_slist, __slist, "sort_by_value_r")
        print_data_line(__slist)
    }
 
    if (name == "true") {
        asort(_slist, __slist, "sort_by_name")
        print_data_line(__slist)
    }
 
    if (namer == "true") {
        asort(_slist, __slist, "sort_by_name_r")
        print_data_line(__slist)
    }
 
    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)
        }
    }
}
0
Ваша оценка: Ничего

Отправить комментарий

Google Friend Connect (leave a quick comment)
loading...
Содержание этого поля является приватным и не предназначено к показу.