Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utilities] Add #disk_usage #155

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/manageiq/appliance_console/logfile_configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "manageiq/appliance_console/utilities"
require 'linux_admin'
require 'pathname'
require 'fileutils'
require 'util/miq-system.rb'

module ManageIQ
module ApplianceConsole
Expand All @@ -18,7 +18,7 @@ def initialize(config = {})
self.disk = config[:disk]
self.new_logrotate_count = nil

self.size = MiqSystem.disk_usage(LOGFILE_DIRECTORY)[0][:total_bytes]
self.size = Utilities.disk_usage(LOGFILE_DIRECTORY)[0][:total_bytes]
self.current_logrotate_count = /rotate\s+(\d+)/.match(File.read(MIQ_LOGS_CONF))[1]
self.evm_was_running = LinuxAdmin::Service.new("evmserverd").running?
end
Expand Down
44 changes: 44 additions & 0 deletions lib/manageiq/appliance_console/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,50 @@ def self.test_network
say(" " + h + ': ' + (Net::Ping::External.new(h).ping ? 'Success!' : 'Failure, Check network settings and IP address or hostname provided.'))
end
end

def self.disk_usage(file = nil)
file_arg = file
file_arg = "-l" if file.nil? || file == ""

unless file_arg == "-l" || File.exist?(file)
raise "file #{file} does not exist"
end

# Collect bytes
result = AwesomeSpawn.run!("df", :params => ["-T", "-P", file_arg]).output.lines.each_with_object([]) do |line, array|
lArray = line.strip.split(" ")
next if lArray.length != 7
fsname, type, total, used, free, used_percentage, mount_point = lArray
next unless total =~ /[0-9]+/
next if array.detect { |hh| hh[:filesystem] == fsname }

array << {
:filesystem => fsname,
:type => type,
:total_bytes => total.to_i * 1024,
:used_bytes => used.to_i * 1024,
:available_bytes => free.to_i * 1024,
:used_bytes_percent => used_percentage.chomp("%").to_i,
:mount_point => mount_point,
}
end

# Collect inodes
AwesomeSpawn.run!("df", :params => ["-T", "-P", "-i", file_arg]).output.lines.each do |line|
lArray = line.strip.split(" ")
next if lArray.length != 7
fsname, _type, total, used, free, used_percentage, _mount_point = lArray
next unless total =~ /[0-9]+/
h = result.detect { |hh| hh[:filesystem] == fsname }
next if h.nil?

h[:total_inodes] = total.to_i
h[:used_inodes] = used.to_i
h[:available_inodes] = free.to_i
h[:used_inodes_percent] = used_percentage.chomp("%").to_i
end
result
end
end
end
end
2 changes: 1 addition & 1 deletion spec/logfile_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
miq_logs_conf.write(original_miq_logs_conf)
miq_logs_conf.close

allow(MiqSystem).to receive(:disk_usage).and_return([{:total_bytes => "4"}])
allow(ManageIQ::ApplianceConsole::Utilities).to receive(:disk_usage).and_return([{:total_bytes => "4"}])
allow(subject).to receive(:clear_screen)
allow(subject).to receive(:say)
end
Expand Down
125 changes: 125 additions & 0 deletions spec/utilities_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
describe ManageIQ::ApplianceConsole::Utilities do
context ".disk_usage(file)" do
require 'fileutils'

let(:file) { Pathname.new(__dir__).join("empty file").to_s }

before do
FileUtils.touch(file)
end

after do
FileUtils.rm_f(file)
end

it "handles file with a space" do
usage_hash = described_class.disk_usage(file).first
expect(usage_hash[:filesystem]).to be_kind_of String
expect(usage_hash[:filesystem]).to be_present
expect(usage_hash[:total_bytes]).to be > 0
Copy link
Member Author

@NickLaMuro NickLaMuro Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of note: These tests won't pass on MacOS since the versions of df are different, but for now I am just ignoring that problem, as I hope these tests will pass just fine in CI.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the specs for this were completely ripped from here:

