wlanmonitor-simple.rb

Thierry Rakotoarivelo, 31/08/2010 05:18 pm

Download (4 kB)

 
1
#!/usr/bin/ruby1.8
2
#
3
# Copyright (c) 2010 National ICT Australia (NICTA), Australia
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining a copy
6
# of this software and associated documentation files (the "Software"), to deal
7
# in the Software without restriction, including without limitation the rights
8
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
# copies of the Software, and to permit persons to whom the Software is
10
# furnished to do so, subject to the following conditions:
11
#
12
# The above copyright notice and this permission notice shall be included in
13
# all copies or substantial portions of the Software.
14
#
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
# THE SOFTWARE.
22
#
23
#
24
#
25

    
26
require "oml4r"
27

    
28
APPNAME = "wlanmonitor"
29
APPPATH = "/sbin/wlanconfig"
30
APPVERSION = "1.2"
31

    
32
class MPStat < OML4R::MPBase
33
    name :wlanstat
34
    param :src_addr
35
    param :dst_addr
36
    param :aid, :type => :long
37
    param :channel, :type => :long
38
    param :rate
39
    param :rssi, :type => :long
40
    param :dbm, :type => :long
41
    param :idle, :type => :long
42
    param :txseq, :type => :long
43
    param :rxseq, :type => :long
44
    # wlanconfig returns other metrics which we ignore here
45
end
46

    
47
class Wrapper
48

    
49
  def process_output(output)
50
    # wlanconfig returns a sequence of lines
51
    # The 1st line is a list of labels for the fields of the remaining lines
52
    # Each remaining line is for a detected stations, and follows the format:
53
    # ADDR AID CHAN RATE RSSI DBM IDLE TXSEQ RXSEQ CAPS ACAPS ERP STATE MODE
54
    lines = output.split("\n")
55
    labels = lines.delete_at(0)
56
    lines.each { |row|
57
      column = row.split(" ")
58
      # Inject the measurements into OML 
59
      MPStat.inject("#{column[0]}", column[1], column[2],
60
                    "#{column[3]}", column[4], column[5],
61
                    column[6], column[7], column[8])
62
    }
63
  end
64

    
65
  def initialize(args)
66
    
67
    # Initialise some variable specific to this wrapper
68
    @interface = nil
69
    @interval = 1
70

    
71
    # Now call the Init of OML4R with the command line arguments (args)
72
    # and a block defining the arguments specific to this wrapper
73
    OML4R::init(args, :appID => APPNAME) { |argParser|
74
      argParser.banner = "\nExecute a wrapper around #{APPNAME}\n" +
75
                         "Use -h or --help for a list of options\n\n" 
76
      argParser.on("-i", "--interface IFNAME", 
77
                   "Name of Interface to monitor") { |name| @interface = name }
78
      argParser.on("-s", "--sampling DURATION", 
79
                   "Interval in second between collected samples") { |time| 
80
                      @interval = time 
81
                   }
82
      argParser.on_tail("-v", "--version", 
83
                        "Show the version\n") { |v| 
84
                           puts "Version: #{APPVERSION}"; exit 
85
                        }
86
    }
87

    
88
    # Finally do some checking specific to this wrapper
89
    # e.g. here we do not proceed if the user did not give us a 
90
    # valid interface to monitor
91
    unless @interface != nil
92
      raise "You did not specify an interface to monitor! (-i option)"
93
    end
94
  end
95
    
96
  def start()
97
    while true
98
      # Run the wlanconfig command with the following syntax
99
      #   "wlanconfig <interface> list"
100
      cmd = "#{APPPATH} #{@interface} list"
101
      output = `#{cmd}`
102
      # Process its output
103
      process_output(output)
104
      # Wait for a given duration and loop again
105
      sleep @interval.to_i
106
    end
107
  end
108

    
109
end
110

    
111
begin
112
  app = Wrapper.new(ARGV)
113
  app.start()
114
rescue Exception => ex
115
  puts "Received an Exception when executing the wrapper!"
116
  puts "The Exception is: #{ex}\n"
117
end