forked from obfuscurity/nagios-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_graphite_multi
More file actions
executable file
·134 lines (114 loc) · 3.38 KB
/
Copy pathcheck_graphite_multi
File metadata and controls
executable file
·134 lines (114 loc) · 3.38 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env ruby
require "rubygems"
require "optparse"
require "rest-client"
require "json"
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3
@@options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options]"
@@options[:url] = nil
opts.on("-u", "--url URL", "Target url") do |url|
@@options[:url] = url
end
@@options[:metric] = nil
opts.on("-m", "--metric NAME[|NAME2|NAME3|...]", String, "Metric path string (multiples may be quoted and separated by | (not , as that would break graphite functions)") do |metric|
@@options[:metric] = metric.split("|")
end
@@options[:shortname] = nil
opts.on("-s", "--shortname SHORTNAME[,SHORTNAME2,SHORTNAME3...]", Array, "Metric short name used for performance data (accepts array)") do |shortname|
@@options[:shortname] = shortname
end
@@options[:duration] = 5
opts.on("-d", "--duration LENGTH", "Length, in minute of data to parse (default: 5)") do |duration|
@@options[:duration] = duration
end
@@options[:warning] = nil
opts.on("-w", "--warning VALUE[,VALUE2,VALUE3...]", Array, "Warning threshold") do |warning|
@@options[:warning] = warning
end
@@options[:critical] = nil
opts.on("-c", "--critical VALUE[,VALUE2,VALUE3...]", Array, "Critical threshold") do |critical|
@@options[:critical] = critical
end
opts.on( "-h", "--help", "Display this screen" ) do
puts opts
exit
end
end
optparse.parse!
if (@@options[:url].nil? || @@options[:metric].nil? || @@options[:warning].nil? || @@options[:critical].nil?)
puts optparse
exit 2
end
def url(m)
base_url = @@options[:url]
duration = @@options[:duration].to_s
base_url + "/render/?target=" + m + "&format=json&from=-" + duration + "mins"
end
status = Array.new
message = ""
perfdata = "|"
@@options[:metric].each do |m|
data = {}
data["total"] = 0
id = @@options[:metric].index(m)
shortname = @@options[:shortname][id]
warning = @@options[:warning][id].to_i
critical = @@options[:critical][id].to_i
shortname = m if @@options[:shortname][id].nil?
warning = @@options[:warning][0].to_i if @@options[:warning][id].nil?
critical = @@options[:critical][0].to_i if @@options[:critical][id].nil?
JSON.parse(RestClient.get(url(m))).each do |cache|
data["#{cache['target']}"] = 0
count = 0
cache["datapoints"].each do |point|
unless (point[0].nil?)
data["#{cache['target']}"] += point[0]
count += 1
end
end
if (count == 0)
count = 1
end
data["#{cache['target']}"] /= count
data["total"] += data["#{cache['target']}"]
end
total = data["total"].to_i
perfdata += " #{shortname}=#{total};#{warning};#{critical};"
if (message == "" )
message += "#{shortname}: #{total}"
else
message += ", #{shortname}: #{total}"
end
if (critical > warning)
if (total >= critical)
status << "CRITICAL"
elsif (total >= warning)
status << "WARNING"
else
status << "OK"
end
else
if (total <= critical)
status << "CRITICAL"
elsif (total <= warning)
status << "WARNING"
else
status << "OK"
end
end
end
if (!status.grep(/CRITICAL/).empty?)
puts "CRITICAL - #{message} #{perfdata}"
exit EXIT_CRITICAL
elsif (!status.grep(/WARNING/).empty?)
puts "WARNING - #{message} #{perfdata}"
exit EXIT_WARNING
else
puts "OK - #{message} #{perfdata}"
exit EXIT_OK
end