arbi 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
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
+ def tablize(args)
21
+ lens, res, div = [], "", ""
22
+
23
+ args.each {|row|
24
+ row.each_with_index {|e, i|
25
+ lens[i] ||= 0
26
+ lens[i] = [lens[i], e.to_s.length].max
27
+ }
28
+ }
29
+
30
+ div = ?+ + lens.map {|s|
31
+ (?- * s) + ?+
32
+ }.join
33
+
34
+ res += div + "\n"
35
+
36
+ args.each_with_index {|row, index|
37
+ res += ?|
38
+ row.each_with_index {|e, i|
39
+ res += (e || ?-).to_s.center(lens[i]) + ?|
40
+ }
41
+
42
+ res += "\n" + div if index == 0
43
+
44
+ res += "\n"
45
+ }
46
+
47
+ res += div + "\n"
48
+ end
@@ -0,0 +1,23 @@
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
+
21
+ module Arbi
22
+ VERSION = '1.0.8'
23
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 7
9
- version: 1.0.7
8
+ - 8
9
+ version: 1.0.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - shura
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-15 00:00:00 +02:00
17
+ date: 2011-03-13 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -30,6 +30,32 @@ dependencies:
30
30
  version: "0"
31
31
  type: :runtime
32
32
  version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: eventmachine
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
33
59
  description: ""
34
60
  email: shura1991@gmail.com
35
61
  executables:
@@ -41,13 +67,30 @@ extra_rdoc_files: []
41
67
 
42
68
  files:
43
69
  - lib/arbi.rb
44
- - lib/arbi/common.rb
45
- - lib/arbi/plugins/ram.rb
46
- - lib/arbi/plugins/diskspace.rb
47
- - lib/arbi/plugins/cpu.rb
48
- - lib/arbi/plugins/batteries.rb
49
- - lib/arbi/plugins/net.rb
50
- - lib/arbi/plugins/thermal.rb
70
+ - lib/arbi/modules.rb
71
+ - lib/arbi/timeline.rb
72
+ - lib/arbi/server.rb
73
+ - lib/arbi/cli/server.rb
74
+ - lib/arbi/cli/client.rb
75
+ - lib/arbi/utils/table.rb
76
+ - lib/arbi/utils/numeric.rb
77
+ - lib/arbi/client.rb
78
+ - lib/arbi/version.rb
79
+ - lib/arbi/modules/adapter.rb
80
+ - lib/arbi/modules/net.rb
81
+ - lib/arbi/modules/ram.rb
82
+ - lib/arbi/modules/cpu.rb
83
+ - lib/arbi/modules/sys/adapter.rb
84
+ - lib/arbi/modules/sys/thermal.rb
85
+ - lib/arbi/modules/sys/battery.rb
86
+ - lib/arbi/modules/version.rb
87
+ - lib/arbi/modules/help.rb
88
+ - lib/arbi/modules/battery.rb
89
+ - lib/arbi/modules/diskstat.rb
90
+ - lib/arbi/modules/acpi/adapter.rb
91
+ - lib/arbi/modules/acpi/utils.rb
92
+ - lib/arbi/modules/acpi/battery.rb
93
+ - lib/arbi/config.rb
51
94
  - bin/arbid
52
95
  - bin/arbi
53
96
  has_rdoc: true
