sensu-plugins-log-pattern 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80637aee656f586af0f648d4a088b5a084ea9e16
4
+ data.tar.gz: 309df90ec8a6ec62e59ef58c7b2e22cffdc67a75
5
+ SHA512:
6
+ metadata.gz: 93579e0ff5da9895016760109ce54e91439a0d35f173173bc5839b2969eb9b3ad60846da93adfcfba07e5d9a4fd1701d0fcdc756f9a1ac687795c23977c9ebb6
7
+ data.tar.gz: 1682205c02cc12616c0c71746dbe13d42a500083b2f18ec6c5f8e94f2042caafd6a2a6d011939b70f52a9c23015fbabafe7e6f2403dc25335f944859d5f30f7b
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Change Log
2
+ This project adheres to [Semantic Versioning](http://semver.org/).
3
+
4
+ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)
5
+
6
+ ## Unreleased
7
+
8
+ ## [0.0.1] - 2016-01-06
9
+ ### Added
10
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015 Matteo Cerutti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Sensu plugin for monitoring patterns in log files
2
+
3
+ A sensu plugin to monitor patterns in log files.
4
+
5
+ ## Usage
6
+
7
+ The plugin accepts the following command line options:
8
+
9
+ ```
10
+ Usage: check-log-pattern.rb (options)
11
+ -c, --crit <COUNT> Critical if number of matches exceeds COUNT
12
+ -f, --file <PATH> Comma separated list of files (including globs) where pattern will be searched
13
+ --ignore-case Ignore case sensitive
14
+ -i, --ignore-pattern <PATTERN> Comma separated list of patterns to ignore
15
+ -p, --pattern <PATTERN> Comma separated list of patterns to search for (required)
16
+ --print-matches Print log lines that match patterns
17
+ -s, --source <file> Defines the log source (default: file) (required)
18
+ --state-dir <PATH> (default: /var/cache/check-log-pattern)
19
+ State directory
20
+ -w, --warn <COUNT> Warning if number of matches exceeds COUNT (default: 1)
21
+ ```
22
+
23
+ Currently, only file is supported as source. Systemd journald support will be added in future releases.
24
+
25
+ ## Author
26
+ Matteo Cerutti - <matteo.cerutti@hotmail.co.uk>
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # check-log-pattern.rb
4
+ #
5
+ # Author: Matteo Cerutti <matteo.cerutti@hotmail.co.uk>
6
+ #
7
+
8
+ require 'sensu-plugin/check/cli'
9
+ require 'fileutils'
10
+ require 'digest/md5'
11
+
12
+ class CheckLogPattern < Sensu::Plugin::Check::CLI
13
+ option :source,
14
+ :description => "Defines the log source (default: file)",
15
+ :short => "-s <file>",
16
+ :long => "--source <file>",
17
+ :in => ["file"],
18
+ :default => "file",
19
+ :required => true
20
+
21
+ option :file,
22
+ :description => "Comma separated list of files (including globs) where pattern will be searched",
23
+ :short => "-f <PATH>",
24
+ :long => "--file <PATH>",
25
+ :proc => proc { |s| s.split(',') },
26
+ :default => []
27
+
28
+ option :pattern,
29
+ :description => "Comma separated list of patterns to search for",
30
+ :short => "-p <PATTERN>",
31
+ :long => "--pattern <PATTERN>",
32
+ :proc => proc { |s| s.split(',') },
33
+ :required => true
34
+
35
+ option :ignore_pattern,
36
+ :description => "Comma separated list of patterns to ignore",
37
+ :short => "-i <PATTERN>",
38
+ :long => "--ignore-pattern <PATTERN>",
39
+ :proc => proc { |s| s.split(',') },
40
+ :default => []
41
+
42
+ option :state_dir,
43
+ :description => "State directory",
44
+ :long => "--state-dir <PATH> (default: /var/cache/check-log-pattern)",
45
+ :default => "/var/cache/check-log-pattern"
46
+
47
+ option :ignore_case,
48
+ :description => "Ignore case sensitive",
49
+ :long => "--ignore-case",
50
+ :boolean => true,
51
+ :default => false
52
+
53
+ option :print_matches,
54
+ :description => "Print log lines that match patterns",
55
+ :long => "--print-matches",
56
+ :boolean => true,
57
+ :default => false
58
+
59
+ option :warn,
60
+ :description => "Warning if number of matches exceeds COUNT (default: 1)",
61
+ :short => "-w <COUNT>",
62
+ :long => "--warn <COUNT>",
63
+ :default => 1
64
+
65
+ option :crit,
66
+ :description => "Critical if number of matches exceeds COUNT",
67
+ :short => "-c <COUNT>",
68
+ :long => "--crit <COUNT>",
69
+ :default => nil
70
+
71
+ def initialize()
72
+ super
73
+
74
+ @files = []
75
+
76
+ case config[:source]
77
+ when "file"
78
+ raise "Must specify one or more files with the --file command line option when source is file" unless config[:file].size > 0
79
+
80
+ # determine list of files
81
+ config[:file].each do |file|
82
+ @files += Dir.glob(file)
83
+ end
84
+ end
85
+
86
+ raise "Warning threshold must be lower than the critical threshold" if (config[:crit] != nil and config[:warn] > config[:crit])
87
+
88
+ # prepare state directory
89
+ FileUtils.mkdir_p(config[:state_dir]) unless File.directory?(config[:state_dir])
90
+ end
91
+
92
+ def run
93
+ case config[:source]
94
+ when "file"
95
+ problems = 0
96
+ matches = {}
97
+
98
+ @files.each do |file|
99
+ hash = Digest::MD5.hexdigest("#{file}_#{config[:pattern]}")
100
+ cursor_file = config[:state_dir] + "/" + hash + ".last_cursor"
101
+
102
+ if File.exists?(cursor_file)
103
+ last_cursor = File.read(cursor_file).chomp.to_i
104
+ else
105
+ last_cursor = 0
106
+ end
107
+
108
+ fd = File.open(file)
109
+ fd.seek(last_cursor, File::SEEK_SET) if last_cursor > 0
110
+ bread = 0
111
+ fd.each_line do |line|
112
+ bread += line.bytesize
113
+
114
+ str = config[:ignore_case] ? line.downcase : line
115
+
116
+ config[:ignore_pattern].each do |pattern|
117
+ next if match = str.match(pattern)
118
+ end
119
+
120
+ config[:pattern].each do |pattern|
121
+ if match = str.match(pattern)
122
+ matches[pattern] ||= {}
123
+ matches[pattern][file] ||= []
124
+ matches[pattern][file] << line
125
+ problems += 1
126
+ end
127
+ end
128
+ end
129
+
130
+ # update cursor file
131
+ File.open(cursor_file, 'w') { |f| f.write(last_cursor + bread) }
132
+ end
133
+
134
+ msg = []
135
+ bottom = []
136
+
137
+ matches.each do |pattern, files|
138
+ files.each do |file, lines|
139
+ msg << "#{lines.size} lines matching '#{pattern}' in #{file}"
140
+
141
+ if config[:print_matches]
142
+ bottom << " Lines matching '#{pattern}' in #{file}:"
143
+ lines.each do |line|
144
+ bottom << " * #{line.chomp}"
145
+ end
146
+ bottom << ""
147
+ end
148
+ end
149
+ end
150
+
151
+ msg << "\n\n" if config[:print_matches]
152
+
153
+ if config[:crit]
154
+ critical("Found " + msg.join(', ') + bottom.join("\n")) if problems > config[:crit]
155
+ end
156
+ warning("Found " + msg.join(', ') + bottom.join("\n")) if problems > config[:warn]
157
+ ok("Found no lines matching '#{config[:pattern].join(', ')}'")
158
+ end
159
+ end
160
+ end
@@ -0,0 +1 @@
1
+ require 'sensu-plugins-log-pattern/version'
@@ -0,0 +1,9 @@
1
+ module SensuPluginsLogPattern
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-log-pattern
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matteo Cerutti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sensu-plugin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.0
27
+ description: This plugin provides facilities for monitoring patterns in log files
28
+ email: "<matteo.cerutti@hotmail.co.uk>"
29
+ executables:
30
+ - check-log-pattern.rb
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE
36
+ - README.md
37
+ - bin/check-log-pattern.rb
38
+ - lib/sensu-plugins-log-pattern.rb
39
+ - lib/sensu-plugins-log-pattern/version.rb
40
+ homepage: https://github.com/m4ce/sensu-plugins-log-pattern
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ maintainer: "@m4ce"
45
+ development_status: active
46
+ production_status: stable
47
+ release_draft: 'false'
48
+ release_prerelease: 'false'
49
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
50
+ in /etc/default/sensu
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 1.9.3
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.4.5.1
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Sensu plugins for monitoring patterns in log files
70
+ test_files: []