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 = 60 * 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 = Integer.parseInt(args.substring(0, spOffset));
54          period = period * 1000;
55        } catch (NumberFormatException e) {
56          StringBuilder buffer = new StringBuilder();
57          buffer.append("SystemMetrics: sample interval ");
58          buffer.append(args.substring(0, spOffset));
59          buffer.append(" can't be parsed.");
60          log.warn(buffer.toString());
61        }
62      }    
63      return args;
64    }
65  
66    @Override
67    public void start(long offset) throws AdaptorException {
68      if(timer == null) {
69        timer = new Timer();
70        runner = new SigarRunner(dest, SystemMetrics.this);
71      }
72      timer.scheduleAtFixedRate(runner, 0, period);
73      
74    }
75  
76    @Override
77    public String getCurrentStatus() {
78      StringBuilder buffer = new StringBuilder();
79      buffer.append(type);
80      buffer.append(" ");
81      buffer.append(period/1000);
82      return buffer.toString();
83    }
84  
85    @Override
86    public long shutdown(AdaptorShutdownPolicy shutdownPolicy)
87        throws AdaptorException {
88      timer.cancel();
89      return 0;
90    }
91  
92  }