1
|
package com.monitor.zabbix.comp;
|
1
|
package com.monitor.zabbix.comp;
|
2
|
|
2
|
|
3
|
-import java.io.IOException;
|
|
|
4
|
-
|
|
|
5
|
-import javax.annotation.Resource;
|
|
|
6
|
-
|
3
|
+import com.monitor.zabbix.model.*;
|
7
|
import org.apache.commons.lang.StringUtils;
|
4
|
import org.apache.commons.lang.StringUtils;
|
|
|
5
|
+import org.apache.commons.lang.SystemUtils;
|
|
|
6
|
+import org.codehaus.jackson.map.ObjectMapper;
|
8
|
import org.slf4j.Logger;
|
7
|
import org.slf4j.Logger;
|
9
|
import org.slf4j.LoggerFactory;
|
8
|
import org.slf4j.LoggerFactory;
|
10
|
import org.springframework.beans.factory.annotation.Value;
|
9
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
10
|
+import org.springframework.http.HttpHeaders;
|
|
|
11
|
+import org.springframework.http.MediaType;
|
11
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
12
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
12
|
import org.springframework.stereotype.Component;
|
13
|
import org.springframework.stereotype.Component;
|
13
|
import org.springframework.web.client.RestTemplate;
|
14
|
import org.springframework.web.client.RestTemplate;
|
14
|
|
15
|
|
15
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
16
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
17
|
-import com.monitor.zabbix.enums.SystemCpuEnum;
|
|
|
18
|
-import com.monitor.zabbix.model.ItemRequest;
|
|
|
19
|
-import com.monitor.zabbix.model.LoginRequest;
|
|
|
20
|
-import com.monitor.zabbix.model.LoginResponse;
|
|
|
21
|
-import com.monitor.zabbix.model.ZabbixRequest;
|
16
|
+import javax.annotation.Resource;
|
|
|
17
|
+import javax.xml.bind.SchemaOutputResolver;
|
|
|
18
|
+
|
|
|
19
|
+import java.io.IOException;
|
|
|
20
|
+import java.util.ArrayList;
|
|
|
21
|
+import java.util.Date;
|
|
|
22
|
+import java.util.List;
|
|
|
23
|
+
|
|
|
24
|
+import static org.springframework.http.converter.FormHttpMessageConverter.DEFAULT_CHARSET;
|
22
|
|
25
|
|
23
|
/**
|
26
|
/**
|
24
|
* Created by yoho on 2016/10/9.
|
27
|
* Created by yoho on 2016/10/9.
|
|
@@ -74,39 +77,50 @@ public class ZabbixHttpComp { |
|
@@ -74,39 +77,50 @@ public class ZabbixHttpComp { |
74
|
|
77
|
|
75
|
LoginRequest request = LoginRequest.buildRequest("admin", "zabbix");
|
78
|
LoginRequest request = LoginRequest.buildRequest("admin", "zabbix");
|
76
|
|
79
|
|
|
|
80
|
+/* HttpHeaders headers = new HttpHeaders();
|
|
|
81
|
+
|
|
|
82
|
+ headers.setContentType(new MediaType("application", "json-rpc", DEFAULT_CHARSET));*/
|
|
|
83
|
+
|
77
|
LoginResponse response = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request, LoginResponse.class);
|
84
|
LoginResponse response = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request, LoginResponse.class);
|
78
|
|
85
|
|
79
|
System.out.println(response);
|
86
|
System.out.println(response);
|
80
|
-
|
|
|
81
|
- JSONObject json = new JSONObject();
|
|
|
82
|
-
|
|
|
83
|
- for(SystemCpuEnum cpu : SystemCpuEnum.values()){
|
|
|
84
|
-
|
|
|
85
|
- System.out.println(cpu.key());
|
|
|
86
|
-
|
|
|
87
|
- ItemRequest request1 = ItemRequest.buildRequest(10105, cpu.key(),"extend","name");
|
|
|
88
|
-
|
|
|
89
|
- request1.setAuth(response.getResult());
|
|
|
90
|
-
|
|
|
91
|
- request1.setId(response.getId());
|
|
|
92
|
-
|
|
|
93
|
- String itemId = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request1, String.class);
|
|
|
94
|
-
|
|
|
95
|
- //取出返回的json串中的itemid
|
|
|
96
|
- JSONObject jsonObject=JSONObject.parseObject(itemId);
|
|
|
97
|
- String result=jsonObject.getString("result").replace("[", "").replace("]", "");
|
|
|
98
|
-
|
|
|
99
|
- JSONObject jsonObject0=JSONArray.parseObject(result);
|
|
|
100
|
- String itemid=jsonObject0.getString("itemid");
|
|
|
101
|
-
|
|
|
102
|
- //重新组成json
|
|
|
103
|
- json.put(itemid,cpu.key());
|
|
|
104
|
-
|
|
|
105
|
- }
|
|
|
106
|
-
|
|
|
107
|
- System.out.println(json);
|
|
|
108
|
-
|
|
|
109
|
- }
|
|
|
110
|
|
87
|
|
111
|
-}
|
|
|
112
|
|
88
|
|
|
|
89
|
+ List<String> hostIps = new ArrayList<>();
|
|
|
90
|
+
|
|
|
91
|
+ hostIps.add("192.168.102.76");
|
|
|
92
|
+
|
|
|
93
|
+ HostInterfaceRequest request1 = HostInterfaceRequest.buildRequest(hostIps);
|
|
|
94
|
+
|
|
|
95
|
+ request1.setAuth(response.getResult());
|
|
|
96
|
+
|
|
|
97
|
+ HostInterfaceResponse response1 = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request1, HostInterfaceResponse.class);
|
|
|
98
|
+
|
|
|
99
|
+ System.out.println(response1);
|
|
|
100
|
+
|
|
|
101
|
+ List<String> items = new ArrayList<>();
|
|
|
102
|
+
|
|
|
103
|
+ items.add("23716");
|
|
|
104
|
+
|
|
|
105
|
+ items.add("23717");
|
|
|
106
|
+
|
|
|
107
|
+ items.add("23718");
|
|
|
108
|
+
|
|
|
109
|
+ long start=System.currentTimeMillis()-120*60*1000;
|
|
|
110
|
+
|
|
|
111
|
+ int start1=(int)(start/1000);
|
|
|
112
|
+ System.out.println(start1);
|
|
|
113
|
+ long end=System.currentTimeMillis()-110*60*1000;
|
|
|
114
|
+ int end1=(int)(end/1000);
|
|
|
115
|
+ System.out.println(end1);
|
|
|
116
|
+ HistoryRequest request2 = HistoryRequest.buildRequest("10105", items, String.valueOf(start1), String.valueOf(end1));
|
|
|
117
|
+
|
|
|
118
|
+
|
|
|
119
|
+ request2.setAuth(response.getResult());
|
|
|
120
|
+
|
|
|
121
|
+ HistoryResponse response2 = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request2, HistoryResponse.class);
|
|
|
122
|
+
|
|
|
123
|
+ System.out.println(response2);
|
|
|
124
|
+
|
|
|
125
|
+ }
|
|
|
126
|
+} |