sensu-run 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: '0899a5379f38f596c11c1fb651c5197e9abf079a'
4
+ data.tar.gz: 69854bd88196ceea54aa6a0f483c5abc3eb2e6ae
5
+ SHA512:
6
+ metadata.gz: 4918f4d4526d767a5eb575548f8d62f8e62cb9428f940a6fca6b9ec466244bb60fda6f02d4ece3caddb2880303c82c624618359330e628695663397c6d7eeb23
7
+ data.tar.gz: 8896023aa4bf9411450fc3afc766259f5c260bbe779a1d4a221310fd69ad152bff6f2c0702d6d387797c75ea97c496e68bb337458539ecd1873e21e6ffa81104
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .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,11 @@
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.1.0] - 2020-01-10
9
+
10
+ ### Added
11
+ - 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-run.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sensu-run (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.2)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.4)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ sensu-run!
33
+
34
+ BUNDLED WITH
35
+ 1.16.2
@@ -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,46 @@
1
+ # Sensu Run
2
+
3
+ Sensu Run is a utility for executing Sensu Go checks on systems that cannot run the Sensu Go Agent. Sensu Run wraps the check command execution, constructs a Sensu Go Event, and posts the Event to a Sensu Go Backend API for processing. This utility is written in Ruby (1.8+) and only uses stdlib in hopes it can run on the vast majority of systems. This utility was inspired by https://hackage.haskell.org/package/sensu-run.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install sensu-run
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ $ sensu-run -h
15
+ Usage: sensu-run [options]
16
+ -h, --help Display this message
17
+ -V, --version Display version
18
+ -n, --namespace NAME Sensu Go namespace
19
+ -c, --check-name NAME Sensu Go check name
20
+ -e, --entity-name NAME Sensu Go entity name
21
+ -t, --check-timeout SECONDS Sensu Go check execution timeout
22
+ -T, --check-ttl SECONDS Sensu Go check TTL
23
+ -H, --handlers NAME[,NAME] Sensu Go event handlers
24
+ -b, --backends URL[,URL] URL or comma-delimited list of Sensu Go Backend API URLs
25
+ -k, --api-key KEY Sensu Go Backend API key
26
+ ```
27
+
28
+ Example:
29
+
30
+ ```
31
+ $ sensu-run -c true -e laptop -b http://localhost:8080 -k '46691493-dee5-46d8-8d2b-f37a18424afc' -- true
32
+ ```
33
+
34
+ ## Development
35
+
36
+ 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.
37
+
38
+ 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).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sensu/sensu-run.
43
+
44
+ ## License
45
+
46
+ 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/run"
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,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ unless $:.include?(File.dirname(__FILE__) + "/../lib/")
4
+ $: << File.dirname(__FILE__) + "/../lib"
5
+ end
6
+
7
+ require "sensu/run"
8
+
9
+ options = Sensu::Run.read_cli_opts
10
+
11
+ check = Sensu::Run::Check.new(options)
12
+ check.exec
13
+ event = check.event
14
+
15
+ api = Sensu::Run::APIClient.new(options)
16
+ api.post_event(event)
@@ -0,0 +1,52 @@
1
+ require "optparse"
2
+ require "sensu/run/version"
3
+ require "sensu/run/check"
4
+ require "sensu/run/api_client"
5
+
6
+ module Sensu
7
+ module Run
8
+ def self.read_cli_opts(arguments=ARGV)
9
+ options = {}
10
+
11
+ optparse = OptionParser.new do |opts|
12
+ opts.on("-h", "--help", "Display this message") do
13
+ puts opts
14
+ exit
15
+ end
16
+ opts.on("-V", "--version", "Display version") do
17
+ puts VERSION
18
+ exit
19
+ end
20
+ opts.on("-n", "--namespace NAME", "Sensu Go namespace") do |name|
21
+ options[:namespace] = name
22
+ end
23
+ opts.on("-c", "--check-name NAME", "Sensu Go check name") do |name|
24
+ options[:check_name] = name
25
+ end
26
+ opts.on("-e", "--entity-name NAME", "Sensu Go entity name") do |name|
27
+ options[:entity_name] = name
28
+ end
29
+ opts.on("-t", "--check-timeout SECONDS", "Sensu Go check execution timeout") do |timeout|
30
+ options[:timeout] = timeout.to_i
31
+ end
32
+ opts.on("-T", "--check-ttl SECONDS", "Sensu Go check TTL") do |ttl|
33
+ options[:ttl] = ttl.to_i
34
+ end
35
+ opts.on("-H", "--handlers NAME[,NAME]", "Sensu Go event handlers") do |handlers|
36
+ options[:handlers] = handlers.split(",")
37
+ end
38
+ opts.on("-b", "--backends URL[,URL]", "URL or comma-delimited list of Sensu Go Backend API URLs") do |backends|
39
+ options[:backends] = backends.split(",")
40
+ end
41
+ opts.on("-k", "--api-key KEY", "Sensu Go Backend API key") do |key|
42
+ options[:api_key] = key
43
+ end
44
+ end
45
+
46
+ optparse.parse!(arguments)
47
+ options[:command] = arguments.join(" ")
48
+
49
+ options
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,33 @@
1
+ require "net/http"
2
+ require "uri"
3
+ require "json"
4
+
5
+ module Sensu
6
+ module Run
7
+ class APIClient
8
+ def initialize(options={})
9
+ @options = options
10
+ uri = URI.parse(select_backend)
11
+ @http = Net::HTTP.new(uri.host, uri.port)
12
+ end
13
+
14
+ def select_backend
15
+ @backends ||= []
16
+ if @backends.empty?
17
+ @backends = @options[:backends].shuffle
18
+ end
19
+ @backends.shift
20
+ end
21
+
22
+ def post_event(event)
23
+ namespace = @options.fetch(:namespace, "default")
24
+ request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{namespace}/events")
25
+ request["Content-Type"] = "application/json"
26
+ request["Authorization"] = "Key #{@options[:api_key]}"
27
+ request.body = JSON.dump(event)
28
+ response = @http.request(request)
29
+ puts response.inspect
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require "open3"
2
+
3
+ module Sensu
4
+ module Run
5
+ class Check
6
+ attr_reader :output, :status
7
+
8
+ def initialize(options={})
9
+ @options = options
10
+ end
11
+
12
+ def exec
13
+ stdin, stdout, stderr, wait = Open3.popen3(@options[:command])
14
+ @output = "#{stdout.read}\n#{stderr.read}"
15
+ @status = wait.value.exitstatus
16
+ [@output, @status]
17
+ end
18
+
19
+ def event
20
+ {
21
+ :entity => {
22
+ :metadata => {
23
+ :name => @options[:entity_name],
24
+ :namespace => @options.fetch(:namespace, "default")
25
+ },
26
+ :entity_class => "proxy"
27
+ },
28
+ :check => {
29
+ :metadata => {
30
+ :name => @options[:check_name],
31
+ :namespace => @options.fetch(:namespace, "default")
32
+ },
33
+ :command => @options[:command],
34
+ :output => @output,
35
+ :status => @status
36
+ }
37
+ }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Sensu
2
+ module Run
3
+ VERSION = "0.1.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/run/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sensu-run"
8
+ spec.version = Sensu::Run::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-run"
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", "~> 1.16"
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-run
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Porter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-10 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: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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-run
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - CHANGELOG.md
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - exe/sensu-run
77
+ - lib/sensu/run.rb
78
+ - lib/sensu/run/api_client.rb
79
+ - lib/sensu/run/check.rb
80
+ - lib/sensu/run/version.rb
81
+ - sensu-run.gemspec
82
+ homepage: https://github.com/sensu/sensu-run
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.6.11
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: []