Class: Panopticon::WlanControl
- Inherits:
-
Object
- Object
- Panopticon::WlanControl
- Defined in:
- lib/panopticon/wlan_control.rb
Constant Summary collapse
- DEFAULT_IFNAME =
"wlan0"- DEFAULT_CAPTURE_PATH =
"/cap"- STATE_INIT =
:INIT- STATE_RUNNING =
:RUNNING- STATE_STOP =
:STOP
Class Method Summary collapse
Instance Method Summary collapse
- #capture_file_info ⇒ Object
- #change_channels(arg) ⇒ Object
- #cpu_info ⇒ Object
- #cpu_info_from_mac ⇒ Object
- #cpu_info_from_proc ⇒ Object
- #disk_info ⇒ Object
- #generate_filename ⇒ Object
- #get_config ⇒ Object
- #get_status ⇒ Object
-
#initialize(args = {}) ⇒ WlanControl
constructor
A new instance of WlanControl.
- #memory_info ⇒ Object
- #memory_info_from_mac ⇒ Object
- #memory_info_from_proc ⇒ Object
-
#start_capture(arg) ⇒ Object
request handler for API.
- #stop_capture ⇒ Object
- #wlan_info ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ WlanControl
Returns a new instance of WlanControl.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/panopticon/wlan_control.rb', line 23 def initialize args={} @th_capture = nil @wlan_capture = nil @state = STATE_INIT @ifname = args[:ifname] || DEFAULT_IFNAME $log.debug("hogeeee #{@ifname} #{args[:ifname]}") @capture_path = args[:capture_path] || DEFAULT_CAPTURE_PATH @hostname = `hostname` end |
Class Method Details
.default_options ⇒ Object
16 17 18 19 20 21 |
# File 'lib/panopticon/wlan_control.rb', line 16 def self. return { :ifname => DEFAULT_IFNAME, :capture_path => DEFAULT_CAPTURE_PATH, } end |
Instance Method Details
#capture_file_info ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/panopticon/wlan_control.rb', line 141 def capture_file_info filesize = 0 duration = 0 if @wlan_capture filesize = @wlan_capture.filesize duration = @wlan_capture.duration end return { :filename => @filename, :filesize => filesize, :duration => duration, } end |
#change_channels(arg) ⇒ Object
241 242 |
# File 'lib/panopticon/wlan_control.rb', line 241 def change_channels arg end |
#cpu_info ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/panopticon/wlan_control.rb', line 95 def cpu_info if File.exists?("/proc/stat") return cpu_info_from_proc else return cpu_info_from_mac end end |
#cpu_info_from_mac ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/panopticon/wlan_control.rb', line 128 def cpu_info_from_mac line = `top | head -4 | grep CPU` match = line.match(/^CPU usage: *(\d+\.\d+)% user, *(\d+\.\d+)% sys, *(\d+\.\d+)% idle $/) return 0.0 unless match user = match[1].to_f sys = match[2].to_f idle = match[3].to_f used = 100 - idle return used end |
#cpu_info_from_proc ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/panopticon/wlan_control.rb', line 103 def cpu_info_from_proc used = 0 current_cpu_info = [] File.open("/proc/stat") do |file| current_cpu_info = file.gets.split[1..4].map{|elm| elm.to_i} end if @prev_cpu_info == nil @prev_cpu_info = current_cpu_info return 0 end usage_sub = current_cpu_info[0..2].inject(0){|sum, elm| sum += elm} - @prev_cpu_info[0..2].inject(0){|sum, elm| sum += elm} total_sub = current_cpu_info.inject(0){|sum, elm| sum += elm} - @prev_cpu_info.inject(0){|sum, elm| sum += elm} @prev_cpu_info = current_cpu_info used = ((usage_sub.to_f * 100) / total_sub) return used end |
#disk_info ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/panopticon/wlan_control.rb', line 35 def disk_info line = `df`.split("\n")[1].split # assumes first line to be "/" size = line[1].to_i * 512 used = line[2].to_i * 512 return {:size => size, :used => used } end |
#generate_filename ⇒ Object
182 183 184 |
# File 'lib/panopticon/wlan_control.rb', line 182 def generate_filename return "#{Time.now.strftime("%Y%m%d%H%m%S")}_#{@ifname}_#{$$}.pcapng" end |
#get_config ⇒ Object
244 245 |
# File 'lib/panopticon/wlan_control.rb', line 244 def get_config end |
#get_status ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/panopticon/wlan_control.rb', line 247 def get_status @cached_disk_info = disk_info() @cached_memory_info = memory_info() @cached_cpu_info = cpu_info() @cached_file_info = capture_file_info() @cached_wlan_info = wlan_info() return { :date => Time.now.to_i, :hostname => @hostname, :disk_size => @cached_disk_info[:size], :disk_used => @cached_disk_info[:used], :memory_size => @cached_memory_info[:size], :memory_used => @cached_memory_info[:used], :cpu_usage => @cached_cpu_info, :state => @state, :filename => @filename, :filesize => @cached_file_info[:filesize], :duration => @cached_file_info[:duration], :current_channel => @cached_wlan_info[:current_channel], :channel_walk => @cached_wlan_info[:channel_walk], :utilization => @cached_wlan_info[:utilization], :utilization_channel => @cached_wlan_info[:utilization_channel], } end |
#memory_info ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/panopticon/wlan_control.rb', line 43 def memory_info if File.exists?("/proc/meminfo") # linux or bsd return memory_info_from_proc else # mac os x ? return memory_info_from_mac end end |
#memory_info_from_mac ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/panopticon/wlan_control.rb', line 66 def memory_info_from_mac _, total = `sysctl hw.memsize`.split.map{|n| n.to_i} used = 0 app_mem = 0 comp_mem = 0 file_backed_mem = 0 `vm_stat`.split("\n").each do |line| match = line.match(/^Anonymous pages: *(\d+)\.$/) if match app_mem = match[1].to_i * 4096 end match = line.match(/^Pages occupied by compressor: *(\d+)\.$/) if match comp_mem = match[1].to_i * 4096 end match = line.match(/^File-backed pages: *(\d+)\.$/) if match file_backed_mem = match[1].to_i * 4096 end end used = (app_mem + comp_mem + file_backed_mem) return {:size => total, :used => used} end |
#memory_info_from_proc ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/panopticon/wlan_control.rb', line 53 def memory_info_from_proc total = 0 free = 0 used = 0 File.open("/proc/meminfo") do |file| total = file.gets.split[1].to_i free = file.gets.split[1].to_i used = total - free end return {:size => total, :used => used} end |
#start_capture(arg) ⇒ Object
request handler for API
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/panopticon/wlan_control.rb', line 187 def start_capture arg $log.info("starting new capture") if @state == STATE_RUNNING $log.warn("WlanCapture instance is already running") return end if @th_capture $log.warn("capture thread is already running, terminate it first") return end if @wlan_capture $log.warn("capture instance is running, teminate it first") return end $log.info("starting new capture instance") @wlan_capture = Panopticon::WlanCapture.new(@ifname) # start capturing filename = generate_filename() @th_capture = Thread.new do @wlan_capture.run_capture("#{@capture_path}/#{filename}") end $log.info("started new capture at #{filename}") # move state @state = STATE_RUNNING @filename = filename end |
#stop_capture ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/panopticon/wlan_control.rb', line 220 def stop_capture if @state == STATE_INIT or @state == STATE_STOP $log.err("stopping not runnning capture instance") return end if @wlan_capture @wlan_capture.stop_capture end if @th_capture and @th_capture.alive? $log.info("stopping capture thread") @th_capture.join end @wlan_capture = nil @th_capture = nil @state = STATE_STOP end |
#wlan_info ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/panopticon/wlan_control.rb', line 157 def wlan_info info = { :current_channel => 0, :channel_walk => 0, :utilization => 0, :utilization_channel => 0, } unless @state == STATE_RUNNING return info end if @wlan_capture.nil? $log.err("state mismatch (state = #{@state} <=> capture is running"); return end info[:current_channel] = @wlan_capture.current_channel info[:channel_walk] = @wlan_capture.channel_walk info[:utilization] = @wlan_capture.utilization info[:utilization_channel] = @wlan_capture.utilization_channel return info end |