vidibus-sysinfo 0.0.10 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2011-2013 Andre Pankratz
3
+ Copyright (c) 2011-2014 Andre Pankratz
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -30,11 +30,14 @@ This gem uses various tools to obtain system data. Installing those tools should
30
30
 
31
31
  ### CPU cores
32
32
 
33
- To get the number of CPU cores, call
33
+ To get general system information, call
34
34
 
35
35
  ```ruby
36
- Vidibus::Sysinfo.core
37
- # => 4
36
+ Vidibus::Sysinfo.system.to_h # following metrics as hash
37
+
38
+ Vidibus::Sysinfo.system.cpus # number of CPUs
39
+ Vidibus::Sysinfo.system.cores # number of cores
40
+ Vidibus::Sysinfo.system.sockets # number of sockets
38
41
  ```
39
42
 
40
43
  ### CPU utilization
@@ -42,8 +45,20 @@ To get the number of CPU cores, call
42
45
  To get CPU utilization in percent, call
43
46
 
44
47
  ```ruby
45
- Vidibus::Sysinfo.cpu
46
- # => 18.4 # percent
48
+ Vidibus::Sysinfo.cpu.to_f # usage total
49
+ Vidibus::Sysinfo.cpu.to_i # rounded usage total
50
+ Vidibus::Sysinfo.cpu.to_h # following metrics as hash
51
+
52
+ Vidibus::Sysinfo.cpu.used # usage total
53
+ Vidibus::Sysinfo.cpu.idle # idle
54
+ Vidibus::Sysinfo.cpu.user # on user level
55
+ Vidibus::Sysinfo.cpu.nice # with nice priority
56
+ Vidibus::Sysinfo.cpu.system # on system level
57
+ Vidibus::Sysinfo.cpu.iowait # waiting for IO
58
+ Vidibus::Sysinfo.cpu.irq # caused by service interrupts
59
+ Vidibus::Sysinfo.cpu.soft # caused by software interrupts
60
+ Vidibus::Sysinfo.cpu.steal # caused by virtualization hypervisor
61
+ Vidibus::Sysinfo.cpu.guest # on virtual processors
47
62
  ```
48
63
 
49
64
  ### System load
@@ -52,8 +67,12 @@ The system load value is divided by the number of CPU cores.
52
67
  To get the system load, call
53
68
 
54
69
  ```ruby
55
- Vidibus::Sysinfo.load
56
- # => 0.39
70
+ Vidibus::Sysinfo.load.to_f # system load over last minute
71
+ Vidibus::Sysinfo.load.to_h # following metrics as hash
72
+
73
+ Vidibus::Sysinfo.load.one # system load over last minute
74
+ Vidibus::Sysinfo.load.five # system load over five minutes
75
+ Vidibus::Sysinfo.load.fifteen # system load over fifteen minutes
57
76
  ```
58
77
 
59
78
  ### Monthly traffic
@@ -61,8 +80,13 @@ To get the system load, call
61
80
  To get the total traffic of this month in gigabytes, call
62
81
 
63
82
  ```ruby
64
- Vidibus::Sysinfo.traffic
65
- # => 7992.15 # gigabytes
83
+ Vidibus::Sysinfo.traffic.to_f # traffic total
84
+ Vidibus::Sysinfo.traffic.to_i # rounded traffic total
85
+ Vidibus::Sysinfo.traffic.to_h # following metrics as hash
86
+
87
+ Vidibus::Sysinfo.traffic.total # traffic total
88
+ Vidibus::Sysinfo.traffic.input # input traffic
89
+ Vidibus::Sysinfo.traffic.output # output traffic
66
90
  ```
67
91
 
68
92
  ### Currently used throughput
@@ -70,8 +94,13 @@ To get the total traffic of this month in gigabytes, call
70
94
  To get the currently used throughput in MBit/s, call
71
95
 
72
96
  ```ruby
73
- Vidibus::Sysinfo.throughput
74
- # => 38.71 # MBit/s
97
+ Vidibus::Sysinfo.throughput.to_f # throughput total
98
+ Vidibus::Sysinfo.throughput.to_i # rounded throughput total
99
+ Vidibus::Sysinfo.throughput.to_h # following metrics as hash
100
+
101
+ Vidibus::Sysinfo.throughput.total # throughput total
102
+ Vidibus::Sysinfo.throughput.input # input throughput
103
+ Vidibus::Sysinfo.throughput.output # output throughput
75
104
  ```
