arbi 1.0.7 → 1.0.8

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.
@@ -0,0 +1,50 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Arbi
21
+
22
+ module Modules
23
+
24
+ module Acpi
25
+
26
+ module Utils
27
+ protected
28
+
29
+ def hashize(file)
30
+ h = {}
31
+
32
+ File.readlines(file).each {|line|
33
+ if (line =~ /^([^:]+):\s*(.+?)\s*$/)
34
+ key, value = $1, $2
35
+ key = key.downcase.gsub(/ /, '_')
36
+ value = Numeric.parse(value) || value
37
+ h[key] = value
38
+ h[key.to_sym] = value
39
+ end
40
+ } if File.exist?(file)
41
+
42
+ h
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,22 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'arbi/modules/acpi/adapter'
21
+
22
+ require 'arbi/modules/sys/adapter' unless Arbi::Modules::Acpi::Adapter.valid?
@@ -0,0 +1,22 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'arbi/modules/acpi/battery'
21
+
22
+ require 'arbi/modules/sys/battery' unless Arbi::Modules::Acpi::Battery.valid?
@@ -0,0 +1,88 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Arbi
21
+
22
+ module Modules
23
+
24
+ class Cpu < Arbi::Modules::Module
25
+ def initialize(data = [])
26
+ super(data)
27
+
28
+ @mutex = Mutex.new
29
+ @prev = {:idle => {}, :total => {}}
30
+ @datas = []
31
+
32
+ every 2, :timeout => 10, &self.method(:update)
33
+ end
34
+
35
+ def valid?
36
+ File.exist?('/proc/stat')
37
+ end
38
+
39
+ def refresh
40
+ @mutex.synchronize {
41
+ @data = @datas.dup
42
+ }
43
+ end
44
+
45
+ def format
46
+ tablize([['NAME', 'PERCENT']] + @data.map {|x|
47
+ [x[:name] || x['name'], x[:percent] || x['percent']]
48
+ })
49
+ end
50
+
51
+ protected
52
+ def update
53
+ percents = {}
54
+ File.readlines('/proc/stat').each {|line|
55
+ if line =~ /^cpu/
56
+ name, *datas = line.split(/\s+/)
57
+ name = (name == 'cpu' ? 'AVERAGE' : name)
58
+ total = datas.map(&:to_i).inject(:+)
59
+ idle = datas[3].to_i
60
+
61
+ @prev[:idle][name] = @prev[:total][name] = 0 if !@prev[:idle].include?(name) or !@prev[:total].include?(name)
62
+
63
+ percents[name] = "#{100 * ((total - @prev[:total][name]) - (idle - @prev[:idle][name])) / (total - @prev[:total][name])}%"
64
+ @prev[:idle][name] = idle
65
+ @prev[:total][name] = total
66
+ end
67
+ }
68
+
69
+ average = {'AVERAGE' => percents['AVERAGE']} and percents.delete('AVERAGE') and percents.merge!(average)
70
+ self.datas = percents.to_a
71
+ end
72
+
73
+ def datas=(percs)
74
+ @mutex.synchronize {
75
+ @datas = []
76
+ percs.each {|(name, perc)|
77
+ @datas << {
78
+ name: name,
79
+ percent: perc
80
+ }
81
+ }
82
+ }
83
+ end
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,81 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'sys/filesystem'
21
+
22
+ module Arbi
23
+
24
+ module Modules
25
+
26
+ module Sys
27
+ include ::Sys
28
+ end
29
+
30
+ class Diskstat < Module
31
+ def valid?
32
+ true
33
+ end
34
+
35
+ def refresh
36
+ @data = []
37
+ self.devices.each {|device|
38
+ begin
39
+ diskstat = Sys::Filesystem.stat(device[:point])
40
+ device.merge!({
41
+ usage: "#{100 - (100.0 / diskstat.blocks * diskstat.blocks_available).round}%",
42
+ space: self.unitize(diskstat.blocks * diskstat.fragment_size)
43
+ })
44
+
45
+ @data << device
46
+ rescue
47
+ end
48
+ }
49
+ end
50
+
51
+ def format
52
+ tablize([['DEVICE', 'POINT', 'USE', 'SPACE']] + @data.map {|dev|
53
+ [dev[:device] || dev['device'], dev[:point] || dev['point'], dev[:usage] || dev['usage'],
54
+ dev[:space] || dev['space']]
55
+ })
56
+ end
57
+
58
+ protected
59
+ def devices
60
+ Sys::Filesystem.mounts.map {|fs|
61
+ {device: fs.name, point: fs.mount_point}
62
+ }
63
+ end
64
+
65
+ def unitize misure
66
+ u = 'b'
67
+ %w(Kb Mb Gb Tb).each {|i|
68
+ if misure >= 1024
69
+ misure /= 1024.0
70
+ u = i
71
+ else
72
+ return misure.round.to_s + u
73
+ end
74
+ }
75
+ misure.round.to_s + u
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,40 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Arbi
21
+
22
+ module Modules
23
+
24
+ class Help < Module
25
+ def valid?
26
+ true
27
+ end
28
+
29
+ def refresh
30
+ @data = Arbi::Modules.modules.keys
31
+ end
32
+
33
+ def format
34
+ @data.join("\n")
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,120 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'socket'
21
+
22
+ module Arbi
23
+
24
+ module Modules
25
+
26
+ class Net < Arbi::Modules::Module
27
+ def initialize(data = [])
28
+ super(data)
29
+
30
+ @mutex = Mutex.new
31
+ @prev = {:up => {}, :down => {}}
32
+ @datas = []
33
+
34
+ every 2, :timeout => 10, &self.method(:update)
35
+ end
36
+
37
+ def valid?
38
+ File.exist?('/proc/net/dev') and File.exist?('/proc/net/route') and
39
+ File.exist?('/proc/net/wireless')
40
+ end
41
+
42
+ def refresh
43
+ @mutex.synchronize {
44
+ @data = @datas.dup
45
+ }
46
+ end
47
+
48
+ def format
49
+ tablize([['IFACE', 'UP', 'DOWN', 'STATE', 'QUALITY', 'ESSID']] + @data.map {|x|
50
+ [x[:name] || x['name'], x[:up] || x['up'], x[:down] || x['down'],
51
+ ((x[:state] || x['state']) ? 'on' : 'off'), x[:quality] || x['quality'],
52
+ x[:essid] || x['essid']]
53
+ })
54
+ end
55
+
56
+ def update
57
+ stats = {}
58
+ File.read('/proc/net/dev').gsub(/^.*\|.*?\n/m, '').each_line {|line|
59
+ name, *datas = line.strip.split(/:?\s+/)
60
+ down = datas[0].to_i
61
+ up = datas[8].to_i
62
+
63
+ @prev[:up][name] = @prev[:down][name] = 0 if !@prev[:up].include?(name) and !@prev[:down].include?(name)
64
+
65
+ stats[name] = {
66
+ up: "%.1fkB" % [( up - @prev[ :up ][name]).to_f / 1024],
67
+ down: "%.1fkB" % [(down - @prev[:down][name]).to_f / 1024],
68
+ state: (File.read('/proc/net/route') =~ /#{Regexp.escape(name)}/ ? true : false),
69
+ quality: nil,
70
+ essid: nil
71
+ }
72
+
73
+ if stats[name][:state]
74
+ stats[name][:quality] = quality(name)
75
+ begin
76
+ stats[name][ :essid ] = essid(name) if stats[name][:quality]
77
+ rescue Exception => e
78
+ Arbi.debug("ESSID: #{e}")
79
+ end
80
+ end
81
+
82
+ @prev[ :up ][name] = up
83
+ @prev[:down][name] = down
84
+ }
85
+
86
+ self.datas = stats
87
+ rescue Exception => e
88
+ Arbi.debug(e.to_s)
89
+ end
90
+
91
+ protected
92
+ def quality(interface)
93
+ File.read('/proc/net/wireless').match(/^\s*#{Regexp.escape(interface)}:.*$/)[0].strip.split(/:?\s+/)[2].gsub('.', '') + "%"
94
+ rescue
95
+ nil
96
+ end
97
+
98
+ def essid(interface)
99
+ iwreq = [interface, " " * 32, 32, 0].pack("a16pII")
100
+ sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
101
+ sock.ioctl(0x8B1B, iwreq)
102
+ return iwreq.unpack("a16pII")[1].strip
103
+ rescue
104
+ nil
105
+ end
106
+
107
+ def datas=(stats)
108
+ @mutex.synchronize {
109
+ @datas = []
110
+ stats.each {|(key, value)|
111
+ value[:name] = key
112
+ @datas << value
113
+ }
114
+ }
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ end
@@ -0,0 +1,52 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of arbi.
5
+ #
6
+ # arbi is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # arbi is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with arbi. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'arbi/modules/acpi/utils'
21
+
22
+ module Arbi
23
+
24
+ module Modules
25
+
26
+ class Ram < Arbi::Modules::Module
27
+ include Arbi::Modules::Acpi::Utils
28
+
29
+ def valid?
30
+ File.exist?('/proc/meminfo')
31
+ end
32
+
33
+ def refresh
34
+ @data = []
35
+
36
+ mem = hashize('/proc/meminfo')
37
+ perc = 100.0 / mem[:memtotal] * (mem[:memfree] + mem[:buffers] + mem[:cached])
38
+
39
+ @data = [
40
+ ("%.1f%%" % [100.0 - perc]),
41
+ ("%.1f%%" % [perc])
42
+ ]
43
+ end
44
+
45
+ def format
46
+ tablize([['USED', 'FREE'], @data])
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end