sensu-push 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4580b15946280939052b174118be33ac004d3d4b24545226188fec0f1cbdc87b
4
+ data.tar.gz: 07b280cb2f099c825d9e115c6afa7fbadd61c829d9f5d74655c9b13c8f78e160
5
+ SHA512:
6
+ metadata.gz: 5bf53680c77961b9d239d252c3b5a577da0a1b80b09635ecef835336693be9843f48301271bbbd88f72cf9528fcacde8eaaa99812fb47ab46af1fc7de3358a50
7
+ data.tar.gz: 929513f8b53417543f7a085231a8ff537fac9484601d90b0b2aebc52ea28b12fe78d4aeb81401098c40f0b528716d8c0c1344118decc8ec58b9781c7b37ec932
@@ -0,0 +1,13 @@
1
+ Gemfile.lock
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ vendor/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,25 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic
6
+ Versioning](http://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.3.0] - 2020-01-10
9
+
10
+ ### Fixed
11
+ - Entity creation no longer throws a method missing exception
12
+
13
+ ### Added
14
+ - TLS support
15
+
16
+ ## [0.2.0] - 2020-01-10
17
+
18
+ ### Added
19
+ - Create proxy Entity if missing
20
+ - Check TTL
21
+
22
+ ## [0.1.0] - 2020-01-10
23
+
24
+ ### Added
25
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sensu-push.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sensu Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # Sensu Push
2
+
3
+ Sensu Push is a utility for executing Sensu Go checks on systems that
4
+ cannot run the Sensu Go Agent. Sensu Push wraps the check command
5
+ execution, constructs a Sensu Go Entity and Event, and communicates
6
+ with the Sensu Go Backend API to create the Entity (if missing) and
7
+ submit the Event for processing. This utility is written in Ruby
8
+ (1.8+) and only uses stdlib in hopes it can run on the vast majority
9
+ of systems.
10
+
11
+ ## Installation
12
+
13
+ ```
14
+ gem install sensu-push
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```
20
+ $ sensu-push -h
21
+ Usage: sensu-push [options]
22
+ -h, --help Display this message
23
+ -V, --version Display version
24
+ -n, --namespace NAME Sensu Go namespace
25
+ -c, --check-name NAME Sensu Go check name
26
+ -e, --entity-name NAME Sensu Go entity name
27
+ -t, --check-timeout SECONDS Sensu Go check execution timeout
28
+ -T, --check-ttl SECONDS Sensu Go check TTL
29
+ -H, --handlers NAME[,NAME] Sensu Go event handlers
30
+ -b, --backends URL[,URL] URL or comma-delimited list of Sensu Go Backend API URLs
31
+ -k, --api-key KEY Sensu Go Backend API key
32
+ -s, --insecure-skip-tls-verify Skip TLS verify peer when using HTTPS
33
+ ```
34
+
35
+ Example:
36
+
37
+ ```
38
+ $ sensu-push -c true -e laptop -b http://localhost:8080 -k '46691493-dee5-46d8-8d2b-f37a18424afc' -- true
39
+ ```
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sensu/sensu-push.
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sensu/push"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ unless $:.include?(File.dirname(__FILE__) + "/../lib/")
4
+ $: << File.dirname(__FILE__) + "/../lib"
5
+ end
6
+
7
+ require "sensu/push"
8
+
9
+ options = Sensu::Push.read_cli_opts
10
+
11
+ entity = Sensu::Push::Entity.new(options)
12
+ check = Sensu::Push::Check.new(options)
13
+ check.exec!
14
+ event = Sensu::Push::Event.new(entity, check)
15
+
16
+ api = Sensu::Push::APIClient.new(options)
17
+ api.create_entity_if_missing(entity)
18
+ api.create_event(event)
@@ -0,0 +1,57 @@
1
+ require "optparse"
2
+ require "sensu/push/version"
3
+ require "sensu/push/entity"
4
+ require "sensu/push/check"
5
+ require "sensu/push/event"
6
+ require "sensu/push/api_client"
7
+
8
+ module Sensu
9
+ module Push
10
+ def self.read_cli_opts(arguments=ARGV)
11
+ options = {}
12
+
13
+ optparse = OptionParser.new do |opts|
14
+ opts.on("-h", "--help", "Display this message") do
15
+ puts opts
16
+ exit
17
+ end
18
+ opts.on("-V", "--version", "Display version") do
19
+ puts VERSION
20
+ exit
21
+ end
22
+ opts.on("-n", "--namespace NAME", "Sensu Go namespace") do |name|
23
+ options[:namespace] = name
24
+ end
25
+ opts.on("-c", "--check-name NAME", "Sensu Go check name") do |name|
26
+ options[:check_name] = name
27
+ end
28
+ opts.on("-e", "--entity-name NAME", "Sensu Go entity name") do |name|
29
+ options[:entity_name] = name
30
+ end
31
+ opts.on("-t", "--check-timeout SECONDS", "Sensu Go check execution timeout") do |timeout|
32
+ options[:timeout] = timeout.to_i
33
+ end
34
+ opts.on("-T", "--check-ttl SECONDS", "Sensu Go check TTL") do |ttl|
35
+ options[:ttl] = ttl.to_i
36
+ end
37
+ opts.on("-H", "--handlers NAME[,NAME]", "Sensu Go event handlers") do |handlers|
38
+ options[:handlers] = handlers.split(",")
39
+ end
40
+ opts.on("-b", "--backends URL[,URL]", "URL or comma-delimited list of Sensu Go Backend API URLs") do |backends|
41
+ options[:backends] = backends.split(",")
42
+ end
43
+ opts.on("-k", "--api-key KEY", "Sensu Go Backend API key") do |key|
44
+ options[:api_key] = key
45
+ end
46
+ opts.on("-s", "--insecure-skip-tls-verify", "Skip TLS verify peer when using HTTPS") do |key|
47
+ options[:skip_tls_verify_peer] = true
48
+ end
49
+ end
50
+
51
+ optparse.parse!(arguments)
52
+ options[:command] = arguments.join(" ")
53
+
54
+ options
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,67 @@
1
+ require "uri"
2
+ require "json"
3
+
4
+ begin
5
+ require "net/https"
6
+ rescue LoadError
7
+ require "net/http"
8
+ end
9
+
10
+ module Sensu
11
+ module Push
12
+ class APIClient
13
+ def initialize(options={})
14
+ @options = options
15
+ uri = URI.parse(select_backend)
16
+ @http = Net::HTTP.new(uri.host, uri.port)
17
+ if uri.scheme == "https"
18
+ @http.use_ssl = true
19
+ @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
20
+ if options[:skip_tls_verify_peer]
21
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
22
+ end
23
+ end
24
+ end
25
+
26
+ def select_backend
27
+ @backends ||= []
28
+ if @backends.empty?
29
+ @backends = @options[:backends].shuffle
30
+ end
31
+ @backends.shift
32
+ end
33
+
34
+ def entity_exists?(entity)
35
+ request = Net::HTTP::Get.new("/api/core/v2/namespaces/#{entity.namespace}/entities/#{entity.name}")
36
+ request["Content-Type"] = "application/json"
37
+ request["Authorization"] = "Key #{@options[:api_key]}"
38
+ response = @http.request(request)
39
+ response.code == "200"
40
+ end
41
+
42
+ def create_entity(entity)
43
+ request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{entity.namespace}/entities")
44
+ request["Content-Type"] = "application/json"
45
+ request["Authorization"] = "Key #{@options[:api_key]}"
46
+ request.body = JSON.dump(entity.to_hash)
47
+ response = @http.request(request)
48
+ puts response.inspect
49
+ end
50
+
51
+ def create_entity_if_missing(entity)
52
+ unless entity_exists?(entity)
53
+ create_entity(entity)
54
+ end
55
+ end
56
+
57
+ def create_event(event)
58
+ request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{event.entity.namespace}/events")
59
+ request["Content-Type"] = "application/json"
60
+ request["Authorization"] = "Key #{@options[:api_key]}"
61
+ request.body = JSON.dump(event.to_hash)
62
+ response = @http.request(request)
63
+ puts response.inspect
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,35 @@
1
+ require "open3"
2
+
3
+ module Sensu
4
+ module Push
5
+ class Check
6
+ attr_reader :name, :namespace, :output, :status
7
+
8
+ def initialize(options={})
9
+ @options = options
10
+ @name = @options[:check_name]
11
+ @namespace = @options.fetch(:namespace, "default")
12
+ end
13
+
14
+ def exec!
15
+ stdin, stdout, stderr, wait = Open3.popen3(@options[:command])
16
+ @output = "#{stdout.read}\n#{stderr.read}"
17
+ @status = wait.value.exitstatus
18
+ [@output, @status]
19
+ end
20
+
21
+ def to_hash
22
+ {
23
+ :metadata => {
24
+ :name => @name,
25
+ :namespace => @namespace
26
+ },
27
+ :command => @options[:command],
28
+ :output => @output,
29
+ :status => @status,
30
+ :ttl => @options.fetch(:ttl, 0)
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ module Sensu
2
+ module Push
3
+ class Entity
4
+ attr_reader :name, :namespace
5
+
6
+ def initialize(options={})
7
+ @options = options
8
+ @name = options[:entity_name]
9
+ @namespace = options.fetch(:namespace, "default")
10
+ end
11
+
12
+ def to_hash
13
+ {
14
+ :metadata => {
15
+ :name => @name,
16
+ :namespace => @namespace
17
+ },
18
+ :entity_class => "proxy"
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module Sensu
2
+ module Push
3
+ class Event
4
+ attr_reader :entity, :check
5
+
6
+ def initialize(entity, check)
7
+ @entity = entity
8
+ @check = check
9
+ end
10
+
11
+ def to_hash
12
+ {
13
+ :entity => @entity.to_hash,
14
+ :check => @check.to_hash
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Sensu
2
+ module Push
3
+ VERSION = "0.3.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sensu/push/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sensu-push"
8
+ spec.version = Sensu::Push::VERSION
9
+ spec.authors = ["Sean Porter"]
10
+ spec.email = ["portertech@gmail.com", "engineering@sensu.io"]
11
+
12
+ spec.summary = "A utility for executing Sensu Go checks on systems that cannot run the Sensu Go Agent"
13
+ spec.description = "A utility for executing Sensu Go checks on systems that cannot run the Sensu Go Agent"
14
+ spec.homepage = "https://github.com/sensu/sensu-push"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-push
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Porter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: A utility for executing Sensu Go checks on systems that cannot run the
56
+ Sensu Go Agent
57
+ email:
58
+ - portertech@gmail.com
59
+ - engineering@sensu.io
60
+ executables:
61
+ - sensu-push
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - CHANGELOG.md
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - exe/sensu-push
76
+ - lib/sensu/push.rb
77
+ - lib/sensu/push/api_client.rb
78
+ - lib/sensu/push/check.rb
79
+ - lib/sensu/push/entity.rb
80
+ - lib/sensu/push/event.rb
81
+ - lib/sensu/push/version.rb
82
+ - sensu-push.gemspec
83
+ homepage: https://github.com/sensu/sensu-push
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.1.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: A utility for executing Sensu Go checks on systems that cannot run the Sensu
106
+ Go Agent
107
+ test_files: []