#!/bin/sh
set +x
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
LOG_FILE="/var/log/inspect.log"
CurInspectNum="141"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
isPass=0
>${RESULTFILE}
cpuset="PASS"
actual_quota="PASS"
cfg_quota="PASS"
no_need_check="Not a VM"
function LOG
{
    time=$(date)
    echo [${time}][$$][$CurInspectFun]$@ >> $LOG_FILE
}

function checkVirtural()
{
    grep streamStoreMode /opt/huawei/snas/etc/snas.ini | grep -q ds
    if [ $? -ne 0 ];then
        echo "cpuset:${no_need_check}||cfg_quota: ---||actual_quota: ---" >> ${RESULTFILE} 2>&1
        LOG "INFO:NOT virtural machine, ignore."
        isPass=3
        return
    fi
    vir_xmlA=$(find /etc/libvirt/qemu -name "*.xml" | head -n 1)
    vir_xmlB=$(find /etc/libvirt/qemu -name "*.xml" | tail -n 1)
    if [ -z "${vir_xmlA}" ];then
        echo "cpuset:NO VM||cfg_quota: ---||actual_quota: ---" >> ${RESULTFILE} 2>&1
        LOG "INFO:have no virtural machine, pass."
        return
    fi
    kvm_pids=$(ps -ef | grep qemu-kvm | grep -v grep | awk '{print $2}')
    for pid in $kvm_pids
    do
        pidsInfo=$(ps -T -p $pid) 
        for num in 0 1 2 3 4 5 6 7
        do
            tmp_spid=$(echo "${pidsInfo}" | grep -w "CPU ${num}/KVM" | awk '{print $2}')
            real_cpuset=$(taskset -pc $tmp_spid | awk -F": " ' {print $2}')
            cpusetA=$(cat $vir_xmlA | grep vcpu=\'${num}\' |awk -F "cpuset=" '{print $2}' | awk -F"'" '{print $2}')
            cpusetB=$(cat $vir_xmlB | grep vcpu=\'${num}\' |awk -F "cpuset=" '{print $2}' | awk -F"'" '{print $2}')
            if [ "$real_cpuset" = "$cpusetA" ] || [ "$real_cpuset" = "$cpusetB" ];then
                LOG "cpuset:$pid cpu_$num $tmp_spid is correct.$real_cpuset"
            else
                isPass=1
                cpuset="NOT_PASS"
                LOG "cpuset:$pid cpu_$num $tmp_spid is not correct.$real_cpuset"
            fi
        done
    done
    cpu_cnt=$(lscpu | grep ^CPU\(s\) | awk -F ":" '{print $2}' | sed 's/ //g')
    periods_info=$(cat $( ls /sys/fs/cgroup/cpu,cpuacct/machine.slice/machine-qemu*/cpu.cfs_period_us))
    for period in $periods_info
    do
        exp_quota=$(echo "$period * $cpu_cnt * 30 / 100"|bc)
        actual_quotaA=$(cat $( find /etc/libvirt/qemu -name "*.xml" | head -n 1) | grep -w "global_quota" | grep -Poa '[0-9]*')
        actual_quotaB=$(cat $( find /etc/libvirt/qemu -name "*.xml" | tail -n 1) | grep -w "global_quota" | grep -Poa '[0-9]*')
        cfs_quotaA=$(cat $( ls /sys/fs/cgroup/cpu,cpuacct/machine.slice/machine-qemu*/cpu.cfs_quota_us | head -n 1)|grep "${Exp_quota}")
        cfs_quotaB=$(cat $( ls /sys/fs/cgroup/cpu,cpuacct/machine.slice/machine-qemu*/cpu.cfs_quota_us | tail -n 1)|grep "${Exp_quota}")
        if [ $exp_quota -ne $actual_quotaA ] && [ $exp_quota -ne $actual_quotaB ];then
            isPass=1
            actual_quota="NOT_PASS"
            LOG "exp_quota:$exp_quota. $actual_quotaA and $actual_quotaB are not correct."
        fi
        if [ $exp_quota -ne $cfs_quotaA ] && [ $exp_quota -ne $cfs_quotaB ];then
            isPass=1
            cfg_quota="NOT_PASS"
            LOG "exp_quota:$exp_quota. $cfs_quotaA and $cfs_quotaB are not correct."
        fi
    done
    echo "cpuset:${cpuset} || cfg_quota:${cfg_quota} || actual_quota:${actual_quota}" >> ${RESULTFILE} 2>&1

}

checkVirtural
echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1

