Skip to content

Commit

Permalink
Ruby plugins: apply code changes as suggested by "rubocop --auto-corr…
Browse files Browse the repository at this point in the history
…ect"
  • Loading branch information
sumpfralle committed Aug 25, 2020
1 parent b0b39b0 commit 809639a
Show file tree
Hide file tree
Showing 33 changed files with 924 additions and 930 deletions.
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Allow "=begin"/"=end" multiline comments in order to protect munin's
# magic markers ("#%#").
Style/BlockComments:
Enabled: false

AllCops:
NewCops: enable
29 changes: 15 additions & 14 deletions plugins/bsd/netstat_bsd_m_
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ This plugin shows various statistics from 'netstat -m'
Required privileges: none
OS:
Supposed: BSD
Tested: FreeBSD 8.2
Supposed: BSD
Tested: FreeBSD 8.2
Author: Artem Sheremet <[email protected]>
#%# family=auto
#%# capabilities=autoconf suggest
=end


# original filename
PLUGIN_NAME = 'netstat_bsd_m_'
PLUGIN_NAME = 'netstat_bsd_m_'.freeze

class String
def escape
self.gsub /[^\w]/, '_'
gsub(/[^\w]/, '_')
end

unless method_defined? :start_with?
Expand All @@ -37,17 +35,20 @@ class String
end

def netstat_m(filter = nil)
Hash[`netstat -m`.split($/).map { |line|
if line =~ /^([\d\/K]+) (.*) \(([\w\/+]+)\)$/
Hash[`netstat -m`.split($/).map do |line|
if line =~ %r{^([\d/K]+) (.*) \(([\w/+]+)\)$}
# 7891K/22385K/30276K bytes allocated to network (current/cache/total)
values, desc, names = $1, $2, $3
[desc, names.split('/').zip(values.split '/')] if filter.nil? or desc.escape == filter
values = Regexp.last_match(1)
desc = Regexp.last_match(2)
names = Regexp.last_match(3)
[desc, names.split('/').zip(values.split('/'))] if filter.nil? || (desc.escape == filter)
elsif line =~ /^(\d+) (.*)$/
# 12327 requests for I/O initiated by sendfile
value, desc = $1, $2
[desc, [[:value, value]]] if filter.nil? or desc.escape == filter
value = Regexp.last_match(1)
desc = Regexp.last_match(2)
[desc, [[:value, value]]] if filter.nil? || (desc.escape == filter)
end
}.compact]
end.compact]
end

stat_name = File.basename($0, '.*').escape
Expand All @@ -74,7 +75,7 @@ when 'config'
CONFIG
puts values.map { |name, _|
esc_name = name.to_s.escape
"#{esc_name}.draw " + if %w(total max).include? name
"#{esc_name}.draw " + if %w[total max].include? name
'LINE'
elsif stack
if first
Expand Down
36 changes: 19 additions & 17 deletions plugins/ejabberd/ejabberd_scanlog
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ require 'yaml'
#

LOG_FILE = ENV['log'] || '/var/log/ejabberd/ejabberd.log'
CACHE_FILE = '/tmp/ejabberd_scanlog_cache' # cache file position
CACHE_FILE = '/tmp/ejabberd_scanlog_cache'.freeze # cache file position

DEFAULT_CACHE = { :start => 0 }
DEFAULT_CACHE = { start: 0 }.freeze

$debug_mode = ARGV.first == 'debug'

Expand All @@ -32,7 +32,7 @@ if $debug_mode
else
begin
log_info = YAML.load IO.read(CACHE_FILE)
rescue
rescue StandardError
log_info = DEFAULT_CACHE
end

Expand All @@ -43,7 +43,7 @@ else
end

if ARGV.first == 'reset'
log_info = { :start => File.size(LOG_FILE) - 1 }
log_info = { start: File.size(LOG_FILE) - 1 }
puts 'Log reset'
end

Expand Down Expand Up @@ -80,17 +80,17 @@ KNOWN_LOG_TYPES = [
['mysql',
'Failed connecting to']],
['Timeout while running a hook',
['ejabberd_hooks',
'timeout']],
%w[ejabberd_hooks
timeout]],
['SQL transaction restarts exceeded',
['SQL transaction restarts exceeded']],
['Unexpected info',
['nexpected info']],
['Other sql_cmd timeout',
['sql_cmd']],
['System limit hit: ports', # check with length(erlang:ports())., set in ejabberdctl config file
['system_limit',
'open_port']],
%w[system_limit
open_port]],
['Other system limit hit', # processes? check with erlang:system_info(process_count)., erlang:system_info(process_limit)., set in ejabberdctl cfg
['system_limit']],
['Generic server terminating',
Expand All @@ -115,28 +115,30 @@ KNOWN_LOG_TYPES = [
['mysql_conn: init failed receiving data']],
['TCP Error',
['Failed TCP']]
]
].freeze

def log_type(text)
KNOWN_LOG_TYPES.find_index { |entry|
KNOWN_LOG_TYPES.find_index do |entry|
entry[1].all? { |substr| text.include? substr }
}
end
end

new_data.split("\n=").each { |report|
new_data.split("\n=").each do |report|
next if report.empty?

report =~ /\A(\w+) REPORT==== (.*) ===\n(.*)\z/m
type, time, text = $1, $2, $3
next unless type and time and text
type = Regexp.last_match(1)
time = Regexp.last_match(2)
text = Regexp.last_match(3)
next unless type && time && text

log_info[type] = (log_info[type] || 0) + 1
if sub_type = log_type(text)
log_info[sub_type] = (log_info[sub_type] || 0) + 1
elsif $debug_mode
warn "Unparsed log entry #{type}: #{text} at #{time}"
end
}
end

