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
|
# File 'lib/cawcaw/command/hadoop/dfs.rb', line 4
def self.run(argv, input_stream=$stdin, output_stream=$stdout)
params = Cawcaw.parse_opts(argv)
params[:graph_title] ||= "HDFS size"
params[:graph_args] ||= "--base 1000"
params[:graph_vlabel] ||= "bytes"
params[:graph_category] ||= "hadoop"
params[:graph_info] ||= "HDFS size"
params[:label_warning] ||= 5000000
params[:label_critical] ||= 5000000
if params[:stdin_flag]
hdfs_paths = []
while line = input_stream.gets
line.chomp!
hdfs_paths.push line
end
else
hdfs_paths = argv.shift
hdfs_paths = hdfs_paths.split(/,/) unless hdfs_paths.nil?
hdfs_paths ||= []
hdfs_paths.delete("")
end
command = argv.shift
if hdfs_paths == []
Cawcaw.logger.error("hdfs-path is not set")
return 1
end
absolute_hdfs_paths = {}
hdfs_paths.each do |hdfs_path|
if /^\// =~ hdfs_path
absolute_hdfs_paths[hdfs_path] = hdfs_path
else
absolute_hdfs_paths[hdfs_path] = "#{params[:hadoop_working_directory]}/#{hdfs_path}"
end
end
case command
when "autoconf"
output_stream.puts "yes"
when "config"
output_stream.puts "graph_title \#{params[:graph_title]}\ngraph_args \#{params[:graph_args]}\ngraph_vlabel \#{params[:graph_vlabel]}\ngraph_category \#{params[:graph_category]}\ngraph_info \#{params[:graph_info]}\n EOF\n \n label_draw = \"AREA\"\n \n hdfs_paths.each do |hdfs_path|\n label = hdfs_path.gsub(/[^a-zA-Z0-9]/, \"_\").downcase\n output_stream.puts <<-EOF\n\#{label}.label \#{hdfs_path}\n\#{label}.info \#{hdfs_path} size\n\#{label}.draw \#{label_draw}\n\#{label}.warning \#{params[:label_warning]}\n\#{label}.critical \#{params[:label_critical]}\n EOF\n label_draw = \"STACK\"\n end\n else\n result = `\#{params[:hadoop_command]} dfs -dus \#{hdfs_paths.map{|hdfs_path|absolute_hdfs_paths[hdfs_path]}.join(\" \")}`\n # STDERR.puts result\n hdfs_sizes = {}\n result.split(/\\r?\\n/).each do |line|\n record = line.split(/\\s+/)\n hdfs_uri = record[0]\n hdfs_uri = URI.parse(hdfs_uri)\n hdfs_sizes[hdfs_uri.path] = record[1]\n end\n # STDERR.puts hdfs_sizes.inspect\n \n hdfs_paths.each do |hdfs_path|\n hdfs_size = hdfs_sizes[absolute_hdfs_paths[hdfs_path]]\n label = hdfs_path.gsub(/[^a-zA-Z0-9]/, \"_\").downcase\n output_stream.puts \"\#{label}.value \#{hdfs_size}\"\n end\n end\n \n return 0\nend\n"
|