Skip to content

Instantly share code, notes, and snippets.

@patmandenver
Created Mar 4, 2015
Embed
What would you like to do?
A Sensu Plugin for checking the # of emails received in the inbox in X minutes, you can also filter on the sender's email
#!/usr/bin/ruby
#
#
#The MIT License (MIT)
#
#Copyright (c) 2015 Patrick Bailey
#
#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.
#
#
require 'sensu-plugin/check/cli'
require 'viewpoint'
include Viewpoint::EWS
class CountEmails < Sensu::Plugin::Check::CLI
option :minutes,
:description => "How many minutes prior to check emails",
:short => '-m MINUTES',
:long => '--minutes MINUTES',
:proc => proc {|a| a.to_i},
:default => 15,
:required => true
option :from_filter,
:description => "Only count emails received from this email",
:short => '-f FROM_FILTER',
:long => '--from FROM_FILTER',
:required => false
option :warn_limit,
:description => "Threshold, above which a warning is issued",
:short => '-w WARN_LIMIT',
:long => '--warn WARN_LIMIT',
:proc => proc {|a| a.to_i},
:required => false
option :alarm_limit,
:description => "Threshold, above which an alarm is issued",
:short => '-a ALARM',
:long => '--alarm ALARM',
:proc => proc {|a| a.to_i},
:required => false
def initilize
super
end
def run
endpoint = ' https://example.com/ews/Exchange.asmx'
user = 'username'
pass = 'password'
cli = Viewpoint::EWSClient.new endpoint, user, pass, server_version: SOAP::ExchangeWebService::VERSION_2010_SP1
folder = cli.get_folder_by_name 'inbox'
end_t = Time.now
start_t = end_t - config[:minutes]*60
emails = folder.items_between(start_t, end_t)
count = 0
filter_note = ""
#Filter if a from address is given
if !config[:from_filter].nil?
filter_note = " filtering on '" + config[:from_filter] + "'"
for email in emails
if email.sender.email == config[:from_filter]
count += 1
end
end
else
#No address to filter on count all email
count = emails.length
end
if !config[:alarm_limit].nil? && count >= config[:alarm_limit]
critical(message(count, config[:minutes], filter_note))
elsif !config[:warn_limit].nil? && count >= config[:warn_limit]
warning(message(count, config[:minutes], filter_note))
else
ok(message(count, config[:minutes], filter_note))
end
end
def message(count, minutes, filter_note)
count.to_s + " emails have been received within the last " + minutes.to_s + " minutes" + filter_note
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment