我正在构建一个小型的监控解决方案,希望了解在以前的读数大于当前读数的情况下,什么是正确/最佳的行为。例如,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

我还根据上面的算法制作了一个小的示例图:
流量图是基于以下内容构建的:
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

对我来说,这个小算法似乎工作正常。
如果有什么不清楚的地方请告诉我,我会改进我的问题。

最佳答案

我能看出你所呼应的问题是,在正常操作的情况下,是计数器的变化。路由器重新启动后,它将显示一些绝对值。现在有办法比较这两个。如果你想显示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

您也可以删除elif部分,只打印负值以指示计数器/路由器重新启动

07-24 09:24