#!/bin/bash # # Monitor zswap usage on Linux. Takes the raw values from sysfs and # feeds them as graphs into Check_MK if DEBUG=TRUE: # # root@athene:/sys/kernel/debug/zswap# grep -R . # duplicate_entry:0 # pool_limit_hit:0 # pool_total_size:389521408 # reject_alloc_fail:0 # reject_compress_poor:0 # reject_kmemcache_fail:0 # reject_reclaim_fail:0 # same_filled_pages:34247 # stored_pages:248801 # written_back_pages:0 # # Unless DEBUG=TRUE only compression_ratio, pool_total_size_mb, # stored_size_mb and same_filled_pages (if available) will be reported. # # Two exceptions: pool_total_size will be converted from bytes to MB # and reported as pool_total_size_mb, stored_pages will be converted # from pages to MB and reported as stored_size_mb # # In case the plugin detects that zswap is not in use it will simply # exit. In case it detects zswap and zram running in parallel the # status will change to UNKN. # # (c) Thomas Kaiser, 2020. # # This file is part of Check_MK. # The official homepage is at http://mathias-kettner.de/check_mk. # # check_mk is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation in version 2. check_mk is distributed # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with- # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. See the GNU General Public License for more de- # tails. You should have received a copy of the GNU General Public # License along with GNU Make; see the file COPYING. If not, write # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, # Boston, MA 02110-1301 USA. export PATH=/usr/sbin:/usr/bin:/sbin:/bin DEBUG=FALSE # set to TRUE to get all available statistics in raw format if [ ! -d /sys/module/zswap/parameters ]; then # zswap not available so exit right now exit 0 fi # check whether swap is enabled and zram and zswap might coexist: Zram_Devices=$(sed 1d /proc/swaps | grep -c '^/dev/zram') Non_Zram_Devices=$(sed 1d /proc/swaps | grep -c -v '^/dev/zram') if [ ${Zram_Devices} -gt 0 -a ${Non_Zram_Devices} -gt 0 ]; then echo -e "<<>>\n(${0##*/}) Zswap 3 UNKN - Both ZRAM and ZSWAP are active at the same time. Check configuration please." exit 0 elif [ ${Non_Zram_Devices} -eq 0 ]; then # zswap not active so exit right now exit 0 fi if [ ! -d /sys/kernel/debug/zswap ]; then echo -e "<<>>\n(${0##*/}) Zswap 3 UNKN - /sys/kernel/debug/zswap not accessible, no zswap stats available" exit 0 fi ZswapStats="$(grep -R . /sys/kernel/debug/zswap/ | while read ; do sed 's/:/=/' <<< ${REPLY##*/} ; done | tr "\n" " ")" eval ${ZswapStats} if [ ${pool_total_size:-0} -gt 0 ]; then # https://unix.stackexchange.com/a/412760 compression_ratio=$(awk -F" " '{printf ("%0.1f",$1*4096/$2); }' <<<"${stored_pages} ${pool_total_size}") CompressionStatus=", compression ratio: ${compression_ratio:-0}" fi [ "X${compression_ratio}" = "X" ] && compression_ratio=0 # convert size in memory and on disk to MB pool_total_size_mb=$(awk '{printf ("%0.2f",$1/1048576); }' <<<"${pool_total_size}") stored_size_mb=$(awk '{printf ("%0.2f",$1/256); }' <<<"${stored_pages}") # read module parameters read max_pool_percent /dev/null read compressor /dev/null read zpool /dev/null if [ -n ${compressor} -a -n ${zpool} ]; then ModuleParams="Using ${compressor}/${zpool}, " fi if [ ${pool_limit_hit} -eq 0 ]; then CheckState="0 OK" ZswapStatus="${ModuleParams}${pool_total_size_mb:-0} MB in RAM, ${stored_size_mb:-0} MB on disk${CompressionStatus}" else CheckState="1 WARN" ZswapStatus="${ModuleParams}${pool_total_size_mb:-0} MB in RAM, ${stored_size_mb:-0} MB on disk, ${pool_limit_hit:-0} pool limit hits${CompressionStatus}" fi if [ "X${DEBUG}" = "XTRUE" ]; then ZswapGraphs="stored_size_mb=${stored_size_mb:-0} pool_total_size_mb=${pool_total_size_mb:-0} compression_ratio=${compression_ratio:-0} max_pool_percent=${max_pool_percent:-0} ${ZswapStats}" elif [ -n ${same_filled_pages} ]; then # same_filled_pages only available with recent kernels ZswapGraphs="stored_size_mb=${stored_size_mb:-0} pool_total_size_mb=${pool_total_size_mb:-0} compression_ratio=${compression_ratio:-0} pool_limit_hit=${pool_limit_hit:-0} same_filled_pages=${same_filled_pages:-0}" else ZswapGraphs="stored_size_mb=${stored_size_mb:-0} pool_total_size_mb=${pool_total_size_mb:-0} compression_ratio=${compression_ratio:-0} pool_limit_hit=${pool_limit_hit:-0}" fi echo -e "<<>>\n(${0##*/}) Zswap ${CheckState} - ${ZswapStatus} | ${ZswapGraphs}"