Class: Ipcad2squid
- Inherits:
-
Object
- Object
- Ipcad2squid
- Defined in:
- lib/ipcad2squid.rb
Instance Attribute Summary collapse
-
#cmd1 ⇒ Object
readonly
Returns the value of attribute cmd1.
-
#cmd2 ⇒ Object
readonly
Returns the value of attribute cmd2.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#net ⇒ Object
readonly
Returns the value of attribute net.
-
#ttime ⇒ Object
readonly
Returns the value of attribute ttime.
Instance Method Summary collapse
- #clear_accounting ⇒ Object
-
#initialize(options = {}) ⇒ Ipcad2squid
constructor
A new instance of Ipcad2squid.
- #output ⇒ Object
- #write_to_file ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Ipcad2squid
Returns a new instance of Ipcad2squid.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ipcad2squid.rb', line 6 def initialize(={}) @cmd1 = [:cmd1] || 'netkit-rsh localhost sh ip accounting' @cmd2 = [:cmd2] || 'netkit-rsh localhost show ip accounting checkpoint' @net = [:net] || '192.168.0' @filename = [:filename] || '/var/log/squid/access.log' begin @ttime = `#{@cmd1}`.split("\n").grep(/saved/) { |saved| saved.split.last }.first rescue @ttime = '0' end end |
Instance Attribute Details
#cmd1 ⇒ Object (readonly)
Returns the value of attribute cmd1.
4 5 6 |
# File 'lib/ipcad2squid.rb', line 4 def cmd1 @cmd1 end |
#cmd2 ⇒ Object (readonly)
Returns the value of attribute cmd2.
4 5 6 |
# File 'lib/ipcad2squid.rb', line 4 def cmd2 @cmd2 end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
4 5 6 |
# File 'lib/ipcad2squid.rb', line 4 def filename @filename end |
#net ⇒ Object (readonly)
Returns the value of attribute net.
4 5 6 |
# File 'lib/ipcad2squid.rb', line 4 def net @net end |
#ttime ⇒ Object (readonly)
Returns the value of attribute ttime.
4 5 6 |
# File 'lib/ipcad2squid.rb', line 4 def ttime @ttime end |
Instance Method Details
#clear_accounting ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/ipcad2squid.rb', line 30 def clear_accounting # move statistics to checkpoint and clear begin `netkit-rsh localhost clear ip accounting` rescue puts "Couldn't clear ip accounting" end end |
#output ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ipcad2squid.rb', line 18 def output # get all data for subnet accounting_data = `#{@cmd2}`.split("\n").grep(/#{@net}/) # parse and output data accounting_data.map do |raw_data| data = raw_data.split next unless data[1] =~ /#{@net}/ "#{@ttime}.000 1 #{data[1]} TCP_MISS/200 #{data[3]} CONNECT #{data[0]}:#{data[4]} - DIRECT/#{data[1]} -" end.join("\n").gsub(/^$\n/, '') end |
#write_to_file ⇒ Object
39 40 41 |
# File 'lib/ipcad2squid.rb', line 39 def write_to_file File.open(@filename, "a") { |file| file.write(output) } end |