Class: Ipcad2squid

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @cmd1     = options[:cmd1]     || 'netkit-rsh localhost sh ip accounting'
  @cmd2     = options[:cmd2]     || 'netkit-rsh localhost show ip accounting checkpoint'
  @net      = options[:net]      || '192.168.0'
  @filename = options[: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

#cmd1Object (readonly)

Returns the value of attribute cmd1.



4
5
6
# File 'lib/ipcad2squid.rb', line 4

def cmd1
  @cmd1
end

#cmd2Object (readonly)

Returns the value of attribute cmd2.



4
5
6
# File 'lib/ipcad2squid.rb', line 4

def cmd2
  @cmd2
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/ipcad2squid.rb', line 4

def filename
  @filename
end

#netObject (readonly)

Returns the value of attribute net.



4
5
6
# File 'lib/ipcad2squid.rb', line 4

def net
  @net
end

#ttimeObject (readonly)

Returns the value of attribute ttime.



4
5
6
# File 'lib/ipcad2squid.rb', line 4

def ttime
  @ttime
end

Instance Method Details

#clear_accountingObject



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

#outputObject



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_fileObject



39
40
41
# File 'lib/ipcad2squid.rb', line 39

def write_to_file
  File.open(@filename, "a") { |file| file.write(output) }
end