76
105
 
77
106
  Throughput detection is performed by analyzing the output of /proc/net/dev.
@@ -84,39 +113,53 @@ an optional argument:
84
113
 
85
114
  ```ruby
86
115
  Vidibus::Sysinfo.throughput(10)
87
- # => 33.28 # MBit/s
88
116
  ```
89
117
 
90
- ### Consumed storage
118
+ ### Storage
91
119
 
92
- To get the consumed storage of the main device in gigabytes, call
120
+ To get storage information on the main device in gigabytes, call
93
121
 
94
122
  ```ruby
95
- Vidibus::Sysinfo.storage
96
- # => 647.89 # gigabytes
123
+ Vidibus::Sysinfo.storage.to_f # used storage
124
+ Vidibus::Sysinfo.storage.to_i # rounded used storage
125
+ Vidibus::Sysinfo.storage.to_h # following metrics as hash
126
+
127
+ Vidibus::Sysinfo.storage.used # used storage
128
+ Vidibus::Sysinfo.storage.free # free storage
129
+ Vidibus::Sysinfo.storage.total # disk size
97
130
  ```
98
131
 
99
- ### Used memory
132
+ ### Memory
100
133
 
101
134
  To get the currently used memory in megabytes, call
102
135
 
103
136
  ```ruby
104
- Vidibus::Sysinfo.memory
105
- # => 3281.38 # megabytes
137
+ Vidibus::Sysinfo.memory.to_f # used memory
138
+ Vidibus::Sysinfo.memory.to_i # rounded used memory
139
+ Vidibus::Sysinfo.memory.to_h # following metrics as hash
140
+
141
+ Vidibus::Sysinfo.memory.used # used memory
142
+ Vidibus::Sysinfo.memory.free # free memory
143
+ Vidibus::Sysinfo.memory.total # memory size
106
144
  ```
107
145
 
108
146
  This will ignore memory used for system caching.
109
147
 
110
148
 
111
- ### Used swap
149
+ ### Swap
112
150
 
113
151
  To get the currently used swap in megabytes, call
114
152
 
115
153
  ```ruby
116
- Vidibus::Sysinfo.swap
117
- # => 0.0 # megabytes
154
+ Vidibus::Sysinfo.swap.to_f # used swap
155
+ Vidibus::Sysinfo.swap.to_i # rounded used swap
156
+ Vidibus::Sysinfo.swap.to_h # following metrics as hash
157
+
158
+ Vidibus::Sysinfo.swap.used # used swap
159
+ Vidibus::Sysinfo.swap.free # free swap
160
+ Vidibus::Sysinfo.swap.total # swap size
118
161
  ```
119
162
 
120
163
  ## Copyright
121
164
 
122
- Copyright (c) 2011-2013 Andre Pankratz. See LICENSE for details.
165
+ Copyright (c) 2011-2014 Andre Pankratz. See LICENSE for details.
@@ -7,6 +7,7 @@ module Vidibus
7
7
 
8
8
  # Provides common methods.
9
9
  module Base
10
+ include Helper
10
11
 
11
12
  def call
12
13
  output, error = perform(command)
@@ -45,12 +46,6 @@ module Vidibus
45
46
  end
46
47
  end
47
48
 
48
- # Rounds float with given precision.
49
- def round(float, precision = 2)
50
- m = 10**precision
51
- (float.to_f*m).round.to_f/m
52
- end
53
-
54
49
  # Converts given amount from unit to megabytes.
55
50
  # Treats GB and GiB identically because in our context GB == GiB.
56
51
  def megabytes(value, unit = "B")
@@ -12,14 +12,40 @@ module Vidibus
12
12
  module Cpu
13
13
  extend Base
14
14
 
15
+ class Result < Vidibus::Sysinfo::Result
16
+ attrs :user, :nice, :system, :iowait, :irq, :soft, :steal, :guest, :idle, :used
17
+
18
+ def to_i
19
+ round(used, 0).to_i
20
+ end
21
+
22
+ def to_f
23
+ used
24
+ end
25
+ end
26
+
15
27
  class << self
16
28
  def command
17
29
  "mpstat 1 5 | grep Average:"
18
30
  end
19
31
 
20
32
  def parse(output)
