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   * Adaptor that is able to collect system metrics by using Hyperic Sigar.
21   * <P>
22   * This adaptor is added to an Agent like so:
23   * <code>
24   * add SystemMetrics [dataType] [seconds]
25   * </code>
26   * <ul>
27   * <li><code>dataType</code> - The chukwa data type, use SystemMetrics to map to 
28   * default SystemMetrics demux parser.</li>
29   * <li><code>seconds</code> - Interval to collect system metrics, default is 60 seconds.</li>
30   * </ul>
31   * </P>
32   */
33  package org.apache.hadoop.chukwa.datacollection.adaptor.sigar;
34  
35  import java.util.Timer;
36  
37  import org.apache.hadoop.chukwa.datacollection.adaptor.AbstractAdaptor;
38  import org.apache.hadoop.chukwa.datacollection.adaptor.AdaptorException;
39  import org.apache.hadoop.chukwa.datacollection.adaptor.AdaptorShutdownPolicy;
40  import org.apache.log4j.Logger;
41  
42  public class SystemMetrics extends AbstractAdaptor {
43    static Logger log = Logger.getLogger(SystemMetrics.class);
44    private long period = 5 * 1000;
45    private SigarRunner runner;
46    private Timer timer;
47    
48    @Override
49    public String parseArgs(String args) {
50      int spOffset = args.indexOf(' ');
51      if (spOffset > 0) {
52        try {
53          period = Long.parseLong(args.substring(0, spOffset));
54          period = period * 1000;
55          start(spOffset);
56        } catch (NumberFormatException e) {
57          StringBuilder buffer = new StringBuilder();
58          buffer.append("SystemMetrics: sample interval ");
59          buffer.append(args.substring(0, spOffset));
60          buffer.append(" can't be parsed.");
61          log.warn(buffer.toString());
62        } catch (AdaptorException e) {
63          log.warn("Error parsing parameter for SystemMetrics adaptor.");
64        }
65      }    
66      return args;
67    }
68  
69    @Override
70    public void start(long offset) throws AdaptorException {
71      if(timer == null) {
72        timer = new Timer();
73        runner = new SigarRunner(dest, SystemMetrics.this);
74      }
75      timer.scheduleAtFixedRate(runner, 0, period);
76      
77    }
78  
79    @Override
80    public String getCurrentStatus() {
81      StringBuilder buffer = new StringBuilder();
82      buffer.append(type);
83      buffer.append(" ");
84      buffer.append(period/1000);
85      return buffer.toString();
86    }
87  
88    @Override
89    public long shutdown(AdaptorShutdownPolicy shutdownPolicy)
90        throws AdaptorException {
91      timer.cancel();
92      return 0;
93    }
94  
95  }