#!/bin/bash

# auth: hanguoyin
# date: 2020-09-27
# Desc: 监控mlx 网卡 groups 使用情况

basepath=$(cd `dirname $0`; pwd)
filename=`echo $0|awk -F '/' '{print $NF}'`
tmpfile="$basepath/tmp/${filename}.tmp"
tmpdir=`dirname $tmpfile`
[ ! -d $tmpdir ] && mkdir $tmpdir

# only for mellanox
if [[ $(lspci | grep -ci "mellanox") -eq 0 ]];then
	exit 0
fi

# need mft tools
if [[ $(rpm -qa|grep -c mft) -eq 0 ]];then
	exit 0
fi


nic="net2"
businfo=$(ethtool  -i $nic| grep bus-info | awk '{print $2}')
mlxdump -d $businfo fsdump --type FT --gvmi=0 --no_zero > $tmpfile
num_groups=$(python $basepath/parse_fsdump.py --file $tmpfile | grep 'ft_id=0x300000 num_masks' | awk '{print $3}'| awk -F[=] '{print $NF}')
num_masks=$(python $basepath/parse_fsdump.py --file $tmpfile | grep 'ft_id=0x300000 num_masks' | awk '{print $2}'| awk -F[=] '{print $NF}')

if [ -n $result ];then
	echo '[{"value":'$num_groups',"tags":"mlx=num_groups"}, {"value":'$num_masks',"tags":"mlx=num_masks"}]'
fi

exit 0