21
- if output.match(/([\d\.]+)$/)
22
- round(100.0 - $1.to_f)
33
+ matches = output.scan(/([\d\.]+)/)
34
+ if matches.any?
35
+ matches.flatten!
36
+ data = {
37
+ user: matches[0].to_f,
38
+ nice: matches[1].to_f,
39
+ system: matches[2].to_f,
40
+ iowait: matches[3].to_f,
41
+ irq: matches[4].to_f,
42
+ soft: matches[5].to_f,
43
+ steal: matches[6].to_f,
44
+ guest: matches[7].to_f,
45
+ idle: matches[8].to_f
46
+ }
47
+ data[:used] = round(100.0 - data[:idle])
48
+ Result.new(data)
23
49
  end
24
50
  end
25
51
 
@@ -0,0 +1,7 @@
1
+ module Helper
2
+ # Rounds float with given precision.
3
+ def round(float, precision = 2)
4
+ m = 10**precision
5
+ (float.to_f*m).round.to_f/m
6
+ end
7
+ end
@@ -8,16 +8,31 @@ module Vidibus
8
8
  module Load
9
9
  extend Base
10
10
 
11
+ class Result < Vidibus::Sysinfo::Result
12
+ attrs :one, :five, :fifteen
13
+
14
+ def to_f
15
+ one
16
+ end
17
+ end
18
+
11
19
  class << self
12
20
  def command
13
21
  "uptime"
14
22
  end
15
23
 
16
24
  def parse(output)
