Skip to content

Commit

Permalink
- Modified nagios script to alert under a threshold of remaining SMS …
Browse files Browse the repository at this point in the history
…& add a script to only check remaining credits (for cron usage)

Signed-off-by: Nicolas Szalay <nicolas.szalay@fotolia.com>
  • Loading branch information
Nicolas Szalay committed Dec 17, 2010
1 parent 2a46532 commit a63d8bf
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README
@@ -0,0 +1,4 @@
Small utility to send Nagios alerts on a mobile phone using OVH SMS API

nagios_notify_sms_ovh.rb => sends SMS
single_checker.rb => only checks for remaining credit
2 changes: 2 additions & 0 deletions conf.sample.yml
Expand Up @@ -17,3 +17,5 @@ errorMail:
server: localhost
to: errorMail@localhost
from: errorMail@localhost
credit:
threshold: 500
13 changes: 11 additions & 2 deletions nagios_notify_sms_ovh.rb
Expand Up @@ -38,7 +38,7 @@ def initialize(*args)
options[:config] = config
end

opts.on('-m', '--mode=MODE', [:host, :service], 'Select alert object type (server, host)') do |mode|
opts.on('-m', '--mode=MODE', [:host, :service], 'Select alert object type (server, host, credit)') do |mode|
options[:mode] = mode
end

Expand Down Expand Up @@ -132,7 +132,6 @@ class MissingArg < Exception
message = "#{type} #{state} #{hostname}/#{service}@#{time.hour}:#{time.min} #{details}"

# Send the SMS through the OVH API

begin
wsdl = 'https://www.ovh.com/soapi/soapi-re-1.9.wsdl'
soapi = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
Expand All @@ -141,6 +140,7 @@ class MissingArg < Exception
unless options[:dont_send_sms]
result = soapi.telephonySmsSend(session, config['ovhManager']['smsAccount'], config['ovhManager']['fromNumber'], phone_number, message, nil, nil, nil, nil)
end

rescue Exception => e
puts "Error : #{e}"
msg = <<END_OF_MESSAGE
Expand All @@ -155,5 +155,14 @@ class MissingArg < Exception
exit 1
end

# then check the number of SMS left
smsleft = soapi.telephonySmsCreditLeft(session, config['ovhManager']['smsAccount']).to_i
if smsleft < config['credit']['threshold'] then
msg = "only #{smsleft} credits left for the nagios SMS alert !"
Net::SMTP.start(config['errorMail']['server']) do |smtp|
smtp.send_message(msg, config['errorMail']['from'], config['errorMail']['to'])
end
end

# Logout
soapi.logout(session)
79 changes: 79 additions & 0 deletions single_checker.rb
@@ -0,0 +1,79 @@
#!/usr/bin/env ruby

require 'yaml'
require 'soap/wsdlDriver'
require 'optparse'
require 'optparse/time'
require 'net/smtp'

# Get rid of the SSL errors
class Net::HTTP
alias_method :old_initialize, :initialize
def initialize(*args)
old_initialize(*args)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end

# Parse the options
options = Hash.new
options[:config] = nil
options[:dont_send_sms] = false

opts = OptionParser.new do |opts|
opts.banner = 'Usage: nagios_notify_sms_ovh.rb [options]'
opts.separator ''
opts.separator 'Options :'

opts.on('-c', '--config=PATH', 'Path to the config file') do |config|
options[:config] = config
end

opts.on('--dont-send-sms', 'Dont send the SMS') do |send|
options[:dont_send_sms] = true
end

opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end

begin
opts.parse!(ARGV)
rescue OptionParser::ParseError => e
abort e.message+"\nTry --help for a list of options"
end

# Check if all arguments are here
class MissingArg < Exception
end
begin
raise MissingArg, 'You must specify a config file' if options[:config] == nil

rescue MissingArg => e
abort 'Error : '+e.message+"\nTry --help for a list of options"
end

# Load the config file
config = YAML.load_file(options[:config])

begin
wsdl = 'https://www.ovh.com/soapi/soapi-re-1.9.wsdl'
soapi = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver

session = soapi.login(config['ovhManager']['nicHandle'], config['ovhManager']['password'], 'en', false)

#check the number of SMS left
smsleft = soapi.telephonySmsCreditLeft(session, config['ovhManager']['smsAccount']).to_i
if smsleft < config['credit']['threshold'] then
msg = "only #{smsleft} credits left for the nagios SMS alert !"
puts msg
Net::SMTP.start(config['errorMail']['server']) do |smtp|
smtp.send_message(msg, config['errorMail']['from'], config['errorMail']['to'])
end
end
end
# Logout
soapi.logout(session)

0 comments on commit a63d8bf

Please sign in to comment.