1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
| import info.ganglia.gmetric4j.gmetric.GMetric;
import info.ganglia.gmetric4j.gmetric.GMetricSlope;
import info.ganglia.gmetric4j.gmetric.GMetricType;
import info.ganglia.gmetric4j.gmetric.GangliaException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MonitorUtil {
private static final Logger logger = LoggerFactory.getLogger(MonitorUtil.class);
private static final HedwigConfig HC = HedwigConfig.getInstance();
private static class GMetricHOlder {
static String GANGLIA_HOST = HC.getString("ganglia.host", "");
final static int GANGLIA_PORT = HC.getInt("ganglia.port", 7649);
final static int GANGLIA_TTL = HC.getInt("ganglia.ttl", 1);
static {
if(StringUtils.isEmpty(GANGLIA_HOST)){
GANGLIA_HOST = NetworkUtils.getEth0Address();
}
System.out.println("[GANGLIA MONITOR] begin to init gmetric");
System.out.println("[GANGLIA MONITOR] GANGLIA_HOST : " + GANGLIA_HOST);
System.out.println("[GANGLIA MONITOR] GANGLIA_PORT : " + GANGLIA_PORT);
System.out.println("[GANGLIA MONITOR] GANGLIA_TTL : " + GANGLIA_TTL);
}
final static GMetric G_METRIC = new GMetric(GANGLIA_HOST, GANGLIA_PORT,
GMetric.UDPAddressingMode.UNICAST, GANGLIA_TTL, true);
}
private static GMetric getGMetric() {
return GMetricHOlder.G_METRIC;
}
/**
* monitor a metric
* @param name Name of the metric
* @param value Value of the metric
* @param type Type of the metric.
* Either string|int8|uint8|int16|uint16|int32|uint32|float|double
* @param units Unit of measure for the value
* @param slope Either zero|positive|negative|both
* @param group Group Name of the metric
*/
public static void monitor(String name, String value, GMetricType type,
String units, GMetricSlope slope, String group) {
GMetric gm = getGMetric();
try {
gm.announce(name, value, type, units, slope, 60, 0, group);
} catch (GangliaException e) {
logger.error("monitor fail. name : {}, value:{}, error:{}",
name, value, e.getMessage(), e);
}
}
/**
* monitor a metric
* @param name Name of the metric
* @param value Value of the metric
* @param group Group Name of the metric
*/
public static void monitor(String name, String value, String group){
monitor(name, value, GMetricType.STRING, " ", GMetricSlope.BOTH, group);
}
/**
* monitor a positive metric
* @param name Name of the metric
* @param value Value of the metric
* @param group Group Name of the metric
*/
public static void counter(String name, String value, String group){
monitor(name, value, GMetricType.STRING, " ", GMetricSlope.POSITIVE, group);
}
/**
* monitor a metric
* @param name Name of the metric
* @param value Value of the metric
* @param group Group Name of the metric
*/
public static void monitor(String name, float value, String group){
monitor(name, Float.toString(value),GMetricType.DOUBLE, " ", GMetricSlope.BOTH, group);
}
/**
* monitor a positive metric
* @param name Name of the metric
* @param value Value of the metric
* @param group Group Name of the metric
*/
public static void counter(String name, float value, String group){
monitor(name, Float.toString(value),GMetricType.FLOAT, " ", GMetricSlope.POSITIVE, group);
}
}
|