String url = "http://ZabbixServer/zabbix/api_jsonrpc.php"; zabbixApi = new DefaultZabbixApi(url); zabbixApi.init();
boolean login = zabbixApi.login("login", "password");
{ "jsonrpc": "2.0", "method": "host.get", // API "params": { // "output": "extend", // , // "filter": { // , "host": [ // Host: "Zabbix server", // "Linux server" ] } }, "auth": "038e1d7b1735c6a5436ee9eae095879e", "id": 1 }
JSONObject filter = new JSONObject(); filter.put("host", “127.0.0.1”); Request request = RequestBuilder.newBuilder() .method("host.get") .paramEntry("filter", filter) .build();
JSONObject response = zabbixApi.call(request);
{ "id":2, "jsonrpc":"2.0", "result":[ { "hostid":"10503" } ] }
System.out.println(response);
String hostid = response.getJSONArray("result").getJSONObject(0).getString("hostid");
request = RequestBuilder.newBuilder() .method("host.update") .paramEntry("hostid", hostid) .paramEntry("host", "localhost") .paramEntry("name", "localhost") .build(); response = zabbixApi.call(request);
request = RequestBuilder.newBuilder() .method("hostinterface.get") .paramEntry("hostids", new String [] {hostid}) .build(); response = zabbixApi.call(request); interfaceid = response.getJSONArray("result") .getJSONObject(0).getString("interfaceid");
request = RequestBuilder.newBuilder() .method("hostinterface.update") .paramEntry("interfaceid", interfaceid) .paramEntry("dns", "localhost") .paramEntry("useip", 0) .build(); response = zabbixApi.call(request);
HOSTNAME localhost
import com.alibaba.fastjson.JSONObject; import io.github.hengyunabc.zabbix.api.DefaultZabbixApi; import io.github.hengyunabc.zabbix.api.Request; import io.github.hengyunabc.zabbix.api.RequestBuilder; import java.io.File; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static String EXECUTION_DIR = new File("").getAbsolutePath(); public static String fileHostParams = EXECUTION_DIR + "/HostName.txt"; public static DefaultZabbixApi zabbixApi; /* */ public static void main(String[] args) throws Exception { String url = "http://ZabbixServer/zabbix/api_jsonrpc.php"; try { zabbixApi = new DefaultZabbixApi(url); zabbixApi.init(); } catch (Exception e) { e.printStackTrace(); } boolean login = zabbixApi.login("login", "password"); Map<String, String> params = readhostNameFromFile(); String hostid = ""; String interfaceid = ""; for (Map.Entry hostParam : params.entrySet()) { String hostName = hostParam.getKey().toString(); String ip = hostParam.getValue().toString(); // Request request = RequestBuilder.newBuilder() .method("host.exists") .paramEntry("host", ip) .build(); JSONObject response = zabbixApi.call(request); boolean hostexist = response.getBooleanValue("result"); if (hostexist) { // JSONObject filter = new JSONObject(); filter.put("host", ip); request = RequestBuilder.newBuilder() .method("host.get") .paramEntry("filter", filter) .build(); System.out.println(request); response = zabbixApi.call(request); hostid = response.getJSONArray("result").getJSONObject(0).getString("hostid"); // request = RequestBuilder.newBuilder() .method("host.update") .paramEntry("hostid", hostid) .paramEntry("host", hostName) .paramEntry("name", hostName) .build(); zabbixApi.call(request); // request = RequestBuilder.newBuilder() .method("hostinterface.get") .paramEntry("output", new String[]{"interfaceid"}) .paramEntry("hostids", hostid) .build(); response = zabbixApi.call(request); interfaceid = response.getJSONArray("result").getJSONObject(0).getString("interfaceid"); // request = RequestBuilder.newBuilder() .method("hostinterface.update") .paramEntry("interfaceid", interfaceid) .paramEntry("dns", hostName.toLowerCase()) .paramEntry("useip", 0) .build(); zabbixApi.call(request); } else { System.out.println("Host with name - " + ip + " not found;"); } } } /** * DNS * @return maping <hostName, IP> */ public static Map<String, String> readhostNameFromFile() throws Exception { Map<String, String> hostParams = new HashMap<>(); File file = new File(fileHostParams); Scanner sc = new Scanner(file); //Ignor header from file sc.nextLine(); while (sc.hasNext()) { String[] line = sc.nextLine(); hostParams.put(line[0], getIPbyHostName(line[0])); } return hostParams; } /** * Return IP address by dns host name * @param hostName * @return IP */ public static String getIPbyHostName(String hostName) { String ipAddr = ""; try { InetAddress inetAddr = InetAddress.getByName(hostName); byte[] addr = inetAddr.getAddress(); for (int i = 0; i < addr.length; i++) { if (i > 0) { ipAddr += "."; } ipAddr += addr[i] & 0xFF; } } catch (UnknownHostException e) { System.out.println("Host not found: " + e.getMessage()); } return ipAddr; } }
Source: https://habr.com/ru/post/277027/
All Articles