Mar*_*tin 6 algorithm bash cisco
我正在构建一个小型监控解决方案,并希望了解在先前读数大于当前读数的情况下,正确/最佳行为是什么.例如,ifHCOutOctets
SNMP对象计算从Cisco路由器中的接口传输的字节数.如果此计数器重置为0,例如由于路由器重启,图形应用程序应该如何运行?在我的选项中,以下算法是正确的行为:
if [ ! $prev_val ]; then
# This reading will be used to set the baseline value for "prev_val" variable
# if "prev_val" does not already exist.
prev_val="$cur_val"
elif (( prev_val > cur_val )); then
# Counter value has set to zero.
# Use the "cur_val" variable.
echo "$cur_val"
prev_val="$cur_val"
else
# In case "cur_val" is higher than or equal to "prev_val",
# use the "cur_val"-"prev_val"
echo $(( cur_val - prev_val ))
prev_val="$cur_val"
fi
Run Code Online (Sandbox Code Playgroud)
我还根据上面的算法制作了一个小例子图:
基于此构建流量图:
reading 1: cur_val=0, prev_val will be 0
reading 2: 0-0=0(0 Mbps), cur_val=0, prev_val will be 0
reading 3: 20-0=20(160 Mbps), cur_val=20, prev_val will be 20
reading 4: 20-20=0(0 Mbps), cur_val=20, prev_val will be 20
reading 5: 50-20=30(240 Mbps), cur_val=50, prev_val will be 50
reading 6: 40(320Mbps), cur_val=40, prev_val will be 40
reading 7: 70-40=30(240 Mbps), cur_val=70, prev_val will be 70
reading 8: no data from SNMP agent
reading 9: 90-70=20(160 Mbps), cur_val=90, prev_val will be 90
Run Code Online (Sandbox Code Playgroud)
对我来说,看起来这个小算法正常工作.
如果有任何不清楚的地方,请告诉我,我会改进我的问题.
我可以看到您所回显的问题是,在正常操作的情况下,计数器会发生变化。路由器重启后,它会显示一些绝对值。现在有方法可以比较这 2 个读数。如果您想显示 2 个读数的增量,我建议:
if [ ! $prev_val ]; then
# This reading will be used to set the baseline value for "prev_val" variable
# if "prev_val" does not already exist.
prev_val="$cur_val"
elif (( prev_val > cur_val )); then
# Counter value has set to zero.
# Use the "cur_val" variable.
echo "Router/counter restarted"
# restart the counter as well
prev_val="$cur_val"
else
# In case "cur_val" is higher than or equal to "prev_val",
# use the "cur_val"-"prev_val"
echo $((cur_val-prev_val))
fi
Run Code Online (Sandbox Code Playgroud)
您还可以删除elif
部分并仅打印负值以指示计数器/路由器重新启动