Class: Panopticon::WlanUtilization

Inherits:
Object
  • Object
show all
Defined in:
lib/panopticon/wlan_utilization.rb

Constant Summary collapse

FREQ2CHAN =
{
  0     => 0, # why radiotap.channel.freq can be 0?
  # 2.4GHz
  2412  => 1,
  2417  => 2,
  2422  => 3,
  2427  => 4,
  2432  => 5,
  2437  => 6,
  2442  => 7,
  2447  => 8,
  2452  => 9,
  2457  => 10,
  2462  => 11,
  2467  => 12,
  2472  => 13,
  2484  => 14,

  # japan client only
  5170  => 34,
  5190  => 38,
  5210  => 42,
  5230  => 46,

  # W52
  5180  => 36,
  5200  => 40,
  5220  => 44,
  5240  => 48,

  # W53
  5260  => 52,
  5280  => 56,
  5300  => 60,
  5320  => 64,

  # W56
  5500  => 100,
  5520  => 104,
  5540  => 108,
  5560  => 112,
  5580  => 116,
  5600  => 120,
  5620  => 124,
  5640  => 128,
  5660  => 132,
  5680  => 136,
  5700  => 140,
  5745  => 149,
  5765  => 153,
  5785  => 157,
  5805  => 161,
  5825  => 165,


  # 802.11j
  4920  => 184,
  4940  => 188,
  4960  => 192,
  4980  => 194,
}
REG_FREQ =
/^\s*frequency:\s+(\d+) /
REG_ACTIVE =
/^\s*channel active time:\s+(\d+) (|m)s$/
REG_BUSY =
/^\s*channel busy time:\s+(\d+) (|m)s$/

Instance Method Summary collapse

Constructor Details

#initialize(ifname) ⇒ WlanUtilization

Returns a new instance of WlanUtilization.



69
70
71
72
73
74
75
# File 'lib/panopticon/wlan_utilization.rb', line 69

def initialize ifname
  @ifname = ifname
  @ifname_exists = interface_exists?(ifname)
  unless @ifname_exists
    STDERR.puts("WARNING: interface #{ifname} does not exists, run in dummy mode")
  end
end

Instance Method Details

#current_dataObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/panopticon/wlan_utilization.rb', line 77

def current_data
  unless @ifname.match(/^wlan\d+$/)
    $log.debug("non-wlan skips utilization acquirement")
    return [0, 0, 0, 0]
  end

  unless @ifname_exists
    $log.warn("interface not available for utilizationm aquirement")
    return [0, 0, 0, 0]
  end

  str = get_survey_dump()
  active = 0
  busy = 0
  channel = 0

  str.split("\n").map{|line| line.strip}.each do |line|
    case line
    when REG_FREQ
      channel = FREQ2CHAN[$1.to_i]
    when REG_ACTIVE
      active = $1.to_i
      active *= 1000 if $2 != "m"
    when REG_BUSY
      busy = $1.to_i
      busy *= 1000 if $2 != "m"
    end
  end
  return [0, 0, 0, 0] if active == 0

  return [channel, active, busy, fto2f(busy.to_f * 100 / active.to_f) ]
end