logstash-filter-ipam 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f160ac54f8bc792c5c8e532de086002091d27cc571784e3d9139001d5c9530d
4
- data.tar.gz: 3ea8071243425f5e7426079c86f1f5ceeed4bc233d52abc1f7c98031a554d965
3
+ metadata.gz: aa73397a601bf30ef7770ae0acbdbca481597337b99d4e5645fecddbd3b644da
4
+ data.tar.gz: eaeb7ce6e8ef074f38805f18783092f1b4a41e322740881db07a786888907170
5
5
  SHA512:
6
- metadata.gz: fe0ba9b1b3b94b31e18ab3414b8531bdde0d50ca4b3aaeb5e27b11f21e0c4fb38308a7e052f69d09ae318c5af4b061f494890e120ba7598630c33e52c1c7fe62
7
- data.tar.gz: fae7985e7626548eaaffc903509a8829fb8a0cc985bfe8364c91bb543a1ec41f765ced9d9de6199bdffbec6a6c7e9e78c27504702725c1b6da5115004c58ea16
6
+ metadata.gz: c9c1c7939ececb9984e9aa85e6d411324958b9c7e38b680bbef37e7308d5a530ec89e121e064c074aaf6acf69888cae7304a3940588f162df4f12ea0bb6c5cf9
7
+ data.tar.gz: 55868dcf04026c5e36f3942439f904322c55db2ab740227aebd2ff006eb3642980ccf10f826bd3f85e761d1a579b4a81919171bbc3345ec4bd6ed6bc29c117c7
@@ -2,9 +2,10 @@
2
2
  require "logstash/filters/base"
3
3
  require "logstash/namespace"
4
4
  require "ipaddr"
5
+ require "mysql"
5
6
  require "json"
6
7
 
7
- # This filter will replace the contents of the default
8
+ # This filter will replace the contents of the default
8
9
  # message field with whatever you specify in the configuration.
9
10
  #
10
11
  # It is only intended to be used as an .
@@ -12,50 +13,105 @@ class LogStash::Filters::Ipam < LogStash::Filters::Base
12
13
 
13
14
  # Setting the config_name here is required. This is how you
14
15
  # configure this filter from your Logstash config.
15
- #
16
- # filter {
17
- # {
18
- # message => "My message..."
19
- # }
20
- # }
21
- #
22
16
  config_name "ipam"
23
-
24
- # Replace the message with this value.
17
+
18
+ # Logstash filter options.
25
19
  config :ip, :validate => :string, :required => true
26
- config :file, :validate => :string, :default => "/opt/subnets/subnets.json"
27
20
  config :field, :validate => :string, :default => "subnets"
21
+ config :gateway, :validate => :boolean, :default => false
28
22
 
23
+ # Mysql connection options.
24
+ config :mysql_host, :validate => :string, :required => true
25
+ config :mysql_user, :validate => :string, :required => true
26
+ config :mysql_pass, :validate => :string, :required => true
27
+ config :mysql_db, :validate => :string, :default => "phpipam"
29
28
 
30
- public
31
- def register
32
- # Add instance variables
33
- end # def register
29
+ # File storage options.
30
+ config :time_reset, :validate => :number, :default => 600
31
+ config :file, :validate => :string, :default => "/tmp/logstash-filter-ipam.json"
34
32
 
35
- public
36
- def filter(event)
37
-
38
- results = Array.new
39
- file = File.read(@file)
40
- json = JSON.parse(file)
41
- subnets = json["subnets"]
42
33
 
34
+ public
35
+ def register
36
+ # Check if the IP string is an actual IP, or stop process
43
37
  begin
44
- ip = IPAddr.new(@ip)
38
+ @ip = IPAddr.new(@ip)
45
39
  rescue ArgumentError => e
46
40
  @logger.warn("Invalid IP address, skipping", :address => @ip, :event => event)
47
41
  nil
48
42
  end
43
+ end # def register
44
+
45
+ private
46
+ def downloadIpamSubnets(event)
47
+ begin
48
+ client = Mysql::Client.new(:host => @mysql_host,
49
+ :username => @mysql_user,
50
+ :password => @mysql_pass,
51
+ :database => @mysql_db)
52
+ result = client.query("SELECT id, FROM subnets")
53
+ client.close()
54
+ return JSON.parse(result)
55
+ rescue
56
+ @logger.warn("Impossible to retrieve data from Mysql.", :address => @mysql_host, :event => event)
57
+ end
58
+ end
59
+
60
+ private
61
+ def getSubnets(event)
62
+ # Reading files
63
+ begin
64
+ file = File.read(@file)
65
+ json JSON.parse(file)
66
+ return json["subnets"]
67
+ rescue
68
+ @logger.warn("Impossible to read into file.", :address => @file, :event => event)
69
+ end
70
+ end
49
71
 
72
+ private
73
+ def checkIpSubnets(ip, subnets)
74
+ results = Array.new
50
75
  subnets.each do |sub|
51
- if IPAddr.new(sub['subnet'] + "/" + sub['netmask'].to_s) === ip
76
+ if !@gateway && sub['subnet'] == "0.0.0.0"
77
+ next
78
+ end
79
+ if IPAddr.new(sub['subnet'] + "/" + sub['netmask'].to_s).include?(ip)
52
80
  results.push(sub)
53
81
  end
54
82
  end
83
+ return results
84
+ end
85
+
86
+ private
87
+ def checkFile(event)
88
+ if (!File.exist?(@file) || File.mtime(@file).utc < (Time.now - @time_reset).utc)
89
+ begin
90
+ file = File.open(@file, 'w')
91
+ file.write(downloadIpamSubnets(event))
92
+ rescue
93
+ @logger.warn("Impossible to write into file.", :address => @file, :event => event)
94
+ end
95
+ end
96
+ end
97
+
98
+ public
99
+ def filter(event)
100
+ # Check file
101
+ # if doesn't exist => create with content.
102
+ # if need reset => update content.
103
+ checkFile(event)
104
+
105
+ # Get Subnets Checking the IP
106
+ # if can't read => Warning
107
+ # if gateway is false => won't register "0.0.0.0" subnets
108
+ ipamSubnets = getIpamSubnets(event)
109
+ subnets = checkIpSubnets(@ip, subnets)
55
110
 
56
111
  # Set field only if there is some subnets checked.
57
- if results.length > 0
58
- event.set(@field, results)
112
+ if subnets.length > 0
113
+ event.set(@field, subnets)
114
+ else
59
115
  # filter_matched should go in the last line of our successful code
60
116
  filter_matched(event)
61
117
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-filter-ipam'
3
- s.version = '0.1.1'
3
+ s.version = '0.1.2'
4
4
  s.licenses = ['Apache-2.0']
5
5
  s.summary = 'Correlation with IPAM.'
6
6
  s.description = 'Filter that allows getting subnets from existing file extracted from IPAM for an IP address.'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-filter-ipam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corentin Dekimpe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-20 00:00:00.000000000 Z
11
+ date: 2017-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement