1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-21 18:41:03 +00:00

Ruby plugins: apply code changes as suggested by "rubocop --auto-correct"

This commit is contained in:
Lars Kruse 2020-08-25 17:06:15 +02:00
parent b0b39b018e
commit 809639ab65
33 changed files with 904 additions and 910 deletions

View file

@ -35,7 +35,6 @@ Some magical munin foo...
=end
# Require this module, it is part of the standard ruby lib AFAIK
require 'net/http'
@ -49,7 +48,7 @@ stat = nil
# Check executable "name" for parameter count
params = $0.split('_')
if params.size != 3
puts "Incorrect number of parameters"
puts 'Incorrect number of parameters'
exit 1
end
@ -59,11 +58,11 @@ host = params[1]
stat = params[2]
unless ENV['debug'].nil?
puts "user = " + user
puts "pass = " + pass
puts "host = " + host
puts "port = " + port
puts "stat = " + stat
puts 'user = ' + user
puts 'pass = ' + pass
puts 'host = ' + host
puts 'port = ' + port
puts 'stat = ' + stat
end
# Dump the graph configuration data
@ -101,7 +100,7 @@ s = response.body
# Make sure we got the page successfully
if response.code != '200'
puts "Getting web page failed:"
puts 'Getting web page failed:'
case response.code
when '401'
puts 'Probably because the username and password are incorrect'
@ -124,12 +123,12 @@ when 'syncrate'
when 'attenuation'
a = s.scan(/.*share\.lineatt.*\n.*share\.down[^0-9]*([0-9]+).*share\.up[^0-9]*([0-9]+).*$/)
b, c = a[0]
puts 'down.value ' + (b.to_i).to_s + "\n" + 'up.value ' + (c.to_i).to_s
puts 'down.value ' + b.to_i.to_s + "\n" + 'up.value ' + c.to_i.to_s
exit 0
when 'margin', 'noise'
a = s.scan(/.*share\.noise.*\n.*share\.down[^0-9]*([0-9]+).*share\.up[^0-9]*([0-9]+).*$/)
b, c = a[0]
puts 'down.value ' + (b.to_i).to_s + "\n" + 'up.value ' + (c.to_i).to_s
puts 'down.value ' + b.to_i.to_s + "\n" + 'up.value ' + c.to_i.to_s
exit 0
else
puts 'Statistic ' + stat.to_s + ' not known, would you like me to fabricate it for you?'

View file

