import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLOutput;
import java.util.*;
import com.sun.management.HotSpotDiagnosticMXBean;
import jxl.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jxl.read.biff.BiffException;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.transform.Source;
public class Test {
public static String URl = "http://zaser02.bb20.site//zabbix//api_jsonrpc.php";
public static String AUTH = null;
private static final String USERNAME =
private static final String PASSWORD =
/*
public static String URl = "http://54.237.181.219///api_jsonrpc.php";
public static String AUTH = null;
private static final String USERNAME = "Admin";
private static final String PASSWORD = "zabbix";*/
private static String sendPost(Map map) {
String param = JSON.toJSONString(map);
HttpURLConnection connection = null;
DataOutputStream out = null;
BufferedReader reader = null;
StringBuffer sb = null;
try {
URL url = new URL(URl);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(param);
out.flush();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return sb.toString();
}
private static void setAuth() {
Map<String, Object> params = new HashMap<>();
params.put("user", USERNAME);
params.put("password", PASSWORD);
Map<String, Object> map = new HashMap<>();
map.put("jsonrpc", "2.0");
map.put("method", "user.login");
map.put("params", params);
map.put("auth", null);
map.put("id", 0);
String response = sendPost(map);
JSONObject json = JSON.parseObject(response);
AUTH = json.getString("result");
System.out.println(AUTH);
}
private static String getAuth() {
if (AUTH == null) {
setAuth();
}
return AUTH;
}
/**
* @param hostName //主机名
* @return
*/
private static String getHostIdByHostName(String hostName) {
Map<String, Object> filter = new HashMap<>();
filter.put("host", hostName);
Map<String, Object> params = new HashMap<>();
params.put("output", "extend");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "host.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
String hostid = json.getString("hostid");
return hostid;
} else {
return null;
}
}
//返回主机名
private static String getHostByHostId(String hostId) {
Map<String, Object> filter = new HashMap<>();
filter.put("hostids", hostId);
Map<String, Object> params = new HashMap<>();
params.put("output", "name");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "host.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
System.out.println(response);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
System.out.println(result);
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
String host = json.getString("host");
System.out.println(host);
return host;
} else {
return null;
}
}
//修改表达式
private static void updateDescription(String name,String e){
System.out.println("修改表达式中...");
Map<String, Object> filter = new HashMap<String, Object>();
//filter.put("host", name);
Map<String, Object> params = new HashMap<String, Object>();
params.put("triggerid", name);
params.put("description", e);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "trigger.update");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
System.out.println("sss");
String response = sendPost(map);
//System.out.println(response);
}
//修改触发器的URL
private static void updateUrl(String name,String e){
System.out.println("修改url中...");
Map<String, Object> filter = new HashMap<String, Object>();
//filter.put("host", name);
Map<String, Object> params = new HashMap<String, Object>();
params.put("triggerid", name);
params.put("url", e);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "trigger.update");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
System.out.println("sss");
String response = sendPost(map);
}
//获取web监控项
private static void getHostWeb(String hostname, String hostId,Map<String, String> webStep,Map<String, String> stepUrl){
System.out.println("获取web监控项...");
//filter.put("host", name);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("selectSteps", "extend");
params.put("hostids", hostId);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "httptest.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
JSONObject json=null;
for(int i=0;i<result.size();i++){
json = result.getJSONObject(i);
System.out.println(json);
String webName1 = json.getString("name");
JSONArray stepR = json.getJSONArray("steps");
if(stepR.size()>0) {
JSONObject stepJson=null;
for (int j = 0; j < stepR.size(); j++) {
stepJson = stepR.getJSONObject(j);
String url= stepJson.getString("url");
String name= stepJson.getString("name");
if(webStep.containsKey(name)){
System.out.println(name+"该名字已经存在!!!"+hostname);
System.exit(0);
}
webStep.put(name,webName1);
if(stepUrl.containsKey(name)){
System.out.println(name+"该名字已经存在!!!"+hostname);
System.exit(0);
}
stepUrl.put(name,url);
//System.out.println(json.getJSONArray("steps").getJSONObject(0).getString("url"));
//System.out.println(json.getJSONArray("steps").getJSONObject(0).getString("name"));
}
}
}
}
}
//获取对应主机的触发器名称
private static String getHostTrig(String name,String hostId){
Map<String, String> webStep = new HashMap<String, String>();
Map<String, String> stepUrl = new HashMap<String, String>();
getHostWeb(name,hostId,webStep,stepUrl);
System.out.println("获取触发表达式中...");
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("host", name);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("expandExpression","");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "trigger.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
System.out.println(result);
if (result.size() > 0) {
JSONObject json=null;
for(int i=0;i<result.size();i++){
json = result.getJSONObject(i);
String description = json.getString("description");
String expr = json.getString("expression");
String url = json.getString("url");
String trigId= json.getString("triggerid");
if((description.substring(description.length()-8).equals("time out"))){
String[] tt= expr.split(",");
String[] kk= tt[0].split("\\[");
System.out.println("修改time out 中"+ name);
String stepName = tt[1];
String webName = kk[1];
if(stepUrl.containsKey(stepName) && webStep.containsValue(webName)){
updateUrl(trigId,stepUrl.get(stepName));
System.out.println("修改成功:"+name);
}
}else if(description.substring(description.length()-7).equals("NOT 200")){
String[] tt= expr.split("\\[");
String[] kk= tt[1].split(",");
String[] ll= kk[1].split("\\]");
String webName200 = kk[0];
String stepName200 = ll[0];
System.out.println("修改NOT 200中"+ name);
if(stepUrl.containsKey(stepName200) && webStep.containsValue(webName200)){
updateUrl(trigId,stepUrl.get(stepName200));
System.out.println("修改成功:"+name);
}
}
//System.out.println(host);
}
return "11";
} else {
return null;
}
}
//获取含有web监测的所有主机
private static String getHostWithWeb(){
Map<String, Object> filter = new HashMap<String, Object>();
//filter.put("ip", ip);
Map<String, Object> params = new HashMap<String, Object>();
//params.put("output", "extend");
params.put("with_httptests", "extend");
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "host.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
System.out.println(result);
JSONObject json=null;
System.out.println(result.size());
for(int i=0;i<result.size();i++){
json = result.getJSONObject(i);
String host = json.getString("host");
String host1 = json.getString("hostid");
//getHostWeb("10790");
getHostTrig(host,host1);
//System.out.println(host);
//System.out.println(host1);
}
return "11";
} else {
return null;
}
}
private static String getHostIdByIp(String ip) {
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("ip", ip);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "host.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
String host = json.getString("host");
String hostid = json.getString("hostid");
return hostid;
} else {
return null;
}
}
private static String getItemId(String hostId, String key) throws Exception {
JSONArray result = getItemByHostAndKey(hostId, key);
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
if (json != null) {
return json.getString("itemid");
}
}
return null;
}
private static String getGroupId(String group) {
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("name", group);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "hostgroup.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
System.out.println(map);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
String groupid = json.getString("groupid");
return groupid;
} else {
return null;
}
}
private static String getTemplateId(String templateName) {
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("host", templateName);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "template.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
System.out.println(response);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
String templateId = json.getString("templateid");
return templateId;
} else {
return null;
}
}
public static JSONArray getItemByHostAndKey(String hostId, String key) {
if (hostId == null) {
return null;
}
Date start = new Date();
Map<String, Object> search = new HashMap<String, Object>();
search.put("key_", key);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("hostids", hostId);
params.put("search", search);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "item.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
return result;
}
private static int getValueType(String hostId, String key) throws Exception {
JSONArray result = getItemByHostAndKey(hostId, key);
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
if (json != null) {
return json.getIntValue("value_type");
}
}
return 3;
}
public static void hostDelete(HashMap<String,String> hostNameList) {
List<String> params = new ArrayList<>();
Set<String> key = hostNameList.keySet();
for(String k1:key) {
String hostId = getHostIdByHostName(hostNameList.get(k1));
params.add(hostId);
}
System.out.println(params);
Map<String, Object> map = new HashMap<>();
map.put("jsonrpc", "2.0");
map.put("method", "host.delete");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
String error = JSON.parseObject(response).getString("error");
if (error != null) {
System.out.println("主机删除失败,主机名:" + error);
System.out.println(error);
}
String result;
result = JSON.parseObject(response).getString("result");
if (result != null) {
System.out.println("主机删除成功:" + result);
}
}
/**
* 创建主机
*
* @param hostname
* @param hostIp
* @param hostGroupId
* @param templateId
*/
public static void hostCreat(String hostname, String hostIp, Map<String, Object> hostGroupId, Map<String, Object> templateId) {
Map<String, Object> filter = new HashMap<String,Object>();
filter.put("type", "1");
filter.put("main", "1");
filter.put("useip", "1");
filter.put("ip", hostIp);
filter.put("dns", "");
filter.put("port", "10050");
Map<String, Object> params = new HashMap<String,Object>();
params.put("host", hostname);
params.put("interfaces", filter);
params.put("groups", hostGroupId);
params.put("templates", templateId);
Map<String, Object> map = new HashMap<String,Object>();
map.put("jsonrpc", "2.0");
map.put("method", "host.create");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
System.out.println(map);
String response = sendPost(map);
System.out.println(response);
String error = JSON.parseObject(response).getString("error");
if (error != null) {
System.out.println("主机创建失败,主机名:" + hostname);
System.out.println(error);
}
String result;
result = JSON.parseObject(response).getString("result");
if (result != null) {
System.out.println("执行成功:添加主机完成:" + hostname);
}
}
public static HashMap<String, String> readXlsFile(File file) throws IOException {
Sheet sheet;
Workbook book;
HashMap<String,String> ipMap =new HashMap<>();
try {
int i = 1;
int n = 100;
book = Workbook.getWorkbook(file);
sheet = book.getSheet(0);
while (i < n) {
String ip = sheet.getCell(0, i).getContents();
String hostname = sheet.getCell(1, i).getContents();
if ("end".equals(ip) == true) {
i = 100;
break;
}
i++;
ipMap.put(ip, hostname);
}
} catch (BiffException e) {
System.out.println(e.getMessage());
}
return ipMap;
}
public static void main(String[] args) throws Exception {
//getHostTrig("MFC-AWS-PUSHINFO-01","10790");
//getHostWithWeb();
/* String ts = "{MFC-SGW-MASTER01:web.test.time[MFC SGW-Master-domain Alarm,MFC-SGW-MASTER01-domain,resp].nodata(5m)}=1";
String[] tt= ts.split(",");
String[] kk= tt[0].split("\\[");
System.out.println("修改time out 中");
String webName = tt[1];
System.out.println(webName);
String stepName = kk[1];
System.out.println(stepName);*/
/* String[] tt= ts.split("\\[");
String[] kk= tt[1].split(",");
String[] ll= kk[1].split("\\]");
//System.out.println(kk[1]);
System.out.println(ll[0]);
//System.out.println(tt[1]);
System.out.println(kk[0]);
System.out.println(Arrays.toString(tt));
System.out.println(Arrays.toString(kk));
System.out.println(Arrays.toString(ll));*/
/* String group = "Zabbix servers";
System.out.println(getGroupId(group)); //查询对应的主机组id,创建需要用
System.out.println(getTemplateId("Valor Sytem Basic Template"));
Map<String, Object> groupId = new HashMap<String,Object>();
groupId.put("groupid", "4");
Map<String, Object> templateId = new HashMap<>();
templateId.put("templateid", "10263");
File file = new File("D:\\TestJava\\src\\hostname-ip.xls"); //hostname,ip表格
HashMap<String,String> map= readXlsFile(file);//获取ip和hostname 返回hashmap
Set<String> key = map.keySet();
System.out.println(map);
System.out.println(key);
for (String k:key) {
hostCreat(map.get(k), k, groupId, templateId);
}*/
//hostCreat("ceshi","54.237.181.219",groupId,templateId);
//System.out.println(getTemplateId("Valor Sytem Basic Template"));
/* Map<String, Object> groupId = new HashMap<String,Object>();
groupId.put("groupid", "23");
Map<String, Object> templateId = new HashMap<>();
templateId.put("templateid", "10263");
File file = new File("D:\\TestJava\\src\\hostname-ip.xls"); //hostname,ip表格
HashMap<String,String> map= readXlsFile(file);//获取ip和hostname 返回hashmap
Set<String> key = map.keySet();
System.out.println(map);
System.out.println(key);
hostCreat("ceshi","12",groupId,templateId);*/
/* for (String k:key) {
hostCreat(map.get(k), k, groupId, templateId);
}*/
/*
System.out.println(getTemplateId("Valor Sytem Basic Template")); //查询对应的模板id,创建主机需要用到
//System.out.println(getTemplateId("Valor MRT Bandwidth Users Template")); //查询对应的模板id,创建主机需要用到
Map<String, String> groupId = new HashMap<>();
groupId.put("groupid", "4");
Map<String, String> templateId = new HashMap<>();
//templateId.put("templateid", "10263"); //10263 + 10385 为mfc模板.
templateId.put("templateid", "10385");
File file = new File("D:\\TestJava\\src\\hostname-ip.xls"); //hostname,ip表格
HashMap<String,String> map= readXlsFile(file);//获取ip和hostname 返回hashmap
Set<String> key = map.keySet();
for (String k:key) {
hostCreat(map.get(k), k, groupId, templateId);
}
hostDelete(map); //删除对应表格的主机配置*/
//getHostWithWeb();
//getHostTrig("GOOSE-AWS-ENDUSER-ACCESS-01");
//getHostTrig("GOOSE-API-MONITOR");
//System.out.println(getHostByHostId("10084"));
//updateDescription("29775","Upgrade Service monitor Time out");
}
}