1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.inputtools.hdfsusage;
20
21 import java.net.URI;
22
23 import org.apache.hadoop.chukwa.inputtools.jplugin.ChukwaMetricsList;
24 import org.apache.hadoop.chukwa.inputtools.jplugin.JPlugin;
25 import org.apache.hadoop.chukwa.inputtools.jplugin.JPluginStatusMetricsList;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FileStatus;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30
31 public class HDFSUsagePlugin implements JPlugin<HDFSUsageMetrics> {
32 private FileSystem hdfs;
33 private String path;
34 private OrgChart chart;
35
36 @Override
37 public ChukwaMetricsList<HDFSUsageMetrics> getMetrics() throws Throwable {
38 ChukwaMetricsList<HDFSUsageMetrics> metricsList = new ChukwaMetricsList<HDFSUsageMetrics>(getRecordType());
39 FileStatus status[] = hdfs.globStatus(new Path(path));
40 for(int i=0; i<status.length; i++) {
41 long totalSize = hdfs.getContentSummary(status[i].getPath()).getLength();
42 if(totalSize <= 0) {
43 continue;
44 }
45 String name = status[i].getPath().getName();
46 HDFSUsageMetrics usage = new HDFSUsageMetrics();
47 usage.setName(name);
48 usage.setSize(totalSize);
49 usage.setLastModified(status[i].getModificationTime());
50 metricsList.addMetrics(usage);
51
52
53 if(chart != null) {
54 Employee employee = chart.get(name);
55 if(employee != null) {
56 employee = employee.getManager();
57 while(employee != null) {
58 HDFSUsageMetrics managerUsage = new HDFSUsageMetrics();
59 managerUsage.setName(employee.getId());
60 managerUsage.setSize(totalSize);
61 metricsList.addMetrics(managerUsage);
62 employee = employee.getManager();
63 }
64 }
65 }
66 }
67 return metricsList;
68 }
69
70 @Override
71 public void init(String[] args) throws Throwable {
72 for(int i=0; i<args.length; i++) {
73 if(args[i].equals("-c")) {
74 String orgChartClass = args[i+1];
75 chart = (OrgChart) Class.forName(orgChartClass).newInstance();
76 i++;
77 } else if(args[i].equals("-h")) {
78 Configuration conf = new Configuration();
79 hdfs = FileSystem.get(new URI(args[i+1]), conf);
80 i++;
81 } else if(args[i].equals("-p")) {
82 path = args[i+1];
83 i++;
84 }
85 }
86
87 if(hdfs == null) {
88 Configuration conf = new Configuration();
89 hdfs = FileSystem.get(conf);
90 }
91
92 if(path == null) {
93 path = "/user/*";
94 }
95 }
96
97 @Override
98 public JPluginStatusMetricsList getStatus() throws Throwable {
99 JPluginStatusMetricsList list = new JPluginStatusMetricsList(this.getClass().getSimpleName());
100 list.addStatus("hdfs", hdfs.getUri().toString());
101 list.addStatus("path", path);
102 return null;
103 }
104
105 @Override
106 public String getRecordType() {
107 return "HDFSUsage";
108 }
109 }