log_info[:start] += new_data.size
File.open(CACHE_FILE, 'w') { |f| f.write log_info.to_yaml } unless $debug_mode
Expand All @@ -150,7 +152,7 @@ if ARGV.first == 'config'
CONFIG
end

(KNOWN_LOG_TYPES + %w(ERROR WARNING INFO DEBUG)).each.with_index { |log_type, index|
(KNOWN_LOG_TYPES + %w[ERROR WARNING INFO DEBUG]).each.with_index do |log_type, index|
label, index = if log_type.is_a? Array
[log_type.first, index]
else
Expand All @@ -162,4 +164,4 @@ end
else
puts "T#{index}.value #{log_info[index] or 0}"
end
}
end
37 changes: 18 additions & 19 deletions plugins/http/mongrel_memory
Original file line number Diff line number Diff line change
Expand Up @@ -34,59 +34,58 @@ Contributors: Adam Jacob (<[email protected]>)
=end


module Munin
class MongrelProcessMemory
def run
pid_port_map = get_pids()
port_list = Hash.new
pid_port_map = get_pids
port_list = {}
pid_port_map.sort.each do |pid, port|
rss = `pmap -x #{pid} | grep total`.split(" ")[3]
rss = `pmap -x #{pid} | grep total`.split(' ')[3]
puts "mongrel_#{port}.value #{rss}"
end
end

def get_pids
h = Hash.new
h = {}
pids = []
pids += `pgrep mongrel_rails`.split("\n")
pids += `pgrep ruby`.split("\n")
pids.each { |pid|
pids.each do |pid|
l = `pargs -l #{pid}`
l =~ /-p (\d+)/
h[pid] = $1 if $1
}
h[pid] = Regexp.last_match(1) if Regexp.last_match(1)
end
h
end

def autoconf
get_pids().length > 0
get_pids.length > 0
end
end
end

mpm = Munin::MongrelProcessMemory.new

case ARGV[0]
when "config"
puts "graph_title Mongrel Memory"
puts "graph_vlabel RSS"
puts "graph_category memory"
puts "graph_args --base 1024 -l 0"
puts "graph_scale yes"
puts "graph_info Tracks the size of individual mongrel processes"
when 'config'
puts 'graph_title Mongrel Memory'
puts 'graph_vlabel RSS'
puts 'graph_category memory'
puts 'graph_args --base 1024 -l 0'
puts 'graph_scale yes'
puts 'graph_info Tracks the size of individual mongrel processes'
mpm.get_pids.values.sort.each do |port|
puts "mongrel_#{port}.label mongrel_#{port}"
puts "mongrel_#{port}.info Process memory"
puts "mongrel_#{port}.type GAUGE"
puts "mongrel_#{port}.min 0"
end
when "autoconf"
when 'autoconf'
if mpm.autoconf
puts "yes"
puts 'yes'
exit 0
end
puts "no"
puts 'no'
exit 0
else
mpm.run
Expand Down
51 changes: 24 additions & 27 deletions plugins/http/mongrel_process_memory
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,24 @@ Contributors: Adam Jacob (<[email protected]>)
=end


module Munin
class MongrelProcessMemory
def run
h = get_pids()
ps_output = ""
h = get_pids
ps_output = ''
# I have no doubt that this is a terrible way of doing this.
h.each do |k, v|
ps_output = ps_output + `ps --no-heading l #{k}`
h.each do |k, _v|
ps_output += `ps --no-heading l #{k}`
end

if ps_output
port_list = Hash.new
port_list = {}
ps_output.each_line do |l|
if l =~ /-p (\d+)/
port = $1
l_ary = l.split(/\s+/)
if l_ary.length > 6
port_list[port] = l_ary[7].to_i * 1024
end
end
next unless l =~ /-p (\d+)/

port = Regexp.last_match(1)
l_ary = l.split(/\s+/)
port_list[port] = l_ary[7].to_i * 1024 if l_ary.length > 6
end
port_list.sort.each do |port|
puts "mongrel_#{port[0]}.value #{port[1]}"
Expand All @@ -58,14 +55,14 @@ module Munin
end

def get_pids
h = Hash.new
h = {}
pids = []
pids = `pgrep mongrel_rails`
pids.each { |p|
pids.each do |p|
l = `ps #{p}`
l =~ /-p (\d+)/
h[p] = $1
}
h[p] = Regexp.last_match(1)
end
h
end

Expand All @@ -78,25 +75,25 @@ end
mpm = Munin::MongrelProcessMemory.new

case ARGV[0]
when "config"
puts "graph_title Mongrel Memory"
puts "graph_vlabel RSS"
puts "graph_category memory"
puts "graph_args --base 1024 -l 0"
puts "graph_scale yes"
puts "graph_info Tracks the size of individual mongrel processes"
when 'config'
puts 'graph_title Mongrel Memory'
puts 'graph_vlabel RSS'
puts 'graph_category memory'
puts 'graph_args --base 1024 -l 0'
puts 'graph_scale yes'
puts 'graph_info Tracks the size of individual mongrel processes'
mpm.get_pids.values.sort.each do |port|
puts "mongrel_#{port}.label mongrel_#{port}"
puts "mongrel_#{port}.info Process memory"
puts "mongrel_#{port}.type GAUGE"
puts "mongrel_#{port}.min 0"
end
when "autoconf"
when 'autoconf'
if mpm.autoconf
puts "yes"
puts 'yes'
exit 0
end
puts "no"
puts 'no'
exit 0
else
mpm.run
Expand Down
Loading

0 comments on commit 809639a

Please sign in to comment.