Class: LoadAverage
- Inherits:
-
Object
- Object
- LoadAverage
- Defined in:
- lib/sensu-plugins-load-checks/load-average.rb
Instance Method Summary collapse
- #cpu_count ⇒ Object
- #exceed?(thresholds) ⇒ Boolean
- #failed? ⇒ Boolean
-
#initialize ⇒ LoadAverage
constructor
A new instance of LoadAverage.
- #load_avg ⇒ Object
- #to_s ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize ⇒ LoadAverage
Returns a new instance of LoadAverage.
2 3 4 5 |
# File 'lib/sensu-plugins-load-checks/load-average.rb', line 2 def initialize @cores = cpu_count @avg = load_avg end |
Instance Method Details
#cpu_count ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/sensu-plugins-load-checks/load-average.rb', line 17 def cpu_count if File.exist?('/proc/cpuinfo') File.read('/proc/cpuinfo').scan(/^processor/).count else `sysctl -n hw.ncpu`.to_i end rescue StandardError 0 end |
#exceed?(thresholds) ⇒ Boolean
31 32 33 |
# File 'lib/sensu-plugins-load-checks/load-average.rb', line 31 def exceed?(thresholds) @avg.zip(thresholds).any? { |a, t| a >= t } end |
#failed? ⇒ Boolean
27 28 29 |
# File 'lib/sensu-plugins-load-checks/load-average.rb', line 27 def failed? @avg.nil? || @cores.zero? end |
#load_avg ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/sensu-plugins-load-checks/load-average.rb', line 7 def load_avg if File.exist?('/proc/loadavg') # linux File.read('/proc/loadavg').split.take(3).map { |a| (a.to_f / @cores).round(2) } rescue nil # rubocop:disable RescueModifier else # fallback for FreeBSD `uptime`.split(' ')[-3..-1].map(&:to_f).map { |a| (a.to_f / @cores).round(2) } rescue nil # rubocop:disable RescueModifier end end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/sensu-plugins-load-checks/load-average.rb', line 35 def to_s @avg.join(', ') end |
#total ⇒ Object
39 40 41 |
# File 'lib/sensu-plugins-load-checks/load-average.rb', line 39 def total @avg.map { |a| (a / @cores) }.join(', ') end |