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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/check-disk-io_gem.rb', line 86
def run
disks = Diskstats.from_proc.reject { |s| s.device_name =~ /^sr0|fd0|ram|loop|dm-/ }.map(&:device_name)
counters = Hash[disks.each_with_object({}).to_a]
for i in 0..1 do
disks.each do |disk|
new_hash = {
disk => {
i => {
timestamp: Time.new.to_i,
writes_success: Diskstats.from_proc.select { |s| s.device_name == disk }.map(&:writes_success)
}
}
}
counters[disk] = counters[disk].merge(new_hash[disk])
sleep 3
end
end
alerts = []
counters.each do |counter|
counter[1].keys.sort.each_with_index do |key, index|
if index == counter[1].length - 1
last_writes_success = counter[1][key][:writes_success]
first_writes_success = counter[1][0][:writes_success]
result = last_writes_success.join.to_i - first_writes_success.join.to_i
device = counter[0]
if result >= config[:critical].to_i && result > config[:warning].to_i
alerts << [device, 'critical', result]
end
if result >= config[:warning].to_i && result < config[:critical].to_i
alerts << [device, 'warning', result]
end
end
end
end
if alerts.empty?
message 'Everything is ok'
ok
else
status = ''
messages = ''
alerts.each do |alert|
messages << "\r\n/dev/#{alert[0]} has reached #{alert[2]} IOPS"
if alert[1] == 'critical' && status.empty?
status = 'critical'
end
end
message messages
critical if status == 'critical'
warning if status != 'critical'
end
rescue => err
puts "Exception: #{err}"
err
end
|