This project has retired. For details please refer to its Attic page.
NodeActivityPlugin xref
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.plugin.nodeactivity;
20  
21  
22  import org.apache.hadoop.chukwa.inputtools.mdl.DataConfig;
23  import org.apache.hadoop.chukwa.inputtools.plugin.ExecPlugin;
24  import org.apache.hadoop.chukwa.inputtools.plugin.IPlugin;
25  import org.json.simple.JSONObject;
26  
27  public class NodeActivityPlugin extends ExecPlugin {
28    private String cmde = null;
29    private DataConfig dataConfig = null;
30  
31    public NodeActivityPlugin() {
32      dataConfig = new DataConfig();
33      cmde = dataConfig.get("mdl.plugin.NodeActivityPlugin.cmde");
34    }
35  
36    @Override
37    public String getCmde() {
38      return cmde;
39    }
40  
41    @Override
42    public JSONObject postProcess(JSONObject execResult) {
43      try {
44        if ( (Integer) execResult.get("status") < 0) {
45          return execResult;
46        }
47  
48        String res = (String) execResult.get("stdout");
49  
50        String[] tab = res.split("\n");
51        int totalFreeNode = 0;
52        int totalUsedNode = 0;
53        int totalDownNode = 0;
54  
55        for (int i = 0; i < tab.length; i++) {
56          if (tab[i].indexOf("state =") < 0) {
57            tab[i] = null;
58            continue;
59          }
60  
61          String[] line = tab[i].split("state =");
62          tab[i] = null;
63  
64          if (line[1].trim().equals("free")) {
65            totalFreeNode++;
66          } else if (line[1].trim().equals("job-exclusive")) {
67            totalUsedNode++;
68          } else {
69            totalDownNode++;
70          }
71        }
72  
73        execResult.put("totalFreeNode", totalFreeNode);
74        execResult.put("totalUsedNode", totalUsedNode);
75        execResult.put("totalDownNode", totalDownNode);
76        execResult.put("source", "NodeActivity");
77  
78        execResult.put("status", 100);
79  
80      } catch (Throwable e) {
81        try {
82          execResult.put("source", "NodeActivity");
83          execResult.put("status", -100);
84          execResult.put("errorLog", e.getMessage());
85        } catch (Exception e1) {
86          e1.printStackTrace();
87        }
88        e.printStackTrace();
89  
90      }
91  
92      return execResult;
93    }
94  
95    public static void main(String[] args) {
96      IPlugin plugin = new NodeActivityPlugin();
97      JSONObject result = plugin.execute();
98      System.out.print("Result: " + result);
99  
100   }
101 
102 }