This project has retired. For details please refer to its Attic page.
SystemMetrics 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  /**
20   * Demux parser for system metrics data collected through
21   * org.apache.hadoop.chukwa.datacollection.adaptor.sigar.SystemMetrics.
22   */
23  package org.apache.hadoop.chukwa.extraction.hbase;
24  
25  import java.nio.charset.Charset;
26  import java.security.NoSuchAlgorithmException;
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.Map.Entry;
30  import java.util.Set;
31  
32  import org.apache.hadoop.chukwa.extraction.engine.ChukwaRecord;
33  import org.json.simple.JSONArray;
34  import org.json.simple.JSONObject;
35  import org.json.simple.JSONValue;
36  
37  public class SystemMetrics extends AbstractProcessor {
38  
39    public SystemMetrics() throws NoSuchAlgorithmException {
40      super();
41    }
42  
43    @Override
44    protected void parse(byte[] recordEntry) throws Throwable {
45      String buffer = new String(recordEntry, Charset.forName("UTF-8"));
46      JSONObject json = (JSONObject) JSONValue.parse(buffer);
47      time = ((Long) json.get("timestamp")).longValue();
48      ChukwaRecord record = new ChukwaRecord();
49      JSONArray cpuList = (JSONArray) json.get("cpu");
50      double combined = 0.0;
51      double user = 0.0;
52      double sys = 0.0;
53      double idle = 0.0;
54      int actualSize = 0;
55      for (int i = 0; i < cpuList.size(); i++) {
56        JSONObject cpu = (JSONObject) cpuList.get(i);
57        // Work around for sigar returning null sometimes for cpu metrics on
58        // pLinux
59        if (cpu.get("combined") == null) {
60          continue;
61        }
62        actualSize++;
63        combined = combined + Double.parseDouble(cpu.get("combined").toString());
64        user = user + Double.parseDouble(cpu.get("user").toString());
65        sys = sys + Double.parseDouble(cpu.get("sys").toString());
66        idle = idle + Double.parseDouble(cpu.get("idle").toString());
67        for(Entry<String, Object> entry : (Set<Map.Entry>) cpu.entrySet()) {
68          String key = entry.getKey();
69          addRecord("cpu." + key + "." + i, String.valueOf(entry.getValue()));
70        }
71      }
72      combined = combined / actualSize;
73      user = user / actualSize;
74      sys = sys / actualSize;
75      idle = idle / actualSize;
76      addRecord("cpu.combined", Double.toString(combined));
77      addRecord("cpu.user", Double.toString(user));
78      addRecord("cpu.idle", Double.toString(idle));
79      addRecord("cpu.sys", Double.toString(sys));
80  
81      addRecord("Uptime", json.get("uptime").toString());
82      JSONArray loadavg = (JSONArray) json.get("loadavg");
83      addRecord("LoadAverage.1", loadavg.get(0).toString());
84      addRecord("LoadAverage.5", loadavg.get(1).toString());
85      addRecord("LoadAverage.15", loadavg.get(2).toString());
86  
87      record = new ChukwaRecord();
88      JSONObject memory = (JSONObject) json.get("memory");
89      for(Entry<String, Object> entry : (Set<Map.Entry>) memory.entrySet()) {
90        String key = entry.getKey();
91        addRecord("memory." + key, String.valueOf(entry.getValue()));
92      }
93  
94      record = new ChukwaRecord();
95      JSONObject swap = (JSONObject) json.get("swap");
96      for(Map.Entry<String, Object> entry : (Set<Map.Entry>) swap.entrySet()) {
97        addRecord("swap." + entry.getKey(), String.valueOf(entry.getValue()));
98      }
99  
100     double rxBytes = 0;
101     double rxDropped = 0;
102     double rxErrors = 0;
103     double rxPackets = 0;
104     double txBytes = 0;
105     double txCollisions = 0;
106     double txErrors = 0;
107     double txPackets = 0;
108     record = new ChukwaRecord();
109     JSONArray netList = (JSONArray) json.get("network");
110     for (int i = 0; i < netList.size(); i++) {
111       JSONObject netIf = (JSONObject) netList.get(i);
112       for(Map.Entry<String, Object> entry : (Set<Map.Entry>) netIf.entrySet()) {
113         String key = entry.getKey();
114         long value = 0;
115         if(entry.getValue() instanceof Long) {
116           value = (Long) entry.getValue();
117         }
118         record.add(key + "." + i, String.valueOf(entry.getValue()));
119         if (i != 0) {
120           if (key.equals("RxBytes")) {
121             rxBytes = rxBytes + value;
122           } else if (key.equals("RxDropped")) {
123             rxDropped = rxDropped + value;
124           } else if (key.equals("RxErrors")) {
125             rxErrors = rxErrors + value;
126           } else if (key.equals("RxPackets")) {
127             rxPackets = rxPackets + value;
128           } else if (key.equals("TxBytes")) {
129             txBytes = txBytes + value;
130           } else if (key.equals("TxCollisions")) {
131             txCollisions = txCollisions + value;
132           } else if (key.equals("TxErrors")) {
133             txErrors = txErrors + value;
134           } else if (key.equals("TxPackets")) {
135             txPackets = txPackets + value;
136           }
137         }
138       }
139     }
140 
141     addRecord("network.RxBytes", Double.toString(rxBytes));
142     addRecord("network.RxDropped", Double.toString(rxDropped));
143     addRecord("network.RxErrors", Double.toString(rxErrors));
144     addRecord("network.RxPackets", Double.toString(rxPackets));
145     addRecord("network.TxBytes", Double.toString(txBytes));
146     addRecord("network.TxCollisions", Double.toString(txCollisions));
147     addRecord("network.TxErrors", Double.toString(txErrors));
148     addRecord("network.TxPackets", Double.toString(txPackets));
149 
150     double readBytes = 0;
151     double reads = 0;
152     double writeBytes = 0;
153     double writes = 0;
154     double total = 0;
155     double used = 0;
156     record = new ChukwaRecord();
157     JSONArray diskList = (JSONArray) json.get("disk");
158     for (int i = 0; i < diskList.size(); i++) {
159       JSONObject disk = (JSONObject) diskList.get(i);
160       for(Entry<String, Object> entry : (Set<Map.Entry>) disk.entrySet()) {
161         String key = entry.getKey();
162         long value = 0;
163         if(entry.getValue() instanceof Long) {
164           value = (Long) entry.getValue();
165         }
166         record.add(key + "." + i, String.valueOf(entry.getValue()));
167         if (key.equals("ReadBytes")) {
168           readBytes = readBytes + value;
169         } else if (key.equals("Reads")) {
170           reads = reads + Long.valueOf(value);;
171         } else if (key.equals("WriteBytes")) {
172           writeBytes = writeBytes + value;
173         } else if (key.equals("Writes")) {
174           writes = writes + value;
175         } else if (key.equals("Total")) {
176           total = total + value;
177         } else if (key.equals("Used")) {
178           used = used + value;
179         }
180       }
181     }
182     double percentUsed = used / total;
183     addRecord("disk.ReadBytes", Double.toString(readBytes));
184     addRecord("disk.Reads", Double.toString(reads));
185     addRecord("disk.WriteBytes", Double.toString(writeBytes));
186     addRecord("disk.Writes", Double.toString(writes));
187     addRecord("disk.Total", Double.toString(total));
188     addRecord("disk.Used", Double.toString(used));
189     addRecord("disk.PercentUsed", Double.toString(percentUsed));
190   }
191 
192 }