1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819/**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 */33package org.apache.hadoop.chukwa.datacollection.adaptor.sigar;
3435import java.util.Timer;
3637import org.apache.hadoop.chukwa.datacollection.adaptor.AbstractAdaptor;
38import org.apache.hadoop.chukwa.datacollection.adaptor.AdaptorException;
39import org.apache.hadoop.chukwa.datacollection.adaptor.AdaptorShutdownPolicy;
40import org.apache.log4j.Logger;
4142publicclassSystemMetricsextendsAbstractAdaptor {
43static Logger log = Logger.getLogger(SystemMetrics.class);
44privatelong period = 5 * 1000;
45privateSigarRunner runner;
46private Timer timer;
4748 @Override
49public String parseArgs(String args) {
50int spOffset = args.indexOf(' ');
51if (spOffset > 0) {
52try {
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 }
66return args;
67 }
6869 @Override
70publicvoid start(long offset) throws AdaptorException {
71if(timer == null) {
72 timer = new Timer();
73 runner = newSigarRunner(dest, SystemMetrics.this);
74 }
75 timer.scheduleAtFixedRate(runner, 0, period);
7677 }
7879 @Override
80public String getCurrentStatus() {
81 StringBuilder buffer = new StringBuilder();
82 buffer.append(type);
83 buffer.append(" ");
84 buffer.append(period/1000);
85return buffer.toString();
86 }
8788 @Override
89publiclong shutdown(AdaptorShutdownPolicy shutdownPolicy)
90throwsAdaptorException {
91 timer.cancel();
92return 0;
93 }
9495 }