Authored by fanzelei

Merge branch 'dev-zabbix' of http://git.yoho.cn/ops/monitor-service into dev-zabbix

1 -zabbix_server=http://127.0.0.1:8080/zabbix/index.php  
2 -zabbix_user=zabbix 1 +zabbix_server=http://192.168.102.76/zabbix/api_jsonrpc.php
  2 +
  3 +zabbix_user=admin
  4 +
3 zabbix_pwd=zabbix 5 zabbix_pwd=zabbix
4 6
  7 +zabbix_type=twemproxy
  8 +
  9 +zabbix_key=cpu
  10 +
5 11
6 12
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 +}
1 package com.monitor.zabbix.model; 1 package com.monitor.zabbix.model;
2 2
  3 +import lombok.Data;
  4 +
  5 +import java.util.List;
  6 +
3 /** 7 /**
4 * Created by yoho on 2016/10/9. 8 * Created by yoho on 2016/10/9.
5 */ 9 */
6 public class HistoryRequest extends ZabbixRequest { 10 public class HistoryRequest extends ZabbixRequest {
  11 +
  12 + public static HistoryRequest buildRequest(String hostId, List<String> itemIds, String fromTime, String tillTIme) {
  13 + HistoryRequest request = new HistoryRequest();
  14 +
  15 + request.setMethod("history.get");
  16 +
  17 + request.setParams(new Params(hostId, itemIds, fromTime, tillTIme));
  18 +
  19 + request.setId((int) System.currentTimeMillis());
  20 + return request;
  21 + }
  22 +
  23 +
  24 + @Data
  25 + public static class Params {
  26 + String hostids;
  27 +
  28 + String output = "extend";
  29 +
  30 + Integer history = 0;
  31 +
  32 + List<String> itemids;
  33 +
  34 + String time_from;
  35 +
  36 + String time_till;
  37 +
  38 + public Params(String hostids, List<String> itemids, String time_from, String time_till) {
  39 +
  40 + this.hostids = hostids;
  41 +
  42 + this.itemids = itemids;
  43 +
  44 + this.time_from = time_from;
  45 +
  46 + this.time_till = time_till;
  47 + }
  48 +
  49 + }
7 } 50 }
  1 +package com.monitor.zabbix.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import java.util.List;
  6 +
  7 +/**
  8 + * Created by yoho on 2016/10/10.
  9 + */
  10 +@Data
  11 +public class HistoryResponse {
  12 +
  13 + String jsonrpc;
  14 +
  15 + List<HistoryItem> result;
  16 +
  17 + Integer id;
  18 +
  19 + @Data
  20 + public static class HistoryItem {
  21 +
  22 + String itemid;
  23 +
  24 + String clock;
  25 +
  26 + String value;
  27 +
  28 + String ns;
  29 +
  30 + }
  31 +
  32 +
  33 +}
1 package com.monitor.zabbix.model; 1 package com.monitor.zabbix.model;
2 2
  3 +import lombok.Data;
  4 +
  5 +import java.util.List;
  6 +
  7 +
3 /** 8 /**
4 * Created by yoho on 2016/10/9. 9 * Created by yoho on 2016/10/9.
5 */ 10 */
6 public class HostInterfaceRequest extends ZabbixRequest { 11 public class HostInterfaceRequest extends ZabbixRequest {
7 12
8 - public static HostInterfaceRequest buildRequest(String hostIp) { 13 + public static HostInterfaceRequest buildRequest(List<String> hostIps) {
9 14
10 HostInterfaceRequest request = new HostInterfaceRequest(); 15 HostInterfaceRequest request = new HostInterfaceRequest();
11 16
12 request.setMethod("hostinterface.get"); 17 request.setMethod("hostinterface.get");
13 18
14 - request.setId((int)System.currentTimeMillis()); 19 + request.setId((int) System.currentTimeMillis());
  20 +
  21 + request.setParams(new Params(new Filter(hostIps)));
  22 +
  23 + return request;
  24 +
  25 + }
  26 +
  27 +
  28 + @Data
  29 + public static class Params {
  30 +
  31 + Filter filter;
  32 +
  33 + String output = "extend";
  34 +
  35 +
  36 + public Params(Filter filter) {
  37 +
  38 + this.filter = filter;
  39 + }
  40 + }
15 41
16 - return request; 42 + @Data
  43 + public static class Filter {
17 44
  45 + List<String> ip;
18 46
  47 + public Filter(List<String> ips) {
  48 + this.ip = ips;
  49 + }
19 } 50 }
20 51
21 } 52 }
  1 +package com.monitor.zabbix.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import java.util.List;
  6 +
  7 +/**
  8 + * Created by yoho on 2016/10/10.
  9 + */
  10 +@Data
  11 +public class HostInterfaceResponse {
  12 + String jsonrpc;
  13 +
  14 + List<HostInterface> result;
  15 +
  16 + Integer id;
  17 +
  18 + @Data
  19 + public static class HostInterface {
  20 +
  21 + String interfaceid;
  22 +
  23 + String hostid;
  24 +
  25 + String main;
  26 +
  27 + String type;
  28 +
  29 + String useip;
  30 +
  31 + String ip;
  32 +
  33 + String dns;
  34 +
  35 + String port;
  36 + }
  37 +
  38 +}