Module: Vidibus::Sysinfo::Traffic
- Extended by:
- Base
- Defined in:
- lib/vidibus/sysinfo/traffic.rb
Overview
Returns total traffic of this month in gigabytes.
Calls ‘vnstat`
Installation on Debian:
apt-get install vnstat
vnstat -u -i eth0
Defined Under Namespace
Classes: Result
Class Method Summary collapse
Methods included from Base
Methods included from Helper
Class Method Details
.command ⇒ Object
36 37 38 |
# File 'lib/vidibus/sysinfo/traffic.rb', line 36 def command "vnstat -m" end |
.explain(error) ⇒ Object
61 62 63 64 65 |
# File 'lib/vidibus/sysinfo/traffic.rb', line 61 def explain(error) if error.match("vnstat: command not found") return "vnstat is not installed. On Debian, you can install it with 'apt-get install vnstat' and 'vnstat -u -i eth0'" end end |
.parse(output) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vidibus/sysinfo/traffic.rb', line 40 def parse(output) month = /(\s*\w{3} \'\d{2})/ traffic = /\s*(\d+(?:\.\d+)?) (ki?B|Mi?B|Gi?B|Ti?B)\s*/i last_month = output.split(/\r?\n/)[-3] if last_month && last_month.match(/#{month}#{traffic}\|#{traffic}\|#{traffic}+/m) input_amount = $2.to_f input_unit = $3 output_amount = $4.to_f output_unit = $5 Result.new({ input: gigabytes(input_amount, input_unit), output: gigabytes(output_amount, output_unit) }) elsif output.match("Not enough data available yet") Result.new({ input: 0.0, output: 0.0 }) end end |