https://github.com/ManageIQ/manageiq-gems-pending/blob/fbcaf76d5894407dd1b25f58e4f02faac361428e/spec/util/miq-system_spec.rb#L26-L148

So nothing new is added, besides the omission of the :macosx specific specs.

end
end

context ".disk_usage" do
it "linux" do
linux_df_output_bytes = <<EOF
Filesystem Type 1024-blocks Used Available Capacity Mounted on
/dev/mapper/fedora-root ext4 40185208 5932800 32188024 16% /
devtmpfs devtmpfs 3961576 0 3961576 0% /dev
tmpfs tmpfs 3969532 7332 3962200 1% /dev/shm
tmpfs tmpfs 3969532 1144 3968388 1% /run
tmpfs tmpfs 3969532 0 3969532 0% /sys/fs/cgroup
tmpfs tmpfs 3969532 348 3969184 1% /tmp
/dev/sda1 ext4 487652 131515 326441 29% /boot
/dev/mapper/fedora-home ext4 192360020 9325732 173239936 6% /home
EOF
linux_df_output_inodes = <<EOF
Filesystem Type Inodes IUsed IFree IUse% Mounted on
/dev/mapper/fedora-root ext4 2564096 146929 2417167 6% /
devtmpfs devtmpfs 990394 549 989845 1% /dev
tmpfs tmpfs 992383 31 992352 1% /dev/shm
tmpfs tmpfs 992383 726 991657 1% /run
tmpfs tmpfs 992383 13 992370 1% /sys/fs/cgroup
tmpfs tmpfs 992383 38 992345 1% /tmp
/dev/sda1 ext4 128016 385 127631 1% /boot
/dev/mapper/fedora-home ext4 12222464 488787 11733677 4% /home
EOF
expected = [
{
:filesystem => "/dev/mapper/fedora-root",
:type => "ext4",
:total_bytes => 41149652992,
:used_bytes => 6075187200,
:available_bytes => 32960536576,
:used_bytes_percent => 16,
:mount_point => "/",
:total_inodes => 2564096,
:used_inodes => 146929,
:available_inodes => 2417167,
:used_inodes_percent => 6
},
{
:filesystem => "devtmpfs",
:type => "devtmpfs",
:total_bytes => 4056653824,
:used_bytes => 0,
:available_bytes => 4056653824,
:used_bytes_percent => 0,
:mount_point => "/dev",
:total_inodes => 990394,
:used_inodes => 549,
:available_inodes => 989845,
:used_inodes_percent => 1
},
{
:filesystem => "tmpfs",
:type => "tmpfs",
:total_bytes => 4064800768,
:used_bytes => 7507968,
:available_bytes => 4057292800,
:used_bytes_percent => 1,
:mount_point => "/dev/shm",
:total_inodes => 992383,
:used_inodes => 38,
:available_inodes => 992345,
:used_inodes_percent => 1
},
{
:filesystem => "/dev/sda1",
:type => "ext4",
:total_bytes => 499355648,
:used_bytes => 134671360,
:available_bytes => 334275584,
:used_bytes_percent => 29,
:mount_point => "/boot",
:total_inodes => 128016,
:used_inodes => 385,
:available_inodes => 127631,
:used_inodes_percent => 1
},
{
:filesystem => "/dev/mapper/fedora-home",
:type => "ext4",
:total_bytes => 196976660480,
:used_bytes => 9549549568,
:available_bytes => 177397694464,
:used_bytes_percent => 6,
:mount_point => "/home",
:total_inodes => 12222464,
:used_inodes => 488787,
:available_inodes => 11733677,
:used_inodes_percent => 4
}
]

expect(AwesomeSpawn).to receive(:run!)
.with("df", :params => ["-T", "-P", "-l"])
.and_return(double(:output => linux_df_output_bytes))
expect(AwesomeSpawn).to receive(:run!)
.with("df", :params => ["-T", "-P", "-i", "-l"])
.and_return(double(:output => linux_df_output_inodes))

expect(described_class.disk_usage).to eq(expected)
end
end
end