@ -32,13 +32,13 @@ require 'digest/md5'
require 'nokogiri'
def output
nics = Hash.new
nics["LAN"] = Hash.new
nics["WAN"] = Hash.new
nics["WLAN"] = Hash.new
password = ENV['router_password'] || ""
router_path = ENV['router_ip_address'] || "10.0.0.1"
router_path = "http://" + router_path
nics = {}
nics['LAN'] = {}
nics['WAN'] = {}
nics['WLAN'] = {}
password = ENV['router_password'] || ''
router_path = ENV['router_ip_address'] || '10.0.0.1'
router_path = 'http://' + router_path
agent = Mechanize.new
x = agent.get(router_path)
salt = x.body.match(/salt = "(.*)"/)[1]
@ -48,7 +48,7 @@ def output
padded_password = password + "\x01" * pad_size
# pad it the rest of the way, length 64 for user
salted_password = salt + padded_password + ("\x01" * (63 - salt.length - padded_password.length)) + "U"
salted_password = salt + padded_password + ("\x01" * (63 - salt.length - padded_password.length)) + 'U'
login_hash = salt + Digest::MD5.hexdigest(salted_password)
# authenticate against the router using the hash that we just built
@ -61,87 +61,87 @@ def output
doc.xpath('//interface').each do |interface|
children = interface.children
name = children.search('name')[0].text
nics[name]["packets_sent"] = children.search('packets_sent')[0].text
nics[name]["packets_received"] = children.search('packets_received')[0].text
nics[name]["tx_dropped"] = children.search('tx_dropped')[0].text
nics[name]['packets_sent'] = children.search('packets_sent')[0].text
nics[name]['packets_received'] = children.search('packets_received')[0].text
nics[name]['tx_dropped'] = children.search('tx_dropped')[0].text
begin
nics[name]["tx_collisions"] = children.search('tx_collisions')[0].text
nics[name]['tx_collisions'] = children.search('tx_collisions')[0].text
rescue Exception
nics[name]["tx_collisions"] = "0"
nics[name]['tx_collisions'] = '0'
end
nics[name]["rx_dropped"] = children.search('rx_dropped')[0].text
nics[name]["rx_errors"] = children.search('rx_errors')[0].text
nics[name]['rx_dropped'] = children.search('rx_dropped')[0].text
nics[name]['rx_errors'] = children.search('rx_errors')[0].text
end
# get wifi associations and print out info for munin graph
puts "multigraph clients"
puts 'multigraph clients'
clients_xml = agent.get("#{router_path}/wifi_assoc.xml").body
j = 0
doc = Nokogiri::XML(clients_xml.to_s)
doc.xpath('//assoc').each do |assoc|
doc.xpath('//assoc').each do |_assoc|
j += 1
end
puts "wifi_assoc.value " + j.to_s
puts 'wifi_assoc.value ' + j.to_s
# get dhcp clients and print out info for munin graph
clients_xml = agent.get("#{router_path}/dhcp_clients.xml").body
j = 0
doc = Nokogiri::XML(clients_xml.to_s)
doc.xpath('//client').each do |client|
doc.xpath('//client').each do |_client|
j += 1
end
puts "dhcp_clients.value " + j.to_s
puts 'dhcp_clients.value ' + j.to_s
puts "multigraph uptime"
puts 'multigraph uptime'
# get uptime of connection
clients_xml = agent.get("#{router_path}/wan_connection_status.xml").body
doc = Nokogiri::XML(clients_xml.to_s)
uptime = doc.children.search('wan_interface_up_time_0')[0].text
puts "uptime.value " + sprintf("%.2f", (Float(uptime) / 86400))
puts 'uptime.value ' + format('%.2f', (Float(uptime) / 86_400))
# graph overall interface packets transferred per interval
puts "multigraph if_packets"
for i in ["LAN", "WAN", "WLAN"] do
puts "#{i}_recv.value " + nics[i]["packets_received"]
puts "#{i}_send.value " + nics[i]["packets_sent"]
puts 'multigraph if_packets'
%w[LAN WAN WLAN].each do |i|
puts "#{i}_recv.value " + nics[i]['packets_received']
puts "#{i}_send.value " + nics[i]['packets_sent']
end
# graph overall interface dropped packets per interval
puts "multigraph if_drop"
for i in ["LAN", "WAN", "WLAN"] do
puts "#{i}_recv.value " + nics[i]["rx_dropped"]
puts "#{i}_send.value " + nics[i]["tx_dropped"]
puts 'multigraph if_drop'
%w[LAN WAN WLAN].each do |i|
puts "#{i}_recv.value " + nics[i]['rx_dropped']
puts "#{i}_send.value " + nics[i]['tx_dropped']
end
# graph overall interface collisions & errors per interval
puts "multigraph if_collerr"
for i in ["LAN", "WAN", "WLAN"] do
puts "#{i}_coll.value " + nics[i]["tx_collisions"]
puts "#{i}_err.value " + nics[i]["rx_errors"]
puts 'multigraph if_collerr'
%w[LAN WAN WLAN].each do |i|
puts "#{i}_coll.value " + nics[i]['tx_collisions']
puts "#{i}_err.value " + nics[i]['rx_errors']
end
# graph stats for each interface
for i in ["LAN", "WAN", "WLAN"] do
%w[LAN WAN WLAN].each do |i|
puts "multigraph if_packets.#{i}"
puts "send.value " + nics[i]["packets_sent"]
puts "recv.value " + nics[i]["packets_received"]
puts 'send.value ' + nics[i]['packets_sent']
puts 'recv.value ' + nics[i]['packets_received']
puts "multigraph if_drop.#{i}"
puts "send.value " + nics[i]["tx_dropped"]
puts "recv.value " + nics[i]["rx_dropped"]
puts 'send.value ' + nics[i]['tx_dropped']
puts 'recv.value ' + nics[i]['rx_dropped']
puts "multigraph if_collerr.#{i}"
puts "coll.value " + nics[i]["tx_collisions"]
puts "err.value " + nics[i]["rx_errors"]
puts 'coll.value ' + nics[i]['tx_collisions']
puts 'err.value ' + nics[i]['rx_errors']
end
end
def config
# build the configuration for graphs
puts "multigraph if_packets"
puts 'multigraph if_packets'
puts 'graph_title D-Link DIR-655 interface traffic'
puts 'graph_category network'
puts 'graph_order LAN_recv LAN_send WAN_recv WAN_send WLAN_recv WLAN_send'
puts 'graph_vlabel packets in (-) / out (+) per ${graph_period}'
for i in ["LAN", "WAN", "WLAN"] do
%w[LAN WAN WLAN].each do |i|
puts "#{i}_recv.type DERIVE"
puts "#{i}_recv.graph no"
puts "#{i}_recv.min 0"
@ -151,12 +151,12 @@ def config
puts "#{i}_send.min 0"
end
puts "multigraph if_drop"
puts 'multigraph if_drop'
puts 'graph_title D-Link DIR-655 interface drops'
puts 'graph_category network'
puts 'graph_order LAN_recv LAN_send WAN_recv WAN_send WLAN_recv WLAN_send'
puts 'graph_vlabel packets / ${graph_period}'
for i in ["LAN", "WAN", "WLAN"] do
%w[LAN WAN WLAN].each do |i|
puts "#{i}_recv.type DERIVE"
puts "#{i}_recv.graph no"
puts "#{i}_recv.min 0"
@ -166,12 +166,12 @@ def config
puts "#{i}_send.min 0"
end
puts "multigraph if_collerr"
puts 'multigraph if_collerr'
puts 'graph_title D-Link DIR-655 interface collisions & errors'
puts 'graph_category network'
puts 'graph_order LAN_coll LAN_err WAN_coll WAN_err WLAN_coll WLAN_coll'
puts 'graph_vlabel packets / ${graph_period}'
for i in ["LAN", "WAN", "WLAN"] do
%w[LAN WAN WLAN].each do |i|
puts "#{i}_coll.label #{i} collisions"
puts "#{i}_coll.type DERIVE"
puts "#{i}_coll.min 0"
@ -180,26 +180,26 @@ def config
puts "#{i}_err.min 0"
end
puts "multigraph clients"
puts "graph_title D-Link DIR-655 client information"
puts "graph_category system"
puts "graph_order dhcp_clients wifi_assoc"
puts "graph_vlabel number of clients"
puts "dhcp_clients.label DHCP clients"
puts "dhcp_clients.type GAUGE"
puts "dhcp_clients.min 0"
puts "wifi_assoc.label wifi clients"
puts "wifi_assoc.type GAUGE"
puts "wifi_assoc.min 0"
puts 'multigraph clients'
puts 'graph_title D-Link DIR-655 client information'
puts 'graph_category system'
puts 'graph_order dhcp_clients wifi_assoc'
puts 'graph_vlabel number of clients'
puts 'dhcp_clients.label DHCP clients'
puts 'dhcp_clients.type GAUGE'
puts 'dhcp_clients.min 0'
puts 'wifi_assoc.label wifi clients'
puts 'wifi_assoc.type GAUGE'
puts 'wifi_assoc.min 0'
puts "multigraph uptime"
puts "graph_title Uptime"
puts 'multigraph uptime'
puts 'graph_title Uptime'
puts 'graph_vlabel uptime in days'
puts 'graph_category system'
puts 'uptime.label uptime'
puts 'uptime.draw AREA'
for i in ["LAN", "WAN", "WLAN"] do
%w[LAN WAN WLAN].each do |i|
puts "multigraph if_packets.#{i}"
puts "graph_title D-Link DIR-655 #{i} traffic"
puts 'graph_category network'
@ -243,8 +243,8 @@ def config
end
# main
if ARGV.length == 1 and ARGV[0] == 'config'
config()
if (ARGV.length == 1) && (ARGV[0] == 'config')
config
else
output()
output
end

