psm-ruby-stats 1.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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d19466a8158bb48a61e2d2e29f4cf1ae59c6ff7f91ed23910e39d4794716fd76
4
+ data.tar.gz: c428115a6cd3e00f9eceeccda9c599a34106d7de4c3aadd283f20ab0fdf2801e
5
+ SHA512:
6
+ metadata.gz: 4a3d43c80a66e18cab3f912415b127404dc215aa1a106eea823226c513076187cc7277886dce639b1a7f64453979ae2f24bdc8a5196b75a6deaf380ea35086c5
7
+ data.tar.gz: f572f61fd954d243a0d48268a1e5bbeaf60ea13e9a1590827e0d686d624d015b24d666cb3360d54e061c3cc48177065a325d72d0fb2993164d265a79242819c5
@@ -0,0 +1,8 @@
1
+ version: 2
2
+
3
+ updates:
4
+ - package-ecosystem: "bundler"
5
+ directory: "/"
6
+ schedule:
7
+ interval: weekly
8
+ vendor: true
@@ -0,0 +1,22 @@
1
+ name: Publish Gem
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ tags:
8
+ - v*
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v1
15
+
16
+ - name: Release Gem
17
+ if: contains(github.ref, 'refs/tags/v')
18
+ uses: cadwallion/publish-rubygems-action@master
19
+ env:
20
+ GITHUB_TOKEN: ${{secrets.GH_TOKEN}}
21
+ RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
22
+ RELEASE_COMMAND: rake release
@@ -0,0 +1,52 @@
1
+ name: Verify
2
+ on: [push]
3
+
4
+ jobs:
5
+ linters:
6
+ name: Linters
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - name: Checkout code
10
+ uses: actions/checkout@v2
11
+ - name: Setup Ruby
12
+ uses: ruby/setup-ruby@v1
13
+ - name: Ruby gem cache
14
+ uses: actions/cache@v1
15
+ with:
16
+ path: vendor/bundle
17
+ key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
18
+ restore-keys: |
19
+ ${{ runner.os }}-gems-
20
+ - name: Install gems
21
+ run: |
22
+ bundle config path vendor/bundle
23
+ bundle install --jobs 4 --retry 3
24
+ bundle clean --force
25
+
26
+ - name: Run linters
27
+ run: |
28
+ bundle exec rubocop --parallel
29
+
30
+ tests:
31
+ name: Tests
32
+ runs-on: ubuntu-latest
33
+ steps:
34
+ - name: Checkout code
35
+ uses: actions/checkout@v2
36
+ - name: Setup Ruby
37
+ uses: ruby/setup-ruby@v1
38
+ - name: Ruby gem cache
39
+ uses: actions/cache@v1
40
+ with:
41
+ path: vendor/bundle
42
+ key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
43
+ restore-keys: |
44
+ ${{ runner.os }}-gems-
45
+ - name: Install gems
46
+ run: |
47
+ bundle config path vendor/bundle
48
+ bundle install --jobs 4 --retry 3
49
+ bundle clean --force
50
+ - name: Run tests
51
+ run: |
52
+ bundle exec rspec --profile
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ # .rubocop.yml
2
+ inherit_gem:
3
+ rubocop-rails_config:
4
+ - config/rails.yml
5
+
6
+ # Override Setting
7
+ AllCops:
8
+ TargetRubyVersion: 3.0
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.0.1
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,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rubocop-rails_config"
6
+ gem "usagewatch", "~> 0.0.7"
7
+ gem "rspec"
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,142 @@
1
+ # ruby-stats-psm
2
+
3
+ Credits to https://github.com/skcript for the initial code.
4
+
5
+ ## DESCRIPTION:
6
+ 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.
7
+
8
+ Here are some of the statistics you can get about your system:
9
+ - CPU Usage
10
+ - CPU load
11
+ - Memory Usage
12
+ - Disk Statistics (Free Space, etc)
13
+ - TCP/UDP Packet Exchanges
14
+ - Gets some data about the processes that are running
15
+
16
+ ## Getting Started
17
+
18
+ gem install usagewatch_ext
19
+
20
+ ```ruby
21
+ require 'usagewatch_ext'
22
+
23
+ usw = Usagewatch
24
+
25
+ usw.uw_diskused
26
+ usw.uw_diskused_perc
27
+ usw.uw_cpuused
28
+ usw.uw_tcpused
29
+ usw.uw_udpused
30
+ usw.uw_memused
31
+ usw.uw_load
32
+ usw.uw_bandrx
33
+ usw.uw_bandtx
34
+ usw.uw_diskioreads
35
+ usw.uw_diskiowrites
36
+ usw.uw_cputop
37
+ usw.uw_memtop
38
+ usw.uw_apacheconns
39
+ ```
40
+
41
+ ## Example
42
+
43
+ ```bash
44
+ Run:
45
+
46
+ linux_example.rb
47
+
48
+ Example Output:
49
+
50
+ 11.56 Gigabytes Disk Used
51
+ 7.0% Disk Used
52
+ 0.25% CPU Used
53
+ 30 TCP Connections Used
54
+ 0 UDP Connections Used
55
+ 43% Active Memory Used
56
+ 0.01 Average System Load Of The Past Minute
57
+ 0.008 Mbit/s Current Bandwidth Received
58
+ 0.2 Mbit/s Current Bandwidth Transmitted
59
+ 0/s Current Disk Reads Completed
60
+ 2/s Current Disk Writes Completed
61
+ Top Ten Processes By CPU Consumption:
62
+ [["/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"]]
63
+ Top Ten Processes By Memory Consumption:
64
+ [["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"]]
65
+ ```
66
+
67
+ ## Methods available
68
+
69
+ ##### Linux
70
+ uw_diskused
71
+ uw_diskused_on(location)
72
+ uw_diskused_perc
73
+ uw_diskavailable
74
+ uw_diskavailable_on(location)
75
+ uw_cpuused
76
+ uw_tcpused
77
+ uw_udpused
78
+ uw_memused
79
+ uw_load
80
+ uw_bandrx
81
+ uw_bandtx
82
+ uw_diskioreads
83
+ uw_diskiowrites
84
+ uw_cputop
85
+ uw_memtop
86
+ uw_httpconns
87
+
88
+ ##### Mac
89
+ uw_diskused
90
+ uw_diskused_on(location)
91
+ uw_diskused_perc
92
+ uw_diskavailable
93
+ uw_diskavailable_on(location)
94
+ uw_cputop
95
+ uw_memtop
96
+ uw_load
97
+ uw_cpuused
98
+ uw_memused
99
+ uw_httpconns
100
+ uw_bandrx
101
+ uw_bandtx
102
+
103
+
104
+ ## Notes
105
+
106
+ * Disk Used is a sum of all partitions calculated in Gigabytes
107
+
108
+ * Disk Used Percentage is a total percentage of all disk partitions used
109
+
110
+ * Disk Used On is disk space used on the location passed calculated in Gigabytes, returns "location invalid" if invalid location passed
111
+
112
+ * Disk Available is a sum of all partitions calculated in Gigabytes
113
+
114
+ * Disk Available On is disk space available on the location passed calculated in Gigabytes, returns "location invalid" if invalid location passed
115
+
116
+ * CPU Used is a percentage of current CPU being used
117
+
118
+ * TCP/UDP Connections Used is a total count of each respectively
119
+
120
+ * Active Memory Used is a percentage of active system memory being used
121
+
122
+ * Load is the average load of the past minute
123
+
124
+ * Bandwidth is current received and transmitted in Megabits
125
+
126
+ * Disk IO is current disk reads and writes completed per second
127
+
128
+ * Top Ten Processes By CPU Consumption are based on percent CPU used
129
+
130
+ * Top Ten Processes By Memory Consumption are base on percent Memory used
131
+
132
+ * HTTP Conns is the number of connections on 80 port
133
+
134
+ ## Tested Using
135
+
136
+ **RUBY VERSIONS:**
137
+ - ruby 3.0.1
138
+
139
+ **TESTED OS VERSIONS:**
140
+ - Ubuntu 20.04
141
+
142
+ License MIT
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new
7
+
8
+ task default: :spec
9
+ task test: :spec
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # License: (MIT), Copyright (C) 2013 Author Phil Chen, Contributor Ruben Espinosa
5
+
6
+ require "usagewatch"
7
+
8
+ usw = Usagewatch
9
+
10
+ puts "#{usw.uw_diskused} Gigabytes Used"
11
+ puts "#{usw.uw_diskused_perc} Perventage of Gigabytes Used"
12
+ puts "#{usw.uw_cpuused}% CPU Used"
13
+ puts "#{usw.uw_tcpused} TCP Connections Used"
14
+ puts "#{usw.uw_udpused} UDP Connections Used"
15
+ puts "#{usw.uw_memused}% Active Memory Used"
16
+ puts "#{usw.uw_load} Average System Load Of The Past Minute"
17
+ puts "#{usw.uw_bandrx} Mbit/s Current Bandwidth Received"
18
+ puts "#{usw.uw_bandtx} Mbit/s Current Bandwidth Transmitted"
19
+ puts "#{usw.uw_diskioreads}/s Current Disk Reads Completed"
20
+ puts "#{usw.uw_diskiowrites}/s Current Disk Writes Completed"
21
+ puts "Top Ten Processes By CPU Consumption: #{usw.uw_cputop}"
22
+ puts "Top Ten Processes By Memory Consumption: #{usw.uw_memtop}"
23
+ puts "#{usw.uw_httpconns} Connections in 80 Port"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RubyStatsPsm
4
+ def uw_httpconns
5
+ execute_command("netstat -an | grep :80 |wc -l").to_i
6
+ end
7
+ end
@@ -0,0 +1,293 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RubyStatsPsm
4
+ # Show the amount of total disk used in Gigabytes
5
+ def uw_diskused
6
+ df = execute_command("df")
7
+ parts = df.split(" ").map { |s| s.to_i }
8
+ sum = 0
9
+ (9..parts.size - 1).step(6).each { |i|
10
+ sum += parts[i]
11
+ }
12
+ round = sum.round(2)
13
+ ((round / 1024) / 1024).round(2)
14
+ end
15
+
16
+ def uw_diskavailable
17
+ df = execute_command("df -kl")
18
+ sum = 0.00
19
+ df.each_line.with_index do |line, line_index|
20
+ next if line_index.eql? 0
21
+ line = line.split(" ")
22
+ next if /localhost/.match?(line[0]) # ignore backup filesystem
23
+ sum += ((line[3].to_f) / 1024) / 1024
24
+ end
25
+ sum.round(2)
26
+ end
27
+
28
+ def uw_diskused_perc
29
+ execute_command("df --output=pcent / | tr -dc '0-9'").to_i
30
+ end
31
+
32
+ def uw_cpuused
33
+ execute_command("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage }'").to_f
34
+ end
35
+
36
+ def self.uw_cputop
37
+ ps = `ps aux | awk '{print $11, $3}' | sort -k2nr | head -n 10`
38
+ array = []
39
+ ps.each_line do |line|
40
+ line = line.chomp.split(" ")
41
+ array << [line.first.gsub(/[\[\]]/, ""), line.last]
42
+ end
43
+ array
44
+ end
45
+
46
+ # Show the number of TCP connections used
47
+ def self.uw_tcpused
48
+ if File.exist?("/proc/net/sockstat")
49
+ File.open("/proc/net/sockstat", "r") do |ipv4|
50
+ @sockstat = ipv4.read
51
+ end
52
+
53
+ @tcp4data = @sockstat.split
54
+ @tcp4count = @tcp4data[5]
55
+ end
56
+
57
+ if File.exist?("/proc/net/sockstat6")
58
+ File.open("/proc/net/sockstat6", "r") do |ipv6|
59
+ @sockstat6 = ipv6.read
60
+ end
61
+
62
+ @tcp6data = @sockstat6.split
63
+ @tcp6count = @tcp6data[2]
64
+ end
65
+
66
+ @totaltcpused = @tcp4count.to_i + @tcp6count.to_i
67
+ end
68
+
69
+ # Show the number of UDP connections used
70
+ def self.uw_udpused
71
+ if File.exist?("/proc/net/sockstat")
72
+ File.open("/proc/net/sockstat", "r") do |ipv4|
73
+ @sockstat = ipv4.read
74
+ end
75
+
76
+ @udp4data = @sockstat.split
77
+ @udp4count = @udp4data[16]
78
+ end
79
+
80
+ if File.exist?("/proc/net/sockstat6")
81
+ File.open("/proc/net/sockstat6", "r") do |ipv6|
82
+ @sockstat6 = ipv6.read
83
+ end
84
+
85
+ @udp6data = @sockstat6.split
86
+ @udp6count = @udp6data[5]
87
+ end
88
+
89
+ @totaludpused = @udp4count.to_i + @udp6count.to_i
90
+ end
91
+
92
+ # Show the percentage of Active Memory used
93
+ def self.uw_memused
94
+ if File.exist?("/proc/meminfo")
95
+ File.open("/proc/meminfo", "r") do |file|
96
+ @result = file.read
97
+ end
98
+ end
99
+
100
+ @memstat = @result.split("\n").collect { |x| x.strip }
101
+ @memtotal = @memstat[0].gsub(/[^0-9]/, "")
102
+ @memactive = @memstat[5].gsub(/[^0-9]/, "")
103
+ @memactivecalc = (@memactive.to_f * 100) / @memtotal.to_f
104
+ @memusagepercentage = @memactivecalc.round
105
+ end
106
+
107
+ # return hash of top ten proccesses by mem consumption
108
+ # example [["apache2", 12.0], ["passenger", 13.2]]
109
+ def self.uw_memtop
110
+ ps = execute_command("ps aux | awk '{print $11, $4}' | sort -k2nr | head -n 10")
111
+ array = []
112
+ ps.each_line do |line|
113
+ line = line.chomp.split(" ")
114
+ array << [line.first.gsub(/[\[\]]/, ""), line.last]
115
+ end
116
+ array
117
+ end
118
+
119
+ # Show the average system load of the past minute
120
+ def self.uw_load
121
+ if File.exist?("/proc/loadavg")
122
+ File.open("/proc/loadavg", "r") do |file|
123
+ @loaddata = file.read
124
+ end
125
+
126
+ @load = @loaddata.split(/ /).first.to_f
127
+ end
128
+ end
129
+
130
+ # Bandwidth Received Method
131
+ def self.bandrx
132
+ if File.exist?("/proc/net/dev")
133
+ File.open("/proc/net/dev", "r") do |file|
134
+ @result = file.read
135
+ end
136
+ end
137
+
138
+ @arrRows = @result.split("\n")
139
+
140
+ @arrEthLoRows = @arrRows.grep(/eth|lo/)
141
+
142
+ rowcount = (@arrEthLoRows.count - 1)
143
+
144
+ for i in (0..rowcount)
145
+ @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, " ").strip.split(" ")
146
+ end
147
+
148
+ @arrColumns = Array.new
149
+ for l in (0..rowcount)
150
+ @temp = Array.new
151
+ @temp[0] = @arrEthLoRows[l][1]
152
+ @temp[1] = @arrEthLoRows[l][9]
153
+ @arrColumns << @temp
154
+ end
155
+
156
+ columncount = (@arrColumns[0].count - 1)
157
+
158
+ @arrTotal = Array.new
159
+ for p in (0..columncount)
160
+ @arrTotal[p] = 0
161
+ end
162
+
163
+ for j in (0..columncount)
164
+ for k in (0..rowcount)
165
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
166
+ end
167
+ end
168
+
169
+ @bandrxtx = @arrTotal
170
+ end
171
+
172
+ # Current Bandwidth Received Calculation in Mbit/s
173
+ def self.uw_bandrx
174
+ @new0 = self.bandrx
175
+ sleep 1
176
+ @new1 = self.bandrx
177
+
178
+ @bytesreceived = @new1[0].to_i - @new0[0].to_i
179
+ @bitsreceived = (@bytesreceived * 8)
180
+ @megabitsreceived = (@bitsreceived.to_f / 1024 / 1024).round(3)
181
+ end
182
+
183
+ # Bandwidth Transmitted Method
184
+ def self.bandtx
185
+ if File.exist?("/proc/net/dev")
186
+ File.open("/proc/net/dev", "r") do |file|
187
+ @result = file.read
188
+ end
189
+ end
190
+
191
+ @arrRows = @result.split("\n")
192
+
193
+ @arrEthLoRows = @arrRows.grep(/eth|lo/)
194
+
195
+ rowcount = (@arrEthLoRows.count - 1)
196
+
197
+ for i in (0..rowcount)
198
+ @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, " ").strip.split(" ")
199
+ end
200
+
201
+ @arrColumns = Array.new
202
+ for l in (0..rowcount)
203
+ @temp = Array.new
204
+ @temp[0] = @arrEthLoRows[l][1]
205
+ @temp[1] = @arrEthLoRows[l][9]
206
+ @arrColumns << @temp
207
+ end
208
+
209
+ columncount = (@arrColumns[0].count - 1)
210
+
211
+ @arrTotal = Array.new
212
+ for p in (0..columncount)
213
+ @arrTotal[p] = 0
214
+ end
215
+
216
+ for j in (0..columncount)
217
+ for k in (0..rowcount)
218
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
219
+ end
220
+ end
221
+
222
+ @bandrxtx = @arrTotal
223
+ end
224
+
225
+ # Current Bandwidth Transmitted in Mbit/s
226
+ def self.uw_bandtx
227
+ @new0 = self.bandtx
228
+ sleep 1
229
+ @new1 = self.bandtx
230
+
231
+ @bytestransmitted = @new1[1].to_i - @new0[1].to_i
232
+ @bitstransmitted = (@bytestransmitted * 8)
233
+ @megabitstransmitted = (@bitstransmitted.to_f / 1024 / 1024).round(3)
234
+ end
235
+
236
+ # Disk Usage Method
237
+ def self.diskio
238
+ if File.exist?("/proc/diskstats")
239
+ File.open("/proc/diskstats", "r") do |file|
240
+ @result = file.read
241
+ end
242
+ end
243
+
244
+ @arrRows = @result.split("\n")
245
+
246
+ rowcount = (@arrRows.count - 1)
247
+
248
+ for i in (0..rowcount)
249
+ @arrRows[i] = @arrRows[i].gsub(/\s+/m, " ").strip.split(" ")
250
+ end
251
+
252
+ @arrColumns = Array.new
253
+ for l in (0..rowcount)
254
+ @temp = Array.new
255
+ @temp[0] = @arrRows[l][3]
256
+ @temp[1] = @arrRows[l][7]
257
+ @arrColumns << @temp
258
+ end
259
+
260
+ columncount = (@arrColumns[0].count - 1)
261
+
262
+ @arrTotal = Array.new
263
+ for p in (0..columncount)
264
+ @arrTotal[p] = 0
265
+ end
266
+
267
+ for j in (0..columncount)
268
+ for k in (0..rowcount)
269
+ @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j]
270
+ end
271
+ end
272
+
273
+ @diskiorw = @arrTotal
274
+ end
275
+
276
+ # Current Disk Reads Completed
277
+ def self.uw_diskioreads
278
+ @new0 = self.diskio
279
+ sleep 1
280
+ @new1 = self.diskio
281
+
282
+ @diskreads = @new1[0].to_i - @new0[0].to_i
283
+ end
284
+
285
+ # Current Disk Writes Completed
286
+ def self.uw_diskiowrites
287
+ @new0 = self.diskio
288
+ sleep 1
289
+ @new1 = self.diskio
290
+
291
+ @diskwrites = @new1[1].to_i - @new0[1].to_i
292
+ end
293
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RubyStatsPsm
4
+ VERSION = "1.0.1"
5
+ OS = RUBY_PLATFORM
6
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+ require_relative "./rubystats_psm/version"
5
+
6
+ class RubyStatsPsm
7
+ include Singleton
8
+ text = "OS is not supported in this version."
9
+
10
+ if OS.include? "linux"
11
+ require "usagewatch/linux"
12
+ require_relative "./rubystats_psm/linux"
13
+ require_relative "./rubystats_psm/skcript_linux"
14
+ elsif OS.match?(/cygwin|mswin|mingw|bccwin|wince|emx/)
15
+ puts "Windows" + text
16
+ elsif Os.include? "darwin"
17
+ puts "Darwin" + text
18
+ else
19
+ puts "This" + text
20
+ end
21
+
22
+ private def method_missing(symbol, *args)
23
+ if Usagewatch.respond_to?(symbol)
24
+ Usagewatch.send(symbol, *args)
25
+ end
26
+ super
27
+ end
28
+
29
+ def execute_command(cmd)
30
+ `#{cmd}`
31
+ end
32
+
33
+ class << self
34
+ private def method_missing(symbol, *args)
35
+ instance.send(symbol, *args)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "General usage" do
6
+ it "should include the library" do
7
+ expect(Usagewatch).to be_a(Module)
8
+ end
9
+
10
+ describe "uw_diskused" do
11
+ subject { RubyStatsPsm.uw_diskused }
12
+
13
+ it "should be the GB of disk used" do
14
+ expect(subject).to be_a(Integer)
15
+ expect(subject).to be > 0
16
+ end
17
+ end
18
+
19
+ describe "uw_diskavailable" do
20
+ subject { RubyStatsPsm.uw_diskavailable }
21
+
22
+ it "should be the GB of disk available" do
23
+ expect(subject).to be_a(Float)
24
+ expect(subject).to be > 0
25
+ end
26
+ end
27
+
28
+ describe "uw_cpuused" do
29
+ subject { RubyStatsPsm.uw_cpuused }
30
+
31
+ it "should be the percentage of cpu used" do
32
+ expect(subject).to be_a(Float)
33
+ expect(subject).to be > 0
34
+ end
35
+ end
36
+
37
+ describe "uw_diskused_perc" do
38
+ subject { RubyStatsPsm.uw_diskused_perc }
39
+
40
+ it "should be the percentage of GB of disk used" do
41
+ expect(subject).to be_a(Integer)
42
+ expect(subject).to be_between(0, 100)
43
+ end
44
+ end
45
+
46
+ describe "uw_load" do
47
+ subject { RubyStatsPsm.uw_load }
48
+
49
+ it "should be the average load of the past minute" do
50
+ expect(subject).to be_a(Float)
51
+ expect(subject).to be_between(0, 4)
52
+ end
53
+ end
54
+
55
+ describe "uw_cputop" do
56
+ subject { RubyStatsPsm.uw_cputop }
57
+
58
+ it "should be the average load of the past minute" do
59
+ expect(subject).to be_a(Array)
60
+ expect(subject[0][0]).to be_a(String)
61
+ expect(subject[0][1]).to be_a(String)
62
+ expect(subject.size).to eq(10)
63
+ end
64
+ end
65
+
66
+ describe "uw_cputop" do
67
+ subject { RubyStatsPsm.uw_memtop }
68
+
69
+ it "should be an array of top mem consumption processes " do
70
+ expect(subject).to be_a(Array)
71
+ expect(subject[0][0]).to be_a(String)
72
+ expect(subject[0][1]).to be_a(String)
73
+ expect(subject.size).to eq(10)
74
+ end
75
+ end
76
+
77
+ describe "uw_httpconns" do
78
+ subject { RubyStatsPsm.uw_httpconns }
79
+
80
+ it "should be the number of current apache connections" do
81
+ expect(subject).to be_a(Integer)
82
+ expect(subject).to be >= 0
83
+ end
84
+ end
85
+
86
+ describe "uw_tcpused" do
87
+ subject { RubyStatsPsm.uw_tcpused }
88
+
89
+ it "should TCP Connections Used" do
90
+ expect(subject).to be_a(Integer)
91
+ expect(subject).to be >= 0
92
+ end
93
+ end
94
+
95
+ describe "uw_udpused" do
96
+ subject { RubyStatsPsm.uw_udpused }
97
+
98
+ it "should UDP Connections Used" do
99
+ expect(subject).to be_a(Integer)
100
+ expect(subject).to be >= 0
101
+ end
102
+ end
103
+
104
+ describe "uw_bandrx" do
105
+ subject { RubyStatsPsm.uw_bandrx }
106
+
107
+ it "should be current received" do
108
+ expect(subject).to be_a(Float)
109
+ expect(subject).to be >= 0
110
+ end
111
+ end
112
+
113
+ describe "uw_bandtx" do
114
+ subject { RubyStatsPsm.uw_bandtx }
115
+
116
+ it "should be current sent" do
117
+ expect(subject).to be_a(Float)
118
+ expect(subject).to be >= 0
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubygems"
4
+ # require "bundler/setup"
5
+ require_relative "../lib/rubystats_psm"
6
+ # require "coveralls"
7
+ # Coveralls.wear!
8
+ #
9
+ # OS = RUBY_PLATFORM
10
+ # if OS.include? "darwin"
11
+ # require "usagewatch_ext"
12
+ # puts "Testing Mac Version"
13
+ # elsif OS.include? "linux"
14
+ # require "usagewatch"
15
+ # puts "Testing Linux Version"
16
+ # puts `uname -a`
17
+ # end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require "rubystats_psm/version"
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "psm-ruby-stats"
10
+ spec.version = RubyStatsPsm::VERSION
11
+ spec.authors = ["Personal Social Media"]
12
+ spec.email = ["contact@personalsocialmedia.net"]
13
+ spec.description = "A Ruby Gem with methods to find usage statistics such as CPU, Disk, TCP/UDP Connections, Load, Bandwidth, Disk I/O, and Memory"
14
+ spec.summary = "Extended version of usagewatch_ext"
15
+ spec.homepage = "https://github.com/personal-social-media/ruby-stats"
16
+ spec.license = "MIT"
17
+ spec.rdoc_options << "--main" << "README"
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
+
24
+ spec.add_runtime_dependency("usagewatch", "~> 0.0.7")
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rubocop-rails_config"
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psm-ruby-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Personal Social Media
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-17 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.7
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.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rails_config
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
+ description: A Ruby Gem with methods to find usage statistics such as CPU, Disk, TCP/UDP
56
+ Connections, Load, Bandwidth, Disk I/O, and Memory
57
+ email:
58
+ - contact@personalsocialmedia.net
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".github/dependabot.yml"
64
+ - ".github/workflows/release.yml"
65
+ - ".github/workflows/verify.yml"
66
+ - ".gitignore"
67
+ - ".rspec"
68
+ - ".rubocop.yml"
69
+ - ".ruby-version"
70
+ - ".travis.yml"
71
+ - CHANGELOG.md
72
+ - Gemfile
73
+ - LICENSE
74
+ - README.md
75
+ - Rakefile
76
+ - examples/linux_example.rb
77
+ - lib/rubystats_psm.rb
78
+ - lib/rubystats_psm/linux.rb
79
+ - lib/rubystats_psm/skcript_linux.rb
80
+ - lib/rubystats_psm/version.rb
81
+ - spec/linux_spec.rb
82
+ - spec/spec_helper.rb
83
+ - usagewatch_ext.gemspec
84
+ homepage: https://github.com/personal-social-media/ruby-stats
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options:
90
+ - "--main"
91
+ - README
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.2.15
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Extended version of usagewatch_ext
109
+ test_files:
110
+ - spec/linux_spec.rb
111
+ - spec/spec_helper.rb