This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nms-nmsweb/snmp/附件/shell/cpu.sh
2018-09-27 16:21:05 +08:00

131 lines
3.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
PLACE=".1.3.6.1.4.1.5000001.1.2.1" # ZDJZ-MIB::cpuEntry的根OID
#
# 脚本会接收到两个参数
# $1 --请求类型
# -s --snmpset
# -g --snmpget
# -n --snmpgetnext/snmpwalk 注snmpwalk的本质是循环执行snmpgetnext以当次snmpgetnext的最终执行OID作为下次snmpgetnext的请求OID
# $2 --请求的OID
#
REQ="$2" # 请求的OID
RET="" # 最终执行的OID
#
# 提取cpu监测结果文件的内容
#
basePath=$(cd `dirname $0`; pwd) # 脚本所在目录
while read line;do # 读取配置文件
eval "$line"
done < ${basePath}/config
declare -A devices_arr # 所有设备所有cpu数据存放的数组最后从这里取数据格式devices_arr[“项”“设备序号”“核心序号”]
# 例如取第2台设备的cpu的第3个核心的用户使用率devices_arr[223]
declare -A device_core # 设备对应的cpu核心数格式device_core[1]=81号设备cpu8核
device_index=1 # 设备序号
core_index=1 # cpu核心序号
item_num=8 # 项目数量,根据业务而定
details=`awk 'NR>2{print}' ${snmp_result_dir}/"cpu.csv"` # 取得details内容
details_rownum=`echo "$details"|awk '{print NR}'|tail -n1` # details行数
for((i=0;i<$details_rownum;i++));do
for_i_flag=1
rownum_t=$((i+1))
detail_row=`echo "$details"|awk 'NR=="'$rownum_t'"{print}'` # details某行的整行数据
OLD_IFS="$IFS"
IFS=","
detail_row_arr=($detail_row) # 将整行数据用逗号隔开转为数组
IFS=$OLD_IFS
for((j=4;j<${#detail_row_arr[@]};j++));do
if [[ ${detail_row_arr[3]} =~ "-" ]];then
devices_arr+=([$((j-2))${device_index}${core_index}]=${detail_row_arr[$j]})
else
device_core+=([$device_index]=$((core_index-1)))
device_index=$((device_index+1)) # 设备序号加1
core_index=1 # 重置cpu核心序号
for_i_flag=0
break
fi
done
if [ $for_i_flag == "1" ];then
core_index=$((core_index+1))
fi
done
#
# 处理snmpgetnext请求获取REQ的递增后的下个OID递增规则需要自定义
#
if [ "$1" = "-n" ]; then
caseFlag=1
# 机器编号 -------------------开始
case $REQ in
$PLACE| \
$PLACE.0| \
$PLACE.0.*| \
$PLACE.1) RET=$PLACE.1.1; caseFlag=0 ;;
esac
for((i=1;i<=$((device_index-1))&&$caseFlag==1;i++));do
case $REQ in
$PLACE.1.$((i-1))| \
$PLACE.1.$((i-1)).*) RET=$PLACE.1.$i; caseFlag=0 ;;
esac
done
# 机器编号 -------------------结束
# cpu信息 --------------------开始
for((i=2;i<=$item_num&&$caseFlag==1;i++));do
case $REQ in
$PLACE.$((i-1)).*| \
$PLACE.$i| \
$PLACE.$i.0) RET=$PLACE.$i.1.1; caseFlag=0 ;;
esac
for((j=1;j<=$((device_index-1))&&$caseFlag==1;j++));do
case $REQ in
$PLACE.$i.$((j-1)).*| \
$PLACE.$i.$j) RET=$PLACE.$i.$j.1; caseFlag=0 ;;
esac
for((k=1;k<=${device_core[$j]}&&$caseFlag==1;k++));do
case $REQ in
$PLACE.$i.$j.$((k-1))| \
$PLACE.$i.$j.$((k-1)).*) RET=$PLACE.$i.$j.$k; caseFlag=0 ;;
esac
done
done
done
# cpu信息 --------------------结束
fi
#
# 根据RET输出查询结果
#
echo "$RET"
for((i=1;i<=$((device_index-1));i++));do
case "$RET" in
$PLACE.1.$i) echo "integer"; echo $i; exit 0 ;;
esac
done
for((i=2;i<=$item_num;i++));do
for((j=1;j<=$((device_index-1));j++));do
for((k=1;k<=${device_core[$j]};k++));do
case "$RET" in
$PLACE.$i.$j.$k) echo "string"; echo ${devices_arr[$i$j$k]}; exit 0 ;;
esac
done
done
done
echo "string"; echo "error OID"; exit 0