Skip to content

Instantly share code, notes, and snippets.

@drolfe
Last active April 2, 2021 20:36
Show Gist options
  • Save drolfe/510d05a79069df3a2289 to your computer and use it in GitHub Desktop.
Save drolfe/510d05a79069df3a2289 to your computer and use it in GitHub Desktop.
Ruby Snmp Interface Bandwidth
#!/usr/bin/ruby
require 'rubygems'
require 'snmp'
include SNMP
@results = Array.new
# Interface speed in mbps
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
get_snmp; sleep(10); get_snmp; out_delta = @results[2] - @results[0]; in_delta = @results[3] - @results[1];
out_mbps = out_delta / 10 * 8 / 1024 / 1024; in_mbps = in_delta / 10 * 8 / 1024 / 1024 ;
out_percent = out_mbps.to_f / interface_speed * 100; in_percent = in_mbps.to_f / interface_speed * 100;
puts "out traffic mbps = #{out_mbps}\nin traffic mbps = #{in_mbps}\nout traffic % = #{out_percent}\nin traffic % = #{in_percent}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment