pi-sys 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b262a028c9922df2c8c93f5976198472cdb0458
4
+ data.tar.gz: 8c9f60acf87cf626684e92ced2106f2de236504d
5
+ SHA512:
6
+ metadata.gz: f2e6f0815f25873703c05762248870c3c5e9f199fadeb88a711d5fc1e67b8d3bdfd426b86cd997344777244c3aa1274844dc86f6365c4df81c364cadae1f5c9f
7
+ data.tar.gz: 7f74bad823c5b7ed7133eb4dcc01efa904e2ee4e1d956bac6dd294f3bd85bf7c4fefa8e4ad53e96f5e97866a3eac08d0c5cf45c56cd4e0fdec167a0645fc5074
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 José P. Airosa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ ## pi-sys (Raspbian system monitor)
2
+
3
+ [![Build Status](https://travis-ci.org/joseairosa/pi-sys.png?branch=master)](https://travis-ci.org/joseairosa/pi-sys)
4
+
5
+ Retrieve Raspbian OS system resource status
data/lib/pi-sys.rb ADDED
@@ -0,0 +1,32 @@
1
+ $:.push("#{File.dirname(__FILE__)}/..")
2
+
3
+ require 'lib/pi-sys/stats'
4
+ require 'lib/pi-sys/stats/vcgencmd'
5
+ require 'lib/pi-sys/stats/cpu'
6
+ require 'lib/pi-sys/stats/memory'
7
+ require 'lib/pi-sys/stats/clock'
8
+ require 'lib/pi-sys/stats/codec'
9
+ require 'lib/pi-sys/stats/config'
10
+ require 'lib/pi-sys/stats/disk'
11
+ require 'lib/pi-sys/stats/temperature'
12
+ require 'lib/pi-sys/stats/uptime'
13
+ require 'lib/pi-sys/stats/voltage'
14
+ require 'lib/pi-sys/stats/bandwidth'
15
+
16
+ module PiSys
17
+ STATS = {}
18
+ MODULES = [:cpu, :disk, :memory, :clock, :voltage, :temperature, :codec, :config, :uptime, :bandwidth].map { |mod|
19
+ {mod => const_get(mod.to_s.capitalize).new}
20
+ }.reduce({}, :merge)
21
+
22
+ class << self
23
+ MODULES.each do |mod, _|
24
+ define_method(mod) { MODULES[mod].fetch }
25
+ end
26
+ end
27
+
28
+ def self.fetch
29
+ MODULES.values.map(&:fetch)
30
+ STATS
31
+ end
32
+ end
@@ -0,0 +1,45 @@
1
+ module PiSys
2
+ class Stats
3
+ def initialize(key, variations)
4
+ @key, @variations = key, variations
5
+ STATS[@key] = {}
6
+
7
+ if @variations
8
+ @variations.each { |method|
9
+ self.class.send(:define_method, method, Proc.new{ STATS[@key][method.to_sym ] })
10
+ }
11
+ end
12
+ end
13
+
14
+ def to_hash(statistic, output, start_index=0)
15
+ output.strip.split("\n")[start_index..-1].each do |data_line|
16
+ data = data_line.split(' ')
17
+ unless data.empty?
18
+ if statistic.kind_of? Array
19
+ STATS[statistic[0]] ||= {}
20
+ STATS[statistic[0]][statistic[1]] ||= {}
21
+ STATS[statistic[0]][statistic[1]].merge!(yield(data)) if block_given?
22
+ else
23
+ STATS[statistic] ||= {}
24
+ STATS[statistic].merge!(yield(data)) if block_given?
25
+ end
26
+ end
27
+ end
28
+ STATS[statistic]
29
+ end
30
+
31
+ private
32
+
33
+ def reset
34
+ STATS[@key] = {}
35
+ end
36
+
37
+ def fetch
38
+ reset
39
+ end
40
+
41
+ def run_command(command)
42
+ `#{command}`
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ require 'usagewatch'
2
+
3
+ module PiSys
4
+ class Bandwidth < Stats
5
+
6
+ KEY = :bandwidth
7
+ VARIATIONS = nil
8
+
9
+ def initialize
10
+ super(KEY, VARIATIONS)
11
+ end
12
+
13
+ def fetch
14
+ super
15
+
16
+ # We want this in bytes
17
+ STATS[KEY] = {rx: Usagewatch.uw_bandrx / 8, tx: Usagewatch.uw_bandtx / 8}
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module PiSys
2
+ class Clock < Vcgencmd
3
+ KEY = :clock
4
+ COMMAND = :measure_clock
5
+
6
+ def initialize
7
+ super(KEY, COMMAND)
8
+ end
9
+
10
+ def fetch
11
+ super
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module PiSys
2
+ class Codec < Vcgencmd
3
+ KEY = :codec
4
+ COMMAND = :codec_enabled
5
+
6
+ def initialize
7
+ super(KEY, COMMAND)
8
+ end
9
+
10
+ def fetch
11
+ super
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module PiSys
2
+ class Config < Vcgencmd
3
+ KEY = :config
4
+ COMMAND = :get_config
5
+
6
+ def initialize
7
+ super(KEY, COMMAND)
8
+ end
9
+
10
+ def fetch
11
+ super
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ module PiSys
2
+ class Cpu < Stats
3
+
4
+ KEY = :cpu
5
+ VARIATIONS = nil
6
+
7
+ def initialize
8
+ super(KEY, VARIATIONS)
9
+ end
10
+
11
+ def fetch
12
+ super
13
+
14
+ output = run_command 'pidstat -h'
15
+
16
+ to_hash(KEY, output, 3) do |data|
17
+ name = data[7]
18
+ {name => {user: data[2].to_f, system: data[3].to_f, guest: data[4].to_f, total: data[5].to_f}}
19
+ end
20
+
21
+ Hash[STATS[KEY].sort_by{|_, v| -v[:total]}]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ module PiSys
2
+ class Disk < Stats
3
+
4
+ KEY = :disk
5
+ VARIATIONS = [:io, :usage]
6
+
7
+ def initialize
8
+ super(KEY, VARIATIONS)
9
+ end
10
+
11
+ def fetch
12
+ super
13
+
14
+ output = fetch_io
15
+ to_hash([KEY, :io], output, 3) do |data|
16
+ name = data[5]
17
+ {name => {read: data[2], write: data[3]}}
18
+ end
19
+
20
+ output = fetch_usage
21
+ to_hash([KEY, :usage], output, 1) do |data|
22
+ name = data[0]
23
+ {name => {used: data[2], available: data[3].to_i, percentage: data[4].to_i, mounted_on: data[5]}}
24
+ end
25
+
26
+ STATS[KEY][:usage] = Hash[STATS[KEY][:usage].sort_by { |_, v| -v[:percentage] }]
27
+ STATS[KEY]
28
+ end
29
+
30
+ private
31
+
32
+ def fetch_io
33
+ run_command('pidstat -dh')
34
+ end
35
+
36
+ def fetch_usage
37
+ run_command('df')
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,59 @@
1
+ module PiSys
2
+ class Memory < Stats
3
+
4
+ KEY = :memory
5
+ VARIATIONS = [:status, :usage, :info]
6
+ COMMAND = :get_mem
7
+
8
+ def initialize
9
+ @vcgencmd = Vcgencmd.new(KEY, COMMAND)
10
+ super(KEY, VARIATIONS)
11
+ end
12
+
13
+ def fetch
14
+ super
15
+
16
+ # status
17
+ output = fetch_status
18
+ output.gsub!(/ kB$/, '').gsub(/\: */, ' ')
19
+ to_hash([KEY, :status], output) do |data|
20
+ {data[0].gsub(':','') => data[1].to_i / 1024}
21
+ end
22
+
23
+ STATS[KEY][:status]['MemUsed'] = STATS[KEY][:status]['MemTotal'] - STATS[KEY][:status]['MemFree'] -
24
+ STATS[KEY][:status]['Buffers'] - STATS[KEY][:status]['Cached']
25
+
26
+ # info
27
+ STATS[KEY][:info] = @vcgencmd.fetch
28
+
29
+ # usage
30
+ output = fetch_usage
31
+ to_hash([KEY, :usage], output, 1) do |data|
32
+ name = data[0]
33
+ value = data[1].to_i / 1024
34
+ if STATS[KEY][:usage][name]
35
+ STATS[KEY][:usage][name] += value
36
+ {}
37
+ else
38
+ {name => value}
39
+ end
40
+ end
41
+
42
+ STATS[KEY][:usage] = Hash[STATS[KEY][:usage].sort_by { |_, v| -v }]
43
+ STATS[KEY]
44
+ end
45
+
46
+ private
47
+
48
+ def fetch_status
49
+ file = File.open('/proc/meminfo', 'rb')
50
+ output = file.read
51
+ file.close
52
+ output
53
+ end
54
+
55
+ def fetch_usage
56
+ run_command 'ps -eo comm,rss'
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,14 @@
1
+ module PiSys
2
+ class Temperature < Vcgencmd
3
+ KEY = :temperature
4
+ COMMAND = :measure_temp
5
+
6
+ def initialize
7
+ super(KEY, COMMAND)
8
+ end
9
+
10
+ def fetch
11
+ super
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,31 @@
1
+ module PiSys
2
+ class Uptime < Stats
3
+
4
+ KEY = :uptime
5
+ VARIATIONS = nil
6
+
7
+ def initialize
8
+ super(KEY, VARIATIONS)
9
+ end
10
+
11
+ def fetch
12
+ super
13
+
14
+ output = run_command 'uptime'
15
+
16
+ to_hash(KEY, output) do |data|
17
+ index_of_reference = data.index('user,') || data.index('users,')
18
+ {
19
+ time: data[0],
20
+ up: data[2..index_of_reference-2].join(' ').gsub(/(,$)/i,''),
21
+ users: data[index_of_reference-1],
22
+ load_average: {
23
+ one: data[data.length-3].gsub(',',''),
24
+ five: data[data.length-2].gsub(',',''),
25
+ fiftheen: data[data.length-1].gsub(',','')
26
+ }
27
+ }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ module PiSys
2
+ class Vcgencmd < Stats
3
+
4
+ COMMANDS = {
5
+ measure_clock: %w(arm core h264 isp v3d uart pwm emmc pixel vec hdmi dpi),
6
+ measure_volts: %w(core sdram_c sdram_i sdram_p),
7
+ measure_temp: nil,
8
+ codec_enabled: %w(H264 MPG2 WVC1 MPG4 MJPG WMV9),
9
+ get_config: %w(int),
10
+ get_mem: %w(arm gpu)
11
+ }
12
+
13
+ def initialize(key, command)
14
+ @key, @command = key, command
15
+ super(@key, nil)
16
+ end
17
+
18
+ def fetch(command=@command)
19
+ if COMMANDS.has_key? command
20
+ if COMMANDS[command]
21
+ hash ||= {}
22
+ COMMANDS[command].each do |option|
23
+ output = run_command(command, option)
24
+ hash[option] = Hash[output.strip.split("\n").map {|entry| entry.split('=')}]
25
+ end
26
+ hash
27
+ else
28
+ output = run_command(command)
29
+ Hash[output.strip.split("\n").map {|entry| entry.split('=')}]
30
+ end
31
+ end
32
+ end
33
+
34
+ def run_command(command, option=nil)
35
+ super("/opt/vc/bin/vcgencmd #{command} #{option.to_s}")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module PiSys
2
+ class Voltage < Vcgencmd
3
+ KEY = :voltage
4
+ COMMAND = :measure_volts
5
+
6
+ def initialize
7
+ super(KEY, COMMAND)
8
+ end
9
+
10
+ def fetch
11
+ super
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module PiSys
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pi-sys
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - José Airosa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: usagewatch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-plus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 10.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 10.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.1
69
+ description: Retrieve Raspbian OS system resource status.
70
+ email: me@joseairosa.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/pi-sys/stats/bandwidth.rb
76
+ - lib/pi-sys/stats/clock.rb
77
+ - lib/pi-sys/stats/codec.rb
78
+ - lib/pi-sys/stats/config.rb
79
+ - lib/pi-sys/stats/cpu.rb
80
+ - lib/pi-sys/stats/disk.rb
81
+ - lib/pi-sys/stats/memory.rb
82
+ - lib/pi-sys/stats/temperature.rb
83
+ - lib/pi-sys/stats/uptime.rb
84
+ - lib/pi-sys/stats/vcgencmd.rb
85
+ - lib/pi-sys/stats/voltage.rb
86
+ - lib/pi-sys/stats.rb
87
+ - lib/pi-sys/version.rb
88
+ - lib/pi-sys.rb
89
+ - LICENSE
90
+ - README.md
91
+ homepage:
92
+ licenses: []
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.14
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Raspbian system status
114
+ test_files: []
115
+ has_rdoc: