Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Jones committed Feb 17, 2015
0 parents commit 3bf3e49
Show file tree
Hide file tree
Showing 15 changed files with 428 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
.vagrant/*
.DS_Store
.idea/*
*.gem
25 changes: 25 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

MethodLength:
Max: 200

LineLength:
Max: 160

FileName:
Enabled: false

PerceivedComplexity:
Enabled: false

CyclomaticComplexity:
Enabled: false

ClassLength:
Enabled: false

IfUnlessModifier:
Enabled: false

RegexpLiteral:
Enabled: false

18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: ruby
cache:
- bundler
install:
- bundle install
rvm:
- 1.9.3
- 2.0
- 2.1
notifications:
email:
recipients:
- [email protected]
on_success: change
on_failure: always

script:
- 'bundle exec rake default'
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#### 0.0.1.alpha.1
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Development Documentation](http://sensu-plugins.github.io/development/)
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2015 [email protected]

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## Sensu-Plugins-time-to-live

[![Build Status](https://travis-ci.org/sensu-plugins/sensu-plugins-time-to-live.svg?branch=master)](https://travis-ci.org/sensu-plugins/sensu-plugins-time-to-live)
[![Gem Version](https://badge.fury.io/rb/sensu-plugins-time-to-live.svg)](http://badge.fury.io/rb/sensu-plugins-time-to-live)
[![Code Climate](https://codeclimate.com/github/sensu-plugins/sensu-plugins-time-to-live/badges/gpa.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-time-to-live)
[![Test Coverage](https://codeclimate.com/github/sensu-plugins/sensu-plugins-time-to-live/badges/coverage.svg)](https://codeclimate.com/github/sensu-plugins/sensu-plugins-time-to-live)
[![Dependency Status](https://gemnasium.com/sensu-plugins/sensu-plugins-time-to-live.svg)](https://gemnasium.com/sensu-plugins/sensu-plugins-time-to-live)

## Functionality

## Files
* bin/extension-ttl

## Usage

## Installation

Add the public key (if you haven’t already) as a trusted certificate

```
gem cert --add <(curl -Ls https://raw.githubusercontent.com/sensu-plugins/sensu-plugins.github.io/master/certs/sensu-plugins.pem)
gem install sensu-plugins-time-to-live -P MediumSecurity
```

You can also download the key from /certs/ within each repository.

#### Rubygems

`gem install sensu-plugins-time-to-live`

#### Bundler

Add *sensu-plugins-disk-checks* to your Gemfile and run `bundle install` or `bundle update`

#### Chef

Using the Sensu **sensu_gem** LWRP
```
sensu_gem 'sensu-plugins-time-to-live' do
options('--prerelease')
version '0.0.1'
end
```

Using the Chef **gem_package** resource
```
gem_package 'sensu-plugins-time-to-live' do
options('--prerelease')
version '0.0.1'
end
```

## Notes
35 changes: 35 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'yard'
require 'github/markup'
require 'rubocop/rake_task'
require 'redcarpet'
require 'yard/rake/yardoc_task'

desc 'Don\'t run Rubocop for unsupported versions'
begin
if RUBY_VERSION >= '2.0.0'
args = [:spec, :make_bin_executable, :yard, :rubocop]
else
args = [:spec, :make_bin_executable, :yard]
end
end

YARD::Rake::YardocTask.new do |t|
OTHER_PATHS = %w()
t.files = ['lib/**/*.rb', 'bin/**/*.rb', OTHER_PATHS]
t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md --files CHANGELOG.md)
end

Rubocop::RakeTask.new

RSpec::Core::RakeTask.new(:spec) do |r|
r.pattern = FileList['**/**/*_spec.rb']
end

desc 'Make all plugins executable'
task :make_bin_executable do
`chmod -R +x bin/***/*.rb`
end

task default: args
32 changes: 32 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

config.vm.box = 'chef/centos-6.6'
config.vm.box_download_checksum = true
config.vm.box_download_checksum_type = 'md5'
config.vm.hostname = 'sensu-plugins-dev'

script = <<EOF
sudo yum update -y
sudo yum groupinstall -y development
sudo yum install -y vim nano
#sudo yum install -y ImagicMagic ImageMagick-devel mysql-devel # needed for bundle install
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -L get.rvm.io | bash -s stable
source /home/vagrant/.rvm/scripts/rvm
rvm reload
#rvm install 1.9.3
rvm install 2.1.4
#rvm install 2.0.0
#rvm use 1.9.3@sensu_plugins --create
#rvm use 2.0.0@sensu_plugins --create
rvm use 2.1.4@sensu_plugins --create
rvm use 2.1.4@sensu_plugins --default
EOF

