sensu-plugins-influxdb-handoff 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54c87fe342ff71e94053eeb9b5566f2a30c0f464
4
+ data.tar.gz: 803f1a5ee8529dda5904cd19ffeab850acdb58da
5
+ SHA512:
6
+ metadata.gz: 57b9a500ec47f40cb81b0d5dcf64544ec8ca6009699cbc1876cf6647da363984a105add4df7852e3ba62559d214f289a1ed9b79da06bca541695a3bbf66e1355
7
+ data.tar.gz: 49ab6415674551198a326a3d3a94681df1b185451ef349eb4f90cdf0197d5c9b4d721ff77f3608b1d67a2bb0b7baa47ac19f365058e4998480d2b64677c01341
@@ -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
+ # [0.1.0] - 2017-03-06
7
+ ### Added
8
+ - initial release
9
+
10
+ [0.1.0]: https://github.com/ve-interactive/sensu-plugins-influxdb-handoff/compare/0.1.0...HEAD
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Andy Royle
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.
@@ -0,0 +1,50 @@
1
+ ## sensu-plugins-influxdb-handoff
2
+
3
+ [ ![Build Status](https://travis-ci.org/ve-interactive/sensu-plugins-influxdb-handoff.svg?branch=master)](https://travis-ci.org/ve-interactive/sensu-plugins-influxdb-handoff)
4
+ [![Gem Version](https://badge.fury.io/rb/sensu-plugins-influxdb-handoff.svg)](http://badge.fury.io/rb/sensu-plugins-influxdb-handoff)
5
+ [![Code Climate](https://codeclimate.com/github/ve-interactive/sensu-plugins-influxdb-handoff/badges/gpa.svg)](https://codeclimate.com/github/ve-interactive/sensu-plugins-influxdb-handoff)
6
+ [![Test Coverage](https://codeclimate.com/github/ve-interactive/sensu-plugins-influxdb-handoff/badges/coverage.svg)](https://codeclimate.com/github/ve-interactive/sensu-plugins-influxdb-handoff)
7
+
8
+ ## Functionality
9
+
10
+ ## Files
11
+ * bin/handler-influxdb-handoff.rb
12
+
13
+ ## Usage
14
+
15
+ **example config:**
16
+ ```json
17
+ {
18
+ "handlers": {
19
+ "influxdb-handoff": {
20
+ "command": "/opt/sensu/embedded/bin/handler-influxdb-handoff.rb",
21
+ "type": "pipe",
22
+ "filters": [],
23
+ "severities": ["critical"]
24
+ }
25
+ },
26
+ "influxdb-handoff": {
27
+ "user": "myuser",
28
+ "keyfile": "/home/user/.ssh/id_rsa",
29
+ "handoff_dir": "/var/lib/influxdb/hh/*",
30
+ "lockfile": "/var/run/influxdb/handoff.lock",
31
+ "quiet_period": "300"
32
+ }
33
+ }
34
+ ```
35
+
36
+ **options:**
37
+
38
+ - `user`: ssh user to connect as
39
+ - `keyfile`: private key file to use for ssh, cannot be used with `password`
40
+ - `password`: password for ssh authentication, cannot be used with `keyfile`
41
+ - `handoff_dir`: directory where handoff data is stored
42
+ - `lockfile`: local lockfile to prevent multiple concurrent runs
43
+ - `quiet_period`: period (in seconds) between runs of the handler
44
+
45
+ ## Installation
46
+
47
+ [Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
48
+
49
+ ## Notes
50
+ The ruby executables are installed in a path similar to `/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-influxdb-handoff-0.1.0/bin`
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sensu-handler'
4
+ require 'net/ssh'
5
+ require 'net/http'
6
+ require 'time'
7
+ require 'sensu-plugins-influxdb-handoff'
8
+
9
+ class InfluxDBHandoff < Sensu::Handler
10
+ def filter; end
11
+
12
+ def handle
13
+ puts 'handoff dir handler starting'
14
+
15
+ config = settings['influxdb-handoff']
16
+
17
+ ssh_user = config['user']
18
+ ssh_password = config['password']
19
+ ssh_keyfile = config['keyfile']
20
+ ssh_keydata = config['keydata']
21
+ handoff_dir = config['handoff_dir']
22
+
23
+ lockfile = config['lockfile']
24
+ quiet_period = config['quiet_period'].to_i
25
+
26
+ ssh_options = {}
27
+ ssh_options[:password] = ssh_password unless ssh_password.nil?
28
+ ssh_options[:keys] = ssh_keyfile.nil? ? [] : [ssh_keyfile]
29
+ ssh_options[:key_data] = ssh_keydata unless ssh_keydata.nil?
30
+ ssh_options[:auth_methods] = ssh_password.nil? ? ['publickey'] : ['password']
31
+
32
+ host = @event['client']['name']
33
+ executed = @event['check']['executed'].to_i
34
+ action = @event['action']
35
+
36
+ if action == 'resolve'
37
+ puts 'action = resolve, nothing to do'
38
+ return
39
+ end
40
+
41
+ if File.exist?(lockfile)
42
+ lockfile_time = IO.readlines(lockfile)[0].to_i
43
+ puts "lockfile_time: #{lockfile_time}"
44
+
45
+ if (executed - lockfile_time) < quiet_period
46
+ puts 'influxdb-handoff: exiting because lockfile is too new'
47
+ return
48
+ end
49
+ end
50
+
51
+ File.open(lockfile, 'w') { |file| file.write(Time.now.to_i) }
52
+
53
+ Net::SSH.start(host, ssh_user, ssh_options) do |ssh|
54
+ result = ssh_exec(ssh, 'sudo service influxdb stop')
55
+ throw_if_not_ok(result)
56
+
57
+ result = ssh_exec(ssh, "rm -rf #{handoff_dir}")
58
+ throw_if_not_ok(result)
59
+
60
+ result = ssh_exec(ssh, 'sudo service influxdb start')
61
+ throw_if_not_ok(result)
62
+ end
63
+
64
+ wait_for_ok(host)
65
+
66
+ puts 'handoff dir handler finished'
67
+
68
+ rescue => error
69
+ STDERR.puts "influxdb-handoff: #{error}"
70
+ end
71
+
72
+ def ssh_exec(ssh, cmd)
73
+ SensuPluginsInfluxDBHandoff::SshExec.ssh_exec!(ssh, cmd)
74
+ end
75
+
76
+ def throw_if_not_ok(result)
77
+ raise "sshexec error: STDOUT: #{result.stdout}, STDERR: #{result.stderr}, code: #{result.exit_status}" unless result.exit_status == 0
78
+ end
79
+
80
+ def wait_for_ok(host)
81
+ uri = URI("http://#{host}:8086/ping")
82
+ res = nil
83
+ count = 0
84
+
85
+ until res.is_a?(Net::HTTPSuccess)
86
+ sleep(10)
87
+ res = Net::HTTP.get_response(uri)
88
+ puts res.code
89
+ count += 1
90
+
91
+ if count >= 20
92
+ raise "timed out waiting for influxdb to start again, last response #{res.code} #{res.message}"
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,2 @@
1
+ require 'sensu-plugins-influxdb-handoff/version'
2
+ require 'sensu-plugins-influxdb-handoff/ssh-exec'
@@ -0,0 +1,100 @@
1
+ require 'net/ssh'
2
+ require 'ostruct'
3
+ require 'logger'
4
+
5
+ # copy-pasted from https://rubygems.org/gems/ssh-exec/ to solve dependency issues
6
+
7
+ module SensuPluginsInfluxDBHandoff
8
+ module SshExec
9
+ log = Logger.new(STDOUT)
10
+ log.level = Logger::INFO
11
+
12
+ class ExecutionError < StandardError
13
+ attr_reader :object
14
+
15
+ def initialize(object)
16
+ @object = object
17
+ end
18
+ end
19
+
20
+ # Execute the given command using the given ssh object and capture its standard output, standard
21
+ # error, and exit status. The implementation is based on this
22
+ # {http://stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library/3386375#3386375
23
+ # StackOveflow answer}.
24
+ # @param ssh the Net::SSH shell to run the command in
25
+ # @param options [Hash] optional settings:
26
+ # `echo_stdout` - whether to echo standard output from the subcommand,
27
+ # `echo_stderr` - whether to echo standard error from the subcommand
28
+ # @return [OpenStruct] a struct containing `stdout`, `stderr`, `exit_status`, and `exit_signal`
29
+ def self.ssh_exec!(ssh, command, options = {})
30
+ options = options.clone
31
+ echo_stdout = options[:echo_stdout]
32
+ echo_stderr = options[:echo_stderr]
33
+ raise "Invalid options: #{options}" unless options.empty?
34
+
35
+ stdout_data = ''
36
+ stderr_data = ''
37
+ exit_code = nil
38
+ exit_signal = nil
39
+ ssh.open_channel do |channel|
40
+ channel.exec(command) do |_, success|
41
+ unless success
42
+ raise "FAILED: couldn't execute command #{command}"
43
+ end
44
+ channel.on_data do |_, data|
45
+ stdout_data += data
46
+ $stdout.write(data) if echo_stdout
47
+ end
48
+
49
+ channel.on_extended_data do |_, _, data|
50
+ stderr_data += data
51
+ $stderr.write(data) if echo_stderr
52
+ end
53
+
54
+ channel.on_request('exit-status') do |_, data|
55
+ exit_code = data.read_long
56
+ end
57
+
58
+ channel.on_request('exit-signal') do |_, data|
59
+ exit_signal = data.read_long
60
+ end
61
+ end
62
+ end
63
+ ssh.loop
64
+ OpenStruct.new(
65
+ stdout: stdout_data,
66
+ stderr: stderr_data,
67
+ exit_status: exit_code,
68
+ exit_signal: exit_signal
69
+ )
70
+ end
71
+
72
+ # Runs the given command in the given SSH shell, and raises ExecutionError
73
+ # in case of failure (nonzero exit status). Logs the command in all cases.
74
+ # Logs the command, exit status, standard output and standard error in case of failure.
75
+ # @param ssh the Net::SSH shell to run the command in
76
+ # @param options [Hash] optional settings:
77
+ # `echo_stdout` - whether to echo stdout from the subcommand,
78
+ # `echo_stderr` - whether to echo stderr from the subcommand,
79
+ # `quiet` - to suppress logging the command and the error in case of non-zero exit status
80
+ # @return [OpenStruct] a struct containing `stdout`, `stderr`, `exit_status`, and `exit_signal`
81
+ def self.ensure_exec(ssh, command, options = {})
82
+ result = ssh_exec!(ssh, command, options)
83
+
84
+ options = options.clone
85
+ quiet = options.delete(:quiet)
86
+
87
+ @@log.info("Running on #{ssh.host}: #{command}") unless quiet
88
+
89
+ if result.exit_status != 0
90
+ @@log.error(
91
+ ("Failed running command #{command}: exit status #{result.exit_status}. " +
92
+ (result.stdout.empty? ? '' : "Standard output:\n#{result.stdout}\n") +
93
+ (result.stderr.empty? ? '' : "Standard error:\n#{result.stderr}")).strip
94
+ ) unless quiet
95
+ raise ExecutionError, "Failed running command #{command}: exit status #{result.exit_status}"
96
+ end
97
+ result
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,9 @@
1
+ module SensuPluginsInfluxDBHandoff
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-influxdb-handoff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andy Royle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-07 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'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-ssh
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codeclimate-test-reporter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: github-markup
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redcarpet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.40.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.40.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: yard
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.8'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.8'
167
+ description: This plugin provides a handler for clearing the influxdb handoff dir
168
+ email: "<ajroyle@gmail.com>"
169
+ executables:
170
+ - handler-influxdb-handoff.rb
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - CHANGELOG.md
175
+ - LICENSE
176
+ - README.md
177
+ - bin/handler-influxdb-handoff.rb
178
+ - lib/sensu-plugins-influxdb-handoff.rb
179
+ - lib/sensu-plugins-influxdb-handoff/ssh-exec.rb
180
+ - lib/sensu-plugins-influxdb-handoff/version.rb
181
+ homepage: https://github.com/ve-interactive/sensu-plugins-influxdb-handoff
182
+ licenses:
183
+ - MIT
184
+ metadata:
185
+ maintainer: ve-interactive
186
+ development_status: active
187
+ production_status: unstable - testing recommended
188
+ release_draft: 'false'
189
+ release_prerelease: 'false'
190
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
191
+ in /etc/default/sensu
192
+ rdoc_options: []
193
+ require_paths:
194
+ - lib
195
+ required_ruby_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: 2.0.0
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ requirements: []
206
+ rubyforge_project:
207
+ rubygems_version: 2.4.8
208
+ signing_key:
209
+ specification_version: 4
210
+ summary: Sensu plugins for clearing the influxdb handoff dir
211
+ test_files: []