@@ -1,3 +0,0 @@
1
- def file_get_content file
2
- File.readlines(file).join
3
- end
@@ -1,85 +0,0 @@
1
- class Batteries
2
- require 'arbi/common'
3
-
4
- def initialize
5
- @AC = {'sanity' => false, 'state' => false, 'percent' => false}
6
- @batteries = Dir.glob "/proc/acpi/battery/*"
7
- end
8
-
9
- def get_infos
10
- batteries = Hash.new
11
- @batteries.each { |battery|
12
- batteries.merge!(get_battery_infos(battery))
13
- }
14
- batteries.merge average batteries
15
- end
16
-
17
- def self.protocolize batteries
18
- str = "batteries:\r\n"
19
- batteries.each { |key, value|
20
- str << "#{key}|#{value["percent"] || '-'}"
21
- if key != "AVERAGE"
22
- str << "|#{value["state"] || '-'}|#{value["sanity"] || '-'}"
23
- end
24
- str << "\r\n"
25
- }
26
- str << "END\r\n"
27
- end
28
-
29
- def self.friendlize raw
30
- raw.strip!
31
- max = raw.lines.map{|x| x.split(/\|/)[0].length}.max + 4
32
- str = "NAME".ljust(max) + "PERC STATE SANITY\r\n"
33
- raw.split(/\r?\n/).each { |line|
34
- datas = line.split /\|/
35
- str << datas[0].ljust(max) + datas[1].ljust(8)
36
- if datas[0] != "AVERAGE"
37
- str << datas[2].ljust(12) + datas[3]
38
- end
39
- str << "\r\n"
40
- }
41
- str
42
- end
43
-
44
- private
45
-
46
- def average batteries
47
- avg, n = 0, 0
48
- batteries.each do |key, value|
49
- if value["percent"]
50
- avg += value["percent"].gsub(/%$/, '').to_i
51
- n += 1
52
- end
53
- end
54
- avg = (avg.to_f / n).to_s.gsub /\.0/, '' + '%'
55
-
56
- ({'AVERAGE' => {
57
- 'sanity' => '-',
58
- 'state' => '-',
59
- 'percent' => avg
60
- }
61
- })
62
- end
63
-
64
- def get_battery_infos dir
65
- info = file_get_content dir + "/info"
66
- state = file_get_content dir + "/state"
67
- present = {'yes' => true, 'no' => false}[state.match(/^present:\s*?([^\s]+?)$/)[1]]
68
- if !present then return {dir.gsub(/^\/proc\/acpi\/battery\//, '') => @AC} end
69
- ({dir.gsub(/^\/proc\/acpi\/battery\//, '') => {
70
- 'sanity' => sprintf('%.1f%%', 100.0 /
71
- (info.match(/^design capacity:\s*?([^\s]+?) mAh$/)[1].to_i) *
72
- (info.match(/^last full capacity:\s*?([^\s]+?) mAh$/)[1].to_i)).gsub(/\.0%$/, '%'),
73
- 'state' => state.match(/^charging state:\s*?([^\s]+?)$/)[1],
74
- 'percent' => sprintf('%.1f%%', 100.0 /
75
- (info.match(/^last full capacity:\s*?([^\s]+?) mAh$/)[1].to_i) *
76
- (state.match(/^remaining capacity:\s*?([^\s]+?) mAh$/)[1].to_i)).gsub(/\.0%$/, '%')
77
- }})
78
- end
79
- end
80
-
81
- if __FILE__ == $0
82
- puts Batteries.friendlize Batteries.protocolize(Batteries.new.get_infos).gsub(/^batteries:\s+|^END\s+/m, '').strip
83
- else
84
- Arbi.add_plugin /^batteries$/i, Batteries.new
85
- end
@@ -1,89 +0,0 @@
1
- class Cpu
2
- require 'arbi/common'
3
- require 'thread'
4
-
5
- def initialize
6
- @prev = {:idle => Hash.new, :total => Hash.new}
7
- @percents = Hash.new
8
- @mutex = Mutex.new
9
- @serv = Thread.new do
10
- while true
11
- @mutex.lock
12
- update
13
- @mutex.unlock
14
- sleep 2
15
- end
16
- end
17
- end
18
-
19
- def get_infos
20
- @mutex.lock
21
- perc = @percents
22
- @mutex.unlock
23
- perc
24
- end
25
-
26
- def self.protocolize percents
27
- str = "cpu:\r\n"
28
- percents.each { |key, value|
29
- str += "#{key}|#{value}\r\n"
30
- }
31
- str + "END\r\n"
32
- end
33
-
34
- def self.friendlize raw
35
- raw = raw.to_s.strip
36
- max = raw.lines.map{|x|x.split(/\|/)[0].length}.max + 4
37
- str = "NAME".ljust(max) + "PERC\r\n"
38
- raw.split(/\r?\n/).each { |line|
39
- datas = line.split /\|/
40
- str << datas[0].ljust(max) + datas[1] + "\r\n"
41
- }
42
- str
43
- end
44
-
45
- def close
46
- @serv.kill
47
- end
48
-
49
- private
50
-
51
- def update
52
- file_get_content('/proc/stat').split("\n").each do |line|
53
- if line =~ /^cpu/
54
- datas = line.split /\s+/
55
- name = datas.shift
56
- name = (name == 'cpu' ? 'AVERAGE' : name)
57
- total = eval datas.join('+')
58
- idle = datas[3].to_i
59
-
60
- if not @prev[:idle].include? name or not @prev[:total].include? name
61
- @prev[:idle][name] = @prev[:total][name] = 0
62
- end
63
-
64
- @percents[name] = (100 * ((total - @prev[:total][name]) - (idle - @prev[:idle][name])) / (total - @prev[:total][name])).to_s + "%"
65
- @prev[:idle][name] = idle
66
- @prev[:total][name] = total
67
- end
68
- end
69
-
70
- average = {"AVERAGE" => @percents["AVERAGE"]} and @percents.delete("AVERAGE") and @percents.merge!(average)
71
- end
72
- end
73
-
74
- if __FILE__ == $0
75
- x = Cpu.new
76
- ["INT", "KILL", "ABRT", "ILL", "QUIT"].each do |sig|
77
- trap(sig) do
78
- x.close
79
- exit 0
80
- end
81
- end
82
- 10.times {
83
- sleep 2
84
- puts Cpu.friendlize Cpu.protocolize(x.get_infos).gsub(/^cpu:\s+|END\s+/m, '')
85
- }
86
- x.close
87
- else
88
- Arbi.add_plugin /^cpu$/i, Cpu.new
89
- end
@@ -1,68 +0,0 @@
1
- class Diskspace
2
- require 'arbi/common'
3
- require 'sys/filesystem'
4
- include Sys
5
-
6
- def get_infos
7
- devices = Array.new
8
- get_devices.each do |device|
9
- begin
10
- diskstat = Filesystem.stat device['point']
11
- device.merge!({'usage' => (100 - (100.0 / diskstat.blocks * diskstat.blocks_available).round).to_s + "%",
12
- 'space' => diskstat.blocks * diskstat.fragment_size})
13
- devices += [device]
14
- rescue
15
- end
16
- end
17
- devices
18
- end
19
-
20
- def self.protocolize devices
21
- str = "devices:\r\n"
22
- devices.each do |device|
23
- str += "#{device['device']}|#{device['point']}|#{device['usage']}|#{self.unitize device['space']}\r\n"
24
- end
25
- str + "END\r\n"
26
- end
27
-
28
- def self.friendlize raw
29
- raw = raw.to_s.strip
30
- dmax = [raw.lines.map{|line|line.split(/\|/)[0].length}.max + 4, 8].max
31
- mmax = [raw.lines.map{|line|line.split(/\|/)[1].length}.max + 4, 7].max
32
- str = "DEVICE".ljust(dmax) + "MOUNT".ljust(mmax) + "USE % SPACE\r\n"
33
- raw.split(/\r?\n/).each { |line|
34
- datas = line.split /\|/
35
- str << datas[0].ljust(dmax) + datas[1].ljust(mmax) + datas[2].ljust(7) + datas[3] + "\r\n"
36
- }
37
- str
38
- end
39
-
40
- private
41
-
42
- def get_devices
43
- devices = Array.new
44
- Filesystem.mounts do |fs|
45
- devices += [{'device' => fs.name, 'point' => fs.mount_point}]
46
- end
47
- devices
48
- end
49
-
50
- def self.unitize misure
51
- u = 'b'
52
- %w(Kb Mb Gb Tb).each do |i|
53
- if misure >= 1024
54
- misure /= 1024.0
55
- u = i
56
- else
57
- return misure.round.to_s + u
58
- end
59
- end
60
- misure.round.to_s + u
61
- end
62
- end
63
-
64
- if __FILE__ == $0
65
- puts Diskspace.friendlize Diskspace.protocolize(Diskspace.new.get_infos).gsub(/^devices:\s+|END\s+$/m, '')
66
- else
67
- Arbi::add_plugin /^diskstats$/i, Diskspace.new
68
- end
@@ -1,120 +0,0 @@
1
- class Net
2
- require 'arbi/common'
3
- require 'thread'
4
-
5
- def initialize
6
- @stats = Hash.new
7
- @prev = {:up => Hash.new, :down => Hash.new}
8
- @mutex = Mutex.new
9
- @serv = Thread.new do
10
- while true
11
- @mutex.lock
12
- update
13
- @mutex.unlock
14
- sleep 2
15
- end
16
- end
17
- end
18
-
19
- def get_infos
20
- @mutex.lock
21
- s = @stats
22
- @mutex.unlock
23
- s
24
- end
25
-
26
- def self.protocolize stats
27
- str = "net:\r\n"
28
- stats.each { |key, value|
29
- str += "#{key}|#{value[:up]}|#{value[:down]}|#{value[:state] ? 'on' : 'off'}"
30
- if value[:quality]
31
- str += "|#{value[:essid]}|#{value[:quality]}"
32
- end
33
- str += "\r\n"
34
- }
35
- str + "END\r\n"
36
- end
37
-
38
- def self.friendlize raw
39
- raw = raw.to_s.strip
40
- nmax = raw.lines.map{|x|x.split(/\|/)[0].length}.max + 4
41
- umax = raw.lines.map{|x|x.split(/\|/)[1].gsub(/^(\d+\.\d).*$/, '\1').length}.max + 2
42
- dmax = raw.lines.map{|x|x.split(/\|/)[2].gsub(/^(\d+\.\d).*$/, '\1').length}.max + 2
43
- emax = [[raw.lines.map{|x|x.split(/\|/)[4].to_s.length}.max + 4, 7].max, 34].min
44
- str = "NAME".ljust(nmax) + "UP DOWN STATE " + "ESSID".ljust(emax) + "QUALITY\r\n"
45
- raw.split(/\r?\n/).each { |line|
46
- datas = line.split(/\|/)
47
- str << datas[0].ljust(nmax) + ('%.1f' % datas[1].to_f).ljust(8) + ('%.1f' % datas[2].to_f).ljust(8) + datas[3].ljust(7)
48
- str << datas[4].ljust(emax) + datas[5] if datas[4]
49
- str << "\r\n"
50
- }
51
- str
52
- end
53
-
54
- private
55
-
56
- def update
57
- file_get_content('/proc/net/dev').gsub(/^.*\|.*?\n/m, '').split("\n").each do |line|
58
- datas = line.gsub(/^\s+|\s+$/, '').split(/:\s+|\s+/)
59
- name = datas.shift
60
- down = datas[0].to_i
61
- up = datas[8].to_i
62
-
63
- if not @prev[:up].include? name or not @prev[:down].include? name
64
- @prev[:up][name] = @prev[:down][name] = 0
65
- end
66
-
67
- @stats[name] = {:up => (up - @prev[:up][name]) / 1024.0,
68
- :down => (down - @prev[:down][name]) / 1024.0,
69
- :state => get_state(name),
70
- :quality => nil,
71
- :essid => nil}
72
- if @stats[name][:state]
73
- @stats[name][:quality] = get_quality(name)
74
- @stats[name][:essid] = get_essid(name) if @stats[name][:quality]
75
- end
76
- @prev[:up][name] = up
77
- @prev[:down][name] = down
78
- end
79
- end
80
-
81
- def get_state interface
82
- if file_get_content('/proc/net/route') =~ /#{interface}/
83
- true
84
- else
85
- false
86
- end
87
- end
88
-
89
- def get_quality interface
90
- begin
91
- file_get_content('/proc/net/wireless').match(/^\s*?#{interface}:.*$/)[0].strip.split(/:\s+|\s+/)[2].gsub('.', '') + "%"
92
- rescue
93
- nil
94
- end
95
- end
96
-
97
- def get_essid interface
98
- require "socket"
99
-
100
- iwreq = [interface, " " * 32, 32, 0].pack("a16pII")
101
- sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
102
- sock.ioctl(0x8B1B, iwreq)
103
- return iwreq.unpack("a16pII")[1].strip
104
- end
105
-
106
- def close
107
- @serv.kill
108
- end
109
- end
110
-
111
- if __FILE__ == $0
112
- x = Net.new
113
- 10.times {
114
- sleep 2
115
- puts Net.friendlize Net.protocolize(x.get_infos).gsub(/^net:\s+|END\s+$/m, '')
116
- }
117
- x.close
118
- else
119
- Arbi::add_plugin /^net$/i, Net.new
120
- end