Class: Fluent::Plugin::ProcInfo::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/proc_info/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid: nil, pid_file: nil, eventer: nil, logger: nil) ⇒ Process

Returns a new instance of Process.



11
12
13
14
15
16
17
18
# File 'lib/fluent/plugin/proc_info/process.rb', line 11

def initialize(pid: nil, pid_file: nil, eventer: nil, logger: nil)
  @pid = pid
  @pid_file = pid_file
  @eventer = eventer
  @logger = logger

  validate_pid
end

Instance Attribute Details

#eventerObject (readonly)

Returns the value of attribute eventer.



9
10
11
# File 'lib/fluent/plugin/proc_info/process.rb', line 9

def eventer
  @eventer
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/fluent/plugin/proc_info/process.rb', line 9

def logger
  @logger
end

Instance Method Details

#probe(probes: []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fluent/plugin/proc_info/process.rb', line 20

def probe(probes: [])
  events = []

  probes.each do |probe|
    events += send("probe_#{probe}")
  rescue StandardError => e
    log.error("Process probe error with probe `#{probe}`: #{e}")
  end

  events
end

#probe_netObject



32
33
34
# File 'lib/fluent/plugin/proc_info/process.rb', line 32

def probe_net
  probe_net_tcp + probe_net_udp
end

#probe_net_tcpObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fluent/plugin/proc_info/process.rb', line 36

def probe_net_tcp
  net_inodes = proc.pid(pid).fd.select { |fd| fd.type == 'socket' }.map(&:inode)
  proc_net_tcp = proc.net.tcp
  tcp_sockets = net_inodes.map { |net_inode| proc_net_tcp.by_inode(net_inode) }.compact

  incomings = {}
  outgoings = {}
  remainings = []

  local_port_groups = tcp_sockets.group_by(&:local_port)
  local_port_groups.each_value do |sockets|
    next if sockets.empty?

    if sockets.size == 1 && sockets.first.state_str != 'LISTEN'
      remainings << sockets.first
      next
    end
    incomings.update(sockets.group_by { |socket| [socket.local_port, socket.state_str] })
  end

  r_tcp_sockets = remainings
  remainings = []
  remote_port_groups = r_tcp_sockets.group_by(&:remote_port)
  remote_port_groups.each_value do |sockets|
    next if sockets.empty?

    if sockets.size == 1
      remainings << sockets.first
      next
    end
    outgoings.update(sockets.group_by { |socket| [socket.remote_port, socket.state_str] })
  end

  remainings = remainings.group_by(&:state_str)

  events = incomings.map do |group_attrs, sockets|
    eventer.generate_event(
      name: 'connection_count',
      value: sockets.size,
      metadata: {
        protocol: 'tcp',
        direction: 'in',
        port: group_attrs[0],
        state: group_attrs[1]
      }
    )
  end
  outgoings.each do |group_attrs, sockets|
    events << eventer.generate_event(
      name: 'connection_count',
      value: sockets.size,
      metadata: {
        protocol: 'tcp',
        direction: 'out',
        port: group_attrs[0],
        state: group_attrs[1]
      }
    )
  end
  remainings.each do |state, sockets|
    events << eventer.generate_event(
      name: 'connection_count',
      value: sockets.size,
      metadata: {
        protocol: 'tcp',
        direction: 'unknown',
        port: nil,
        state: state
      }
    )
  end

  events
end

#probe_net_udpObject



111
112
113
# File 'lib/fluent/plugin/proc_info/process.rb', line 111

def probe_net_udp
  []
end