Module: Usagewatch
- Defined in:
- lib/usagewatch/linux.rb,
lib/usagewatch/version.rb
Overview
License: (MIT), Copyright © 2013 usagewatch Author Phil Chen, contributor Ruben Espinosa
Constant Summary collapse
- VERSION =
"0.0.7"
Class Method Summary collapse
-
.bandrx ⇒ Object
Bandwidth Received Method.
-
.bandtx ⇒ Object
Bandwidth Transmitted Method.
-
.diskio ⇒ Object
Disk Usage Method.
-
.uw_bandrx ⇒ Object
Current Bandwidth Received Calculation in Mbit/s.
-
.uw_bandtx ⇒ Object
Current Bandwidth Transmitted in Mbit/s.
-
.uw_cputop ⇒ Object
return hash of top ten proccesses by cpu consumption example [[“apache2”, 12.0], [“passenger”, 13.2]].
-
.uw_cpuused ⇒ Object
Show the percentage of CPU used.
-
.uw_diskioreads ⇒ Object
Current Disk Reads Completed.
-
.uw_diskiowrites ⇒ Object
Current Disk Writes Completed.
-
.uw_diskused ⇒ Object
Show the amount of total disk used in Gigabytes.
-
.uw_diskused_perc ⇒ Object
Show the percentage of disk used.
-
.uw_load ⇒ Object
Show the average system load of the past minute.
-
.uw_memtop ⇒ Object
return hash of top ten proccesses by mem consumption example [[“apache2”, 12.0], [“passenger”, 13.2]].
-
.uw_memused ⇒ Object
Show the percentage of Active Memory used.
-
.uw_tcpused ⇒ Object
Show the number of TCP connections used.
-
.uw_udpused ⇒ Object
Show the number of UDP connections used.
Class Method Details
.bandrx ⇒ Object
Bandwidth Received Method
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/usagewatch/linux.rb', line 144 def self.bandrx if File.exists?("/proc/net/dev") File.open("/proc/net/dev", "r") do |file| @result = file.read end end @arrRows = @result.split("\n") @arrEthLoRows = @arrRows.grep(/eth|lo/) rowcount = (@arrEthLoRows.count - 1) for i in (0..rowcount) @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ") end @arrColumns = Array.new for l in (0..rowcount) @temp = Array.new @temp[0] = @arrEthLoRows[l][1] @temp[1] = @arrEthLoRows[l][9] @arrColumns << @temp end columncount = (@arrColumns[0].count - 1) @arrTotal = Array.new for p in (0..columncount) @arrTotal[p] = 0 end for j in (0..columncount) for k in (0..rowcount) @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j] end end @bandrxtx = @arrTotal end |
.bandtx ⇒ Object
Bandwidth Transmitted Method
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/usagewatch/linux.rb', line 199 def self.bandtx if File.exists?("/proc/net/dev") File.open("/proc/net/dev", "r") do |file| @result = file.read end end @arrRows = @result.split("\n") @arrEthLoRows = @arrRows.grep(/eth|lo/) rowcount = (@arrEthLoRows.count - 1) for i in (0..rowcount) @arrEthLoRows[i] = @arrEthLoRows[i].gsub(/\s+/m, ' ').strip.split(" ") end @arrColumns = Array.new for l in (0..rowcount) @temp = Array.new @temp[0] = @arrEthLoRows[l][1] @temp[1] = @arrEthLoRows[l][9] @arrColumns << @temp end columncount = (@arrColumns[0].count - 1) @arrTotal = Array.new for p in (0..columncount) @arrTotal[p] = 0 end for j in (0..columncount) for k in (0..rowcount) @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j] end end @bandrxtx = @arrTotal end |
.diskio ⇒ Object
Disk Usage Method
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/usagewatch/linux.rb', line 254 def self.diskio if File.exists?("/proc/diskstats") File.open("/proc/diskstats", "r") do |file| @result = file.read end end @arrRows = @result.split("\n") rowcount = (@arrRows.count - 1) for i in (0..rowcount) @arrRows[i] = @arrRows[i].gsub(/\s+/m, ' ').strip.split(" ") end @arrColumns = Array.new for l in (0..rowcount) @temp = Array.new @temp[0] = @arrRows[l][3] @temp[1] = @arrRows[l][7] @arrColumns << @temp end columncount = (@arrColumns[0].count - 1) @arrTotal = Array.new for p in (0..columncount) @arrTotal[p] = 0 end for j in (0..columncount) for k in (0..rowcount) @arrTotal[j] = @arrColumns[k][j].to_i + @arrTotal[j] end end @diskiorw= @arrTotal end |
.uw_bandrx ⇒ Object
Current Bandwidth Received Calculation in Mbit/s
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/usagewatch/linux.rb', line 187 def self.uw_bandrx @new0 = self.bandrx sleep 1 @new1 = self.bandrx @bytesreceived = @new1[0].to_i - @new0[0].to_i @bitsreceived = (@bytesreceived * 8) @megabitsreceived = (@bitsreceived.to_f / 1024 / 1024).round(3) end |
.uw_bandtx ⇒ Object
Current Bandwidth Transmitted in Mbit/s
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/usagewatch/linux.rb', line 242 def self.uw_bandtx @new0 = self.bandtx sleep 1 @new1 = self.bandtx @bytestransmitted = @new1[1].to_i - @new0[1].to_i @bitstransmitted = (@bytestransmitted * 8) @megabitstransmitted = (@bitstransmitted.to_f / 1024 / 1024).round(3) end |
.uw_cputop ⇒ Object
return hash of top ten proccesses by cpu consumption example [[“apache2”, 12.0], [“passenger”, 13.2]]
48 49 50 51 52 53 54 55 56 |
# File 'lib/usagewatch/linux.rb', line 48 def self.uw_cputop ps = `ps aux | awk '{print $11, $3}' | sort -k2nr | head -n 10` array = [] ps.each_line do |line| line = line.chomp.split(" ") array << [line.first.gsub(/[\[\]]/, ""), line.last] end array end |
.uw_cpuused ⇒ Object
Show the percentage of CPU used
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/usagewatch/linux.rb', line 23 def self.uw_cpuused @proc0 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ") sleep 1 @proc1 = File.readlines('/proc/stat').grep(/^cpu /).first.split(" ") @proc0usagesum = @proc0[1].to_i + @proc0[2].to_i + @proc0[3].to_i @proc1usagesum = @proc1[1].to_i + @proc1[2].to_i + @proc1[3].to_i @procusage = @proc1usagesum - @proc0usagesum @proc0total = 0 for i in (1..4) do @proc0total += @proc0[i].to_i end @proc1total = 0 for i in (1..4) do @proc1total += @proc1[i].to_i end @proctotal = (@proc1total - @proc0total) @cpuusage = (@procusage.to_f / @proctotal.to_f) @cpuusagepercentage = (100 * @cpuusage).to_f.round(2) end |
.uw_diskioreads ⇒ Object
Current Disk Reads Completed
295 296 297 298 299 300 301 302 |
# File 'lib/usagewatch/linux.rb', line 295 def self.uw_diskioreads @new0 = self.diskio sleep 1 @new1 = self.diskio @diskreads = @new1[0].to_i - @new0[0].to_i end |
.uw_diskiowrites ⇒ Object
Current Disk Writes Completed
305 306 307 308 309 310 311 312 |
# File 'lib/usagewatch/linux.rb', line 305 def self.uw_diskiowrites @new0 = self.diskio sleep 1 @new1 = self.diskio @diskwrites = @new1[1].to_i - @new0[1].to_i end |
.uw_diskused ⇒ Object
Show the amount of total disk used in Gigabytes
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/usagewatch/linux.rb', line 5 def self.uw_diskused @df = `df` @parts = @df.split(" ").map { |s| s.to_i } @sum = 0 for i in (9..@parts.size - 1).step(6) do @sum += @parts[i] end @round = @sum.round(2) @totaldiskused = ((@round/1024)/1024).round(2) end |
.uw_diskused_perc ⇒ Object
Show the percentage of disk used.
17 18 19 20 |
# File 'lib/usagewatch/linux.rb', line 17 def self.uw_diskused_perc df = `df --total` df.split(" ").last.to_f.round(2) end |
.uw_load ⇒ Object
Show the average system load of the past minute
133 134 135 136 137 138 139 140 141 |
# File 'lib/usagewatch/linux.rb', line 133 def self.uw_load if File.exists?("/proc/loadavg") File.open("/proc/loadavg", "r") do |file| @loaddata = file.read end @load = @loaddata.split(/ /).first.to_f end end |
.uw_memtop ⇒ Object
return hash of top ten proccesses by mem consumption example [[“apache2”, 12.0], [“passenger”, 13.2]]
122 123 124 125 126 127 128 129 130 |
# File 'lib/usagewatch/linux.rb', line 122 def self.uw_memtop ps = `ps aux | awk '{print $11, $4}' | sort -k2nr | head -n 10` array = [] ps.each_line do |line| line = line.chomp.split(" ") array << [line.first.gsub(/[\[\]]/, ""), line.last] end array end |
.uw_memused ⇒ Object
Show the percentage of Active Memory used
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/usagewatch/linux.rb', line 106 def self.uw_memused if File.exists?("/proc/meminfo") File.open("/proc/meminfo", "r") do |file| @result = file.read end end @memstat = @result.split("\n").collect{|x| x.strip} @memtotal = @memstat[0].gsub(/[^0-9]/, "") @memactive = @memstat[5].gsub(/[^0-9]/, "") @memactivecalc = (@memactive.to_f * 100) / @memtotal.to_f @memusagepercentage = @memactivecalc.round end |
.uw_tcpused ⇒ Object
Show the number of TCP connections used
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/usagewatch/linux.rb', line 59 def self.uw_tcpused if File.exists?("/proc/net/sockstat") File.open("/proc/net/sockstat", "r") do |ipv4| @sockstat = ipv4.read end @tcp4data = @sockstat.split @tcp4count = @tcp4data[5] end if File.exists?("/proc/net/sockstat6") File.open("/proc/net/sockstat6", "r") do |ipv6| @sockstat6 = ipv6.read end @tcp6data = @sockstat6.split @tcp6count = @tcp6data[2] end @totaltcpused = @tcp4count.to_i + @tcp6count.to_i end |
.uw_udpused ⇒ Object
Show the number of UDP connections used
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/usagewatch/linux.rb', line 83 def self.uw_udpused if File.exists?("/proc/net/sockstat") File.open("/proc/net/sockstat", "r") do |ipv4| @sockstat = ipv4.read end @udp4data = @sockstat.split @udp4count = @udp4data[16] end if File.exists?("/proc/net/sockstat6") File.open("/proc/net/sockstat6", "r") do |ipv6| @sockstat6 = ipv6.read end @udp6data = @sockstat6.split @udp6count = @udp6data[5] end @totaludpused = @udp4count.to_i + @udp6count.to_i end |