zabbix-ruby-client 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG.md +9 -0
- data/README.md +6 -9
- data/lib/zabbix-ruby-client/data.rb +6 -4
- data/lib/zabbix-ruby-client/plugin_base.rb +41 -0
- data/lib/zabbix-ruby-client/plugins/apache.rb +1 -1
- data/lib/zabbix-ruby-client/plugins/apt.rb +32 -10
- data/lib/zabbix-ruby-client/plugins/cpu.rb +32 -23
- data/lib/zabbix-ruby-client/plugins/disk.rb +52 -33
- data/lib/zabbix-ruby-client/plugins/load.rb +22 -15
- data/lib/zabbix-ruby-client/plugins/memory.rb +48 -26
- data/lib/zabbix-ruby-client/plugins/network.rb +26 -17
- data/lib/zabbix-ruby-client/plugins/nginx.rb +1 -0
- data/lib/zabbix-ruby-client/plugins/postgres.rb +64 -0
- data/lib/zabbix-ruby-client/version.rb +1 -1
- data/spec/files/plugins/sample_buggy.rb +11 -0
- data/spec/files/plugins/sample_discover.rb +12 -0
- data/spec/files/system/apache_status +7 -6
- data/spec/files/system/apt-check +1 -0
- data/spec/files/system/df +1 -0
- data/spec/files/system/diskstats +1 -0
- data/spec/files/system/loadavg +1 -0
- data/spec/files/system/meminfo +46 -0
- data/spec/files/system/net_dev +1 -0
- data/spec/files/system/proc_cpu +1 -0
- data/spec/files/task_discovery.yml +3 -0
- data/spec/lib/cli_spec.rb +44 -6
- data/spec/lib/data_spec.rb +58 -1
- data/spec/lib/plugin_base_spec.rb +54 -0
- data/spec/lib/plugins/apache_spec.rb +78 -5
- data/spec/lib/plugins/apt_spec.rb +45 -0
- data/spec/lib/plugins/cpu_spec.rb +51 -1
- data/spec/lib/plugins/disk_spec.rb +77 -1
- data/spec/lib/plugins/load_spec.rb +41 -0
- data/spec/lib/plugins/memory_spec.rb +83 -2
- data/spec/lib/plugins/network_spec.rb +60 -1
- data/spec/lib/plugins_spec.rb +3 -1
- data/templates/client/postgres.yml +3 -0
- data/zabbix-ruby-client.gemspec +1 -0
- data/zabbix-templates/postgres_tpl.xml +833 -0
- metadata +79 -15
- checksums.yaml +0 -7
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
Zabbbix Ruby Client Changelog
|
2
2
|
-----------------------------
|
3
3
|
|
4
|
+
### v0.0.18 - 2013-12-13
|
5
|
+
|
6
|
+
* total fix on CPU usage plugin
|
7
|
+
* added a postgres plugin (per database)
|
8
|
+
* refactoring of plugins
|
9
|
+
* fixed typo on count of start processes in apache plugin
|
10
|
+
* made plugin collection optional, so you can have a plugin for only discovery
|
11
|
+
* more tests and coverage
|
12
|
+
|
4
13
|
### v0.0.17 - 2013-10-31
|
5
14
|
|
6
15
|
* bugfix on requires
|
data/README.md
CHANGED
@@ -91,10 +91,13 @@ There are a set of standart plugins included in the package, aimed at linux syst
|
|
91
91
|
* sysinfo (uses uname -a) is populating the host info in the inventory, and should be ran at setup and/or monthly [sysinfo_tpl](master/zabbix-templates/sysinfo_tpl.xml)
|
92
92
|
* apache (depends on mod_status with status_extended on) [apache_tpl](master/zabbix-templates/apache_tpl.xml)
|
93
93
|
* mysql (uses mysqladmin extended-status) [mysql_tpl](master/zabbix-templates/mysql_tpl.xml)
|
94
|
+
* postgres (uses psql and pg_stat_database) [postgres_tpl](master/zabbix-templates/postgres_tpl.xml)
|
95
|
+
* best is to use a .pgpass file see http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.html
|
94
96
|
* nginx (requires httpStubStatus nginx module) [nginx_tpl](master/zabbix-templates/nginx_tpl.xml)
|
95
97
|
* redis (uses redis-cli info) [redis_tpl](master/zabbix-templates/redis_tpl.xml)
|
96
98
|
* args [ "/path/to/redis-cli", "options to connect" ]
|
97
99
|
|
100
|
+
|
98
101
|
You can add extra plugins in a plugins/ dir in the working dir, just by copying one of the existing plugins in the repo and change to your need. All plugins present in plugins/ will be loaded if present in the config file you use. That can be convenient to test by using the -t flag, for example `bundle exec zrc -t testplugin.yml` where testplugin.yml only contains the name and args for yoiur plugin.
|
99
102
|
|
100
103
|
## Custom plugins how-to
|
@@ -159,7 +162,7 @@ I first created on both sides an autossh user with no console and no password:
|
|
159
162
|
sudo useradd -m -r -s /bin/false -d /usr/local/zabtunnel zabtunnel
|
160
163
|
```
|
161
164
|
|
162
|
-
On the zabbix server I created a key that I transfered to the clients in `/usr/local/zabtunnel
|
165
|
+
On the zabbix server I created a key that I transfered to the clients in `/usr/local/zabtunnel/.ssh/authorized_keys`
|
163
166
|
|
164
167
|
```
|
165
168
|
sudo su -s /bin/bash - zabtunnel
|
@@ -197,25 +200,19 @@ ListenIP 127.0.0.1
|
|
197
200
|
|
198
201
|
## Todo
|
199
202
|
|
200
|
-
*
|
201
|
-
* write tests
|
203
|
+
* improve templates for graphs
|
202
204
|
* add more plugins
|
203
205
|
* memcache
|
204
|
-
* redis
|
205
206
|
* mysql master/slave
|
207
|
+
* postgres replication
|
206
208
|
* monit
|
207
209
|
* passenger
|
208
|
-
* nginx (done)
|
209
210
|
* logged users
|
210
211
|
* denyhosts
|
211
212
|
* postfix
|
212
213
|
* sendgrid
|
213
214
|
* airbrake
|
214
|
-
* disk occupation (done)
|
215
215
|
* try to work out a way to create host/graphs/alerts from the client using Zabbix API
|
216
|
-
* verify compatibility with ruby 1.9 (done)
|
217
|
-
* create a resend system that will store timed data on client if connection to server fails, and will send pending (missed) data on next batch.
|
218
|
-
|
219
216
|
|
220
217
|
## Contributing
|
221
218
|
|
@@ -11,18 +11,20 @@ module ZabbixRubyClient
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def run_plugin(plugin, args = nil)
|
14
|
-
Plugins.load(plugin) ||
|
14
|
+
Plugins.load(plugin) || ZabbixRubyClient::Log.error( "Plugin #{plugin} not found.")
|
15
15
|
if Plugins.loaded[plugin]
|
16
16
|
begin
|
17
|
-
|
17
|
+
if Plugins.loaded[plugin].respond_to?(:collect)
|
18
|
+
@items += Plugins.loaded[plugin].send(:collect, @host, *args)
|
19
|
+
end
|
18
20
|
if Plugins.loaded[plugin].respond_to?(:discover)
|
19
21
|
key, value = Plugins.loaded[plugin].send(:discover, *args)
|
20
22
|
@discover[key] ||= []
|
21
23
|
@discover[key] << [ value ]
|
22
24
|
end
|
23
25
|
rescue Exception => e
|
24
|
-
Log.fatal "Oops"
|
25
|
-
Log.fatal e.message
|
26
|
+
ZabbixRubyClient::Log.fatal "Oops"
|
27
|
+
ZabbixRubyClient::Log.fatal e.message
|
26
28
|
end
|
27
29
|
end
|
28
30
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "zabbix-ruby-client/logger"
|
2
|
+
|
3
|
+
module ZabbixRubyClient
|
4
|
+
module PluginBase
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def httprequest(url)
|
8
|
+
end
|
9
|
+
|
10
|
+
def perform(command)
|
11
|
+
end
|
12
|
+
|
13
|
+
def getline(file, pattern=false)
|
14
|
+
if File.readable? file
|
15
|
+
File.open(file,'r') do |f|
|
16
|
+
f.each do |l|
|
17
|
+
line = l.strip
|
18
|
+
if pattern
|
19
|
+
if Regexp.new(pattern).match line
|
20
|
+
Log.debug "File #{file}: #{line}"
|
21
|
+
return line
|
22
|
+
end
|
23
|
+
else
|
24
|
+
return line
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
Log.warn "File #{file}: pattern \"#{pattern}\" not found."
|
29
|
+
false
|
30
|
+
else
|
31
|
+
if File.file? file
|
32
|
+
Log.error "File not readable: #{file}"
|
33
|
+
else
|
34
|
+
Log.error "File not found: #{file}"
|
35
|
+
end
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -32,7 +32,7 @@ module ZabbixRubyClient
|
|
32
32
|
back << "#{host} apache[c_log] #{time} #{ret["Score"]["L"]}"
|
33
33
|
back << "#{host} apache[c_read] #{time} #{ret["Score"]["R"]}"
|
34
34
|
back << "#{host} apache[c_send] #{time} #{ret["Score"]["W"]}"
|
35
|
-
back << "#{host} apache[c_start] #{time}#{ret["Score"]["S"]}"
|
35
|
+
back << "#{host} apache[c_start] #{time} #{ret["Score"]["S"]}"
|
36
36
|
return back
|
37
37
|
end
|
38
38
|
|
@@ -7,19 +7,41 @@ module ZabbixRubyClient
|
|
7
7
|
|
8
8
|
def collect(*args)
|
9
9
|
host = args[0]
|
10
|
-
|
11
|
-
if
|
12
|
-
|
10
|
+
info = get_info
|
11
|
+
if info
|
12
|
+
time = Time.now.to_i
|
13
|
+
back = []
|
14
|
+
back << "#{host} apt[security] #{time} #{info[0]}"
|
15
|
+
back << "#{host} apt[pending] #{time} #{info[1]}"
|
16
|
+
back << "#{host} apt[status] #{time} TODO apt #{info[0]}/#{info[1]}"
|
17
|
+
return back
|
13
18
|
else
|
14
|
-
Log.warn "Are you running on ubuntu ?"
|
15
19
|
return []
|
16
20
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def get_info
|
26
|
+
info = aptinfo
|
27
|
+
if info
|
28
|
+
back = info.split(/;/).map(&:to_i)
|
29
|
+
back
|
30
|
+
else
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def aptinfo
|
36
|
+
output = `/usr/lib/update-notifier/apt-check 2>&1`
|
37
|
+
if $?.to_i == 0
|
38
|
+
Log.debug self
|
39
|
+
Log.debug output
|
40
|
+
output
|
41
|
+
else
|
42
|
+
Log.warn "Oh you don't have apt ?"
|
43
|
+
false
|
44
|
+
end
|
23
45
|
end
|
24
46
|
|
25
47
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# for more info check
|
1
|
+
# for more info check
|
2
2
|
# http://www.linuxhowtos.org/System/procstat.htm
|
3
3
|
# http://juliano.info/en/Blog:Memory_Leak/Understanding_the_Linux_load_average
|
4
4
|
require "zabbix-ruby-client/logger"
|
@@ -7,34 +7,43 @@ module ZabbixRubyClient
|
|
7
7
|
module Plugins
|
8
8
|
module Cpu
|
9
9
|
extend self
|
10
|
+
extend ZabbixRubyClient::PluginBase
|
10
11
|
|
11
12
|
def collect(*args)
|
12
13
|
host = args[0]
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
info = get_info
|
15
|
+
back = []
|
16
|
+
if info
|
17
|
+
time = Time.now.to_i
|
18
|
+
info.each do |k,v|
|
19
|
+
back << "#{host} cpu[#{k}] #{time} #{v}"
|
20
|
+
end
|
19
21
|
end
|
20
|
-
|
21
|
-
|
22
|
+
back
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
back = []
|
25
|
-
back << "#{host} cpu[user] #{time} #{user}"
|
26
|
-
back << "#{host} cpu[nice] #{time} #{nice}"
|
27
|
-
back << "#{host} cpu[system] #{time} #{sys}"
|
28
|
-
back << "#{host} cpu[iowait] #{time} #{wait}"
|
29
|
-
back << "#{host} cpu[irq] #{time} #{irq}"
|
30
|
-
back << "#{host} cpu[soft] #{time} #{soft}"
|
31
|
-
back << "#{host} cpu[steal] #{time} #{steal}"
|
32
|
-
back << "#{host} cpu[guest] #{time} #{guest}"
|
33
|
-
back << "#{host} cpu[idle] #{time} #{idle}"
|
34
|
-
back << "#{host} cpu[used] #{time} #{used}"
|
35
|
-
back << "#{host} cpu[total] #{time} #{total}"
|
36
|
-
return back
|
25
|
+
private
|
37
26
|
|
27
|
+
def get_info
|
28
|
+
ret = {}
|
29
|
+
info = getline("/proc/stat", "^cpu ")
|
30
|
+
if info
|
31
|
+
back = info.split(/\s+/).map(&:to_i)
|
32
|
+
ret["user"] = back[1]
|
33
|
+
ret["nice"] = back[2]
|
34
|
+
ret["system"] = back[3]
|
35
|
+
ret["idle"] = back[4]
|
36
|
+
ret["iowait"] = back[5]
|
37
|
+
ret["irq"] = back[6]
|
38
|
+
ret["soft"] = back[7]
|
39
|
+
ret["steal"] = back[8] || 0
|
40
|
+
ret["guest"] = back[9] || 0
|
41
|
+
ret["used"] = back[1...4].reduce(&:+)
|
42
|
+
ret["total"] = back[1...9].reduce(&:+)
|
43
|
+
ret
|
44
|
+
else
|
45
|
+
false
|
46
|
+
end
|
38
47
|
end
|
39
48
|
|
40
49
|
end
|
@@ -2,48 +2,37 @@
|
|
2
2
|
# http://www.xaprb.com/blog/2010/01/09/how-linux-iostat-computes-its-results/
|
3
3
|
# http://www.mjmwired.net/kernel/Documentation/iostats.txt
|
4
4
|
require "zabbix-ruby-client/logger"
|
5
|
+
require "zabbix-ruby-client/plugin_base"
|
5
6
|
|
6
7
|
module ZabbixRubyClient
|
7
8
|
module Plugins
|
8
9
|
module Disk
|
9
10
|
extend self
|
11
|
+
extend ZabbixRubyClient::PluginBase
|
10
12
|
|
11
13
|
def collect(*args)
|
12
14
|
host = args[0]
|
13
15
|
dev = args[1]
|
14
16
|
mapped = args[3] || dev
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
logger.warn "Oh there is no such device."
|
35
|
-
return []
|
36
|
-
end
|
37
|
-
back << "#{host} disk.io[#{mapped},read_ok] #{time} #{read_ok}"
|
38
|
-
back << "#{host} disk.io[#{mapped},read_merged] #{time} #{read_merged}"
|
39
|
-
back << "#{host} disk.io[#{mapped},read_sector] #{time} #{read_sector}"
|
40
|
-
back << "#{host} disk.io[#{mapped},read_time] #{time} #{read_time}"
|
41
|
-
back << "#{host} disk.io[#{mapped},write_ok] #{time} #{write_ok}"
|
42
|
-
back << "#{host} disk.io[#{mapped},write_merged] #{time} #{write_merged}"
|
43
|
-
back << "#{host} disk.io[#{mapped},write_sector] #{time} #{write_sector}"
|
44
|
-
back << "#{host} disk.io[#{mapped},write_time] #{time} #{write_time}"
|
45
|
-
back << "#{host} disk.io[#{mapped},io_time] #{time} #{io_time}"
|
46
|
-
back << "#{host} disk.io[#{mapped},io_weighted] #{time} #{io_weighted}"
|
17
|
+
|
18
|
+
info = get_info(dev, mapped)
|
19
|
+
if info
|
20
|
+
time = Time.now.to_i
|
21
|
+
back = []
|
22
|
+
back << "#{host} disk.space[#{mapped},size] #{time} #{info[1].to_i * 1000}"
|
23
|
+
back << "#{host} disk.space[#{mapped},used] #{time} #{info[2].to_i * 1000}"
|
24
|
+
back << "#{host} disk.space[#{mapped},available] #{time} #{info[3].to_i * 1000}"
|
25
|
+
back << "#{host} disk.space[#{mapped},percent_used] #{time} #{info[4].gsub(/%/,'')}"
|
26
|
+
back << "#{host} disk.io[#{mapped},read_ok] #{time} #{info[9]}"
|
27
|
+
back << "#{host} disk.io[#{mapped},read_merged] #{time} #{info[10]}"
|
28
|
+
back << "#{host} disk.io[#{mapped},read_sector] #{time} #{info[11]}"
|
29
|
+
back << "#{host} disk.io[#{mapped},read_time] #{time} #{info[12]}"
|
30
|
+
back << "#{host} disk.io[#{mapped},write_ok] #{time} #{info[13]}"
|
31
|
+
back << "#{host} disk.io[#{mapped},write_merged] #{time} #{info[14]}"
|
32
|
+
back << "#{host} disk.io[#{mapped},write_sector] #{time} #{info[15]}"
|
33
|
+
back << "#{host} disk.io[#{mapped},write_time] #{time} #{info[16]}"
|
34
|
+
back << "#{host} disk.io[#{mapped},io_time] #{time} #{info[17]}"
|
35
|
+
back << "#{host} disk.io[#{mapped},io_weighted] #{time} #{info[18]}"
|
47
36
|
end
|
48
37
|
return back
|
49
38
|
end
|
@@ -52,7 +41,37 @@ module ZabbixRubyClient
|
|
52
41
|
device = args[0]
|
53
42
|
mount = args[1]
|
54
43
|
mapped = args[2] || device
|
55
|
-
[ "disk.dev.discovery",
|
44
|
+
[ "disk.dev.discovery",
|
45
|
+
"{\"{#DISK_DEVICE}\": \"#{mapped}\", \"{#DISK_MOUNT}\": \"#{mount}\"}"
|
46
|
+
]
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def get_info(disk, device)
|
52
|
+
info = diskinfo(disk)
|
53
|
+
if info
|
54
|
+
back = info.split(/\s+/)
|
55
|
+
io = getline("/proc/diskstats", " #{device} ")
|
56
|
+
if io
|
57
|
+
back += io.split(/\s+/)
|
58
|
+
end
|
59
|
+
back
|
60
|
+
else
|
61
|
+
false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def diskinfo(disk)
|
66
|
+
output = `df | grep "#{disk}"`
|
67
|
+
if $?.to_i == 0
|
68
|
+
Log.debug self
|
69
|
+
Log.debug output
|
70
|
+
output
|
71
|
+
else
|
72
|
+
Log.warn "Oh there is no such device."
|
73
|
+
false
|
74
|
+
end
|
56
75
|
end
|
57
76
|
|
58
77
|
end
|
@@ -1,33 +1,40 @@
|
|
1
1
|
# for more info check
|
2
2
|
# http://juliano.info/en/Blog:Memory_Leak/Understanding_the_Linux_load_average
|
3
|
-
require "zabbix-ruby-client/
|
3
|
+
require "zabbix-ruby-client/plugin_base"
|
4
4
|
|
5
5
|
module ZabbixRubyClient
|
6
6
|
module Plugins
|
7
7
|
module Load
|
8
8
|
extend self
|
9
|
+
extend ZabbixRubyClient::PluginBase
|
9
10
|
|
10
11
|
def collect(*args)
|
11
12
|
host = args[0]
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
info = get_info
|
14
|
+
if info
|
15
|
+
time = Time.now.to_i
|
16
|
+
back = []
|
17
|
+
back << "#{host} load[one] #{time} #{info[0]}"
|
18
|
+
back << "#{host} load[five] #{time} #{info[1]}"
|
19
|
+
back << "#{host} load[fifteen] #{time} #{info[2]}"
|
20
|
+
back << "#{host} load[procs] #{time} #{info[3]}"
|
21
|
+
return back
|
16
22
|
else
|
17
|
-
Log.warn "Oh you don't have a /proc ?"
|
18
23
|
return []
|
19
24
|
end
|
25
|
+
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
time = Time.now.to_i
|
24
|
-
back = []
|
25
|
-
back << "#{host} load[one] #{time} #{one}"
|
26
|
-
back << "#{host} load[five] #{time} #{five}"
|
27
|
-
back << "#{host} load[fifteen] #{time} #{fifteen}"
|
28
|
-
back << "#{host} load[procs] #{time} #{procs}"
|
29
|
-
return back
|
27
|
+
private
|
30
28
|
|
29
|
+
def get_info
|
30
|
+
info = getline("/proc/loadavg")
|
31
|
+
if info
|
32
|
+
back = info.split(/\s+/)
|
33
|
+
back[3] = back[3].split(/\//)[0]
|
34
|
+
back
|
35
|
+
else
|
36
|
+
false
|
37
|
+
end
|
31
38
|
end
|
32
39
|
|
33
40
|
end
|
@@ -1,49 +1,71 @@
|
|
1
1
|
require "zabbix-ruby-client/logger"
|
2
|
+
require "zabbix-ruby-client/plugin_base"
|
2
3
|
|
3
4
|
module ZabbixRubyClient
|
4
5
|
module Plugins
|
5
6
|
module Memory
|
6
7
|
extend self
|
8
|
+
extend ZabbixRubyClient::PluginBase
|
7
9
|
|
8
10
|
def collect(*args)
|
9
11
|
host = args[0]
|
10
|
-
|
11
|
-
if
|
12
|
-
|
12
|
+
info = get_info
|
13
|
+
if info
|
14
|
+
time = Time.now.to_i
|
15
|
+
back = []
|
16
|
+
back << "#{host} memory[total] #{time} #{info['MemTotal']}"
|
17
|
+
back << "#{host} memory[free] #{time} #{info['MemFree']}"
|
18
|
+
back << "#{host} memory[used] #{time} #{info['MemUsed']}"
|
19
|
+
back << "#{host} memory[percent_used] #{time} #{info['MemPercent']}"
|
20
|
+
back << "#{host} memory[swap_total] #{time} #{info['SwapTotal']}"
|
21
|
+
back << "#{host} memory[swap_free] #{time} #{info['SwapFree']}"
|
22
|
+
back << "#{host} memory[swap_used] #{time} #{info['SwapUsed']}"
|
23
|
+
back << "#{host} memory[swap_percent_used] #{time} #{info['SwapPercent']}"
|
24
|
+
return back
|
13
25
|
else
|
14
|
-
Log.warn "Please install sysstat."
|
15
26
|
return []
|
16
27
|
end
|
28
|
+
end
|
17
29
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
30
|
+
private
|
31
|
+
|
32
|
+
def get_info
|
33
|
+
info = meminfo
|
34
|
+
if info
|
35
|
+
back = splitinfo(info)
|
36
|
+
back["MemTotal"] = back["MemTotal"] * 1024
|
37
|
+
back["MemFree"] = (back['MemFree'] + back['Buffers'] + back['Cached']) * 1024
|
38
|
+
back["MemUsed"] = back["MemTotal"] - back["MemFree"]
|
39
|
+
back["MemPercent"] = (back["MemUsed"] / back["MemTotal"].to_f * 100).to_i
|
40
|
+
back['SwapTotal'] = back['SwapTotal'] * 1024
|
41
|
+
back['SwapFree'] = back['SwapFree'] * 1024
|
42
|
+
back['SwapUsed'] = back['SwapTotal'] - back['SwapFree']
|
43
|
+
back['SwapPercent'] = 0
|
44
|
+
unless back['SwapTotal'] == 0
|
45
|
+
back['SwapPercent'] = (back['SwapUsed'] / back['SwapTotal'].to_f * 100).to_i
|
46
|
+
end
|
47
|
+
back
|
48
|
+
else
|
49
|
+
false
|
28
50
|
end
|
51
|
+
end
|
29
52
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
return back
|
53
|
+
def meminfo
|
54
|
+
output = `cat /proc/meminfo`
|
55
|
+
if $?.to_i == 0
|
56
|
+
Log.debug self
|
57
|
+
Log.debug output
|
58
|
+
output
|
59
|
+
else
|
60
|
+
Log.warn "Oh there is no such device."
|
61
|
+
false
|
62
|
+
end
|
41
63
|
end
|
42
64
|
|
43
65
|
def splitinfo(info)
|
44
66
|
info.split(/\n/).map(&:strip).reduce({}) do |a,line|
|
45
67
|
_, key, value = *line.match(/^(\w+):\s+(\d+)\s/)
|
46
|
-
a[key] = value.to_i
|
68
|
+
a[key] = value.to_i if key
|
47
69
|
a
|
48
70
|
end
|
49
71
|
end
|