MetricMonitorTest.java
1.65 KB
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
package com.dianping.cat.demo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.junit.Test;
public class MetricMonitorTest {
@Test
public void test() throws Exception {
String host = "cat.qa.dianpingoa.com";
String group = "db-mysql-tg01s6.nh[10.1.1.136]";
for (int i = 0; i < 1000; i++) {
SendData(host, group, "Load", 100);
SendData(host, group, "QUESTIONS", 100);
SendData(host, group, "swapTotal", 100);
SendData(host, group, "diskAvail", 100);
SendData(host, group, "Aborted_clients", 100);
SendData(host, group, "Innodb_buffer_pool_pages_dirty", 100);
SendData(host, group, "Innodb_deadlocks", 100);
Thread.sleep(5000);
}
}
public void SendData(String host, String group, String key, int value) throws Exception {
String url = "http://" + host + "/cat/r/monitor?timestamp=%s&group=%s&domain=cat&key=%s&op=sum&value=%s";
long timestamp = System.currentTimeMillis();
url = String.format(url, timestamp, group, key, value);
String ret = sendGet(url);
System.out.println(ret);
}
public String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}