View file

@ -65,45 +65,43 @@ rights to this plugin are waived. Do with it as you wish.
require 'snmp'
idx_oid = "enterprises.3955.89.108.1.1.2"
max_oid = "enterprises.3955.89.108.1.1.6"
cur_oid = "enterprises.3955.89.108.1.1.5"
idx_oid = 'enterprises.3955.89.108.1.1.2'
max_oid = 'enterprises.3955.89.108.1.1.6'
cur_oid = 'enterprises.3955.89.108.1.1.5'
community = ENV['community'] || "public"
community = ENV['community'] || 'public'
version = ENV['version'] == '1' ? :SNMPv1 : :SNMPv2c
case ARGV[0]
when "snmpconf"
puts "require 1.3.6.1.4.1.3955.89.108.1.1.2.1. [0-9]"
puts "require 1.3.6.1.4.1.3955.89.108.1.1.5.1. [0-9]"
puts "require 1.3.6.1.4.1.3955.89.108.1.1.6.1. [0-9]"
exit 0;
when "config"
when 'snmpconf'
puts 'require 1.3.6.1.4.1.3955.89.108.1.1.2.1. [0-9]'
puts 'require 1.3.6.1.4.1.3955.89.108.1.1.5.1. [0-9]'
puts 'require 1.3.6.1.4.1.3955.89.108.1.1.6.1. [0-9]'
exit 0
when 'config'
host = $0.match('^(?:|.*\/)snmp_([^_]+)')[1]
puts "host_name #{host}"
puts "graph_title PoE Power Usage"
puts "graph_vlabel Watts"
puts "graph_category sensors"
puts 'graph_title PoE Power Usage'
puts 'graph_vlabel Watts'
puts 'graph_category sensors'
max_current = 0
SNMP::Manager.open(:Host => host,
:Community => community,
:Version => version) do |manager|
SNMP::Manager.open(Host: host,
Community: community,
Version: version) do |manager|
manager.walk([idx_oid, max_oid]) do |row|
puts "iface_#{row[0].value}.label Port #{row[0].value}"
puts "iface_#{row[0].value}.cdef iface_#{row[0].value},1000,/"
puts "iface_#{row[0].value}.line #{row[1].value.to_f / 1000}"
if row[1].value > max_current
max_current = row[1].value
end
max_current = row[1].value if row[1].value > max_current
end
end
puts "graph_args --upper-limit #{max_current.to_f / 1000}"
exit 0
else
host = $0.match('^(?:|.*\/)snmp_([^_]+)')[1]
SNMP::Manager.open(:Host => host,
:Community => community,
:Version => version) do |manager|
SNMP::Manager.open(Host: host,
Community: community,
Version: version) do |manager|
manager.walk([idx_oid, cur_oid]) do |row|
puts "iface_#{row[0].value}.value #{row[1].value}"
end