001/*
002 * GangliaContext.java
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package org.apache.hadoop.metrics.ganglia;
022
023import java.io.IOException;
024import java.net.DatagramPacket;
025import java.net.SocketAddress;
026import java.net.UnknownHostException;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.metrics.ContextFactory;
032import org.apache.hadoop.net.DNS;
033
034/**
035 * Context for sending metrics to Ganglia version 3.1.x.
036 * 
037 * 3.1.1 has a slightly different wire portal compared to 3.0.x.
038 */
039public class GangliaContext31 extends GangliaContext {
040
041  String hostName = "UNKNOWN.example.com";
042
043  private static final Log LOG = 
044    LogFactory.getLog("org.apache.hadoop.util.GangliaContext31");
045
046  public void init(String contextName, ContextFactory factory) {
047    super.init(contextName, factory);
048
049    LOG.debug("Initializing the GangliaContext31 for Ganglia 3.1 metrics.");
050
051    // Take the hostname from the DNS class.
052
053    Configuration conf = new Configuration();
054
055    if (conf.get("slave.host.name") != null) {
056      hostName = conf.get("slave.host.name");
057    } else {
058      try {
059        hostName = DNS.getDefaultHost(
060          conf.get("dfs.datanode.dns.interface","default"),
061          conf.get("dfs.datanode.dns.nameserver","default"));
062      } catch (UnknownHostException uhe) {
063        LOG.error(uhe);
064        hostName = "UNKNOWN.example.com";
065      }
066    }
067  }
068
069  protected void emitMetric(String name, String type,  String value) 
070    throws IOException
071  {
072    if (name == null) {
073      LOG.warn("Metric was emitted with no name.");
074      return;
075    } else if (value == null) {
076      LOG.warn("Metric name " + name +" was emitted with a null value.");
077      return;
078    } else if (type == null) {
079      LOG.warn("Metric name " + name + ", value " + value + " has no type.");
080      return;
081    }
082
083    LOG.debug("Emitting metric " + name + ", type " + type + ", value " + 
084      value + " from hostname" + hostName);
085
086    String units = getUnits(name);
087    int slope = getSlope(name);
088    int tmax = getTmax(name);
089    int dmax = getDmax(name);
090    offset = 0;
091    String groupName = name.substring(0,name.lastIndexOf("."));
092
093    // The following XDR recipe was done through a careful reading of
094    // gm_protocol.x in Ganglia 3.1 and carefully examining the output of
095    // the gmetric utility with strace.
096
097    // First we send out a metadata message
098    xdr_int(128);         // metric_id = metadata_msg
099    xdr_string(hostName); // hostname
100    xdr_string(name);     // metric name
101    xdr_int(0);           // spoof = False
102    xdr_string(type);     // metric type
103    xdr_string(name);     // metric name
104    xdr_string(units);    // units
105    xdr_int(slope);       // slope
106    xdr_int(tmax);        // tmax, the maximum time between metrics
107    xdr_int(dmax);        // dmax, the maximum data value
108
109    xdr_int(1);             /*Num of the entries in extra_value field for 
110                              Ganglia 3.1.x*/
111    xdr_string("GROUP");    /*Group attribute*/
112    xdr_string(groupName);  /*Group value*/
113
114    for (SocketAddress socketAddress : metricsServers) {
115      DatagramPacket packet =
116        new DatagramPacket(buffer, offset, socketAddress);
117      datagramSocket.send(packet);
118    }
119
120    // Now we send out a message with the actual value.
121    // Technically, we only need to send out the metadata message once for
122    // each metric, but I don't want to have to record which metrics we did and
123    // did not send.
124    offset = 0;
125    xdr_int(133);         // we are sending a string value
126    xdr_string(hostName); // hostName
127    xdr_string(name);     // metric name
128    xdr_int(0);           // spoof = False
129    xdr_string("%s");     // format field
130    xdr_string(value);    // metric value
131        
132    for (SocketAddress socketAddress : metricsServers) {
133      DatagramPacket packet = 
134        new DatagramPacket(buffer, offset, socketAddress);
135      datagramSocket.send(packet);
136    }
137  }
138
139}