device_space_left 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +3 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +15 -0
- data/Rakefile +16 -0
- data/init.rb +2 -0
- data/lib/device_space_left.rb +112 -0
- data/spec/device_space_left/device_space_left_spec.rb +64 -0
- data/spec/spec_helper.rb +4 -0
- metadata +71 -0
data/CHANGELOG.rdoc
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
= DeviceSpaceLeft
|
2
|
+
|
3
|
+
Small ruby gem/plugin giving you the ability to retrieve easily the space available on your devices.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
* DeviseSpaceLeft.percent_used('/home')
|
8
|
+
* DeviseSpaceLeft.percent_free('/dev/md2')
|
9
|
+
* DeviseSpaceLeft.space_available('/dev/md2')
|
10
|
+
* DeviseSpaceLeft.space_used('/dev/md2')
|
11
|
+
* DeviseSpaceLeft.size('/dev/md2')
|
12
|
+
|
13
|
+
Tested on Ubuntu and OsX
|
14
|
+
|
15
|
+
Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
spec_files = Rake::FileList["spec/**/*_spec.rb"]
|
6
|
+
|
7
|
+
|
8
|
+
desc "Run specs for current Rails version"
|
9
|
+
Spec::Rake::SpecTask.new do |t|
|
10
|
+
t.spec_files = spec_files
|
11
|
+
t.spec_opts = lambda {
|
12
|
+
["-c --format specdoc"]
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => :spec
|
data/init.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
module DeviceSpaceLeft
|
2
|
+
|
3
|
+
private
|
4
|
+
|
5
|
+
# Return the df linux command output
|
6
|
+
def self.df
|
7
|
+
`df`
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.parse_df_size(size)
|
11
|
+
return size if !(String === size)
|
12
|
+
|
13
|
+
return (size.to_f * 1024 * 1024 * 1024 * 1024).to_i if size.index('T')
|
14
|
+
return (size.to_f * 1024 * 1024 * 1024).to_i if size.index('G')
|
15
|
+
return (size.to_f * 1024 * 1024).to_i if size.index('M')
|
16
|
+
return (size.to_f * 1024).to_i if size.index('K')
|
17
|
+
return (size.to_i) if size.index('B')
|
18
|
+
|
19
|
+
size.to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return a df infos line corresponding to the filesystem name or to the mount point
|
23
|
+
def self.find_df_infos(mount_name)
|
24
|
+
h = df_hashed
|
25
|
+
return h[mount_name.to_sym] if h[mount_name.to_sym]
|
26
|
+
|
27
|
+
h.each_value do |v|
|
28
|
+
return v if v[:filesystem] == mount_name.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
|
34
|
+
# Return a value or a max looking through the df hash
|
35
|
+
def self.find_value_or_max(value_name, mount_name = nil, max_meth = :max)
|
36
|
+
h = df_hashed
|
37
|
+
|
38
|
+
if mount_name
|
39
|
+
t = find_df_infos(mount_name)
|
40
|
+
return t[value_name] if t
|
41
|
+
end
|
42
|
+
|
43
|
+
max = []
|
44
|
+
h.each_value do |v|
|
45
|
+
max << v[value_name]
|
46
|
+
end
|
47
|
+
|
48
|
+
if max_meth == :sum and max
|
49
|
+
total = 0
|
50
|
+
max.each { |v| total += v }
|
51
|
+
return total
|
52
|
+
end
|
53
|
+
|
54
|
+
max.send(max_meth) || 0
|
55
|
+
end
|
56
|
+
|
57
|
+
public
|
58
|
+
|
59
|
+
# Return the df output in a ruby hash
|
60
|
+
def self.df_hashed
|
61
|
+
h = {}
|
62
|
+
|
63
|
+
df.strip.split("\n").each do |l|
|
64
|
+
if l.size > 10
|
65
|
+
tab = l.strip.split(/ +/)
|
66
|
+
if tab.first != 'Filesystem'
|
67
|
+
size = parse_df_size(tab[1])
|
68
|
+
used = parse_df_size(tab[2])
|
69
|
+
available = parse_df_size(tab[3])
|
70
|
+
percent_used = tab[4].gsub('%', '').to_i
|
71
|
+
|
72
|
+
h[tab.last.to_sym] = {:filesystem => tab[0], :size => size, :used => used, :available => available, :percent_used => percent_used, :mount_on => tab.last}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
return h
|
78
|
+
end
|
79
|
+
|
80
|
+
# Return the percentage of used space on the device named "mount_name"
|
81
|
+
# If mount_name is nil => Return the maximum
|
82
|
+
def self.percent_used(mount_name = nil)
|
83
|
+
find_value_or_max(:percent_used, mount_name)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Return the percentage of free space on "mount_name"
|
87
|
+
# If mount_name is nil => Return the minimum
|
88
|
+
def self.percent_free(mount_name = nil)
|
89
|
+
100 - find_value_or_max(:percent_used, mount_name)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Return the space available on "mount_name"
|
93
|
+
# If mount_name is nil => Return the minimum
|
94
|
+
def self.space_available(mount_name = nil)
|
95
|
+
find_value_or_max(:available, mount_name, :min)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Return the space used on "mount_name"
|
99
|
+
# If mount_name is nil => Return the maximum
|
100
|
+
def self.space_used(mount_name = nil)
|
101
|
+
find_value_or_max(:used, mount_name)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Return the size of "mount_name"
|
105
|
+
# If mount_name is nil => Return the total
|
106
|
+
def self.size(mount_name = nil)
|
107
|
+
find_value_or_max(:size, mount_name, :sum)
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe :device_space_left do
|
4
|
+
|
5
|
+
let(:dsl) { DeviceSpaceLeft }
|
6
|
+
|
7
|
+
it "check if df return something" do
|
8
|
+
dsl.df.should_not be nil
|
9
|
+
dsl.df.size.should > 10
|
10
|
+
end
|
11
|
+
|
12
|
+
context "with stubbed df" do
|
13
|
+
|
14
|
+
before do
|
15
|
+
|
16
|
+
fake_df = <<-EOF
|
17
|
+
|
18
|
+
Filesystem Size Used Avail Use% Mounted on
|
19
|
+
/dev/md1 5.0G 2.0G 2.8G 42% /
|
20
|
+
none 987M 200K 986M 1% /dev
|
21
|
+
none 987M 0 987M 0% /dev/shm
|
22
|
+
none 987M 84K 986M 1% /var/run
|
23
|
+
none 987M 0 987M 0% /var/lock
|
24
|
+
none 987M 0 987M 0% /lib/init/rw
|
25
|
+
/dev/md2 4.1T 882G 3.0T 23% /home
|
26
|
+
EOF
|
27
|
+
|
28
|
+
dsl.stub!(:df).and_return(fake_df)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return correct percent used when device given" do
|
32
|
+
dsl.percent_used('/home').should == 23
|
33
|
+
dsl.percent_used('/dev/md2').should == 23
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return max percent used when no device given" do
|
37
|
+
dsl.percent_used(nil).should == 42
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return free percent" do
|
41
|
+
dsl.percent_free('/home').should == 77
|
42
|
+
dsl.percent_free('/dev/md2').should == 77
|
43
|
+
dsl.percent_free(nil).should == 58
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return correct space available" do
|
47
|
+
dsl.space_available('/home').should == 3.0 * 1024 * 1024 * 1024 * 1024
|
48
|
+
dsl.space_available(nil).should == 986 * 1024 * 1024
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return correct space used" do
|
52
|
+
dsl.space_used('/home').should == 882 * 1024 * 1024 * 1024
|
53
|
+
dsl.space_used(nil).should == 882 * 1024 * 1024 * 1024
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return correct total size" do
|
57
|
+
dsl.size('/home').should == (4.1 * 1024 * 1024 * 1024 * 1024).to_i
|
58
|
+
dsl.size(nil).should == 4518541105561
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: device_space_left
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Guillaume Luccisano
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-09 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: device_space_left is a small plugin giving you the ability to retrieve easily the space available on your devices
|
22
|
+
email: guillaume.luccisano@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/device_space_left.rb
|
31
|
+
- spec/device_space_left/device_space_left_spec.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
- CHANGELOG.rdoc
|
34
|
+
- MIT-LICENSE
|
35
|
+
- Rakefile
|
36
|
+
- README.rdoc
|
37
|
+
- init.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/kwi/device_space_left
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 3
|
61
|
+
- 4
|
62
|
+
version: 1.3.4
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: device_space_left
|
66
|
+
rubygems_version: 1.3.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Small ruby gem/plugin giving you the ability to retrieve easily the space available on your devices
|
70
|
+
test_files: []
|
71
|
+
|