sensu-plugins-rancher-service 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: 5ad2adc6afdbfb158abd3b7a6825d550e0559e14
4
+ data.tar.gz: 9039e88c737b03887cb9b2ce3930dff4ab5b7238
5
+ SHA512:
6
+ metadata.gz: 8c689efdcdd3669af40645f2f92a97b5efa99efe1884f843ae4ec594397a052d52b0409b3e9053fb13478888972f4f6018ed2f149bb16628e519d885c4e4a9a6
7
+ data.tar.gz: 5c0e4ecafb91af6a4879bf4c1d530c375c42b77814297e83d75f84c40bac7d56b8f2ed692ae7574de90b49c6501a0f96cea0ee14fbc29c40e473cb2eefdfab80
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-08
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,36 @@
1
+ # Sensu plugin for monitoring services running in Rancher
2
+
3
+ A sensu plugin to monitor the health state of services running in Rancher.
4
+
5
+ The plugin leverages the Sensu JIT clients feature (https://sensuapp.org/docs/latest/clients#jit-clients) to create a client for every
6
+ Rancher service discovered. The client name is in the format: \<stack_name>_\<service_name>.rancher.internal.
7
+
8
+ It then generates for every instance (container) running in the service OK/WARN/CRIT/UNKNOWN events via the sensu client socket
9
+ (https://sensuapp.org/docs/latest/clients#client-socket-input), reporting whether the instance is not monitored (WARN), healthy (OK) or unhealthy (CRITICAL).
10
+
11
+ You can optionally disable monitoring for a service by placing in the rancher-compose service metadata the following:
12
+
13
+ ```
14
+ service:
15
+ metadata:
16
+ sensu:
17
+ monitored: false
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ The plugin accepts the following command line options:
23
+
24
+ ```
25
+ Usage: check-rancher-service.rb (options)
26
+ --api-url <URL> Rancher Metadata API URL (default: http://rancher-metadata/2015-07-25)
27
+ --dryrun Do not send events to sensu client socket
28
+ -w, --warn Warn instead of throwing a critical failure
29
+ ```
30
+
31
+ ## Deployment
32
+
33
+ In order to run this check, you might want to deploy a sensu-client running as a container in Rancher, acting as a "sensu proxy".
34
+
35
+ ## Author
36
+ Matteo Cerutti - <matteo.cerutti@hotmail.co.uk>
@@ -0,0 +1,129 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # check-rancher-service.rb
4
+ #
5
+ # Author: Matteo Cerutti <matteo.cerutti@hotmail.co.uk>
6
+ #
7
+
8
+ require 'sensu-plugin/check/cli'
9
+ require 'net/http'
10
+ require 'json'
11
+
12
+ class CheckRancherService < Sensu::Plugin::Check::CLI
13
+ option :api_url,
14
+ :description => "Rancher Metadata API URL (default: http://rancher-metadata/2015-07-25)",
15
+ :long => "--api-url <URL>",
16
+ :proc => proc { |s| s.gsub(/\/$/, '') },
17
+ :default => "http://rancher-metadata/2015-07-25"
18
+
19
+ option :warn,
20
+ :description => "Warn instead of throwing a critical failure",
21
+ :short => "-w",
22
+ :long => "--warn",
23
+ :boolean => true,
24
+ :default => false
25
+
26
+ option :dryrun,
27
+ :description => "Do not send events to sensu client socket",
28
+ :long => "--dryrun",
29
+ :boolean => true,
30
+ :default => false
31
+
32
+ def initialize()
33
+ super
34
+ end
35
+
36
+ def send_client_socket(data)
37
+ if config[:dryrun]
38
+ puts data.inspect
39
+ else
40
+ sock = UDPSocket.new
41
+ sock.send(data + "\n", 0, "127.0.0.1", 3030)
42
+ end
43
+ end
44
+
45
+ def send_ok(check_name, source, msg)
46
+ event = {"name" => check_name, "source" => source, "status" => 0, "output" => "OK: #{msg}", "handler" => config[:handler]}
47
+ send_client_socket(event.to_json)
48
+ end
49
+
50
+ def send_warning(check_name, source, msg)
51
+ event = {"name" => check_name, "source" => source, "status" => 1, "output" => "WARNING: #{msg}", "handler" => config[:handler]}
52
+ send_client_socket(event.to_json)
53
+ end
54
+
55
+ def send_critical(check_name, source, msg)
56
+ event = {"name" => check_name, "source" => source, "status" => 2, "output" => "CRITICAL: #{msg}", "handler" => config[:handler]}
57
+ send_client_socket(event.to_json)
58
+ end
59
+
60
+ def send_unknown(check_name, source, msg)
61
+ event = {"name" => check_name, "source" => source, "status" => 3, "output" => "UNKNOWN: #{msg}", "handler" => config[:handler]}
62
+ send_client_socket(event.to_json)
63
+ end
64
+
65
+ def is_error?(data):
66
+ if data.is_a?(Hash) and data.has_key?('code') and data['code'] == 404
67
+ return true
68
+ else
69
+ return false
70
+
71
+ def api_get(query)
72
+ begin
73
+ uri = URI.parse("#{config[:api_url]}#{query}")
74
+ req = Net::HTTP::Get.new(uri.path, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
75
+ resp = Net::HTTP.new(uri.host, uri.port).request(req)
76
+ data = JSON.parse(resp.body)
77
+
78
+ if is_error?(data)
79
+ return nil
80
+ else
81
+ return data
82
+ end
83
+ rescue
84
+ raise "Failed to query Rancher Metadata API - Caught exception (#{$!})"
85
+ end
86
+ end
87
+
88
+ def get_services()
89
+ api_get("/services")
90
+ end
91
+
92
+ def get_container(name)
93
+ api_get("/containers/#{name}")
94
+ end
95
+
96
+ def run
97
+ services.each do |service|
98
+ source = "#{service['stack_name']}_#{service['name']}.rancher.internal"
99
+
100
+ if service['metadata'].has_key?('sensu') and service['metadata']['sensu'].has_key?('monitored')
101
+ monitored = service['metadata']['sensu']['monitored']
102
+ else
103
+ monitored = true
104
+ end
105
+
106
+ # get containers
107
+ service['containers'].each do |container_name|
108
+ check_name = "rancher-container-#{container_name}-health_state"
109
+ msg = "Instance #{container_name}"
110
+
111
+ unless monitored
112
+ send_ok(check_name, source, "#{msg} not monitored (disabled)")
113
+ else
114
+ health_state = get_container(container_name)['health_state']
115
+ case health_state
116
+ when 'healthy'
117
+ send_ok(check_name, source, "#{msg} is healthy")
118
+
119
+ when nil
120
+ send_warning(check_name, source, "#{msg} not monitored")
121
+
122
+ else
123
+ send_critical(check_name, source, "#{msg} is not healthy")
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1 @@
1
+ require 'sensu-plugins-rancher-service/version'
@@ -0,0 +1,9 @@
1
+ module SensuPluginsRancherService
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-rancher-service
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-08 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 services running in Rancher
28
+ email: "<matteo.cerutti@hotmail.co.uk>"
29
+ executables:
30
+ - check-rancher-service.rb
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE
36
+ - README.md
37
+ - bin/check-rancher-service.rb
38
+ - lib/sensu-plugins-rancher-service.rb
39
+ - lib/sensu-plugins-rancher-service/version.rb
40
+ homepage: https://github.com/m4ce/sensu-plugins-rancher-service
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 rancher-service
70
+ test_files: []