17
- if output.match(/load average:\s+(\d+(?:\.\d+)?)/)
18
- value = $1.to_f
19
- cores = Core.call
20
- round(value/cores)
25
+ number = /\s+(\d+(?:\.\d+)?)/
26
+ if output.match(/load average:#{number},#{number},#{number}/)
27
+ one = $1.to_f
28
+ five = $2.to_f
29
+ fifteen = $3.to_f
30
+ cpus = System.call[:cpus]
31
+ Result.new({
32
+ one: round(one/cpus),
33
+ five: round(five/cpus),
34
+ fifteen: round(fifteen/cpus)
35
+ })
21
36
  end
22
37
  end
23
38
  end
@@ -8,6 +8,18 @@ module Vidibus
8
8
  module Memory
9
9
  extend Base
10
10
 
11
+ class Result < Vidibus::Sysinfo::Result
12
+ attrs :total, :used, :free
13
+
14
+ def to_i
15
+ used
16
+ end
17
+
18
+ def to_f
19
+ used.to_f
20
+ end
21
+ end
22
+
11
23
  class << self
12
24
  def command
13
25
  "free -m | grep Mem:"
@@ -16,9 +28,15 @@ module Vidibus
16
28
  def parse(output)
17
29
  if output.match(/^Mem:\s+([\d\s]+)$/)
18
30
  numbers = $1.split(/\s+/)
19
- used = numbers[1].to_i
31
+ total = numbers[0].to_i
32
+ buffers = numbers[4].to_i
20
33
  cached = numbers[5].to_i
21
- used - cached
34
+ used = numbers[1].to_i - buffers - cached
35
+ Result.new({
36
+ total: total,
37
+ used: used,
38
+ free: total - used
39
+ })
22
40
  end
23
41
  end
24
42
  end
@@ -0,0 +1,34 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+ class Result
4
+ include Helper
5
+
6
+ def initialize(options)
7
+ attrs.each do |attr|
8
+ instance_variable_set("@#{attr}", options[attr])
9
+ end
10
+ end
11
+
12
+ def to_h
13
+ {}.tap do |hash|
14
+ attrs.each do |attr|
15
+ hash[attr] = send(attr)
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def attrs
23
+ self.class.instance_variable_get('@attrs')
24
+ end
25
+
26
+ class << self
27
+ def attrs(*args)
28
+ self.send(:attr, *args)
29
+ @attrs = args
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -8,6 +8,18 @@ module Vidibus
8
8
  module Storage
9
9
  extend Base
10
10
 
11
+ class Result < Vidibus::Sysinfo::Result
12
+ attrs :total, :used, :free
13
+
14
+ def to_i
15
+ round(used, 0).to_i
16
+ end
17
+
18
+ def to_f
19
+ used.to_f
20
+ end
21
+ end
22
+
11
23
  class << self
12
24
  def command
13
25
  'df -m'
@@ -16,9 +28,15 @@ module Vidibus
16
28
  def parse(output)
17
29
  device = /(?:[\/a-z0-9]+)/
18
30
  size = /\s+(\d+)\i?/
19
- if output.match(/#{device}#{size}#{size}/m)
20
- amount = $2.to_f
21
- gigabytes(amount, 'M')
31
+ if matches = output.match(/#{device}#{size}#{size}#{size}/m)
32
+ total = $1.to_f
33
+ used = $2.to_f
34
+ free = $3.to_f
35
+ Result.new({
36
+ total: gigabytes(total, 'M'),
37
+ used: gigabytes(used, 'M'),
38
+ free: gigabytes(free, 'M')
39
+ })
22
40
  end
23
41
  end
24
42
  end
@@ -8,6 +8,18 @@ module Vidibus
8
8
  module Swap
9
9
  extend Base
10
10
 
11
+ class Result < Vidibus::Sysinfo::Result
12
+ attrs :total, :used, :free
13
+
14
+ def to_i
15
+ round(used, 0).to_i
16
+ end
17
+
18
+ def to_f
19
+ used.to_f
20
+ end
21
+ end
22
+
11
23
  class << self
12
24
  def command
13
25
  "free -m | grep Swap:"
@@ -16,7 +28,11 @@ module Vidibus
16
28
  def parse(output)
17
29
  if output.match(/^Swap:\s+([\d\s]+)$/)
18
30
  numbers = $1.split(/\s+/)
19
- numbers[1].to_i
31
+ Result.new({
32
+ total: numbers[0].to_i,
33
+ used: numbers[1].to_i,
34
+ free: numbers[2].to_i
35
+ })
20
36
  end
21
37
  end
22
38
  end
@@ -5,17 +5,28 @@ module Vidibus
5
5
  #
6
6
  # Analyzes lscpu
7
7
  #
8
- module Core
8
+ module System
9
9
  extend Base
10
10
 
11
+ class Result < Vidibus::Sysinfo::Result
12
+ attrs :cpus, :cores, :sockets
13
+ end
14
+
11
15
  class << self
12
16
  def command()
13
17
  'lscpu'
14
18
  end
15
19
 
16
20
  def parse(output)
17
- if cpus = output[/CPU\(s\):\s+(\d+)/i, 1]
18
- cpus.to_i
21
+ sockets = output[/Socket\(s\):\s+(\d+)/i, 1]
22
+ cores = output[/Core\(s\) per socket:\s+(\d+)/i, 1]
23
+ cpus = output[/CPU\(s\):\s+(\d+)/i, 1]
24
+ if sockets && cores && cpus
25
+ Result.new({
26
+ sockets: sockets.to_i,
27
+ cores: cores.to_i,
28
+ cpus: cpus.to_i
29
+ })
19
30
  end
20
31
  end
21
32
 
@@ -8,6 +8,26 @@ module Vidibus
8
8
  module Throughput
9
9
  extend Base
10
10
 
11
+ class Result < Vidibus::Sysinfo::Result
12
+ attrs :input, :output
13
+
14
+ def to_h
15
+ super.merge(total: total)
16
+ end
17
+
18
+ def total
19
+ round(input + output)
20
+ end
21
+
22
+ def to_i
23
+ round(total, 0).to_i
24
+ end
25
+
26
+ def to_f
27
+ total.to_f
28
+ end
29
+ end
30
+
11
31
  class << self
12
32
  def command
13
33
  "cat /proc/net/dev | grep eth0:"
@@ -23,18 +43,24 @@ module Vidibus
23
43
  values << respond(output, error)
24
44
  sleep(seconds) unless values.size > 1
25
45
  end
26
- megs = values[1] - values[0]
27
- mbits = (megs*8)/seconds
28
- round(mbits)
46
+ result = {}
47
+ [:input, :output].each do |type|
48
+ megs = values[1][type] - values[0][type]
49
+ result[type] = round((megs*8)/seconds)
50
+ end
51
+ Result.new(result)
29
52
  end
30
53
 
31
- # Returns sum of transmitted and received bytes in megabytes.
54
+ # Returns received and sent megabytes.
32
55
  def parse(output)
33
56
  if output.match(/eth0\:\s*([\d\s]+)/)
34
57
  numbers = $1.split(/\s+/)
35
- received = numbers[0].to_i
36
- transmitted = numbers[8].to_i
37
- megabytes(received + transmitted)
58
+ input = numbers[0].to_i
59
+ output = numbers[8].to_i
60
+ {
61
+ input: megabytes(input),
62
+ output: megabytes(output)
63
+ }
38
64
  end
39
65
  end
40
66
 
@@ -12,6 +12,26 @@ module Vidibus
12
12
  module Traffic
13
13
  extend Base
14
14
 
15
+ class Result < Vidibus::Sysinfo::Result
16
+ attrs :input, :output
17
+
18
+ def to_h
19
+ super.merge(total: total)
20
+ end
21
+
22
+ def total
23
+ round(input + output)
24
+ end
25
+
26
+ def to_i
27
+ round(total, 0).to_i
28
+ end
29
+
30
+ def to_f
31
+ total.to_f
32
+ end
33
+ end
34
+
15
35
  class << self
16
36
  def command
17
37
  "vnstat -m"
@@ -21,10 +41,15 @@ module Vidibus
21
41
  month = /(\s*\w{3} \'\d{2})/
22
42
  traffic = /\s*(\d+(?:\.\d+)?) (ki?B|Mi?B|Gi?B|Ti?B)\s*/i
23
43
  last_month = output.split(/\r?\n/)[-3]
24
- if last_month and last_month.match(/#{month}#{traffic}\|#{traffic}\|#{traffic}+/m)
25
- amount = $6.to_f
26
- unit = $7
27
- gigabytes(amount, unit)
44
+ if last_month && last_month.match(/#{month}#{traffic}\|#{traffic}\|#{traffic}+/m)
45
+ input_amount = $2.to_f
46
+ input_unit = $3
47
+ output_amount = $4.to_f
48
+ output_unit = $5
49
+ Result.new({
50
+ input: gigabytes(input_amount, input_unit),
51
+ output: gigabytes(output_amount, output_unit)
52
+ })
28
53
  elsif output.match("Not enough data available yet")
29
54
  0.0
30
55
  end
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Sysinfo
3
- VERSION = '0.0.10'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
@@ -1,6 +1,8 @@
1
1
  require 'open3'
2
+ require 'vidibus/sysinfo/helper'
2
3
  require 'vidibus/sysinfo/base'
3
- require 'vidibus/sysinfo/core'
4
+ require 'vidibus/sysinfo/result'
5
+ require 'vidibus/sysinfo/system'
4
6
  require 'vidibus/sysinfo/cpu'
5
7
  require 'vidibus/sysinfo/load'
6
8
  require 'vidibus/sysinfo/traffic'
@@ -13,9 +15,9 @@ module Vidibus
13
15
  module Sysinfo
14
16
  class << self
15
17
 
16
- # Returns number of cpu cores.
17
- def core
18
- Core.call
18
+ # Returns system information like CPUs and cores.
19
+ def system
20
+ System.call
19
21
  end
20
22
 
21
23
  # Returns CPU utilization in percent.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-sysinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-09 00:00:00.000000000 Z
12
+ date: 2014-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -114,12 +114,14 @@ extensions: []
114
114
  extra_rdoc_files: []
115
115
  files:
116
116
  - lib/vidibus/sysinfo/base.rb
117
- - lib/vidibus/sysinfo/core.rb
118
117
  - lib/vidibus/sysinfo/cpu.rb
118
+ - lib/vidibus/sysinfo/helper.rb
119
119
  - lib/vidibus/sysinfo/load.rb
120
120
  - lib/vidibus/sysinfo/memory.rb
121
+ - lib/vidibus/sysinfo/result.rb
121
122
  - lib/vidibus/sysinfo/storage.rb
122
123
  - lib/vidibus/sysinfo/swap.rb
124
+ - lib/vidibus/sysinfo/system.rb
123
125
  - lib/vidibus/sysinfo/throughput.rb
124
126
  - lib/vidibus/sysinfo/traffic.rb
125
127
  - lib/vidibus/sysinfo/version.rb
@@ -143,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
145
  version: '0'
144
146
  segments:
145
147
  - 0
146
- hash: -4266894747005125364
148
+ hash: 2849344004889068849
147
149
  required_rubygems_version: !ruby/object:Gem::Requirement
148
150
  none: false
149
151
  requirements: