Authored by fanzelei

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

zabbix_server=http://127.0.0.1:8080/zabbix/index.php
zabbix_user=zabbix
zabbix_server=http://192.168.102.76/zabbix/api_jsonrpc.php
zabbix_user=admin
zabbix_pwd=zabbix
zabbix_type=twemproxy
zabbix_key=cpu
... ...
package com.monitor.zabbix.comp;
import java.io.IOException;
import javax.annotation.Resource;
import com.monitor.zabbix.model.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.monitor.zabbix.enums.SystemCpuEnum;
import com.monitor.zabbix.model.ItemRequest;
import com.monitor.zabbix.model.LoginRequest;
import com.monitor.zabbix.model.LoginResponse;
import com.monitor.zabbix.model.ZabbixRequest;
import javax.annotation.Resource;
import javax.xml.bind.SchemaOutputResolver;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.springframework.http.converter.FormHttpMessageConverter.DEFAULT_CHARSET;
/**
* Created by yoho on 2016/10/9.
... ... @@ -74,39 +77,50 @@ public class ZabbixHttpComp {
LoginRequest request = LoginRequest.buildRequest("admin", "zabbix");
/* HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "json-rpc", DEFAULT_CHARSET));*/
LoginResponse response = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request, LoginResponse.class);
System.out.println(response);
JSONObject json = new JSONObject();
for(SystemCpuEnum cpu : SystemCpuEnum.values()){
System.out.println(cpu.key());
ItemRequest request1 = ItemRequest.buildRequest(10105, cpu.key(),"extend","name");
request1.setAuth(response.getResult());
request1.setId(response.getId());
String itemId = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request1, String.class);
//取出返回的json串中的itemid
JSONObject jsonObject=JSONObject.parseObject(itemId);
String result=jsonObject.getString("result").replace("[", "").replace("]", "");
JSONObject jsonObject0=JSONArray.parseObject(result);
String itemid=jsonObject0.getString("itemid");
//重新组成json
json.put(itemid,cpu.key());
}
System.out.println(json);
}
}
List<String> hostIps = new ArrayList<>();
hostIps.add("192.168.102.76");
HostInterfaceRequest request1 = HostInterfaceRequest.buildRequest(hostIps);
request1.setAuth(response.getResult());
HostInterfaceResponse response1 = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request1, HostInterfaceResponse.class);
System.out.println(response1);
List<String> items = new ArrayList<>();
items.add("23716");
items.add("23717");
items.add("23718");
long start=System.currentTimeMillis()-120*60*1000;
int start1=(int)(start/1000);
System.out.println(start1);
long end=System.currentTimeMillis()-110*60*1000;
int end1=(int)(end/1000);
System.out.println(end1);
HistoryRequest request2 = HistoryRequest.buildRequest("10105", items, String.valueOf(start1), String.valueOf(end1));
request2.setAuth(response.getResult());
HistoryResponse response2 = template.postForObject("http://192.168.102.76/zabbix/api_jsonrpc.php", request2, HistoryResponse.class);
System.out.println(response2);
}
}
... ...
package com.monitor.zabbix.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/10/9.
*/
public class HistoryRequest extends ZabbixRequest {
public static HistoryRequest buildRequest(String hostId, List<String> itemIds, String fromTime, String tillTIme) {
HistoryRequest request = new HistoryRequest();
request.setMethod("history.get");
request.setParams(new Params(hostId, itemIds, fromTime, tillTIme));
request.setId((int) System.currentTimeMillis());
return request;
}
@Data
public static class Params {
String hostids;
String output = "extend";
Integer history = 0;
List<String> itemids;
String time_from;
String time_till;
public Params(String hostids, List<String> itemids, String time_from, String time_till) {
this.hostids = hostids;
this.itemids = itemids;
this.time_from = time_from;
this.time_till = time_till;
}
}
}
... ...
package com.monitor.zabbix.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/10/10.
*/
@Data
public class HistoryResponse {
String jsonrpc;
List<HistoryItem> result;
Integer id;
@Data
public static class HistoryItem {
String itemid;
String clock;
String value;
String ns;
}
}
... ...
package com.monitor.zabbix.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/10/9.
*/
public class HostInterfaceRequest extends ZabbixRequest {
public static HostInterfaceRequest buildRequest(String hostIp) {
public static HostInterfaceRequest buildRequest(List<String> hostIps) {
HostInterfaceRequest request = new HostInterfaceRequest();
request.setMethod("hostinterface.get");
request.setId((int)System.currentTimeMillis());
request.setId((int) System.currentTimeMillis());
request.setParams(new Params(new Filter(hostIps)));
return request;
}
@Data
public static class Params {
Filter filter;
String output = "extend";
public Params(Filter filter) {
this.filter = filter;
}
}
return request;
@Data
public static class Filter {
List<String> ip;
public Filter(List<String> ips) {
this.ip = ips;
}
}
}
... ...
package com.monitor.zabbix.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/10/10.
*/
@Data
public class HostInterfaceResponse {
String jsonrpc;
List<HostInterface> result;
Integer id;
@Data
public static class HostInterface {
String interfaceid;
String hostid;
String main;
String type;
String useip;
String ip;
String dns;
String port;
}
}
... ...