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 c41e69f
Show file tree
Hide file tree
Showing 18 changed files with 666 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
@@ -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
@@ -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
@@ -0,0 +1,18 @@
language: ruby
cache:
- bundler
install:
- bundle install
rvm:
- 1.9.3
- 2.0
- 2.1
notifications:
email:
recipients:
- mattjones@yieldbot.com
on_success: change
on_failure: always

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

gemspec
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2015 devops@yieldbot.com

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.
94 changes: 94 additions & 0 deletions README.md
@@ -0,0 +1,94 @@
## Sensu-Plugins-mailer

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

## Functionality

## Files
* bin/handler-mailer-mailgun
* bin/handler-mailer-ses
* bin/handler-mailer

## Usage

**handler-mailer-mailgun**
```
{
"mailer-mailgun": {
"mail_from": "sensu@example.com",
"mail_to": "bob@example.com",
"mg_apikey": "mailgunapikeygoeshere",
"mg_domain": "mailgun.domain.com"
}
}
```

**handler-mailer-ses**
```
{
"mailer-ses": {
"mail_from": "sensu@example.com",
"mail_to": "monitor@example.com",
"aws_access_key": "myawsaccesskey",
"aws_secret_key": "myawssecretkey",
"aws_ses_endpoint": "email.us-east-1.amazonaws.com"
}
}
```

**handler-mailer**
```
{
"mailer": {
"admin_gui": "http://admin.example.com:8080/",
"mail_from": "sensu@example.com",
"mail_to": "monitor@example.com",
"smtp_address": "smtp.example.org",
"smtp_port": "25",
"smtp_domain": "example.org"
}
}
```

## 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-mailer -P MediumSecurity
```

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

#### Rubygems

`gem install sensu-plugins-mailer`

#### 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-mailer' do
options('--prerelease')
version '0.0.1'
end
```

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

## Notes
43 changes: 43 additions & 0 deletions Rakefile
@@ -0,0 +1,43 @@

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
@@ -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
27 changes: 27 additions & 0 deletions bin/README.md
@@ -0,0 +1,27 @@
# Notification handlers

## mailer

The following three configuration variables must be set if you want mailer to use remote SMTP settings:

smtp_address - defaults to "localhost"
smtp_port - defaults to "25"
smtp_domain - defaults to "localhost.localdomain"

There is an optional subscriptions hash which can be added to your mailer.json file. This subscriptions hash allows you to define individual mail_to addresses for a given subscription. When the mailer handler runs it will check the clients subscriptions and build a mail_to string with the default mailer.mail_to address as well as any subscriptions the client subscribes to where a mail_to address is found. There can be N number of hashes inside of subscriptions but the key for a given hash inside of subscriptions must match a subscription name.

```json
{
"mailer": {
"mail_from": "sensu@example.com",
"mail_to": "monitor@example.com",
"smtp_address": "smtp.example.org",
"smtp_port": "25",
"smtp_domain": "example.org",
"subscriptions": {
"subscription_name": {
"mail_to": "teamemail@example.com"
}
}
}
```
84 changes: 84 additions & 0 deletions bin/handler-mailer-mailgun.rb
@@ -0,0 +1,84 @@
#! /usr/bin/env ruby
# encoding: UTF-8
# mailer-mailgun.rb
#
# DESCRIPTION:
# This handler formats alerts as mail and sends them off to a pre-defined recipient
# using the Rackspace Mailgun service (http://www.rackspace.com/mailgun).
#
# OUTPUT:
# Delivers email for events.
#
# PLATFORMS:
# All
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: mailgun-ruby
#
# USAGE:
# In mailer-mailgun.json, set the following values:
# * mail_from: The from address that will appear in the email
# * mail_to: The address of the recipent
# * mg_apikey: The apikey of the Mailgun account
# * mg_domain: The domain name that you have configured Mailgun to use
#
# LICENSE:
# Chris Powell powellchristoph@gmail.com
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
require 'mailgun'
require 'timeout'

class Mailer < Sensu::Handler
def short_name
@event['client']['name'] + '/' + @event['check']['name']
end

def action_to_string
@event['action'].eql?('resolve') ? 'RESOLVED' : 'ALERT'
end

def handle
params = {
mail_to: settings['mailer-mailgun']['mail_to'],
mail_from: settings['mailer-mailgun']['mail_from'],
mg_apikey: settings['mailer-mailgun']['mg_apikey'],
mg_domain: settings['mailer-mailgun']['mg_domain']
}

body = <<-BODY.gsub(/^ {14}/, '')
#{@event['check']['output']}
Host: #{@event['client']['name']}
Timestamp: #{Time.at(@event['check']['issued'])}
Address: #{@event['client']['address']}
Check Name: #{@event['check']['name']}
Command: #{@event['check']['command']}
Status: #{@event['check']['status']}
Occurrences: #{@event['occurrences']}
BODY
subject = "#{action_to_string} - #{short_name}: #{@event['check']['notification']}"

mg_client = Mailgun::Client.new params[:mg_apikey]

begin
timeout 10 do
message_params = {
from: params[:mail_from],
to: params[:mail_to],
subject: subject,
text: body
}

mg_client.send_message params[:mg_domain], message_params

puts 'mail -- sent alert for ' + short_name + ' to ' + params[:mail_to]
end
rescue Timeout::Error
puts 'mail -- timed out while attempting to ' + @event['action'] + ' an incident -- ' + short_name
end
end
end

0 comments on commit c41e69f

Please sign in to comment.