Move to Apache License
[amazing.git] / lib / amazing / widgets / net_traffic.rb
blob0a56b8ecb8f1af64871401ddd3275fd7c7fa617c
1 # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 #    http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 require 'amazing/widget'
16 require 'amazing/proc_file'
18 module Amazing
19   module Widgets
20     class NetTraffic < Widget
21       description "Network traffic information"
22       option :interface, "Network interface", "eth0"
23       option :upload_peak, "Maximum upstream of your Internet connection in kB/s", 56
24       option :download_peak, "Maximum downstream of your Internet connection in kB/s", 56
25       field :upload_rate, "Upload rate in kB/s"
26       field :download_rate, "Download rate in kB/s"
27       field :upload_total, "Amount of data uploaded in kB/s"
28       field :download_total, "Amount of data downloaded in kB/s"
29       field :upload_percentage, "Percentage upload"
30       field :download_percentage, "Percentage download"
31       default { @download_percentage.to_i }
33       init do
34         dev = ProcFile.parse_file("net/dev")[2][@interface].split
35         first_down = dev[0].to_f
36         first_up = dev[8].to_f
37         sleep 1
38         dev = ProcFile.parse_file("net/dev")[2][@interface].split
39         second_down = dev[0].to_f
40         second_up = dev[8].to_f
41         @download_rate = (second_down - first_down) / 1024
42         @download_total = second_down / 1024
43         @upload_rate = (second_up - first_up) / 1024
44         @upload_total = second_up / 1024
45         @upload_percentage = @upload_rate * 100 / @upload_peak
46         @download_percentage = @download_rate * 100 / @download_peak
47       end
48     end
49   end
50 end