|
|
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);
|
|
|
|
|
|
}
|
|
|
} |
...
|
...
|
|