usagewatch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTlkNDAyMDAzOGRkMGRlOWJmZjQ0MDE2Y2YyOTNiNmI1NWRlMTI0Mg==
5
+ data.tar.gz: !binary |-
6
+ MDdlMmFkNGMwN2U3NTc1YmVkZTJhZTlhZjRlN2I0NWViYzg5NGVlZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZDVjMWI2ZDAxZTM2NzYwMzA4NDhiNjMxNThmYmNjYTlkMzY5NWQ2ZTU3MmNi
10
+ NjMxODBiNmM4YjFiYzM1MWRjM2FjY2NkYjg3MGYyOTQ4ZWFlZWQ1ODhmYjIy
11
+ OTJmNTU1NGE5ZmRmZjIyYjdkMzg2MGJlOTk0MTg5Y2MwNTM1ZDQ=
12
+ data.tar.gz: !binary |-
13
+ ZmY0MTNmMTkwMWY3ZmFkYjRmNWEyZDI0NzdlZGQ3YzI5NTI1NDJhNjQ2MmM0
14
+ MTAzNmJiZTdhNGNmZWMwMTU3YzhhMmJkY2FhMTQzMzY3MTE1NWQ3NDA2OWNi
15
+ NWUxYWQ2MGZmNWMwOGMxOWIwZjdiNzgwZTg1OTVjNWFlMjY0ZTI=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in usagewatch.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ruben Espinosa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Usagewatch
2
+
3
+ A Ruby Class with methods to find usage statistics on a Linux server such as CPU, Disk, TCP/UDP
4
+ Connections, Load, Bandwidth, Disk I/O, and Memory
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'usagewatch'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install usagewatch
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ require 'usagewatch'
24
+
25
+ include Usagewatch
26
+
27
+ puts "#{uw_diskused} Gigabytes Used"
28
+ puts "#{uw_cpuused}% CPU Used"
29
+ puts "#{uw_tcpused} TCP Connections Used"
30
+ puts "#{uw_udpused} UDP Connections Used"
31
+ puts "#{uw_memused}% Active Memory Used"
32
+ puts "#{uw_load} Average System Load Of The Past Minute"
33
+ puts "#{uw_bandrx} Mbit/s Current Bandwidth Received"
34
+ puts "#{uw_bandtx} Mbit/s Current Bandwidth Transmitted"
35
+ puts "#{uw_diskioreads}/s Current Disk Reads Completed"
36
+ puts "#{uw_diskiowrites}/s Current Disk Writes Completed"
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
46
+
47
+ ## Notes
48
+
49
+ * Disk Used is a sum of all partitions calculated in Gigabytes
50
+
51
+ * Disk Used Percentage is a total percentage of all disk partitions used
52
+
53
+ * CPU Used is a percentage of current CPU being used
54
+
55
+ * TCP/UDP Connections Used is a total count of each respectively
56
+
57
+ * Active Memory Used is a percentage of active system memory being used
58
+
59
+ * Load is the average load of the past minute
60
+
61
+ * Bandwidth is current received and transmitted in Megabits
62
+
63
+ * Disk IO is current disk reads and writes completed per second
64
+
65
+ ## Tested Using
66
+
67
+ RUBY VERSIONS: ruby 1.9.3p429 (2013-05-15) [x86_64-linux], ruby2.0
68
+
69
+ OS VERSIONS: CENTOS 5x 6x, Ubuntu 12.04
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/ruby
2
+
3
+ #License: (MIT), Copyright (C) 2013 Author Phil Chen.
4
+
5
+ require 'usagewatch'
6
+
7
+ include Usagewatch
8
+
9
+ puts "#{uw_diskused} Gigabytes Used"
10
+ puts "#{uw_cpuused}% CPU Used"
11
+ puts "#{uw_tcpused} TCP Connections Used"
12
+ puts "#{uw_udpused} UDP Connections Used"
13
+ puts "#{uw_memused}% Active Memory Used"
14
+ puts "#{uw_load} Average System Load Of The Past Minute"
15
+ puts "#{uw_bandrx} Mbit/s Current Bandwidth Received"
16
+ puts "#{uw_bandtx} Mbit/s Current Bandwidth Transmitted"
17
+ puts "#{uw_diskioreads}/s Current Disk Reads Completed"
18
+ puts "#{uw_diskiowrites}/s Current Disk Writes Completed"
@@ -0,0 +1,3 @@
1
+ module Usagewatch
2
+ VERSION = "0.0.1"
3
+ end
data/lib/usagewatch.rb ADDED
@@ -0,0 +1,344 @@
1
+ require "usagewatch/version"
2
+
3
+ module Usagewatch
4
+ def uw_diskused
5
+ @df = `df`
6
+ @parts = @df.split(" ").map { |s| s.to_i }
7
+ @sum = 0
8
+ for i in (9..@parts.size - 1).step(6) do
9
+ @sum += @parts[i]
10
+ end
11
+ @round = @sum.round(2)
12
+ @totaldiskused = ((@round/1024)/1024).round(2)
13
+
14
+ return @totaldiskused
15
+ end
16
+
17
+ # Show the percentage of disk used.
18
+ def uw_diskused_perc
19
+ df = `df --total`
20
+ df.split(" ").last.to_f.round(2)
21
+ end
22
+
23
+ def uw_cpuused
24
+ @proc0 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
25
+ sleep 1
26
+ @proc1 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
27
+
28
+ @proc0usagesum = @proc0[1].to_i + @proc0[2].to_i + @proc0[3].to_i
29
+ @proc1usagesum = @proc1[1].to_i + @proc1[2].to_i + @proc1[3].to_i
30
+ @procusage = @proc1usagesum - @proc0usagesum
31
+
32
+ @proc0total = 0
33
+ for i in (1..4) do
34
+ @proc0total += @proc0[i].to_i
35
+ end
36
+ @proc1total = 0
37
+ for i in (1..4) do
38
+ @proc1total += @proc1[i].to_i
39
+ end
40
+ @proctotal = (@proc1total - @proc0total)
41
+
42
+ @cpuusage = (@procusage.to_f / @proctotal.to_f)
43
+ @cpuusagepercentage = (100 * @cpuusage).to_f.round(2)
44
+
45
+ return @cpuusagepercentage
46
+ end
47
+
48
+ def uw_tcpused
49
+ if File.exists?("/proc/net/sockstat")
50
+ File.open("/proc/net/sockstat", "r") do |ipv4|
51
+ @sockstat = ipv4.read
52
+ end
53
+
54
+ @tcp4data = @sockstat.split
55
+ @tcp4count = @tcp4data[5]
56
+ end
57
+
58
+ if File.exists?("/proc/net/sockstat6")
59
+ File.open("/proc/net/sockstat6", "r") do |ipv6|
60
+ @sockstat6 = ipv6.read
61
+
62
+ end
63
+
64
+ @tcp6data = @sockstat6.split
65
+ @tcp6count = @tcp6data[2]
66
+ end
67
+
68
+ @totaltcpused = @tcp4count.to_i + @tcp6count.to_i
69
+
70
+ return @totaltcpused
71
+ end
72
+
73
+ def uw_udpused
74
+ if File.exists?("/proc/net/sockstat")
75
+ File.open("/proc/net/sockstat", "r") do |ipv4|
76
+ @sockstat = ipv4.read
77
+ end
78
+
79
+ @udp4data = @sockstat.split
80
+ @udp4count = @udp4data[16]
81
+ end
82
+
83
+ if File.exists?("/proc/net/sockstat6")
84
+ File.open("/proc/net/sockstat6", "r") do |ipv6|
85
+ @sockstat6 = ipv6.read
86
+ end
87
+
88
+ @udp6data = @sockstat6.split
89
+ @udp6count = @udp6data[5]
90
+ end
91
+
92
+ @totaludpused = @udp4count.to_i + @udp6count.to_i
93
+
94
+ return @totaludpused
95
+ end
96
+
97
+ def uw_memused
98
+ if File.exists?("/proc/meminfo")
99
+ File.open("/proc/meminfo", "r") do |file|
100
+ @result = file.read
101
+ end
102
+ end
103
+
104
+ @memstat = @result.split("\n").collect{|x| x.strip}
105
+ @memtotal = @memstat[0].gsub(/[^0-9]/, "")
106
+ @memactive = @memstat[5].gsub(/[^0-9]/, "")
107
+ @memactivecalc = (@memactive.to_f * 100) / @memtotal.to_f
108
+ @memusagepercentage = @memactivecalc.round
109
+
110
+ return @memusagepercentage
111
+ end
112
+
113
+ def uw_load
114
+ if File.exists?("/proc/loadavg")
115
+ File.open("/proc/loadavg", "r") do |file|
116
+ @loaddata = file.read
117
+ end
118
+
119
+ @load = @loaddata.split(/ /).first
120
+ end
121
+
122
+ return @load
123
+ end
124
+
125
+ def uw_bandrx
126
+
127
+ def bandrx
128
+
129
+ if File.exists?("/proc/net/dev")
130
+ File.open("/proc/net/dev", "r") do |file|
131
+ @result = file.read
132
+ end
133
+ end
134
+
135
+ @arrRows = @result.split("\n")
136
+
137
+ @arrEthLoRows = @arrRows.grep(/eth|lo/)
138
+
139
+ rowcount = (@arrEthLoRows.count - 1)
140
+
141
+ for i in (0..rowcount)
142
+ @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ")
143
+ end
144
+
145
+ @arrColumns = Array.new
146
+ for l in (0..rowcount)
147
+ @temp = Array.new
148
+ @temp[0] = @arrEthLoRows[l][1]
149
+ @temp[1] = @arrEthLoRows[l][9]
150
+ @arrColumns << @temp
151
+ end
152
+
153
+ columncount = (@arrColumns[0].count - 1)
154
+
155
+ @arrTotal = Array.new
156
+ for p in (0..columncount)
157
+ @arrTotal[p] = 0
158
+ end
159
+
160
+ for j in (0..columncount)
161
+ for k in (0..rowcount)
162
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
163
+ end
164
+ end
165
+
166
+ @bandrxtx= @arrTotal
167
+
168
+ return @bandrxtx
169
+ end
170
+
171
+ @new0 = bandrx
172
+ sleep 1
173
+ @new1 = bandrx
174
+
175
+ @bytesreceived = @new1[0].to_i - @new0[0].to_i
176
+ @bitsreceived = (@bytesreceived * 8)
177
+ @megabitsreceived = (@bitsreceived.to_f / 1024 / 1024)
178
+
179
+ return @megabitsreceived.round(3)
180
+ end
181
+
182
+ def uw_bandtx
183
+
184
+ def bandtx
185
+
186
+ if File.exists?("/proc/net/dev")
187
+ File.open("/proc/net/dev", "r") do |file|
188
+ @result = file.read
189
+ end
190
+ end
191
+
192
+ @arrRows = @result.split("\n")
193
+
194
+ @arrEthLoRows = @arrRows.grep(/eth|lo/)
195
+
196
+ rowcount = (@arrEthLoRows.count - 1)
197
+
198
+ for i in (0..rowcount)
199
+ @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ")
200
+ end
201
+
202
+ @arrColumns = Array.new
203
+ for l in (0..rowcount)
204
+ @temp = Array.new
205
+ @temp[0] = @arrEthLoRows[l][1]
206
+ @temp[1] = @arrEthLoRows[l][9]
207
+ @arrColumns << @temp
208
+ end
209
+
210
+ columncount = (@arrColumns[0].count - 1)
211
+
212
+ @arrTotal = Array.new
213
+ for p in (0..columncount)
214
+ @arrTotal[p] = 0
215
+ end
216
+
217
+ for j in (0..columncount)
218
+ for k in (0..rowcount)
219
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
220
+ end
221
+ end
222
+
223
+ @bandrxtx = @arrTotal
224
+
225
+ return @bandrxtx
226
+ end
227
+
228
+ @new0 = bandtx
229
+ sleep 1
230
+ @new1 = bandtx
231
+
232
+ @bytestransmitted = @new1[1].to_i - @new0[1].to_i
233
+ @bitstransmitted = (@bytestransmitted * 8)
234
+ @megabitstransmitted = (@bitstransmitted.to_f / 1024 / 1024)
235
+
236
+ return @megabitstransmitted.round(3)
237
+ end
238
+
239
+ def uw_diskioreads
240
+
241
+ def diskio
242
+
243
+ if File.exists?("/proc/diskstats")
244
+ File.open("/proc/diskstats", "r") do |file|
245
+ @result = file.read
246
+ end
247
+ end
248
+
249
+ @arrRows = @result.split("\n")
250
+
251
+ rowcount = (@arrRows.count - 1)
252
+
253
+ for i in (0..rowcount)
254
+ @arrRows[i] = @arrRows[i].gsub(/\s+/m, ' ').strip.split(" ")
255
+ end
256
+
257
+ @arrColumns = Array.new
258
+ for l in (0..rowcount)
259
+ @temp = Array.new
260
+ @temp[0] = @arrRows[l][3]
261
+ @temp[1] = @arrRows[l][7]
262
+ @arrColumns << @temp
263
+ end
264
+
265
+ columncount = (@arrColumns[0].count - 1)
266
+
267
+ @arrTotal = Array.new
268
+ for p in (0..columncount)
269
+ @arrTotal[p] = 0
270
+ end
271
+
272
+ for j in (0..columncount)
273
+ for k in (0..rowcount)
274
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
275
+ end
276
+ end
277
+
278
+ @diskiorw= @arrTotal
279
+
280
+ return @diskiorw
281
+ end
282
+
283
+ @new0 = diskio
284
+ sleep 1
285
+ @new1 = diskio
286
+
287
+ @diskreads = @new1[0].to_i - @new0[0].to_i
288
+
289
+ return @diskreads
290
+ end
291
+
292
+ def uw_diskiowrites
293
+
294
+ def diskio
295
+
296
+ if File.exists?("/proc/diskstats")
297
+ File.open("/proc/diskstats", "r") do |file|
298
+ @result = file.read
299
+ end
300
+ end
301
+
302
+ @arrRows = @result.split("\n")
303
+
304
+ rowcount = (@arrRows.count - 1)
305
+
306
+ for i in (0..rowcount)
307
+ @arrRows[i] = @arrRows[i].gsub(/\s+/m, ' ').strip.split(" ")
308
+ end
309
+
310
+ @arrColumns = Array.new
311
+ for l in (0..rowcount)
312
+ @temp = Array.new
313
+ @temp[0] = @arrRows[l][3]
314
+ @temp[1] = @arrRows[l][7]
315
+ @arrColumns << @temp
316
+ end
317
+
318
+ columncount = (@arrColumns[0].count - 1)
319
+
320
+ @arrTotal = Array.new
321
+ for p in (0..columncount)
322
+ @arrTotal[p] = 0
323
+ end
324
+
325
+ for j in (0..columncount)
326
+ for k in (0..rowcount)
327
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
328
+ end
329
+ end
330
+
331
+ @diskiorw= @arrTotal
332
+
333
+ return @diskiorw
334
+ end
335
+
336
+ @new0 = diskio
337
+ sleep 1
338
+ @new1 = diskio
339
+
340
+ @diskwrites = @new1[1].to_i - @new0[1].to_i
341
+
342
+ return @diskwrites
343
+ end
344
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'usagewatch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "usagewatch"
8
+ spec.version = Usagewatch::VERSION
9
+ spec.authors = ["Phil Chen,Ruben Espinosa"]
10
+ spec.email = ["rderoldan1@gmail.com"]
11
+ spec.description = %q{A Ruby Gem with methods to find usage statistics on a Linux server such as CPU, Disk, TCP/UDP Connections, Load, Bandwidth, Disk I/O, and Memory}
12
+ spec.summary = %q{Statistics on a Linux server}
13
+ spec.homepage = "https://github.com/nethacker/usagewatch"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: usagewatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Phil Chen,Ruben Espinosa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Ruby Gem with methods to find usage statistics on a Linux server such
42
+ as CPU, Disk, TCP/UDP Connections, Load, Bandwidth, Disk I/O, and Memory
43
+ email:
44
+ - rderoldan1@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - examples/example.rb
55
+ - lib/usagewatch.rb
56
+ - lib/usagewatch/version.rb
57
+ - usagewatch.gemspec
58
+ homepage: https://github.com/nethacker/usagewatch
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Statistics on a Linux server
82
+ test_files: []