mool 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mool.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 g.edera
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,326 @@
1
+ # Mool
2
+ **Mool** aims to be as flexible as possible while helping you with powerful components to get all operative system information, such as **CPU**, **Load-Average**, **Disks**, **Memory**, **Service** and **Process**.
3
+
4
+ The best thing to have this gem is how get the information. The information is obtained from different source but mainly from files and basic commands such as **"top"**, **"mpstat"** and **"df"**.
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+ ```ruby
9
+ gem 'mool'
10
+ ```
11
+ And then execute:
12
+ ```console
13
+ $ bundle
14
+ ```
15
+ Or install it yourself as:
16
+ ```console
17
+ $ gem install mool
18
+ ```
19
+
20
+ ## Usage
21
+ ### CPU
22
+ You can check the posible values to use with:
23
+ ```ruby
24
+ > MoolCpu::PROCESSORS
25
+ ["0", "1", "2", "4", "all"]
26
+ ```
27
+ It's posible get all cpu information:
28
+ ```ruby
29
+ > MoolCpu.all
30
+ [
31
+ [0] #<MoolCpu:0x7f82959381a8 @nice=0.0, @gnice=0.0, @total=2.0, @irq=0.0, @usr=2.0, @guest=0.0, @iowait=0.0, @cores=2, @steal=0.0, @sys=0.0, @model_name="Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz", @idle=97.0, @cpu_name="cpu_3", @soft=0.0>,
32
+ [1] #<MoolCpu:0x7f8295937ca8 @nice=0.0, @gnice=0.0, @total=1.0, @irq=0.0, @usr=1.0, @guest=0.0, @iowait=0.0, @cores=2, @steal=0.0, @sys=0.0, @model_name="Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz", @idle=97.0, @cpu_name="cpu_2", @soft=0.0>,
33
+ [2] #<MoolCpu:0x7f82959377a8 @nice=0.0, @gnice=0.0, @total=3.0, @irq=0.0, @usr=1.0, @guest=0.0, @iowait=2.0, @cores=2, @steal=0.0, @sys=0.0, @model_name="Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz", @idle=96.0, @cpu_name="cpu_1", @soft=0.0>,
34
+ [3] #<MoolCpu:0x7f82959372a8 @nice=0.0, @gnice=0.0, @total=4.0, @irq=0.0, @usr=3.0, @guest=0.0, @iowait=0.0, @cores=2, @steal=0.0, @sys=1.0, @model_name="Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz", @idle=95.0, @cpu_name="cpu_0", @soft=0.0>,
35
+ [4] #<MoolCpu:0x7f8295936da8 @nice=0.0, @gnice=0.0, @total=2.0, @irq=0.0, @usr=2.0, @guest=0.0, @iowait=0.0, @cores=0, @steal=0.0, @sys=0.0, @model_name=nil, @idle=96.0, @cpu_name="cpu_all", @soft=0.0>
36
+ ]
37
+ ```
38
+ Or can get a specific cpu information:
39
+ ```ruby
40
+ >> MoolCpu.new(0) or MoolCpu.new("0")
41
+ #<MoolCpu:0x7f82959381a8 @nice=0.0, @gnice=0.0, @total=2.0, @irq=0.0, @usr=2.0, @guest=0.0, @iowait=0.0, @cores=2, @steal=0.0, @sys=0.0, @model_name="Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz", @idle=97.0, @cpu_name="cpu_3", @soft=0.0>
42
+ ```
43
+ ### Load-Average
44
+ ```ruby
45
+ >> MoolLoadAverage.new
46
+ #<MoolLoadAverage:0x7f8295931c90 @total_thread_entities=638, @current_loadavg=0.08, @thread_entities_exec=2, @last_15min_loadavg=0.13, @last_pid_process_created=6264, @last_5min_loadavg=0.07>
47
+ ```
48
+ ### Memory
49
+ ```ruby
50
+ >> MoolMemory.new
51
+ --- !ruby/object:MoolMemory
52
+ active: 2906775552.0
53
+ active_anon: 2175971328.0
54
+ active_file: 730804224.0
55
+ anon_huge_pages: 0.0
56
+ anon_pages: 2174132224.0
57
+ bounce: 0.0
58
+ buffers: 73887744.0
59
+ cached: 1795256320.0
60
+ commit_limit: 7322968064.0
61
+ committed_as: 8012181504.0
62
+ direct_map2_m: 12499025920.0
63
+ direct_map4k: 280199168.0
64
+ dirty: 217088.0
65
+ hardware_corrupted: 0.0
66
+ huge_pages_free: 0.0
67
+ huge_pages_rsvd: 0.0
68
+ huge_pages_surp: 0.0
69
+ huge_pages_total: 0.0
70
+ hugepagesize: 2097152.0
71
+ inactive: 1136472064.0
72
+ inactive_anon: 573755392.0
73
+ inactive_file: 562716672.0
74
+ kernel_stack: 10485760.0
75
+ mapped: 692449280.0
76
+ mem_available: 9536376832.0
77
+ mem_free: 8219561984.0
78
+ mem_total: 12498477056.0
79
+ mem_used: 2409771008.0
80
+ mlocked: 4096.0
81
+ nfs_unstable: 0.0
82
+ page_tables: 47812608.0
83
+ s_reclaimable: 76697600.0
84
+ s_unreclaim: 34598912.0
85
+ shmem: 575635456.0
86
+ slab: 111296512.0
87
+ swap_cached: 0.0
88
+ swap_free: 1073729536.0
89
+ swap_total: 1073729536.0
90
+ unevictable: 4096.0
91
+ unity: Bytes
92
+ vmalloc_chunk: 35183562584064.0
93
+ vmalloc_total: 35184372087808.0
94
+ vmalloc_used: 381046784.0
95
+ writeback: 0.0
96
+ writeback_tmp: 0.0
97
+ ```
98
+ By default the values are in Bytes. So it's posible to changed to different units (Bytes Kbytes, Mbytes, Gbytes).
99
+ ```ruby
100
+ >> MoolMemory.new.to_b
101
+ >> MoolMemory.new.to_kb
102
+ >> MoolMemory.new.to_mb
103
+ >> MoolMemory.new.to_gb
104
+ ```
105
+ ## Service or Process
106
+ To get process information you can provide two params:
107
+ 1) **name**: This name it's used as key.
108
+ 2) **pattern**: Used to match with the command top.
109
+ ```ruby
110
+ >> MoolService.new("profanity Process", "profanity")
111
+ #<MoolService:0x7f4c23f929e8 @messure=[{ :status=>"Sleeping",
112
+ :command=>"profanity",
113
+ :time=>"0:08.22",
114
+ :pattern=>"profanity",
115
+ :nice=>"0",
116
+ :pid=>"1764",
117
+ :memory_in_kb=>"34376",
118
+ :user=>"mool",
119
+ :cpu_percentage=>"0,0",
120
+ :name=>"profanity Process",
121
+ :mem_percentage=>"0,3",
122
+ :priority=>"20" }]>
123
+ ```
124
+
125
+ In this case we have only one messure, but exists especial cases, where the pattern match with more than one process. This cases will have more than one messure, such as:
126
+ ```ruby
127
+ >> MoolService.new("Terminal", "urxvt")
128
+ #<MoolService:0x7f4c23f88f88 @messure=[ { :status=>"Sleeping",
129
+ :command=>"urxvt",
130
+ :time=>"0:02.80",
131
+ :pattern=>"urxvt",
132
+ :nice=>"0",
133
+ :pid=>"1672",
134
+ :memory_in_kb=>"16152",
135
+ :user=>"mool",
136
+ :cpu_percentage=>"0,0",
137
+ :name=>"Terminal",
138
+ :mem_percentage=>"0,1",
139
+ :priority=>"20" },
140
+ { :status=>"Sleeping",
141
+ :command=>"urxvt",
142
+ :time=>"0:00.00",
143
+ :pattern=>"urxvt",
144
+ :nice=>"0",
145
+ :pid=>"1673",
146
+ :memory_in_kb=>"4020",
147
+ :user=>"mool",
148
+ :cpu_percentage=>"0,0",
149
+ :name=>"Terminal",
150
+ :mem_percentage=>"0,0",
151
+ :priority=>"20" } ]>
152
+ ```
153
+ ### Disk
154
+ It's possible to get disk, partition or virtual device information using dev name **MAJOR:MINOR** ("**8:2**"), device name **sda**, **sda1** or virtual device such as "**lvm-sda2**" or "**md0**".
155
+ ```ruby
156
+ >> MoolDisk.new("8:0")
157
+ #<MoolDisk:0x7fdc6e283f00 @logical_name="sda", @total_block=500107862016.0, @devtype="disk", @mount_point="/boot", @swap=false, @minor="0", @devname="sda", @block_free=0.0, @path="/sys/dev/block/8:0", @major="8", @file_system="ext4", @unity="Bytes", @block_used=0.0>
158
+ >> MoolDisk.new("sda1")
159
+ #<MoolDisk:0x7fdc6e266a40 @logical_name="sda1", @total_block=262144000.0, @devtype="partition", @mount_point="/boot", @swap=false, @minor="1", @devname="sda1", @block_free=59062784.0, @path="/sys/dev/block/8:1", @major="8", @file_system="ext4", @unity="Bytes", @block_used=57123840.0>
160
+ >> MoolDisk.new("sdblvm-homelvm")
161
+ #<MoolDisk:0x7fdc6e248658 @logical_name="sdblvm-homelvm", @total_block=445602856960.0, @devtype="disk", @mount_point="/boot", @swap=false, @minor="3", @devname="dm-3", @block_free=187429210112.0, @path="/sys/dev/block/252:3", @major="252", @file_system="btrfs", @unity="Bytes", @block_used=34269296640.0>
162
+ ```
163
+ It's possible get all partition if the object is `@devtype="disk"`, such as:
164
+ ```ruby
165
+ >> MoolDisk.new("sda").partitions
166
+ [
167
+ [0] #<MoolDisk:0x7fdc6eb3cd18 @logical_name="sda1", @total_block=262144000.0, @devtype="partition", @mount_point="/boot", @swap=false, @minor="1", @devname="sda1", @block_free=59062784.0, @path="/sys/dev/block/8:1", @major="8", @file_system="ext4", @unity="Bytes", @block_used=57123840.0>,
168
+ [1] #<MoolDisk:0x7fdc6e9e7788 @logical_name="sda2", @total_block=1073741824.0, @devtype="partition", @swap=true, @minor="2", @devname="sda2", @block_free=0.0, @path="/sys/dev/block/8:2", @major="8", @file_system="cgroup", @unity="Bytes", @block_used=0.0>,
169
+ [2] #<MoolDisk:0x7fdc6e8d7f78 @logical_name="sda3", @total_block=498770927616.0, @devtype="partition", @swap=false, @minor="3", @devname="sda3", @block_free=0.0, @path="/sys/dev/block/8:3", @major="8", @file_system=nil, @unity="Bytes", @block_used=0.0>
170
+ ]
171
+ ```
172
+ Otherwise it's possible too get the slaves. The slaves are virtual devices for examples `lvm` or `raid`.
173
+
174
+ ```ruby
175
+ >> MoolDisk.new("sda3").slaves
176
+ [
177
+ [0] #<MoolDisk:0x7fdc6e6bec00 @logical_name="sdblvm-rootlvm", @total_block=32212254720.0, @devtype="disk", @swap=false, @minor="0", @devname="dm-0", @block_free=9739984896.0, @path="/sys/dev/block/252:0", @major="252", @file_system="ext4", @unity="Bytes", @block_used=5232629760.0>,
178
+ [1] #<MoolDisk:0x7fdc6e672350 @logical_name="sdblvm-tmplvm", @total_block=4294967296.0, @devtype="disk", @mount_point="/boot", @swap=false, @minor="1", @devname="dm-1", @block_free=1926668288.0, @path="/sys/dev/block/252:1", @major="252", @file_system="ext4", @unity="Bytes", @block_used=4227072.0>,
179
+ [2] #<MoolDisk:0x7fdc6e627ff8 @logical_name="sdblvm-varlvm", @total_block=16106127360.0, @devtype="disk", @mount_point="/boot", @swap=false, @minor="2", @devname="dm-2", @block_free=5497151488.0, @path="/sys/dev/block/252:2", @major="252", @file_system="ext4", @unity="Bytes", @block_used=1951399936.0>,
180
+ [3] #<MoolDisk:0x7fdc6e5cec78 @logical_name="sdblvm-homelvm", @total_block=445602856960.0, @devtype="disk", @mount_point="/boot", @swap=false, @minor="3", @devname="dm-3", @block_free=187430152192.0, @path="/sys/dev/block/252:3", @major="252", @file_system="btrfs", @unity="Bytes", @block_used=34268366848.0>
181
+ ]
182
+ ```
183
+
184
+ Other way is get all disk with yours parititons and slaves.
185
+ ```ruby
186
+ >> MoolDisk.all
187
+ - !ruby/object:MoolDisk
188
+ block_free: 0.0
189
+ block_used: 0.0
190
+ devname: sda
191
+ evtype: disk
192
+ file_system: ext4
193
+ logical_name: sda
194
+ major: "8"
195
+ minor: "0"
196
+ mount_point: /boot
197
+ swap: false
198
+ total_block: 500107862016.0
199
+ unity: Bytes
200
+ partitions:
201
+ - !ruby/object:MoolDisk
202
+ block_free: 59062784.0
203
+ block_used: 57123840.0
204
+ devname: sda1
205
+ devtype: partition
206
+ file_system: ext4
207
+ logical_name: sda1
208
+ major: "8"
209
+ minor: "1"
210
+ mount_point: /boot
211
+ partitions: []
212
+ path: /sys/dev/block/8:1
213
+ slaves: []
214
+ swap: false
215
+ total_block: 262144000.0
216
+ unity: Bytes
217
+ - !ruby/object:MoolDisk
218
+ block_free: 0.0
219
+ block_used: 0.0
220
+ devname: sda2
221
+ devtype: partition
222
+ file_system: cgroup
223
+ logical_name: sda2
224
+ major: "8"
225
+ minor: "2"
226
+ partitions: []
227
+ path: /sys/dev/block/8:2
228
+ slaves: []
229
+ swap: true
230
+ total_block: 1073741824.0
231
+ unity: Bytes
232
+ - !ruby/object:MoolDisk
233
+ block_free: 0.0
234
+ block_used: 0.0
235
+ devname: sda3
236
+ devtype: partition
237
+ file_system:
238
+ logical_name: sda3
239
+ major: "8"
240
+ minor: "3"
241
+ partitions: []
242
+ path: /sys/dev/block/8:3
243
+ slaves:
244
+ - !ruby/object:MoolDisk
245
+ block_free: 9739984896.0
246
+ block_used: 5232629760.0
247
+ devname: dm-0
248
+ devtype: disk
249
+ file_system: ext4
250
+ logical_name: sdblvm-rootlvm
251
+ major: "252"
252
+ minor: "0"
253
+ path: /sys/dev/block/252:0
254
+ swap: false
255
+ total_block: 32212254720.0
256
+ unity: Bytes
257
+ - !ruby/object:MoolDisk
258
+ block_free: 1926668288.0
259
+ block_used: 4227072.0
260
+ devname: dm-1
261
+ devtype: disk
262
+ file_system: ext4
263
+ logical_name: sdblvm-tmplvm
264
+ major: "252"
265
+ minor: "1"
266
+ mount_point: /boot
267
+ path: /sys/dev/block/252:1
268
+ swap: false
269
+ total_block: 4294967296.0
270
+ unity: Bytes
271
+ - !ruby/object:MoolDisk
272
+ block_free: 5497149440.0
273
+ block_used: 1951401984.0
274
+ devname: dm-2
275
+ devtype: disk
276
+ file_system: ext4
277
+ logical_name: sdblvm-varlvm
278
+ major: "252"
279
+ minor: "2"
280
+ mount_point: /boot
281
+ path: /sys/dev/block/252:2
282
+ swap: false
283
+ total_block: 16106127360.0
284
+ unity: Bytes
285
+ - !ruby/object:MoolDisk
286
+ block_free: 187430135808.0
287
+ block_used: 34268346368.0
288
+ devname: dm-3
289
+ devtype: disk
290
+ file_system: btrfs
291
+ logical_name: sdblvm-homelvm
292
+ major: "252"
293
+ minor: "3"
294
+ mount_point: /boot
295
+ path: /sys/dev/block/252:3
296
+ swap: false
297
+ total_block: 445602856960.0
298
+ unity: Bytes
299
+ swap: false
300
+ total_block: 498770927616.0
301
+ unity: Bytes
302
+ path: /sys/dev/block/8:0
303
+ slaves: []
304
+ ```
305
+
306
+ Swap partition:
307
+
308
+ ```ruby
309
+ >> MoolDisk.swap
310
+ #<MoolDisk:0x7f711644d890 @file_system="cgroup", @unity="Bytes", @block_used=0.0, @logical_name="sda2", @total_block=1073741824.0, @minor="2", @devtype="partition", @path="/sys/dev/block/8:2", @swap=true, @major="8", @devname="sda2", @block_free=1073741824.0>
311
+ ```
312
+
313
+ ### Version
314
+ 1.0.1
315
+
316
+ License
317
+ ----
318
+ MIT
319
+
320
+ ## Contributing
321
+
322
+ 1. Fork it
323
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
324
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
325
+ 4. Push to the branch (`git push origin my-new-feature`)
326
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ en:
2
+ process:
3
+ status:
4
+ uninterruptible_sleep: 'Uninterruptible sleep'
5
+ running: 'Running'
6
+ sleeping: 'Sleeping'
7
+ traced_or_stopped: 'Stopped'
8
+ zombie: 'Zombie'
@@ -0,0 +1,8 @@
1
+ es:
2
+ process:
3
+ status:
4
+ uninterruptible_sleep: 'Descanso interrumpido'
5
+ running: 'Corriendo'
6
+ sleeping: 'Durmiendo'
7
+ traced_or_stopped: 'Terminado'
8
+ zombie: 'Zombie'
data/lib/mool/cpu.rb ADDED
@@ -0,0 +1,56 @@
1
+ class MoolCpu
2
+ PATH_PROC_CPUINFO = "/proc/cpuinfo"
3
+ PROCESSORS = File.read(PATH_PROC_CPUINFO).scan(/processor\t*: (\d+)/).flatten + ["all"]
4
+
5
+ attr_reader :cpu_name, :model_name, :cores, :usr, :nice, :sys, :iowait, :irq, :soft, :steal, :guest, :gnice, :idle, :total
6
+
7
+ # ["all", "1", "2"]
8
+ def initialize(process_number, opt={})
9
+ raise "Cpu name incorrect!. Posible values: #{MoolCpu::PROCESSORS.join(",")}" unless MoolCpu::PROCESSORS.include?(process_number.to_s)
10
+ result = opt.empty? ? MoolCpu.cpu_info[process_number.to_s] : opt
11
+ @cpu_name = "cpu_#{process_number.to_s}"
12
+ @model_name = result["model_name"]
13
+ @cores = result["cpu_cores"].to_i
14
+ @usr = result["%usr"].to_f
15
+ @nice = result["%nice"].to_f
16
+ @sys = result["%sys"].to_f # This is kernel %
17
+ @iowait = result["%iowait"].to_f
18
+ @irq = result["%irq"].to_f
19
+ @soft = result["%soft"].to_f
20
+ @steal = result["%steal"].to_f
21
+ @guest = result["%guest"].to_f
22
+ @gnice = result["%gnice"].to_f
23
+ @idle = result["%idle"].to_f
24
+ @total = @usr + @nice + @sys + @iowait + @irq + @soft + @steal + @guest
25
+ end
26
+
27
+ def self.cpu_info
28
+ cpu_info = {}
29
+
30
+ mpstat = File.read("|mpstat -P ALL 1 1").split("\n\n")[2].split("\n").map{|i| i.gsub(/^\S+:/, '').strip.split(/\s+/) }
31
+ mpstat_vars = mpstat.shift
32
+ mpstat_vars.shift
33
+ mpstat.each do |data|
34
+ res = {}
35
+ core_name = data.shift
36
+ data.each_with_index { |d, i| res.merge!(mpstat_vars[i] => d) }
37
+ cpu_info.merge!(core_name => res)
38
+ end
39
+
40
+ File.read(PATH_PROC_CPUINFO).gsub(/([^\n])\n([^\n])/, '\1 \2').scan(/processor\t*: (\d+).*model name\t*: (.*) stepping.*cpu cores\t*: (\d+)/).each do |v|
41
+ cpu_info[v[0]]["model_name"] = v[1]
42
+ cpu_info[v[0]]["cpu_cores"] = v[2]
43
+ end
44
+
45
+ cpu_info
46
+ end
47
+
48
+ def self.processors
49
+ File.read(PATH_PROC_CPUINFO).scan(/processor\t*: (\d+)/).flatten + ["all"]
50
+ end
51
+
52
+
53
+ def self.all
54
+ MoolCpu.cpu_info.map{ |key, value| MoolCpu.new(key, value) }
55
+ end
56
+ end
data/lib/mool/disk.rb ADDED
@@ -0,0 +1,117 @@
1
+ class MoolDisk
2
+ PATH_DEV_BLOCK = Dir.glob('/sys/dev/block/*')
3
+
4
+ attr_accessor :path, :major, :minor, :devname, :devtype, :size, :swap, :mount_point, :file_system, :total_block, :block_used, :block_free, :partitions, :slaves, :unity
5
+
6
+ def initialize(dev_name)
7
+ @path = PATH_DEV_BLOCK.select{ |entry| ( File.read("#{entry}/dev").include?(dev_name)) ||
8
+ ( File.read("#{entry}/uevent").include?(dev_name)) ||
9
+ ( File.exist?("#{entry}/dm/name") &&
10
+ File.read("#{entry}/dm/name").include?(dev_name)) }.first
11
+ raise "Does't exist #{dev_name}!" if @path.nil?
12
+ read_uevent
13
+ logical_name
14
+ swap
15
+ capacity
16
+ @unity = Mool::BYTES
17
+ mounting_point
18
+ file_system
19
+ end
20
+
21
+ def mounting_point
22
+ @mount_point ||= File.read("/proc/mounts").scan(/#{@logical_name} (\S+)/).flatten.first if File.read("/proc/mounts").include?(@logical_name)
23
+ end
24
+
25
+ def file_system
26
+ @file_system ||= (Dir.glob("/sys/fs/**/*").select{|a| a.include?(@devname)}.first.split("/")[3] rescue nil)
27
+ end
28
+
29
+ def logical_name
30
+ @logical_name ||= File.exists?("#{@path}/dm/name")? File.read("#{@path}/dm/name").chomp : @devname
31
+ end
32
+
33
+ def read_uevent
34
+ @major, @minor, @devname, @devtype = File.read("#{@path}/uevent").scan(/.*=(\d+)\n.*=(\d+)\n.*=(\S+)\n.*=(\w+)\n/).flatten
35
+ end
36
+
37
+ def dev; @major + @minor; end
38
+
39
+ def is_disk?; @devtype == "disk"; end
40
+
41
+ def is_partition?; @devtype == "partition"; end
42
+
43
+ def swap; @swap ||= File.read("/proc/swaps")[/#{@logical_name} /].present?; end
44
+
45
+ def capacity
46
+ unless (defined?(@total_block) && defined?(@block_used) && defined?(@block_free))
47
+ result = `df`.scan(/(#{@logical_name}|#{@devname})\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/).flatten
48
+ @total_block = File.read("#{@path}/size").chomp.to_f * Mool::BLOCK_SIZE
49
+ @block_used = result[2].to_f * Mool::BLOCK_SIZE
50
+ @block_free = @total_block - @block_used
51
+ end
52
+ end
53
+
54
+ def partitions
55
+ unless defined? @partitions
56
+ @partitions = []
57
+ if is_disk?
58
+ Dir.glob("#{@path}/#{@devname}*").each do |part|
59
+ @partitions << MoolDisk.new(part.split("/").last)
60
+ end
61
+ end
62
+ end
63
+ @partitions
64
+ end
65
+
66
+ def slaves
67
+ unless defined? @slaves
68
+ @slaves = []
69
+ PATH_DEV_BLOCK.select{ |entry| File.directory?("#{entry}/slaves/#{@devname}") }.each do |slave|
70
+ @slaves << MoolDisk.new(slave.split("/").last)
71
+ end
72
+ end
73
+ @slaves
74
+ end
75
+
76
+ def used_percent
77
+ @block_used / @total_block
78
+ end
79
+
80
+ def free_percent
81
+ @block_free / @total_block
82
+ end
83
+
84
+ def self.all
85
+ disks = []
86
+
87
+ PATH_DEV_BLOCK.each do |entry|
88
+ real_path = `readlink -f #{entry}`.chomp
89
+ disks << MoolDisk.new(entry.split("/").last) if (not real_path.include?("virtual")) &&
90
+ (not real_path.include?("/sr")) &&
91
+ (not File.exist?("#{real_path}/partition")) &&
92
+ Dir.glob("#{real_path}/slaves/*").empty?
93
+ end
94
+
95
+ disks.each{ |disk| disk.partitions.each { |part| part.partitions and part.slaves }}
96
+ disks.each{ |disk| disk.slaves.each { |part| part.partitions and part.slaves }}
97
+ disks
98
+ end
99
+
100
+ def to_b; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::BYTES); end
101
+ def to_kb; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::KBYTES); end
102
+ def to_mb; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::MBYTES); end
103
+ def to_gb; Mool.parse_to(self, ["@total_block", "@block_used", "@block_free"], Mool::GBYTES); end
104
+
105
+ def self.swap
106
+ result = File.read("/proc/swaps").scan(/.*\n\/dev\/(\S+)/).flatten.first
107
+ MoolDisk.new(result) unless result.nil?
108
+ end
109
+
110
+ def self.all_usable
111
+ result = MoolDisk.all
112
+ result.each do |disk|
113
+ result += (disk.partitions + disk.slaves + (disk.partitions + disk.slaves).collect{|p| p.partitions + p.slaves }.flatten)
114
+ end
115
+ result.reject(&:blank?).select{|d| (d.partitions + d.slaves).blank? }
116
+ end
117
+ end
@@ -0,0 +1,13 @@
1
+ class MoolLoadAverage
2
+ attr_reader :current_loadavg, :last_5min_loadavg, :last_15min_loadavg, :thread_entities_exec, :total_thread_entities, :last_pid_process_created
3
+
4
+ def initialize
5
+ result = File.read("/proc/loadavg").chomp.split(" ")
6
+ @current_loadavg = result[0].to_f
7
+ @last_5min_loadavg = result[1].to_f
8
+ @last_15min_loadavg = result[2].to_f
9
+ @thread_entities_exec = result[3].split("/").first.to_i # Currently executing kernel scheduling entities
10
+ @total_thread_entities = result[3].split("/").last.to_i # Number of kernel scheduling entities that currently exist on the system
11
+ @last_pid_process_created = result[4].to_i
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ class MoolMemory
2
+ PATH_PROC_MEMORY = "/proc/meminfo"
3
+
4
+ attr_accessor :unity, :mem_used
5
+
6
+ def initialize()
7
+ File.read(PATH_PROC_MEMORY).scan(/(\S+):\s+(\d+)/).each do |meminfo|
8
+ var = meminfo[0].gsub("(", "_").gsub(")", "").underscore
9
+ instance_variable_set("@#{var}", (meminfo[1].to_f * Mool::PARSE_TYPES[Mool::KBYTES]).round(2))
10
+ class_eval{attr_accessor var.to_sym}
11
+ end
12
+ @unity = Mool::BYTES
13
+ @mem_used = @mem_total - (@mem_free + @cached + @buffers + @swap_cached )
14
+ end
15
+
16
+ def to_b; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::BYTES); end
17
+ def to_kb; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::KBYTES); end
18
+ def to_mb; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::MBYTES); end
19
+ def to_gb; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::GBYTES); end
20
+
21
+ end
@@ -0,0 +1,80 @@
1
+ class MoolService
2
+ STATUS_PROCESS = { "D" => I18n.t("process.status.uninterruptible_sleep"),
3
+ "R" => I18n.t("process.status.running"),
4
+ "S" => I18n.t("process.status.sleeping"),
5
+ "T" => I18n.t("process.status.traced_or_stopped"),
6
+ "Z" => I18n.t("process.status.zombie") }
7
+
8
+ attr_reader :messure, :pattern
9
+
10
+ def initialize(name, pattern, opt={})
11
+ raise "Please only use string types!" if (name.class != String or pattern.class != String)
12
+ @messure = []
13
+ @pattern = pattern
14
+
15
+ result = opt[:result] || MoolService.services_status([{:name => name, :pattern => pattern}])[name]
16
+
17
+ result.each do |res|
18
+ #pid,user,pcpu,pmem,rss,priority,args,nice, memory_in_kb, status, cpu_percetage, men_percentage, time
19
+ @messure << { :name => name,
20
+ :pattern => pattern,
21
+ :pid => res[0],
22
+ :user => res[1],
23
+ :cpu_average => res[2],
24
+ :mem_average => res[3],
25
+ :resident_set_size => res[4],
26
+ :priority => res[5],
27
+ :time => res[6],
28
+ :status => MoolService::STATUS_PROCESS[res[7]],
29
+ :nice => res[8],
30
+ :args => res[9],
31
+ :memory_in_kb => res[10],
32
+ :cpu_instant => (res[11] || 0),
33
+ :mem_instant => (res[12] || 0.0)}
34
+ end
35
+ end
36
+
37
+
38
+ def self.all(services)
39
+ raise "Please only use Array type!" if services.class != Array
40
+ _services = {}
41
+
42
+ services_data = MoolService.services_status services
43
+
44
+ services.each do |service|
45
+ _services[service[:name]] = MoolService.new(service[:name],
46
+ service[:pattern],
47
+ { :result => services_data[service[:name]] })
48
+ end
49
+ _services
50
+ end
51
+
52
+ def self.services_status services
53
+ command_ps = MoolService.ps
54
+ command_top = MoolService.top
55
+ result = {}
56
+ services.each do |service|
57
+ result[service[:name]] = MoolService.ps_parser(command_ps, service[:pattern]).collect{ |data| data += MoolService.top_parser(command_top, data[0]) }
58
+ end
59
+ result
60
+ end
61
+
62
+ def self.ps; `ps --no-headers -o pid,user,pcpu,pmem,rss,priority,time,stat,nice,args -A`; end
63
+
64
+ def self.top; `top -c -b -n1`; end
65
+
66
+ private
67
+
68
+ def self.ps_parser(command, pattern)
69
+ pattern = pattern.gsub('/','\/')
70
+ # pid,user,pcpu,pmem,rss,priority,time,stat,nice,args
71
+ command.scan(/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S)\S*\s+(\S+)\s+.*(#{pattern}).*\n/)
72
+ end
73
+
74
+ def self.top_parser(command, pid)
75
+ # memory_in_kb, cpu_percetage, men_percentage
76
+ # command.scan(/[\s+]#{pid}\s+\S+\s+\S+\s+(\S+)\s+\S+\s+(\S+)\s+\S+\s+(\S)\s+(\S+)\s+(\S+)\s+(\S+)\s+.*/)
77
+ result = command.scan(/#{pid}\s+\S+\s+\S+\s+\S+\s+(\S+)\s+\S+\s+\S+\s+\S+\s+(\S+)\s+(\S+)\s+\S+\s+\S+/).flatten
78
+ end
79
+
80
+ end
@@ -0,0 +1,3 @@
1
+ module Mool
2
+ VERSION = "1.0.1"
3
+ end
data/lib/mool.rb ADDED
@@ -0,0 +1,31 @@
1
+ mydir = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require 'i18n'
4
+ I18n.load_path += Dir[File.join(mydir, 'locales', '*.yml')]
5
+
6
+ require 'mool/version'
7
+ require 'mool/service'
8
+ require 'mool/cpu'
9
+ require 'mool/disk'
10
+ require 'mool/memory'
11
+ require 'mool/load_average'
12
+
13
+
14
+ module Mool
15
+ BLOCK_SIZE = 512
16
+ BYTES = "Bytes"
17
+ KBYTES = "KBytes"
18
+ MBYTES = "MBytes"
19
+ GBYTES = "GBytes"
20
+ PARSE_TYPES = { BYTES => 1, KBYTES => 2**10, MBYTES => 2**20, GBYTES => 2**30 }
21
+
22
+ def self.parse_to(obj, vars, parse)
23
+ vars.each do |var|
24
+ value = ((obj.instance_variable_get(var).to_f * PARSE_TYPES[obj.unity]) / PARSE_TYPES[parse])
25
+ obj.instance_variable_set(var, value)
26
+ end
27
+ obj.unity = parse
28
+ obj
29
+ end
30
+
31
+ end
data/mool.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'mool/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "mool"
7
+ gem.version = Mool::VERSION
8
+ gem.platform = Gem::Platform::RUBY
9
+ gem.authors = ["g.edera", "eserdio"]
10
+ gem.email = ["gab.edera@gmail.com"]
11
+ gem.description = "Get operative system information: Disk, Memory, Cpu, Load-average, Processes"
12
+ gem.summary = "Get operative system information (Linux)"
13
+ gem.homepage = "https://github.com/gedera/mool"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mool
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - g.edera
14
+ - eserdio
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2015-12-18 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: "Get operative system information: Disk, Memory, Cpu, Load-average, Processes"
24
+ email:
25
+ - gab.edera@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/locales/en.mool.yml
39
+ - lib/locales/es.mool.yml
40
+ - lib/mool.rb
41
+ - lib/mool/cpu.rb
42
+ - lib/mool/disk.rb
43
+ - lib/mool/load_average.rb
44
+ - lib/mool/memory.rb
45
+ - lib/mool/service.rb
46
+ - lib/mool/version.rb
47
+ - mool.gemspec
48
+ has_rdoc: true
49
+ homepage: https://github.com/gedera/mool
50
+ licenses:
51
+ - MIT
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.6.2
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Get operative system information (Linux)
82
+ test_files: []
83
+