ruby_stats 0.2.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: e5fa62b5a548fe44e10871fc4f4b93534710a4ba
4
+ data.tar.gz: cfb312b6ea5b3a8ca94960797234465415a8c543
5
+ SHA512:
6
+ metadata.gz: a40745d0b88c277c706ad454d8909fc0ed48464787ceb3ceecde816c2fc8a84093133e2424e83fc3641ae2ae9e86481cd9bd8ea9ddbe556b5e4864117f637b8f
7
+ data.tar.gz: 2dff62d3085700090a3472bf0de9a25a405c6e4eb810246dc8832617c6a08385a8051a94ab6a666314afeab560867281a532ee820604308bc71879d631262279
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ - gem --version
data/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+
2
+ #### [Current]
3
+ * [7002242](../../commit/7002242) Bump to 0.1.0 __(Ruben Espinosa)__
4
+ * [b75ccab](../../commit/b75ccab) test __(Ruben Espinosa)__
5
+ * [36a4c9f](../../commit/36a4c9f) Bump to 0.0.4.pre1 __(Ruben Espinosa)__
6
+ * [fade9f9](../../commit/fade9f9) http connections __(Ruben Espinosa)__
7
+ * [bf826b3](../../commit/bf826b3) method improvement __(Ruben Espinosa)__
8
+ * [7bfe9be](../../commit/7bfe9be) suggestions of code climate apllied __(Ruben Espinosa)__
9
+ * [d26b9c8](../../commit/d26b9c8) remove code duplicated __(Ruben Espinosa)__
10
+ * [c426a34](../../commit/c426a34) Update README.md __(Ruben Espinosa)__
11
+ * [b178d0e](../../commit/b178d0e) Update README.md __(Ruben Espinosa)__
12
+ * [1fc9091](../../commit/1fc9091) Update README.md __(Ruben Espinosa)__
13
+ * [255d04d](../../commit/255d04d) testing OS version __(Ruben Espinosa)__
14
+ * [d191a19](../../commit/d191a19) test coverage increase __(Ruben Espinosa)__
15
+ * [7b4a522](../../commit/7b4a522) readme updated __(Ruben Espinosa)__
16
+
17
+ #### v0.0.3
18
+ * [53ccd3a](../../commit/53ccd3a) Bump to 0.0.3 __(Ruben Espinosa)__
19
+ * [a6dbdb3](../../commit/a6dbdb3) test coverage __(Ruben Espinosa)__
20
+
21
+ #### v0.0.2
22
+ * [40a15ae](../../commit/40a15ae) Bump to 0.0.2 __(Ruben Espinosa)__
23
+ * [cbf1041](../../commit/cbf1041) using Usagewacth module insted of original module __(Ruben Espinosa)__
24
+ * [e8e0bb4](../../commit/e8e0bb4) readme updated __(Ruben Espinosa)__
25
+
26
+ #### v0.0.1
27
+ * [45f61a9](../../commit/45f61a9) first commit __(Ruben Espinosa)__
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in usagewatch_ext.gemspec
4
+ gemspec
5
+ gem 'usagewatch'
6
+ gem 'rspec'
7
+ gem 'coveralls', :require => false
8
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Skcript
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,195 @@
1
+ # ruby-stats
2
+ ![A part of our Admin Panel that we use internally](https://cloud.githubusercontent.com/assets/973265/7853394/2e0b03b6-052b-11e5-97cf-7c330eaa78e6.png)
3
+
4
+ ## DESCRIPTION:
5
+ Ruby Stats helps you get your system statistics whenever you need it. We use it internally to monitor our Servers and send it to our monitoring servers in the cloud. This Gem currently supports Ubuntu and Mac. Unfortunately we have no plans to bring it for Windows.
6
+
7
+ Here are some of the statistics you can get about your system:
8
+ - CPU Usage
9
+ - CPU load
10
+ - Memory Usage
11
+ - Disk Statistics (Free Space, etc)
12
+ - TCP/UDP Packet Exchanges
13
+ - Gets some data about the processes that are running
14
+
15
+ ## Getting Started
16
+
17
+ gem install usagewatch_ext
18
+
19
+ ```ruby
20
+ require 'usagewatch_ext'
21
+
22
+ usw = Usagewatch
23
+
24
+ usw.uw_diskused
25
+ usw.uw_diskused_perc
26
+ usw.uw_cpuused
27
+ usw.uw_tcpused
28
+ usw.uw_udpused
29
+ usw.uw_memused
30
+ usw.uw_load
31
+ usw.uw_bandrx
32
+ usw.uw_bandtx
33
+ usw.uw_diskioreads
34
+ usw.uw_diskiowrites
35
+ usw.uw_cputop
36
+ usw.uw_memtop
37
+ usw.uw_apacheconns
38
+ ```
39
+
40
+ ## Example
41
+
42
+ ```bash
43
+ Run:
44
+
45
+ linux_example.rb
46
+
47
+ Example Output:
48
+
49
+ 11.56 Gigabytes Disk Used
50
+ 7.0% Disk Used
51
+ 0.25% CPU Used
52
+ 30 TCP Connections Used
53
+ 0 UDP Connections Used
54
+ 43% Active Memory Used
55
+ 0.01 Average System Load Of The Past Minute
56
+ 0.008 Mbit/s Current Bandwidth Received
57
+ 0.2 Mbit/s Current Bandwidth Transmitted
58
+ 0/s Current Disk Reads Completed
59
+ 2/s Current Disk Writes Completed
60
+ Top Ten Processes By CPU Consumption:
61
+ [["/usr/lib64/erlang/erts-5.8.5/bin/beam.smp", "5.2"], ["ruby", "4.1"], ["ps", "2.0"], ["abrt-dump-oops", "0.8"], ["aoe_ktio", "0.7"], ["aoe_tx", "0.4"], ["ata_sff", "0.2"], ["auditd", "0.1"], ["awk", "0.1"], ["-bash", "0.1"]]
62
+ Top Ten Processes By Memory Consumption:
63
+ [["unicorn", "4.8"], ["unicorn", "4.7"], ["unicorn", "4.6"], ["unicorn", "4.6"], ["unicorn", "4.5"], ["unicorn", "4.5"], ["unicorn", "4.3"], ["unicorn", "4.3"], ["unicorn", "4.2"], ["/usr/lib64/erlang/erts-5.8.5/bin/beam.smp", "4.0"]]
64
+ ```
65
+
66
+ ```bash
67
+ Run:
68
+
69
+ mac_example.rb
70
+
71
+ Example Output:
72
+
73
+ Mac version is under development
74
+ 92.8 Gigabytes Used
75
+ 24.96 Percentage of Gigabytes Used
76
+ 71.47% Active Memory Used
77
+ 7.69% CPU Used
78
+ 1.19 Average System Load Of The Past Minute
79
+ Top Ten Processes By CPU Consumption: [["PluginProcess", "9.0"], ["WindowServer", "2.7"], ["iPhoto", "1.2"], ["Terminal", "1.0"], ["rubymine", "0.5"], ["SystemUIServer", "0.1"], ["(scanunit)", "0.0"], ["(scanunit)", "0.0"], ["(scanunit)", "0.0"], ["(scanunit)", "0.0"]]
80
+ Top Ten Processes By Memory Consumption: [["WebProcess", "8.3"], ["rubymine", "6.4"], ["Safari", "2.0"], ["iPhoto", "1.8"], ["Mail", "1.7"], ["mds", "1.6"], ["ruby", "1.5"], ["WindowServer", "1.3"], ["PluginProcess", "1.2"], ["GitHub", "1.1"]]
81
+ ```
82
+
83
+ ## Methods available
84
+
85
+ ##### Linux
86
+ uw_diskused
87
+ uw_diskused_on(location)
88
+ uw_diskused_perc
89
+ uw_diskavailable
90
+ uw_diskavailable_on(location)
91
+ uw_cpuused
92
+ uw_tcpused
93
+ uw_udpused
94
+ uw_memused
95
+ uw_load
96
+ uw_bandrx
97
+ uw_bandtx
98
+ uw_diskioreads
99
+ uw_diskiowrites
100
+ uw_cputop
101
+ uw_memtop
102
+ uw_httpconns
103
+
104
+ ##### Mac
105
+ uw_diskused
106
+ uw_diskused_on(location)
107
+ uw_diskused_perc
108
+ uw_diskavailable
109
+ uw_diskavailable_on(location)
110
+ uw_cputop
111
+ uw_memtop
112
+ uw_load
113
+ uw_cpuused
114
+ uw_memused
115
+ uw_httpconns
116
+ uw_bandrx
117
+ uw_bandtx
118
+
119
+
120
+ ## Notes
121
+
122
+ * Disk Used is a sum of all partitions calculated in Gigabytes
123
+
124
+ * Disk Used Percentage is a total percentage of all disk partitions used
125
+
126
+ * Disk Used On is disk space used on the location passed calculated in Gigabytes, returns "location invalid" if invalid location passed
127
+
128
+ * Disk Available is a sum of all partitions calculated in Gigabytes
129
+
130
+ * Disk Available On is disk space available on the location passed calculated in Gigabytes, returns "location invalid" if invalid location passed
131
+
132
+ * CPU Used is a percentage of current CPU being used
133
+
134
+ * TCP/UDP Connections Used is a total count of each respectively
135
+
136
+ * Active Memory Used is a percentage of active system memory being used
137
+
138
+ * Load is the average load of the past minute
139
+
140
+ * Bandwidth is current received and transmitted in Megabits
141
+
142
+ * Disk IO is current disk reads and writes completed per second
143
+
144
+ * Top Ten Processes By CPU Consumption are based on percent CPU used
145
+
146
+ * Top Ten Processes By Memory Consumption are base on percent Memory used
147
+
148
+ * HTTP Conns is the number of connections on 80 port
149
+
150
+ ## TODO
151
+ - Open Source our internal Rails based Dashboard to monitor a server
152
+ - More Realtime data updates
153
+ - API to send data to Sensu/Graphite directly
154
+
155
+ ## Tested Using
156
+
157
+ **RUBY VERSIONS:**
158
+ - ruby 1.9.3p429 (2013-05-15) [x86_64-linux]
159
+ - ruby 2.0
160
+ - ruby 2.1.5
161
+
162
+ **TESTED OS VERSIONS:**
163
+ - Ubuntu 14.04
164
+ - Mac OS 10.x
165
+
166
+ ## Credits
167
+ Thanks to Ruben Espinosa, Phil Chen for the wonderful [Usagewatch](https://github.com/nethacker/usagewatch) gem.
168
+
169
+ Getting Help
170
+ ------------
171
+
172
+ We spend about 40% of our time contributing to Open Source software. That's one of our goals for this year. Please [tweet](https://twitter.com/skcriptd) us with any queries you have.
173
+
174
+ License
175
+ -------
176
+
177
+ ruby-stats is Copyright (c) 2015 Skcript. It is free software, and may be
178
+ redistributed under the terms specified in the [LICENSE] file.
179
+
180
+ [LICENSE]: /LICENSE
181
+
182
+
183
+ About
184
+ -----
185
+
186
+ ![Skcript](http://www.skcript.com/static/skcript_norm.png)
187
+
188
+ ruby-stats is maintained and funded by Skcript. The names and logos for
189
+ Skcript are properties of Skcript.
190
+
191
+ We love open source, and we have been doing quite a bit of contributions to the community. Take a look at them [here][skcriptoss]. Also, encourage people around us to get involved in community [operations][community]. [Join us][hiring], if you'd like to see the world change from our HQ.
192
+
193
+ [skcriptoss]: http://skcript.github.io/
194
+ [community]: http://www.skcript.com/community?utm_source=github
195
+ [hiring]: http://www.skcript.com/careers?utm_source=github
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #License: (MIT), Copyright (C) 2013 Author Phil Chen, Contributor Ruben Espinosa
4
+
5
+ require 'usagewatch'
6
+
7
+ usw = Usagewatch
8
+
9
+ puts "#{usw.uw_diskused} Gigabytes Used"
10
+ puts "#{usw.uw_diskused_perc} Perventage of Gigabytes Used"
11
+ puts "#{usw.uw_cpuused}% CPU Used"
12
+ puts "#{usw.uw_tcpused} TCP Connections Used"
13
+ puts "#{usw.uw_udpused} UDP Connections Used"
14
+ puts "#{usw.uw_memused}% Active Memory Used"
15
+ puts "#{usw.uw_load} Average System Load Of The Past Minute"
16
+ puts "#{usw.uw_bandrx} Mbit/s Current Bandwidth Received"
17
+ puts "#{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted"
18
+ puts "#{usw.uw_diskioreads}/s Current Disk Reads Completed"
19
+ puts "#{usw.uw_diskiowrites}/s Current Disk Writes Completed"
20
+ puts "Top Ten Processes By CPU Consumption: #{usw.uw_cputop}"
21
+ puts "Top Ten Processes By Memory Consumption: #{usw.uw_memtop}"
22
+ puts "#{usw.uw_httpconns} Connections in 80 Port"
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/ruby
2
+
3
+ #License: (MIT), Copyright (C) 2013 Author Ruben Espinosa
4
+
5
+ require 'usagewatch_ext'
6
+
7
+ usw = Usagewatch
8
+
9
+ puts "#{usw.uw_diskused} Gigabytes Used"
10
+ puts "#{usw.uw_diskused_perc} Percentage of Gigabytes Used"
11
+ puts "#{usw.uw_memused}% Active Memory Used"
12
+ puts "#{usw.uw_cpuused}% CPU Used"
13
+ puts "#{usw.uw_load} Average System Load Of The Past Minute"
14
+ puts "Top Ten Processes By CPU Consumption: #{usw.uw_cputop}"
15
+ puts "Top Ten Processes By Memory Consumption: #{usw.uw_memtop}"
16
+ puts "#{usw.uw_httpconns} Connections in 80 Port"
17
+ puts "#{usw.uw_bandrx} Mbit/s Current Bandwidth Received"
18
+ puts "#{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted"
@@ -0,0 +1,20 @@
1
+ require "usagewatch_ext/version"
2
+
3
+ module UsagewatchExt
4
+ text = "OS is not supported in this version."
5
+
6
+ if OS.include? "darwin"
7
+ require "usagewatch_ext/mac"
8
+ # puts "Mac version is under development"
9
+ elsif OS.include? "linux"
10
+ require "usagewatch/linux"
11
+ require "usagewatch_ext/linux"
12
+ require "usagewatch_ext/skcript_linux"
13
+ UsagewatchExt = Usagewatch
14
+ elsif OS =~ /cygwin|mswin|mingw|bccwin|wince|emx/
15
+ puts "Windows" + text
16
+ else
17
+ puts "This" + text
18
+ end
19
+
20
+ end
@@ -0,0 +1,6 @@
1
+ module Usagewatch
2
+ # Show the current http connections on 80 port
3
+ def self.uw_httpconns
4
+ `netstat -an | grep :80 |wc -l`.to_i
5
+ end
6
+ end
@@ -0,0 +1,159 @@
1
+ module Usagewatch
2
+
3
+ # Show disk used in GB
4
+ def self.uw_diskused
5
+ df = `df -kl`
6
+ sum = 0.00
7
+ df.each_line.with_index do |line, line_index|
8
+ next if line_index.eql? 0
9
+ line = line.split(" ")
10
+ next if line[0] =~ /localhost/ #ignore backup filesystem
11
+ sum += ((line[2].to_f)/1024)/1024
12
+ end
13
+ sum.round(2)
14
+ end
15
+
16
+ # Show disk space used on location(partition) in GB
17
+ def self.uw_diskused_on(location)
18
+ df = `df`
19
+ df.split("\n")[1..-1].each do |line|
20
+ parts = line.split(" ")
21
+ if parts.last == location
22
+ diskusedon = ((parts[2].to_i.round(2)/1024)/1024).round(2)
23
+ break
24
+ end
25
+ end
26
+ diskusedon ? diskusedon : "location invalid"
27
+ end
28
+
29
+ # Show disk space available in GB
30
+ def self.uw_diskavailable
31
+ df = `df -kl`
32
+ sum = 0.00
33
+ df.each_line.with_index do |line, line_index|
34
+ next if line_index.eql? 0
35
+ line = line.split(" ")
36
+ next if line[0] =~ /localhost/ #ignore backup filesystem
37
+ sum += ((line[3].to_f)/1024)/1024
38
+ end
39
+ totaldiskavailable = sum.round(2)
40
+ end
41
+
42
+ # Show disk space available on location(partition) in GB
43
+ def self.uw_diskavailable_on(location)
44
+ df = `df`
45
+ df.split("\n")[1..-1].each do |line|
46
+ parts = line.split(" ")
47
+ if parts.last == location
48
+ diskavailableon = ((parts[3].to_i.round(2)/1024)/1024).round(2)
49
+ break
50
+ end
51
+ end
52
+ diskavailableon ? diskavailableon : "location invalid"
53
+ end
54
+
55
+ # Show the percentage of disk used.
56
+ def self.uw_diskused_perc
57
+ df, total, used = `df -kl`, 0.0, 0.0
58
+ df.each_line.with_index do |line, line_index|
59
+ line = line.split(" ")
60
+ next if line_index.eql? 0 or line[0] =~ /localhost/ #ignore backup filesystem
61
+ total += to_gb line[3].to_f
62
+ used += to_gb line[2].to_f
63
+ end
64
+ ((used/total) * 100).round(2)
65
+ end
66
+
67
+ # Show the percentage of cpu used
68
+ def self.uw_cpuused
69
+ top = `top -l1 | awk '/CPU usage/'`
70
+ top = top.gsub(/[\,a-zA-Z:]/, "").split(" ")
71
+ top[0].to_f
72
+ end
73
+
74
+ # return hash of top ten proccesses by cpu consumption
75
+ # example [["apache2", 12.0], ["passenger", 13.2]]
76
+ def self.uw_cputop
77
+ top %w"$11 $3"
78
+ end
79
+
80
+ # todo
81
+ #def uw_tcpused
82
+ #
83
+ #end
84
+
85
+ # todo
86
+ #def uw_udpused
87
+ #
88
+ #end
89
+
90
+ # return hash of top ten proccesses by mem consumption
91
+ # example [["apache2", 12.0], ["passenger", 13.2]]
92
+ def self.uw_memtop
93
+ top %w"$11 $4"
94
+ end
95
+
96
+ # Percentage of mem used
97
+ def self.uw_memused
98
+ top = `top -l1 | awk '/PhysMem/'`
99
+ top = top.gsub(/[\.\,a-zA-Z:]/, "").split(" ").reverse
100
+ ((top[1].to_f / (top[0].to_f + top[1].to_f)) * 100).round(2)
101
+ end
102
+
103
+ # Show the average of load in the last minute
104
+ def self.uw_load
105
+ iostat = `iostat -w1 -c 2 | awk '{print $7}'`
106
+ cpu = 0.0
107
+ iostat.each_line.with_index do |line, line_index|
108
+ next if line_index.eql? 0 or line_index.eql? 1 or line_index.eql? 2
109
+ cpu = line.split(" ").last.to_f.round(2)
110
+ end
111
+ cpu
112
+ end
113
+
114
+ def self.uw_bandrx
115
+ read1 =`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'`
116
+ sleep 1
117
+ read2=`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'`
118
+ (((read2.to_f - read1.to_f)/1024)/1024).round(3)
119
+ end
120
+
121
+ def self.uw_bandtx
122
+ send1=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'`
123
+ sleep 1
124
+ send2=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'`
125
+ (((send2.to_f - send1.to_f)/1024)/1024).round(3)
126
+ end
127
+
128
+ #todo
129
+ #def uw_diskioreads
130
+ #
131
+ #end
132
+
133
+ #todo
134
+ #def uw_diskiowrites
135
+ #
136
+ #end
137
+
138
+ # Show the current http connections on 80 port
139
+ def self.uw_httpconns
140
+ `netstat -an | grep :80 |wc -l`.to_i
141
+ end
142
+
143
+ private
144
+
145
+
146
+ def self.top(lines)
147
+ ps = `ps aux | awk '{print #{lines.join(", ")}}' | sort -k2nr | head -n 10`
148
+ array = []
149
+ ps.each_line do |line|
150
+ line = line.chomp.split(" ")
151
+ array << [line.first.gsub(/[\[\]]/, "").split("/").last, line.last]
152
+ end
153
+ array
154
+ end
155
+
156
+ def self.to_gb(bytes)
157
+ (bytes/1024)/1024
158
+ end
159
+ end
@@ -0,0 +1,323 @@
1
+ module Usagewatch
2
+ # Show the amount of total disk used in Gigabytes
3
+ def self.uw_diskused
4
+ @df = `df`
5
+ @parts = @df.split(" ").map { |s| s.to_i }
6
+ @sum = 0
7
+ for i in (9..@parts.size - 1).step(6) do
8
+ @sum += @parts[i]
9
+ end
10
+ @round = @sum.round(2)
11
+ @totaldiskused = ((@round/1024)/1024).round(2)
12
+ end
13
+
14
+ def self.uw_diskavailable
15
+ df = `df -kl`
16
+ sum = 0.00
17
+ df.each_line.with_index do |line, line_index|
18
+ next if line_index.eql? 0
19
+ line = line.split(" ")
20
+ next if line[0] =~ /localhost/ #ignore backup filesystem
21
+ sum += ((line[3].to_f)/1024)/1024
22
+ end
23
+ totaldiskavailable = sum.round(2)
24
+ end
25
+
26
+ # Show the percentage of disk used.
27
+ def self.uw_diskused_perc
28
+ df = `df --total`
29
+ df.split(" ").last.to_f.round(2)
30
+ end
31
+
32
+ # Show the percentage of CPU used
33
+ def self.uw_cpuused
34
+ @proc0 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
35
+ sleep 1
36
+ @proc1 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ")
37
+
38
+ @proc0usagesum = @proc0[1].to_i + @proc0[2].to_i + @proc0[3].to_i
39
+ @proc1usagesum = @proc1[1].to_i + @proc1[2].to_i + @proc1[3].to_i
40
+ @procusage = @proc1usagesum - @proc0usagesum
41
+
42
+ @proc0total = 0
43
+ for i in (1..4) do
44
+ @proc0total += @proc0[i].to_i
45
+ end
46
+ @proc1total = 0
47
+ for i in (1..4) do
48
+ @proc1total += @proc1[i].to_i
49
+ end
50
+ @proctotal = (@proc1total - @proc0total)
51
+
52
+ @cpuusage = (@procusage.to_f / @proctotal.to_f)
53
+ @cpuusagepercentage = (100 * @cpuusage).to_f.round(2)
54
+ end
55
+
56
+ # return hash of top ten proccesses by cpu consumption
57
+ # example [["apache2", 12.0], ["passenger", 13.2]]
58
+ def self.uw_cputop
59
+ ps = `ps aux | awk '{print $11, $3}' | sort -k2nr | head -n 10`
60
+ array = []
61
+ ps.each_line do |line|
62
+ line = line.chomp.split(" ")
63
+ array << [line.first.gsub(/[\[\]]/, ""), line.last]
64
+ end
65
+ array
66
+ end
67
+
68
+ # Show the number of TCP connections used
69
+ def self.uw_tcpused
70
+ if File.exists?("/proc/net/sockstat")
71
+ File.open("/proc/net/sockstat", "r") do |ipv4|
72
+ @sockstat = ipv4.read
73
+ end
74
+
75
+ @tcp4data = @sockstat.split
76
+ @tcp4count = @tcp4data[5]
77
+ end
78
+
79
+ if File.exists?("/proc/net/sockstat6")
80
+ File.open("/proc/net/sockstat6", "r") do |ipv6|
81
+ @sockstat6 = ipv6.read
82
+
83
+ end
84
+
85
+ @tcp6data = @sockstat6.split
86
+ @tcp6count = @tcp6data[2]
87
+ end
88
+
89
+ @totaltcpused = @tcp4count.to_i + @tcp6count.to_i
90
+ end
91
+
92
+ # Show the number of UDP connections used
93
+ def self.uw_udpused
94
+ if File.exists?("/proc/net/sockstat")
95
+ File.open("/proc/net/sockstat", "r") do |ipv4|
96
+ @sockstat = ipv4.read
97
+ end
98
+
99
+ @udp4data = @sockstat.split
100
+ @udp4count = @udp4data[16]
101
+ end
102
+
103
+ if File.exists?("/proc/net/sockstat6")
104
+ File.open("/proc/net/sockstat6", "r") do |ipv6|
105
+ @sockstat6 = ipv6.read
106
+ end
107
+
108
+ @udp6data = @sockstat6.split
109
+ @udp6count = @udp6data[5]
110
+ end
111
+
112
+ @totaludpused = @udp4count.to_i + @udp6count.to_i
113
+ end
114
+
115
+ # Show the percentage of Active Memory used
116
+ def self.uw_memused
117
+ if File.exists?("/proc/meminfo")
118
+ File.open("/proc/meminfo", "r") do |file|
119
+ @result = file.read
120
+ end
121
+ end
122
+
123
+ @memstat = @result.split("\n").collect{|x| x.strip}
124
+ @memtotal = @memstat[0].gsub(/[^0-9]/, "")
125
+ @memactive = @memstat[5].gsub(/[^0-9]/, "")
126
+ @memactivecalc = (@memactive.to_f * 100) / @memtotal.to_f
127
+ @memusagepercentage = @memactivecalc.round
128
+ end
129
+
130
+ # return hash of top ten proccesses by mem consumption
131
+ # example [["apache2", 12.0], ["passenger", 13.2]]
132
+ def self.uw_memtop
133
+ ps = `ps aux | awk '{print $11, $4}' | sort -k2nr | head -n 10`
134
+ array = []
135
+ ps.each_line do |line|
136
+ line = line.chomp.split(" ")
137
+ array << [line.first.gsub(/[\[\]]/, ""), line.last]
138
+ end
139
+ array
140
+ end
141
+
142
+ # Show the average system load of the past minute
143
+ def self.uw_load
144
+ if File.exists?("/proc/loadavg")
145
+ File.open("/proc/loadavg", "r") do |file|
146
+ @loaddata = file.read
147
+ end
148
+
149
+ @load = @loaddata.split(/ /).first.to_f
150
+ end
151
+ end
152
+
153
+ # Bandwidth Received Method
154
+ def self.bandrx
155
+
156
+ if File.exists?("/proc/net/dev")
157
+ File.open("/proc/net/dev", "r") do |file|
158
+ @result = file.read
159
+ end
160
+ end
161
+
162
+ @arrRows = @result.split("\n")
163
+
164
+ @arrEthLoRows = @arrRows.grep(/eth|lo/)
165
+
166
+ rowcount = (@arrEthLoRows.count - 1)
167
+
168
+ for i in (0..rowcount)
169
+ @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ")
170
+ end
171
+
172
+ @arrColumns = Array.new
173
+ for l in (0..rowcount)
174
+ @temp = Array.new
175
+ @temp[0] = @arrEthLoRows[l][1]
176
+ @temp[1] = @arrEthLoRows[l][9]
177
+ @arrColumns << @temp
178
+ end
179
+
180
+ columncount = (@arrColumns[0].count - 1)
181
+
182
+ @arrTotal = Array.new
183
+ for p in (0..columncount)
184
+ @arrTotal[p] = 0
185
+ end
186
+
187
+ for j in (0..columncount)
188
+ for k in (0..rowcount)
189
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
190
+ end
191
+ end
192
+
193
+ @bandrxtx = @arrTotal
194
+ end
195
+
196
+ # Current Bandwidth Received Calculation in Mbit/s
197
+ def self.uw_bandrx
198
+
199
+ @new0 = self.bandrx
200
+ sleep 1
201
+ @new1 = self.bandrx
202
+
203
+ @bytesreceived = @new1[0].to_i - @new0[0].to_i
204
+ @bitsreceived = (@bytesreceived * 8)
205
+ @megabitsreceived = (@bitsreceived.to_f / 1024 / 1024).round(3)
206
+ end
207
+
208
+ # Bandwidth Transmitted Method
209
+ def self.bandtx
210
+
211
+ if File.exists?("/proc/net/dev")
212
+ File.open("/proc/net/dev", "r") do |file|
213
+ @result = file.read
214
+ end
215
+ end
216
+
217
+ @arrRows = @result.split("\n")
218
+
219
+ @arrEthLoRows = @arrRows.grep(/eth|lo/)
220
+
221
+ rowcount = (@arrEthLoRows.count - 1)
222
+
223
+ for i in (0..rowcount)
224
+ @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ")
225
+ end
226
+
227
+ @arrColumns = Array.new
228
+ for l in (0..rowcount)
229
+ @temp = Array.new
230
+ @temp[0] = @arrEthLoRows[l][1]
231
+ @temp[1] = @arrEthLoRows[l][9]
232
+ @arrColumns << @temp
233
+ end
234
+
235
+ columncount = (@arrColumns[0].count - 1)
236
+
237
+ @arrTotal = Array.new
238
+ for p in (0..columncount)
239
+ @arrTotal[p] = 0
240
+ end
241
+
242
+ for j in (0..columncount)
243
+ for k in (0..rowcount)
244
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
245
+ end
246
+ end
247
+
248
+ @bandrxtx = @arrTotal
249
+ end
250
+
251
+ # Current Bandwidth Transmitted in Mbit/s
252
+ def self.uw_bandtx
253
+
254
+ @new0 = self.bandtx
255
+ sleep 1
256
+ @new1 = self.bandtx
257
+
258
+ @bytestransmitted = @new1[1].to_i - @new0[1].to_i
259
+ @bitstransmitted = (@bytestransmitted * 8)
260
+ @megabitstransmitted = (@bitstransmitted.to_f / 1024 / 1024).round(3)
261
+ end
262
+
263
+ # Disk Usage Method
264
+ def self.diskio
265
+
266
+ if File.exists?("/proc/diskstats")
267
+ File.open("/proc/diskstats", "r") do |file|
268
+ @result = file.read
269
+ end
270
+ end
271
+
272
+ @arrRows = @result.split("\n")
273
+
274
+ rowcount = (@arrRows.count - 1)
275
+
276
+ for i in (0..rowcount)
277
+ @arrRows[i] = @arrRows[i].gsub(/\s+/m, ' ').strip.split(" ")
278
+ end
279
+
280
+ @arrColumns = Array.new
281
+ for l in (0..rowcount)
282
+ @temp = Array.new
283
+ @temp[0] = @arrRows[l][3]
284
+ @temp[1] = @arrRows[l][7]
285
+ @arrColumns << @temp
286
+ end
287
+
288
+ columncount = (@arrColumns[0].count - 1)
289
+
290
+ @arrTotal = Array.new
291
+ for p in (0..columncount)
292
+ @arrTotal[p] = 0
293
+ end
294
+
295
+ for j in (0..columncount)
296
+ for k in (0..rowcount)
297
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
298
+ end
299
+ end
300
+
301
+ @diskiorw= @arrTotal
302
+ end
303
+
304
+ # Current Disk Reads Completed
305
+ def self.uw_diskioreads
306
+
307
+ @new0 = self.diskio
308
+ sleep 1
309
+ @new1 = self.diskio
310
+
311
+ @diskreads = @new1[0].to_i - @new0[0].to_i
312
+ end
313
+
314
+ # Current Disk Writes Completed
315
+ def self.uw_diskiowrites
316
+
317
+ @new0 = self.diskio
318
+ sleep 1
319
+ @new1 = self.diskio
320
+
321
+ @diskwrites = @new1[1].to_i - @new0[1].to_i
322
+ end
323
+ end
@@ -0,0 +1,4 @@
1
+ module UsagewatchExt
2
+ VERSION = "0.2.0"
3
+ OS = RUBY_PLATFORM
4
+ end
@@ -0,0 +1,104 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe 'IncludeLibrary' do
5
+ it 'should include the library' do
6
+ a = Usagewatch
7
+ a.should be Usagewatch
8
+ end
9
+ end
10
+
11
+ describe 'DiskUsage' do
12
+ it "should be the GB of disk used" do
13
+ a = Usagewatch.uw_diskused
14
+ a.class.should be(Float)
15
+ a.should_not be_nil
16
+ a.should be >= 0
17
+ end
18
+ end
19
+
20
+ describe 'DiskUsage_on' do
21
+ it "should be the GB of disk used on root partition" do
22
+ a = Usagewatch.uw_diskused_on("/")
23
+ a.class.should be(Float)
24
+ a.should_not be_nil
25
+ a.should be >= 0
26
+ end
27
+ end
28
+
29
+ describe 'DiskAvailable' do
30
+ it "should be the GB of disk available" do
31
+ a = Usagewatch.uw_diskavailable
32
+ a.class.should be(Float)
33
+ a.should_not be_nil
34
+ end
35
+ end
36
+
37
+ describe 'DiskAvailable_on' do
38
+ it "should be the GB of disk used on root partition" do
39
+ a = Usagewatch.uw_diskavailable_on("/")
40
+ a.class.should be(Float)
41
+ a.should_not be_nil
42
+ end
43
+ end
44
+
45
+
46
+ describe 'CPUUsage' do
47
+ it "should be the percentage of cpu used" do
48
+ a = Usagewatch.uw_cpuused
49
+ a.class.should be(Float)
50
+ a.should_not be_nil
51
+ a.should be <= 100
52
+ a.should be >= 0
53
+ end
54
+ end
55
+
56
+ describe 'PercentageDiskUsage' do
57
+ it "should be the percentage of GB of disk used" do
58
+ a = Usagewatch.uw_diskused_perc
59
+ a.class.should be(Float)
60
+ a.should_not be_nil
61
+ a.should be <= 100
62
+ a.should be >= 0
63
+ end
64
+ end
65
+
66
+ describe 'LoadAverage' do
67
+ it "should be the average load of the past minute" do
68
+ a = Usagewatch.uw_load
69
+ a.class.should be(Float)
70
+ a.should_not be_nil
71
+ a.should be >= 0
72
+ end
73
+ end
74
+
75
+ describe 'TopCPUUsage' do
76
+ it "should be an array of top cpu consumption proccesses " do
77
+ a = Usagewatch.uw_cputop
78
+ a.class.should be(Array )
79
+ a.should_not be_nil
80
+ a[0][0].class.should be String
81
+ a[0][1].class.should be String
82
+ a.count.should be == 10
83
+ end
84
+ end
85
+
86
+ describe 'TopMEMUsage' do
87
+ it "should be an array of top mem consumption proccesses " do
88
+ a = Usagewatch.uw_cputop
89
+ a.class.should be(Array )
90
+ a.should_not be_nil
91
+ a[0][0].class.should be String
92
+ a[0][1].class.should be String
93
+ a.count.should be == 10
94
+ end
95
+ end
96
+
97
+ describe 'HTTPConns' do
98
+ it 'should be the number of current apache connections' do
99
+ a = Usagewatch.uw_httpconns
100
+ a.class.should be Fixnum
101
+ a.should_not be_nil
102
+ a.should be >= 0
103
+ end
104
+ end
@@ -0,0 +1,58 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ if OS.include? "linux"
5
+ describe 'TCPConnectios' do
6
+ it 'should TCP Connections Used' do
7
+ a = Usagewatch.uw_tcpused
8
+ a.class.should be Fixnum
9
+ a.should_not be_nil
10
+ a.should be >= 0
11
+ end
12
+ end
13
+
14
+ describe 'UDPConections' do
15
+ it 'should UDP Connections Used ' do
16
+ a = Usagewatch.uw_udpused
17
+ a.class.should be Fixnum
18
+ a.should_not be_nil
19
+ a.should be >= 0
20
+ end
21
+ end
22
+
23
+ describe 'DiskREADS' do
24
+ it 'should be current disk reads ' do
25
+ a = Usagewatch.uw_diskioreads
26
+ a.class.should be Fixnum
27
+ a.should_not be_nil
28
+ a.should be >= 0
29
+ end
30
+ end
31
+
32
+ describe 'DiskWrites' do
33
+ it 'should be current disk writes ' do
34
+ a = Usagewatch.uw_diskiowrites
35
+ a.class.should be Fixnum
36
+ a.should_not be_nil
37
+ a.should be >= 0
38
+ end
39
+ end
40
+
41
+ describe 'Bandwidth' do
42
+ it 'should be current received ' do
43
+ a = Usagewatch.uw_bandrx
44
+ a.class.should be Float
45
+ a.should_not be_nil
46
+ a.should be >= 0
47
+ end
48
+ end
49
+
50
+ describe 'Bandwidth' do
51
+ it 'should be current received ' do
52
+ a = Usagewatch.uw_bandtx
53
+ a.class.should be Float
54
+ a.should_not be_nil
55
+ a.should be >= 0
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+
6
+ OS = RUBY_PLATFORM
7
+ if OS.include? "darwin"
8
+ require "usagewatch_ext"
9
+ puts "Testing Mac Version"
10
+ elsif OS.include? "linux"
11
+ require "usagewatch"
12
+ puts "Testing Linux Version"
13
+ puts `uname -a`
14
+ end
15
+
16
+ RSpec.configure do |config|
17
+
18
+ end
@@ -0,0 +1,28 @@
1
+ require 'rspec'
2
+ require 'spec_helper'
3
+
4
+ describe 'Version' do
5
+ it 'should be the version number' do
6
+ a = UsagewatchExt::VERSION
7
+ a.class.should be(String)
8
+ a.should_not be_nil
9
+ end
10
+ end
11
+
12
+ describe 'OSVersion' do
13
+ it 'should be the OS version' do
14
+ a = UsagewatchExt::OS
15
+ a.class.should be(String)
16
+ a.should_not be_nil
17
+ end
18
+ end
19
+
20
+ if OS.include? "linux"
21
+ describe 'Version' do
22
+ it 'should be the version number of usagewatch' do
23
+ a = Usagewatch::VERSION
24
+ a.class.should be(String)
25
+ a.should_not be_nil
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'usagewatch_ext/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_stats"
8
+ spec.version = UsagewatchExt::VERSION
9
+ spec.authors = ["Ruben Espinosa, Phil Chen, Karthik K"]
10
+ spec.email = ["bello@skcript.com"]
11
+ spec.description = %q{A Ruby Gem with methods to find usage statistics such as CPU, Disk, TCP/UDP Connections, Load, Bandwidth, Disk I/O, and Memory}
12
+ spec.summary = %q{Extended version of usagewatch}
13
+ spec.homepage = "https://github.com/skcript/ruby-stats"
14
+ spec.license = "MIT"
15
+ spec.rdoc_options << '--main' << 'README'
16
+
17
+ spec.post_install_message = "* Thank you for installing ruby-stats. Please report bugs to http://github.com/skcript/ruby-stats/issues/new/"
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+ spec.add_runtime_dependency('usagewatch', '~> 0.0.6')
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency 'rspec'
28
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ruben Espinosa, Phil Chen, Karthik K
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-18 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.0.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A Ruby Gem with methods to find usage statistics such as CPU, Disk, TCP/UDP
70
+ Connections, Load, Bandwidth, Disk I/O, and Memory
71
+ email:
72
+ - bello@skcript.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - examples/linux_example.rb
86
+ - examples/mac_example.rb
87
+ - lib/usagewatch_ext.rb
88
+ - lib/usagewatch_ext/linux.rb
89
+ - lib/usagewatch_ext/mac.rb
90
+ - lib/usagewatch_ext/skcript_linux.rb
91
+ - lib/usagewatch_ext/version.rb
92
+ - spec/general_spec.rb
93
+ - spec/linux_spec.rb
94
+ - spec/spec_helper.rb
95
+ - spec/version_spec.rb
96
+ - usagewatch_ext.gemspec
97
+ homepage: https://github.com/skcript/ruby-stats
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message: "* Thank you for installing ruby-stats. Please report bugs to
102
+ http://github.com/skcript/ruby-stats/issues/new/"
103
+ rdoc_options:
104
+ - "--main"
105
+ - README
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.5
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Extended version of usagewatch
124
+ test_files:
125
+ - spec/general_spec.rb
126
+ - spec/linux_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/version_spec.rb