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

@ -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