Class: Hookit::Resource::Socket

Inherits:
Base
  • Object
show all
Defined in:
lib/hookit/resource/socket.rb

Instance Attribute Summary

Attributes inherited from Base

#dict

Instance Method Summary collapse

Methods inherited from Base

#action, actions, #can_run?, default_action, #default_action, field, #not_if, #only_if

Constructor Details

#initialize(name) ⇒ Socket

Returns a new instance of Socket.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hookit/resource/socket.rb', line 14

def initialize(name)
  service name unless service
  protocol :tcp unless protocol
  max_checks 3 unless max_checks
  super

  case protocol
  when :tcp
    sun_proto_switch = "-f inet -P tcp"
    proto_switch = "4t"
  when :udp
    sun_proto_switch = "-f inet -P udp"
    proto_switch = "4u"
  when :unix
    sun_proto_switch = "-f unix"
    proto_switch = "x"
  else
    sun_proto_switch = ""
    proto_switch = ""
  end

  case platform.os
  when 'sun'
    @active_com   = "netstat -an #{sun_proto_switch} | egrep '\*\.#{port}' | grep LISTEN"
    @inactive_com = "netstat -an #{sun_proto_switch} | grep 'ESTABLISHED' | awk '{ print $1 }' | grep \"$(ifconfig #{interface} | grep inet | awk '{ print $2 }')\.#{port}\""
  else
    @active_com   = "netstat -an#{proto_switch} | egrep ':#{port}' | grep LISTEN"
    @inactive_com = "netstat -an#{proto_switch} | grep 'ESTABLISHED' | awk '{ print $4 }' | grep \"$(ifconfig #{interface} | awk '/inet addr/ { print $2 }' | cut -f2 -d':' | tr -d '\n'):#{port}\""
  end
end

Instance Method Details

#run(action) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/hookit/resource/socket.rb', line 45

def run(action)
  case action
  when :listening
    check_listening!
  when :no_connections
    check_no_connections!
  when :reset
    reset!
  end
end