sensu-plugins-mailhandler 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 139456fe5c0d8ddedb8681ffdbc95e3ce2098009
4
+ data.tar.gz: 115986337ad3aeae5e0a2efb3f6ea1dd8659462e
5
+ SHA512:
6
+ metadata.gz: 6a30b1a523878526fc28c90f41507e1faf61e7087c918e6298e728c1d5b7855e2a8ac7c0c7e94a179a40a97c620a15e9ae3acd981c6ee1328d2fefbd27f0d892
7
+ data.tar.gz: 1d955c8829f7b18b674c3b2708c8d289cafae92eda32681fb5526ba504568e630181feb4083de236f0c08e999e5f88e605d67666cc279eaf50bd8038de262817
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ The MIT License (MIT)
2
+ Copyright © 2016 Lars Bahner <lars.bahner@gmail.com>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8
+ the Software, and to permit persons to whom the Software is furnished to do so,
9
+ subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,157 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # mailhandler.rb
4
+ #
5
+ # This handler has been written after giving up on getting
6
+ # sensu-plugins-mailer to work. Make configuration simpler
7
+ # and less dependent on json. Not many bells and whistles.
8
+ # Easy to set email addresses and get mail off.
9
+ # Code has been unscrupulously copied from sensu-plugins-mailer.
10
+ #
11
+ #
12
+ # OUTPUT:
13
+ # Sends an email.
14
+ #
15
+ #
16
+ # USAGE:
17
+ # Configutation is prioritized as follows:
18
+ #
19
+ # - command line parameters are prioritized first
20
+ # - json config, ie. configuration files seconf
21
+ # - Potential check settings last
22
+ #
23
+ #
24
+ # PLATFORM:
25
+ # Any with ruby.
26
+ #
27
+ #
28
+ # DEPENDENCIES:
29
+ #
30
+ # gem: erubis
31
+ # gem: json
32
+ # gem: mail
33
+ # gem: sensu-plugin
34
+ #
35
+ #
36
+ # LICENSE:
37
+ # Copyright © 2016 Lars Bahner <<lars.bahner@gmail.com>
38
+ #
39
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
40
+ # for details.
41
+
42
+ require 'sensu-handler'
43
+ require 'json'
44
+ require 'mail'
45
+ require 'erubis'
46
+
47
+ $fqdn = %x(hostname -f)
48
+
49
+ class Mailhandler < Sensu::Handler
50
+
51
+ option :body_template,
52
+ description: 'Optional body template (erb) to use for generating message body.',
53
+ short: '-b body_template_file',
54
+ long: '--body-template body_template_file',
55
+ required: false
56
+
57
+ option :from,
58
+ description: 'Define sender of email',
59
+ short: '-f sender',
60
+ long: '--from sender',
61
+ required: false
62
+
63
+ option :json_config,
64
+ description: 'Configuration name',
65
+ short: '-j JSONconfig',
66
+ long: '--json-config JSONconfig',
67
+ required: false,
68
+ default: 'mailhandler'
69
+
70
+ option :smtp_host,
71
+ description: 'SMTP host to connect to for sending email',
72
+ short: '-h smtp host',
73
+ long: '--host smtp host',
74
+ required: false
75
+
76
+ option :smtp_port,
77
+ description: 'Port to connect smtp host on',
78
+ short: '-p smtp port',
79
+ long: '--port smtp port',
80
+ required: false
81
+
82
+ option :to,
83
+ description: 'comma separated list of recipient',
84
+ short: '-t recipient',
85
+ long: '--to recipient',
86
+ required: false
87
+
88
+ # Set sane defaults
89
+ default = Hash.new
90
+
91
+ default['from'] = 'root@' + $fqdn,
92
+ default['to'] = 'lars.bahner@gmail.com',
93
+ default['smtp_host'] = 'localhost',
94
+ default['smtp_port'] = '25'
95
+ default['subject'] = 'Sensu@' + @event['client']['name'] + ': ' + status_text
96
+
97
+ def status_text
98
+ case @event['check']['status']
99
+ when 0
100
+ 'OK'
101
+ when 1
102
+ 'WARNING'
103
+ when 2
104
+ 'CRITICAL'
105
+ else
106
+ 'UNKNOWN'
107
+ end
108
+ end
109
+
110
+ def get_setting(name)
111
+ if config[name]
112
+ config[name]
113
+ elsif settings[config[:json_config][name]]
114
+ settings[config[:json_config][name]]
115
+ elsif @event['check'][name]
116
+ @event['check'][name]
117
+ else
118
+ default[name]
119
+ end
120
+ end
121
+
122
+ def body
123
+ erb_template = if self.get_setting(body_template) && File.readable?(self.get_setting(body_template))
124
+ File.read(body_template)
125
+ else
126
+ <<-BODY
127
+ Status: <%= status_text %>
128
+ Check: <%= @event['client']['name'] %>/<%= @event['check']['name'] %>
129
+ Timestamp: <%= Time.at(@event['check']['issued']) %>
130
+ Occurences: <%= @event['occurrences'] %>
131
+
132
+ It's all in a days work for System Control™.
133
+
134
+ --
135
+ <%= output %>
136
+ BODY
137
+ template = Erubis.new(erb_template)
138
+ template.result(binding)
139
+ end
140
+
141
+ def handle
142
+
143
+ self.get_setting(to).each do |recipient|
144
+ mail = Mail.new do
145
+ from self.get_setting(from)
146
+ to recipient
147
+ subject self.get_setting(subject)
148
+ body self.body()
149
+ delivery_method :smtp, self.get_setting(smtp_host), self.get_setting(smtp_port)
150
+ end
151
+
152
+ mail.deliver!
153
+ end
154
+ end
155
+ end
156
+
157
+ puts "hello"
@@ -0,0 +1 @@
1
+ require sensu-plugins-mailhandler/version
@@ -0,0 +1,9 @@
1
+ module SensuPluginsMailhandler
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
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'sensu-plugins-mailhandler'
3
+ s.version = '0.0.1'
4
+ s.date = '2016-07-20'
5
+ s.summary = 'Handler to easily send email'
6
+ s.description = 'Handler to pretty print an event and send it as email.'
7
+ s.authors = ['Lars Bahner']
8
+ s.email = 'lars.bahner@gmail.com'
9
+ s.files = `git ls-files`.split($\)
10
+ s.executables = 'mailhandler.rb'
11
+ s.homepage = 'http://github.com/bahner/sensu-plugins-mailhandler'
12
+ s.license = 'MIT'
13
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-mailhandler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lars Bahner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Handler to pretty print an event and send it as email.
14
+ email: lars.bahner@gmail.com
15
+ executables:
16
+ - mailhandler.rb
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - bin/mailhandler.rb
22
+ - lib/sensu-plugins-mailhandler.rb
23
+ - lib/sensu-plugins-mailhandler/version.rb
24
+ - sensu-plugins-mailhandler.gemspec
25
+ homepage: http://github.com/bahner/sensu-plugins-mailhandler
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.5.1
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Handler to easily send email
49
+ test_files: []