Skip to content

Instantly share code, notes, and snippets.

@drolfe
Last active June 9, 2021 22:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drolfe/c9164655ac94a3d43e35 to your computer and use it in GitHub Desktop.
Save drolfe/c9164655ac94a3d43e35 to your computer and use it in GitHub Desktop.
Dashing Snmp Interface Stats

Description

Simple Dashing job (and associated dash html) to display SNMP interface percentages

##Dependencies

snmp

Add it to dashing's gemfile:

gem 'snmp'

and run bundle install. Everything should work now :)

##Usage

To use this widget, copy snmp.rb into the /jobs directory. Add the html code from dash.erb into your dashboard file.

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="out-percent" data-view="Meter" data-title="Bandwidth Out %" data-min="0" data-max="100"></div>
</li>
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="in-percent" data-view="Meter" data-title="Bandwidth In %" data-min="0" data-max="100"></div>
</li>
require 'snmp'
include SNMP
@results = Array.new
#Interface speed in mbps -- 10G
interface_speed = 10000
def get_snmp
#Interface index for WS-C3750E Te1/0/1 running Cisco IOS 15.0(2)SE4
interface_index = 10201
#SNMP OID values for IF-MIB::ifHCOutOctets and IF-MIB::ifHCInOctets
lookup_values = [ "1.3.6.1.2.1.31.1.1.1.10", "1.3.6.1.2.1.31.1.1.1.6" ]
Manager.open(:Host => 'localhost', :Version => :SNMPv2c, :Community => 'Public') do |manager|
manager.walk(lookup_values) do |row|
row.each { |vb|
if vb.name.to_s == "IF-MIB::ifHCOutOctets.#{interface_index.to_s}" || vb.name.to_s == "IF-MIB::ifHCInOctets.#{interface_index.to_s}"
@results.push(vb.value.to_i)
end}
end
end
end
SCHEDULER.every '15s' do
#Run get_snmp method to fetch the data
get_snmp; sleep(10); get_snmp; out_delta = @results[2] - @results[0]; in_delta = @results[3] - @results[1];
#Calculate Mbps from fetched delta's
out_mbps = out_delta / 10 * 8 / 1024 / 1024; in_mbps = in_delta / 10 * 8 / 1024 / 1024 ;
#Calculate percentages from fetched delta's
out_percent = out_mbps.to_f / interface_speed * 100; in_percent = in_mbps.to_f / interface_speed * 100;
#Clear @results array brfore the next run
@results.clear
#Update widgets with percentage vaules, use out_mbps and in_mbps to sent data as Mbps
send_event('out-percent', { value: out_percent.round(2) })
send_event('in-percent', { value: in_percent.round(2) })
end
@sethdippold21
Copy link

Did your code at the end of line 17 get cut off by chance? Should this be the full line or is there more?

if vb.name.to_s == "IF-MIB::ifHCOutOctets.#{interface_index.to_s}" || vb.name.to_s == "IF-MIB::ifHCInOctets.#{interface_index.to_s}"

@drolfe
Copy link
Author

drolfe commented Aug 19, 2014

HI, yes sorry, that's what I get for copy and past from the bash window :-|

all fixed now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment