Class: PosixPsutil::Network

Inherits:
Object
  • Object
show all
Includes:
NetworkConstance, PsutilHelper
Defined in:
lib/posixpsutil/linux/system.rb

Constant Summary

Constants included from NetworkConstance

PosixPsutil::NetworkConstance::AF_INET, PosixPsutil::NetworkConstance::AF_INET6, PosixPsutil::NetworkConstance::AF_UNIX, PosixPsutil::NetworkConstance::CONN_CLOSE, PosixPsutil::NetworkConstance::CONN_CLOSE_WAIT, PosixPsutil::NetworkConstance::CONN_CLOSING, PosixPsutil::NetworkConstance::CONN_ESTABLISHED, PosixPsutil::NetworkConstance::CONN_FIN_WAIT1, PosixPsutil::NetworkConstance::CONN_FIN_WAIT2, PosixPsutil::NetworkConstance::CONN_LAST_ACK, PosixPsutil::NetworkConstance::CONN_LISTEN, PosixPsutil::NetworkConstance::CONN_NONE, PosixPsutil::NetworkConstance::CONN_SYN_RECV, PosixPsutil::NetworkConstance::CONN_SYN_SENT, PosixPsutil::NetworkConstance::CONN_TIME_WAIT, PosixPsutil::NetworkConstance::SOCK_DGRAM, PosixPsutil::NetworkConstance::SOCK_STREAM, PosixPsutil::NetworkConstance::TCP_STATUSES

Class Method Summary collapse

Methods included from PsutilHelper

boot_time

Class Method Details

.net_connections(interface = :inet) ⇒ Object

interface can be one of [:inet, :inet4, inet6, :udp, :udp4, :udp6, :tcp, :tcp4, :tcp6, :all, :unix]

the default interface is :inet, contains udp and tcp return #<OpenStruct inode, laddr, raddr, family, type, status, fd, pid>



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/posixpsutil/linux/system.rb', line 450

def self.net_connections(interface=:inet)
  ret = []
  connection = Connection.new
  return nil unless connection.tmap.key?(interface)
  inodes = Processes.get_all_inodes
  connection.tmap[interface].each do |kind|
    f, family, type = kind
    if [AF_INET, AF_INET6].include?(family)
      ret.concat(connection.process_inet("/proc/net/#{f}", family, 
                                         type, inodes))
    else
      ret.concat(connection.process_unix("/proc/net/#{f}", family, inodes))
    end
  end
  ret
end

.net_io_counters(pernic = false) ⇒ Object

Get counters of network io (per network interface)

When pernic is true, return a hash contains network io of each interface, otherwise return sum of all interfaces.

The network io of each/all interface(s) is represented in #<OpenStruct bytes_recv, packets_recv, errin, dropin, bytes_sent, packets_sent, errout, dropout>.



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/posixpsutil/linux/system.rb', line 405

def self.net_io_counters(pernic=false)
  lines = IO.readlines('/proc/net/dev')[2..-1]
  if pernic
    ret = {}
    lines.each do |line|
      colon = line.rindex(':')
      name = line[0...colon].strip()
      fields = line[(colon + 1)..-1].strip.split(' ')
      counter = OpenStruct.new
      counter.bytes_recv = fields[0].to_i
      counter.packets_recv = fields[1].to_i
      counter.errin = fields[2].to_i
      counter.dropin = fields[3].to_i
      counter.bytes_sent = fields[8].to_i
      counter.packets_sent = fields[9].to_i
      counter.errout = fields[10].to_i
      counter.dropout = fields[11].to_i
      ret[name.to_sym] = counter
    end
    return ret
  else
    counter = OpenStruct.new(bytes_recv: 0, packets_recv: 0, 
                             errin: 0, dropin: 0, bytes_sent: 0, 
                             packets_sent: 0, errout: 0, dropout: 0)
    lines.each do |line|
      colon = line.rindex(':')
      fields = line[(colon + 1)..-1].strip.split(' ')
      counter.bytes_recv += fields[0].to_i
      counter.packets_recv += fields[1].to_i
      counter.errin += fields[2].to_i
      counter.dropin += fields[3].to_i
      counter.bytes_sent += fields[8].to_i
      counter.packets_sent += fields[9].to_i
      counter.errout += fields[10].to_i
      counter.dropout += fields[11].to_i
    end
    return counter
  end
end