#!/bin/bash
#################################################
#
#      Filename: monitor_memsnoop.sh
#
#        Author: scott.liu@ucloud.cn
#   Description: ---
#        Create: 2019-03-13 11:44:29
# Last Modified: 2019-03-13 11:49:23
#################################################
EXTFRAG=/sys/kernel/debug/extfrag

function die {
    echo "not support"
    exit 1
}
cd $EXTFRAG 2>/dev/null || die

count=0
num=`cat /sys/kernel/debug/extfrag/unusable_index | grep "Normal" | wc -l`
zone_count=(0 0)

if [ "$num" -eq 0  -o  "$num" -ge 3 ];then
    die
fi

while read line;do
    if [[ $line == *"Normal"* ]]; then
        reverse=`echo $line | awk '{for (i=NF; i>1;i --) printf("%s ", $i); print $i;}'`
        for i in ${reverse%Normal*};do
            if [[ $i == 1.000 ]];then
                ((zone_count[$count]++))
            fi
        done
        ((count++))
    fi

done <unusable_index

if [[ ${zone_count[0]} -gt ${zone_count[1]} ]];then
    ret=${zone_count[0]}
else
    ret=${zone_count[1]}
fi
echo "[{\"value\":$ret,\"tags\":\"\"}]"

