sensucronic 0.1.0

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: 4e6fe2076af1cbc5abb22a7ba3c44f69681e48f6
4
+ data.tar.gz: 4ecb05e178254f4bca2db044de92bc4eff4c934e
5
+ SHA512:
6
+ metadata.gz: e226aa25c316211ba25f6e2956b8f481532849db6a5993dddbe1ca542d427c3fc0609b07bd7516c93fe8e1350e74588f97a07cf62293820fad82f83af1907442
7
+ data.tar.gz: b369c708948112ecbe4b6f7002cf2352b07dc44faab71e1895ddfe3d67a1ab13ca57acae9cba9905f11716f891e56182bf60784ff7830a7f0fe809cc9d7b1ca4
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sensucronic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 fess
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.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Sensucronic
2
+
3
+ sensucronic is a wrapper for your cron jobs to integrate with
4
+ the sensu client input socket.
5
+
6
+ it's written in ruby using mixlib::cli to fit in with the rest of the
7
+ sensu ecosystem
8
+
9
+ ## Installation
10
+
11
+ TODO: release gem so this works.
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'sensucronic'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install sensucronic
26
+
27
+ ## Usage
28
+
29
+ ```
30
+ sensucronic [ OPTIONS ] [ -- ] COMMAND [ ARGS ]
31
+ ```
32
+ ```
33
+ sensucronic [ OPTIONS ] [ -- ] 'COMMAND [ ARGS ]'
34
+ ```
35
+
36
+ sensucronic runs COMMAND with ARGS it generates a json report and submits
37
+ it to the sensu-client input socket
38
+
39
+ the OPTION --dryrun causes sensucronic to issue it's report to stdout
40
+ instead of the sensu client input socket.
41
+
42
+ ```
43
+ prompt% sensucronic --dry-run true
44
+ {
45
+ "command": "true",
46
+ "output": "",
47
+ "status": 0,
48
+ "exitcode": 0,
49
+ "agent": "sensucronic"
50
+ }
51
+ ```
52
+
53
+ exitcodes are propogated to sensu's status field, 0 ok, 1 warn, 2 crit, 3
54
+ unknown but anything > 3 sets status to 3.
55
+
56
+ TODO: add options to configure this.
57
+
58
+ ```
59
+ prompt% sensucronic --dry-run 'echo hi; exit 20'
60
+ {
61
+ "command": "echo\\ hi\\;\\ exit\\ 20",
62
+ "output": "hi\n",
63
+ "status": 3,
64
+ "exitcode": 20,
65
+ "agent": "sensucronic"
66
+ }
67
+ ```
68
+
69
+ given a single quoted argument it will be parsed with a shell
70
+ otherwise it tries to exec it as command followed by arguments
71
+
72
+ ```
73
+ prompt% sensucronic --dry-run exit 3
74
+ {
75
+ "command": "exit 3",
76
+ "output": "Errno::ENOENT\nNo such file or directory - exit",
77
+ "status": 3,
78
+ "exitcode": 127,
79
+ "agent": "sensucronic"
80
+ }
81
+ ```
82
+
83
+ ```
84
+ prompt% sensucronic --dry-run 'exit 3'
85
+ {
86
+ "command": "exit\\ 3",
87
+ "output": "",
88
+ "status": 3,
89
+ "exitcode": 3,
90
+ "agent": "sensucronic"
91
+ }
92
+
93
+ ```
94
+
95
+ use --help to view options
96
+ ```
97
+ prompt% sensucronic --help
98
+ Usage: sensucronic (options)
99
+ -d, --dry-run output result to stdout only
100
+ -h, --help print this message
101
+ -p, --port PORT the port number for the sensu client input socket
102
+ -s, --source SOURCE set the source attribute on the sensu result
103
+ ```
104
+
105
+ ## TODO
106
+ - accept options to add extra fields to the output
107
+ - accept options to configure the status in response to the exitcodes. (always warn, always crit )
108
+
109
+ ## Development
110
+
111
+ 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.
112
+
113
+ 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).
114
+
115
+ ## Contributing
116
+
117
+ Bug reports and pull requests are welcome on GitHub at https://github.com/opentable/sensucronic
118
+
119
+ ## License
120
+
121
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
122
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sensucronic"
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
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -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
data/exe/sensucronic ADDED
@@ -0,0 +1,29 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # sensucronic
4
+ #
5
+ # DESCRIPTION:
6
+ # a cron wrapper to integrate the result of cron execution with sensu
7
+ # it runs your cronjob, capturing the output and exit code and sends
8
+ # the result to sensu
9
+ #
10
+ # PLATFORMS:
11
+ # Linux
12
+ #
13
+ # DEPENDENCIES:
14
+ # gem: sensu-plugin
15
+ #
16
+ # USAGE:
17
+ # sensucronic [ OPTIONS ] [ -- ] COMMAND [ ARGS ]
18
+ #
19
+ # NOTES:
20
+ #
21
+ # LICENSE:
22
+ # fess <fess-sensuchronic@fess.org>
23
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
24
+ # for details.
25
+ #
26
+
27
+ require 'sensucronic'
28
+
29
+ Sensucronic.new.cli
@@ -0,0 +1,3 @@
1
+ class Sensucronic
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,131 @@
1
+ require 'sensucronic/version'
2
+ require 'mixlib/cli'
3
+ require 'shellwords'
4
+ require 'open3'
5
+ require 'json'
6
+
7
+
8
+ class Sensucronic
9
+ include Mixlib::CLI
10
+
11
+ option :dryrun,
12
+ :short => '-d',
13
+ :long => '--dry-run',
14
+ :boolean => true,
15
+ :default => false,
16
+ :description => 'output result to stdout only'
17
+
18
+ option :port,
19
+ :short => '-p PORT',
20
+ :long => '--port PORT',
21
+ :default => 3030,
22
+ :description => 'the port number for the sensu client input socket'
23
+
24
+ option :source,
25
+ :short => '-s SOURCE',
26
+ :long => '--source SOURCE',
27
+ :description => 'set the source attribute on the sensu result'
28
+
29
+ option :help,
30
+ :short => '-h',
31
+ :long => '--help',
32
+ :boolean => true,
33
+ :tail => true,
34
+ :show_options => true,
35
+ :description => 'print this message',
36
+ :exit => 0
37
+
38
+ attr :stdout
39
+ attr :stderr
40
+ attr :status
41
+
42
+ def dryrun
43
+ config[:dryrun]
44
+ end
45
+
46
+ def port
47
+ config[:port]
48
+ end
49
+
50
+ def cli(argv = ARGV)
51
+ parse_options(argv)
52
+ run
53
+ issue_report
54
+ end
55
+
56
+ def issue_report
57
+ if dryrun
58
+ report_stdout
59
+ else
60
+ report_socket
61
+ end
62
+ end
63
+
64
+ def report_stdout
65
+ puts JSON.pretty_generate(report)
66
+ end
67
+
68
+ def file_socket
69
+ "/dev/tcp/localhost/#{port}"
70
+ end
71
+
72
+ def report_socket
73
+ # TODO if needed actually connect via tcp to the
74
+ # client input socket.
75
+ File.open(file_socket, 'w') do |sock|
76
+ sock.write(report.to_json)
77
+ end
78
+ rescue Errno::ENOENT => e
79
+ STDERR.puts "failed to submit to sensu client input socket"
80
+ STDERR.puts e.message
81
+ exit 127
82
+ end
83
+
84
+ def run
85
+ @stdout, @stderr, @status = Open3.capture3(*cli_arguments)
86
+ rescue Errno::ENOENT => e
87
+ @stdout, @stderr, @status = e.class, e.message, $?
88
+ end
89
+
90
+ def output_status
91
+ status.to_s unless status.exited?
92
+ end
93
+
94
+ def output
95
+ [stdout, stderr, output_status]
96
+ .select {|msg| msg && msg != "" }
97
+ .join("\n");
98
+ end
99
+
100
+ def exitsignal
101
+ status.termsig || status.stopsig # I think stop sig will hang?
102
+ end
103
+
104
+ def exitcode
105
+ # be like the bash on signals
106
+ status.exited? ? status.exitstatus : 128 + exitsignal
107
+ end
108
+
109
+ def sensu_status
110
+ case exitcode
111
+ when 0..2
112
+ exitcode
113
+ else
114
+ 3
115
+ end
116
+ end
117
+
118
+ def report
119
+ {
120
+ command: Shellwords.shelljoin(cli_arguments),
121
+ output: output,
122
+ status: sensu_status,
123
+ exitcode: exitcode,
124
+ agent: self.class.to_s.downcase
125
+ }.tap do |r|
126
+ r[:exitsignal] = status.termsig if status.termsig
127
+ r[:source] = config[:source] if config[:source]
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sensucronic/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "sensucronic"
8
+ s.version = Sensucronic::VERSION
9
+ s.authors = ["fess"]
10
+ s.email = ["fess-sensucronic@fess.org"]
11
+
12
+ s.summary = %q{report status of cronjob to sensu}
13
+ #s.description = %q{}
14
+ #s.homepage = "TODO: Put your gem's website or public repo URL here."
15
+ s.license = "MIT"
16
+
17
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ s.bindir = "exe"
21
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_runtime_dependency "mixlib-cli", ">= 1.0" # optimistic
25
+
26
+ s.add_development_dependency "bundler", "~> 1.14"
27
+ s.add_development_dependency "rake", "~> 10.0"
28
+ s.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensucronic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fess
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mixlib-cli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '1.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.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - fess-sensucronic@fess.org
72
+ executables:
73
+ - sensucronic
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - exe/sensucronic
87
+ - lib/sensucronic.rb
88
+ - lib/sensucronic/version.rb
89
+ - sensucronic.gemspec
90
+ homepage:
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.14.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: report status of cronjob to sensu
114
+ test_files: []