View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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        // also contribute to manager's usage
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 }