-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_hdfs_stat.rb
165 lines (157 loc) · 5.05 KB
/
check_hdfs_stat.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/ruby
require 'optparse'
hdfs_path="/opt/hadoop/bin/hdfs"
options = {}
option_parser = OptionParser.new do |opts|
# 这里是这个命令行工具的帮助信息
opts.banner = 'usage:check_hdfs_stat.rb [options] -w warnning -c critical,-h for help'
#
# # Option 作为switch,不带argument,用于将 switch 设置成 true 或 false
options[:switch] = false
# # 下面第一项是 Short option(没有可以直接在引号间留空),第二项是 Long option,第三项是对 Option 的描述
opts.on('-d', 'check hdfs disk urate') do
# # 这个部分就是使用这个Option后执行的代码
options['-d'] = true
end
opts.on('-u', 'check Under replicated blocks num') do
# # 这个部分就是使用这个Option后执行的代码
options['-u'] = true
end
opts.on('-r', 'check Blocks with corrupt replicas num') do
# # 这个部分就是使用这个Option后执行的代码
options['-r'] = true
end
opts.on('-m', 'check Missing blocks num') do
# # 这个部分就是使用这个Option后执行的代码
options['-m'] = true
end
opts.on('-l', 'check Live datanodes num') do
# # 这个部分就是使用这个Option后执行的代码
options['-l'] = true
end
#
# # Option 作为 flag,带argument,用于将argument作为数值解析,比如"name"信息
# #下面的“value”就是用户使用时输入的argument
opts.on('-w warn', '--warnning warn', 'Pass in warnning value') do |value|
options['warn'] = value
end
opts.on('-c crit', '--critical crit', 'Pass in critical value') do |value|
options['crit'] = value
end
#
# # Option 作为 flag,带一组用逗号分割的arguments,用于将arguments作为数组解析# opts.on('-a A,B', '--array A,B', Array, 'List of arguments') do |value|
# options[:array] = value
# end
end.parse!
#puts options
if !options['warn']
warn=80
else
warn=options['warn'].to_f
end
if !options['crit']
crti=90
else
crti=options['crit'].to_f
end
#puts warn
#puts crti
info=`#{hdfs_path} dfsadmin -report`
def check_disk(info,warn,crti)
if warn>crti
puts "critical > warnning must"
exit 255
end
use_info=info.match(/DFS Used%:.*$/).to_s.split(/:/)[1].gsub(/ /,"").to_f
if use_info>=crti
puts "critical:DFS Used:#{use_info}%"
exit 1
elsif use_info>=warn and use_info<crti
puts "warnning:DFS Used:#{use_info}%"
exit 2
else
puts "OK:DFS Used:#{use_info}%"
exit 0
end
end
def check_Under_replicated_blocks(info,warn,crti)
if warn>crti
puts "critical > warnning must"
exit 255
end
urb_info=info.match(/Under replicated blocks:.*$/).to_s.split(/:/)[1].gsub(/ /,"").to_i
if urb_info>=crti
puts "critical:Under replicated blocks:#{urb_info}"
exit 1
elsif urb_info>=warn and urb_info<crti
puts "warnning:Under replicated blocks:#{urb_info}"
exit 2
else
puts "OK:Under replicated blocks:#{urb_info}"
exit 0
end
end
def check_corrupt_replicas_blocks(info,warn,crti)
if warn>crti
puts "critical > warnning must"
exit 255
end
crb_info=info.match(/Blocks with corrupt replicas:.*$/).to_s.split(/:/)[1].gsub(/ /,"").to_i
if use_info>=crti
puts "critical:Blocks with corrupt replicas:#{crb_info}"
exit 1
elsif crb_info>=warn and crb_info<crti
puts "warnning:Blocks with corrupt replicas:#{crb_info}"
exit 2
else
puts "OK:Blocks with corrupt replicas:#{crb_info}"
exit 0
end
end
def check_Missing_blocks(info,warn,crti)
if warn>crti
puts "critical > warnning must"
exit 255
end
mb_info=info.match(/Missing blocks:.*$/).to_s.split(/:/)[1].gsub(/ /,"").to_i
if mb_info>=crti
puts "critical:Missing blocks:#{mb_info}"
exit 1
elsif mb_info>=warn and mb_info<crti
puts "warnning:Missing blocks:#{mb_info}"
exit 2
else
puts "OK:Missing blocks:#{mb_info}"
exit 0
end
end
def check_Live_datanodes(info,warn,crti)
if crti>warn
puts "critical < warnning must"
exit 255
end
ldn_info=info.match(/Live datanodes \(\d+\):/).to_s.match(/\d+/).to_s.to_i
if ldn_info<=crti
puts "critical:Live datanodes:#{ldn_info}"
exit 1
elsif ldn_info<=warn and ldn_info>crti
puts "warnning:Live datanodes:#{ldn_info}"
exit 2
else
puts "OK:Live datanodes:#{ldn_info}"
exit 0
end
end
if options['-l']
check_Live_datanodes(info,warn,crti)
elsif options['-d']
check_disk(info,warn,crti)
elsif options['-m']
check_Missing_blocks(info,warn,crti)
elsif options['-r']
check_corrupt_replicas_blocks(info,warn,crti)
elsif options['-u']
check_Under_replicated_blocks(info,warn,crti)
else
puts 'usage:check_hdfs_stat.rb [options] -w warnning -c critical,-h for help'
end