This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(maint) Add support for *BSD load averages
- Loading branch information
Showing
7 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Bsd | ||
class LoadAverages | ||
FACT_NAME = 'load_averages' | ||
|
||
def call_the_resolver | ||
fact_value = Facter::Resolvers::Bsd::LoadAverages.resolve(:load_averages) | ||
Facter::ResolvedFact.new(FACT_NAME, fact_value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'ffi' | ||
|
||
module Facter | ||
module Bsd | ||
module FfiHelper | ||
module Libc | ||
extend FFI::Library | ||
|
||
ffi_lib 'c' | ||
attach_function :getloadavg, %i[pointer int], :int | ||
end | ||
|
||
def self.read_load_averages | ||
raw_loadavg = FFI::MemoryPointer.new(:double, 3) | ||
|
||
res = Libc.getloadavg(raw_loadavg, 3) | ||
return unless res == 3 | ||
|
||
raw_loadavg.read_array_of_double(res) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
module Resolvers | ||
module Bsd | ||
class LoadAverages < BaseResolver | ||
@semaphore = Mutex.new | ||
@fact_list ||= {} | ||
class << self | ||
private | ||
|
||
def post_resolve(fact_name) | ||
@fact_list.fetch(fact_name) { read_load_averages(fact_name) } | ||
end | ||
|
||
def read_load_averages(fact_name) | ||
require_relative 'ffi/ffi_helper' | ||
@fact_list[:load_averages] = %w[1m 5m 15m].zip(Facter::Bsd::FfiHelper.read_load_averages).to_h | ||
|
||
@fact_list[fact_name] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facts::Bsd::LoadAverages do | ||
describe '#call_the_resolver' do | ||
subject(:fact) { Facts::Bsd::LoadAverages.new } | ||
|
||
let(:value) { { '1m' => 0.01, '5m' => 0.02, '15m' => 0.03 } } | ||
|
||
before do | ||
allow(Facter::Resolvers::Bsd::LoadAverages).to receive(:resolve).with(:load_averages).and_return(value) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Bsd::LoadAverages' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Bsd::LoadAverages).to have_received(:resolve).with(:load_averages) | ||
end | ||
|
||
it 'returns load_averages fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \ | ||
have_attributes(name: 'load_averages', value: value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facter::Bsd::FfiHelper do | ||
let(:averages) { double('FFI::MemoryPointer') } | ||
|
||
before do | ||
allow(FFI::MemoryPointer).to receive(:new).with(:double, 3).and_return(averages) | ||
allow(averages).to receive(:size).and_return(24) | ||
end | ||
|
||
after do | ||
Facter::Resolvers::Bsd::LoadAverages.invalidate_cache | ||
end | ||
|
||
it 'returns load average' do | ||
allow(Facter::Bsd::FfiHelper::Libc).to receive(:getloadavg).and_return(3) | ||
allow(averages).to receive(:read_array_of_double).with(3).and_return([0.19482421875, 0.2744140625, 0.29296875]) | ||
|
||
expect(Facter::Bsd::FfiHelper.read_load_averages).to eq([0.19482421875, 0.2744140625, 0.29296875]) | ||
end | ||
|
||
it 'does not return load average' do | ||
allow(Facter::Bsd::FfiHelper::Libc).to receive(:getloadavg).and_return(-1) | ||
expect(Facter::Bsd::FfiHelper.read_load_averages).to be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facter::Resolvers::Bsd::LoadAverages do | ||
let(:load_averages) { [0.01, 0.02, 0.03] } | ||
|
||
before do | ||
allow(Facter::Bsd::FfiHelper).to receive(:read_load_averages) | ||
.and_return(load_averages) | ||
end | ||
|
||
it 'returns load average' do | ||
result = Facter::Resolvers::Bsd::LoadAverages.resolve(:load_averages) | ||
|
||
expect(result).to eq('15m' => 0.03, '1m' => 0.01, '5m' => 0.02) | ||
end | ||
end |