config.vm.provision 'shell', inline: script, privileged: false
end
144 changes: 144 additions & 0 deletions bin/extension-ttl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Resolves a failed check after a specified time.
#
# Designed for stateless downtstream events that can't resolve themselves.
# Events that continue to come in will reset its TTL timer.
#
# Copyright 2014 Jive Software and contributors.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE for details.

require 'net/http'
require 'json'
require 'sensu-plugin/utils'

module Sensu
module Extension
class TimeToLive < Handler
def name
'ttl'
end

def description
'resets a failed check after a specified time'
end

def options
return @options if @options
@options = {
interval: 60
}
if @settings
if @settings['ttl'] && @settings['ttl'].is_a?(Hash)
@options.merge!(@settings[:ttl])
end
if @settings['api'] && @settings['api'].is_a?(Hash)
@options['api'] = @settings['api']
end
end
@options
end

def run(event_data)
retval = process_event_for_ttl(event_data)
yield(retval, 0)
end

def post_init
@logger.info('Setting up TTL expiration loop')
if options['api']
EM::PeriodicTimer.new(options[:interval]) do
periodic_ttl_expiration
end
else
@logger.info('No API access, deactivating TTL expiration loop')
end
end

def process_event_for_ttl(event_data)
@logger.info('TTL process event')
retval = 'event has no TTL expiration'
event = Oj.load(event_data)
check = event[:check]
new_expiry = check[:ttl] unless check.nil?
unless new_expiry.nil?
client_name = event[:client][:name] unless event[:client].nil?
check_name = check[:name] unless check.nil?
@logger.info("Received event with TTL: #{client_name}_#{check_name} expires in #{new_expiry} seconds")
now = Time.now.to_i
expires_at = now + new_expiry
res = api_post("/stashes/ttl/#{client_name}_#{check_name}", { ttl: expires_at }.to_json)
retval = 'stashed TTL for event - code ' + res.code.to_s
end
retval
end

def periodic_ttl_expiration
@logger.info('Starting execution of periodic TTL expirey')
all_stashes_s = api_get('/stashes')
all_stashes = JSON.parse(all_stashes_s.body)
ttl_stashes = all_stashes.select { |x| x['path'] =~ /\Attl\// }
now = Time.now.to_i
ttl_stashes.each do |stash|
check_and_expire_ttl_stash(stash, now)
end
@logger.info('Done execution of periodic TTL expirey')
end

def check_and_expire_ttl_stash(stash, now)
expiry = stash['content']['ttl'].to_i unless stash['content'].nil?
# #YELLOW
if !expiry.nil? && expiry <= now # rubocop:disable GuardClause
client_name, check_name = names_from_path(stash['path'])
age = (now - expiry).to_s
@logger.info("TTL - entry for #{client_name}_#{check_name} expired #{age} seconds ago")
payload = { client: client_name, check: check_name }
api_post('/resolve', payload.to_json)
api_delete("/stashes/#{stash['path']}")
end
end

def api_post(path, payload)
api_request(Net::HTTP::Post, path, payload)
end

def api_delete(path)
api_request(Net::HTTP::Delete, path, nil)
end

def api_get(path)
api_request(Net::HTTP::Get, path, nil)
end

def api_request(method, path, payload)
http = Net::HTTP.new(options['api']['host'], options['api']['port'])
req = method.new(path)
if options['api']['user'] && options['api']['password']
req.basic_auth(options['api']['user'], options['api']['password'])
end
# #YELLOW
unless payload.nil? # rubocop:disable IfUnlessModifier
req.body = payload
end
http.request(req)
end

def logger
Sensu::Logger.get
end

def get_check_data(event_data)
event = Oj.load(event_data)
check = event['check']
new_expiry = check['ttl'] unless check.nil?
client_name = event['client']['name'] unless event['client'].nil?
check_name = check['name'] unless check.nil?
[new_expiry, client_name, check_name]
end

def names_from_path(path)
subpath = path.split('/', 2)[1]
subpath.split('_', 2)
end
end # class TimeToLive
end # module Extension
end # module Sensu
21 changes: 21 additions & 0 deletions certs/sensu-plugins.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDgDCCAmigAwIBAgIBATANBgkqhkiG9w0BAQUFADBDMRIwEAYDVQQDDAltYXR0
am9uZXMxGDAWBgoJkiaJk/IsZAEZFgh5aWVsZGJvdDETMBEGCgmSJomT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OCvogyujQOjNVGN4gz1wwPr
-----END CERTIFICATE-----
7 changes: 7 additions & 0 deletions lib/sensu-plugins-time-to-live.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Set gem version
#
module SensuPluginsTimeToLive
# Gem version
VERSION = '0.0.1.alpha.1'
end
Loading

0 comments on commit 3bf3e49

Please sign in to comment.