Module: LinuxStat::Net

Defined in:
lib/linux_stat/net.rb

Overview

Shows various Net related information of the current system.

Constant Summary collapse

DEV =
'/proc/net/dev'.freeze

Class Method Summary collapse

Class Method Details

.ipv4_privateObject

Returns the local IP address of the system as a String.

If the information isn’t available, it will a frozen empty string.



12
13
14
15
16
# File 'lib/linux_stat/net.rb', line 12

def ipv4_private
	require 'socket' unless defined?(Socket)
	ip = Socket.ip_address_list.find(&:ipv4_private?)
	ip ? ip.ip? ? ip.ip_unpack[0].freeze : ''.freeze : ''.freeze
end

.total_bytesObject

Returns the total bytes received and transmitted as Hash.

For example:

LinuxStat::Net.usage

=> {:received=>56602867, :transmitted=>6940922}

But if the status isn’t available it will return an empty Hash.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/linux_stat/net.rb', line 27

def total_bytes
	return {} unless File.readable?(DEV)

	data = IO.readlines(DEV).drop(2)
	indices = find_index_of_bytes
	data.reject! { |x| x.strip.start_with?('lo:') }
	r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map { |x| x.reduce(:+) }

	{
		received: r,
		transmitted: t
	}
end

.total_bytes_receivedObject

Returns the total bytes received as Integer.

But if the status isn’t available it will return nil.



45
46
47
48
49
50
51
52
# File 'lib/linux_stat/net.rb', line 45

def total_bytes_received
	return nil unless File.readable?(DEV)

	data = IO.readlines(DEV).drop(2)
	index = find_index_of_bytes[0]
	data.reject! { |x| x.strip.start_with?('lo:') }
	data.map { |x| x.split[index].to_i }.reduce(:+)
end

.total_bytes_transmittedObject

Returns the total bytes transmitted as Integer.

But if the status isn’t available it will return nil.



58
59
60
61
62
63
64
65
# File 'lib/linux_stat/net.rb', line 58

def total_bytes_transmitted
	return nil unless File.readable?(DEV)

	data = IO.readlines(DEV).drop(2)
	index = find_index_of_bytes[-1]
	data.reject! { |x| x.strip.start_with?('lo:') }
	data.map { |x| x.split[index].to_i }.reduce(:+)
end

.usage(interval = 0.1) ⇒ Object Also known as: current_usage

usage(interval = 0.1)

Where interval is the time between polling in seconds. The default is 0.1 seconds.

The return type is a Hash, containg the current internet usage (received, transmit) in B/s.

For example:

LinuxStat::Net.usage

=> {:received=>436060.0, :transmitted=>50350.0}

If the system transmits 100 kb in the interval,

this method will return 1000 kb/s. That is, it estimates

the data it will transmit in one second. Thus, a good and reliable interval is 1 second

It will return an empty Hash if the info (/proc/net/dev) isn’t available.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/linux_stat/net.rb', line 86

def usage(interval = 0.1)
	return {} unless File.readable?(DEV)

	data = IO.readlines(DEV).drop(2)
	indices = find_index_of_bytes
	data.reject! { |x| x.strip.start_with?('lo:'.freeze) }
	r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map { |x| x.reduce(:+) }

	sleep(interval)

	data2 = IO.readlines(DEV).drop(2)
	data2.reject! { |x| x.strip.start_with?('lo:'.freeze) }
	r2, t2 = data2.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map { |x| x.reduce(:+) }

	# Measure the difference
	dr, dt = r2.-(r).fdiv(interval), t2.-(t).fdiv(interval)

	{
		received: dr,
		transmitted: dt
	}
end