-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwilio.rb
68 lines (60 loc) · 1.47 KB
/
twilio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'net/http'
require 'uri'
require 'optparse'
require 'timeout'
def parse_args(args)
options = {}
optparse = OptionParser.new do |opts|
opts.banner = 'Usage: %s -i <store_id> -t <minutes>' % [$0]
options[:service] = 5
opts.on('-s', '--s [string]', :text, 'Twilio Service.') do |x|
options[:service] = x
end
end
optparse.parse!(args)
options
end
# Output one-liner and manage the exit code explicitly.
def do_exit (v, code, msg)
puts msg unless msg == nil
if v == true
exit 3
else
exit code
end
end
def sanity_check(options)
# In life, some arguments cannot be avoided.
error_msg = []
if not (options[:service]) then
error_msg.push('Must specify twilio service.')
end
if error_msg.length > 0 then
# First line is Nagios-friendly.
puts 'UNKNOWN: Insufficient or incompatible arguments.'
# Subsequent lines are for humans.
error_msg.each do |msg|
puts msg
end
msg = '"%s ask ray for for info"' % [$0]
do_exit(true, 3, msg)
end
end
options = parse_args(ARGV)
sanity_check(options)
string=`curl http://status.twilio.com/api/v1/services/#{options[:service]}/events`
value = JSON.parse(string)['events'][0]['status']
case value["level"]
when "NORMAL"
puts "OK - #{value['description']}"
exit 0
when "WARNING"
puts "WARNING - #{value['description']}"
exit 1
else
puts "CRITICAL - #{value['description']}"
exit 2
end