All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.heanbian.block.zabbix.ZabbixTemplate Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package com.heanbian.block.zabbix;

import static org.springframework.http.HttpMethod.POST;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.heanbian.block.zabbix.api.ZabbixHistoryGetRequest;
import com.heanbian.block.zabbix.api.ZabbixHistoryGetResponse;
import com.heanbian.block.zabbix.api.ZabbixHostCreateRequest;
import com.heanbian.block.zabbix.api.ZabbixHostGenericResponse;
import com.heanbian.block.zabbix.api.ZabbixHostGetRequest;
import com.heanbian.block.zabbix.api.ZabbixHostGetResponse;
import com.heanbian.block.zabbix.api.ZabbixHostGroupCreateRequest;
import com.heanbian.block.zabbix.api.ZabbixHostGroupGenericResponse;
import com.heanbian.block.zabbix.api.ZabbixHostGroupGetRequest;
import com.heanbian.block.zabbix.api.ZabbixHostGroupGetResponse;
import com.heanbian.block.zabbix.api.ZabbixHostGroupUpdateRequest;
import com.heanbian.block.zabbix.api.ZabbixHostInterfaceCreateRequest;
import com.heanbian.block.zabbix.api.ZabbixHostInterfaceGenericResponse;
import com.heanbian.block.zabbix.api.ZabbixHostInterfaceGetRequest;
import com.heanbian.block.zabbix.api.ZabbixHostInterfaceGetResponse;
import com.heanbian.block.zabbix.api.ZabbixHostInterfaceUpdateRequest;
import com.heanbian.block.zabbix.api.ZabbixHostUpdateRequest;
import com.heanbian.block.zabbix.api.ZabbixItemCreateRequest;
import com.heanbian.block.zabbix.api.ZabbixItemGenericResponse;
import com.heanbian.block.zabbix.api.ZabbixItemGetRequest;
import com.heanbian.block.zabbix.api.ZabbixItemGetResponse;
import com.heanbian.block.zabbix.api.ZabbixItemUpdateRequest;
import com.heanbian.block.zabbix.api.ZabbixTrendGetRequest;
import com.heanbian.block.zabbix.api.ZabbixTrendGetResponse;
import com.heanbian.block.zabbix.api.ZabbixUserLogin;

/**
 * ZABBIX API 5.0
 * 
 * @author Heanbian
 *
 */
public class ZabbixTemplate {

	private final String url;
	private final String jsonrpc;
	private final String user;
	private final String password;

	private final RestTemplate restTemplate;
	private String zabbixToken = null;

	public ZabbixTemplate(String url, String user, String password) {
		this(url, "2.0", user, password, new RestTemplate());
	}

	public ZabbixTemplate(String url, String jsonpc, String user, String password, RestTemplate restTemplate) {
		this.url = url;
		this.jsonrpc = jsonpc;
		this.user = user;
		this.password = password;
		this.restTemplate = restTemplate;
	}

	public ZabbixResponse> historyGet(String... itemids) {
		return this.historyGet(Arrays.asList(itemids));
	}

	public ZabbixResponse> historyGet(List itemids) {
		ZabbixHistoryGetRequest zabbixHistoryGetRequest = new ZabbixHistoryGetRequest();
		zabbixHistoryGetRequest.setItemids(itemids);
		return this.historyGet(zabbixHistoryGetRequest);
	}

	public ZabbixResponse> historyGet(ZabbixHistoryGetRequest zabbixHistoryGetRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("history.get").setId(18).setAuth(getZabbixToken())
				.setParams(zabbixHistoryGetRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity>> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>>() {
				});

		return response.getBody();
	}

	public ZabbixResponse> trendGet(String... itemids) {
		return this.trendGet(Arrays.asList(itemids));
	}

	public ZabbixResponse> trendGet(List itemids) {
		ZabbixTrendGetRequest zabbixTrendGetRequest = new ZabbixTrendGetRequest();
		zabbixTrendGetRequest.setItemids(itemids);
		return this.trendGet(zabbixTrendGetRequest);
	}

	public ZabbixResponse> trendGet(ZabbixTrendGetRequest zabbixTrendGetRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("trend.get").setId(17).setAuth(getZabbixToken())
				.setParams(zabbixTrendGetRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity>> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>>() {
				});

		return response.getBody();
	}

	public ZabbixResponse> hostGroupGet(String... names) {
		ZabbixHostGroupGetRequest zabbixHostGroupGetRequest = new ZabbixHostGroupGetRequest();
		zabbixHostGroupGetRequest.setFilterNames(names).setOutput("extend");
		return this.hostGroupGet(zabbixHostGroupGetRequest);
	}

	public ZabbixResponse> hostGroupGet(
			ZabbixHostGroupGetRequest zabbixHostGroupGetRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostgroup.get").setId(16).setAuth(getZabbixToken())
				.setParams(zabbixHostGroupGetRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity>> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>>() {
				});

		return response.getBody();
	}

	public ZabbixResponse hostGroupDelete(String... groupids) {
		return this.hostGroupDelete(Arrays.asList(groupids));
	}

	public ZabbixResponse hostGroupDelete(List groupids) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostgroup.delete").setId(15).setAuth(getZabbixToken()).setParams(groupids);

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse hostGroupUpdate(String groupid, String name) {
		return this.hostGroupUpdate(new ZabbixHostGroupUpdateRequest(groupid, name));
	}

	public ZabbixResponse hostGroupUpdate(
			ZabbixHostGroupUpdateRequest zabbixHostGroupUpdateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostgroup.update").setId(14).setAuth(getZabbixToken())
				.setParams(zabbixHostGroupUpdateRequest);

		HttpEntity> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse hostGroupCreate(String name) {
		return this.hostGroupCreate(new ZabbixHostGroupCreateRequest(name));
	}

	public ZabbixResponse hostGroupCreate(
			ZabbixHostGroupCreateRequest zabbixHostGroupCreateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostgroup.create").setId(13).setAuth(getZabbixToken())
				.setParams(zabbixHostGroupCreateRequest);

		HttpEntity> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse> hostInterfaceGet(String hostid) {
		return this.hostInterfaceGet("extend", hostid);
	}

	public ZabbixResponse> hostInterfaceGet(String output, String hostids) {
		ZabbixHostInterfaceGetRequest zabbixHostInterfaceGetRequest = new ZabbixHostInterfaceGetRequest();
		zabbixHostInterfaceGetRequest.setOutput(output).setHostids(hostids);
		return this.hostInterfaceGet(zabbixHostInterfaceGetRequest);
	}

	public ZabbixResponse> hostInterfaceGet(
			ZabbixHostInterfaceGetRequest zabbixHostInterfaceGetRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostinterface.get").setId(12).setAuth(getZabbixToken())
				.setParams(zabbixHostInterfaceGetRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity>> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>>() {
				});

		return response.getBody();
	}

	public ZabbixResponse hostInterfaceDelete(String... interfaceids) {
		return this.hostInterfaceDelete(Arrays.asList(interfaceids));
	}

	public ZabbixResponse hostInterfaceDelete(List interfaceids) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostinterface.delete").setId(10).setAuth(getZabbixToken())
				.setParams(interfaceids);

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse hostInterfaceCreate(
			ZabbixHostInterfaceCreateRequest zabbixHostInterfaceCreateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostinterface.create").setId(9).setAuth(getZabbixToken())
				.setParams(zabbixHostInterfaceCreateRequest);

		HttpEntity> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse hostInterfaceUpdate(String interfaceid, String port) {
		return this.hostInterfaceUpdate(new ZabbixHostInterfaceUpdateRequest(interfaceid, port));
	}

	public ZabbixResponse hostInterfaceUpdate(
			ZabbixHostInterfaceUpdateRequest zabbixHostInterfaceUpdateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("hostinterface.update").setId(11).setAuth(getZabbixToken())
				.setParams(zabbixHostInterfaceUpdateRequest);

		HttpEntity> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST,
				request, new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse itemDelete(String... itemids) {
		return this.itemDelete(Arrays.asList(itemids));
	}

	public ZabbixResponse itemDelete(List itemids) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("item.delete").setId(4).setAuth(getZabbixToken()).setParams(itemids);

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse itemCreate(ZabbixItemCreateRequest zabbixItemCreateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("item.create").setId(3).setAuth(getZabbixToken())
				.setParams(zabbixItemCreateRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse itemUpdate(ZabbixItemUpdateRequest zabbixItemUpdateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("item.update").setId(5).setAuth(getZabbixToken())
				.setParams(zabbixItemUpdateRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	public ZabbixResponse> itemGet(ZabbixItemGetRequest zabbixItemGetRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("item.get").setId(6).setAuth(getZabbixToken())
				.setParams(zabbixItemGetRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity>> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>>() {
				});
		return response.getBody();
	}

	public ZabbixResponse> hostGet(String... hosts) {
		ZabbixHostGetRequest zabbixHostGetRequest = new ZabbixHostGetRequest();
		return this.hostGet(zabbixHostGetRequest.setFilterHosts(hosts));
	}

	public ZabbixResponse> hostGet(ZabbixHostGetRequest zabbixHostGetRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("host.get").setId(2).setAuth(getZabbixToken())
				.setParams(zabbixHostGetRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity>> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>>() {
				});

		return response.getBody();
	}

	public ZabbixResponse hostCreate(ZabbixHostCreateRequest zabbixHostCreateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("host.create").setId(7).setAuth(getZabbixToken())
				.setParams(zabbixHostCreateRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>() {
				});
		return response.getBody();
	}

	public ZabbixResponse hostUpdate(ZabbixHostUpdateRequest zabbixHostUpdateRequest) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("host.update").setId(8).setAuth(getZabbixToken())
				.setParams(zabbixHostUpdateRequest.getParams());

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>() {
				});
		return response.getBody();
	}

	public ZabbixResponse hostDelete(String... hostids) {
		return this.hostDelete(Arrays.asList(hostids));
	}

	public ZabbixResponse hostDelete(List hostids) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest> dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("host.delete").setId(4).setAuth(getZabbixToken()).setParams(hostids);

		HttpEntity>> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>() {
				});

		return response.getBody();
	}

	private String getZabbixToken() {
		if (this.zabbixToken == null) {
			createZabbixToken();
		}
		return this.zabbixToken;
	}

	private void createZabbixToken() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		ZabbixRequest dto = new ZabbixRequest<>();
		dto.setJsonrpc(jsonrpc).setMethod("user.login").setId(1).setParams(new ZabbixUserLogin(user, password));

		HttpEntity> request = new HttpEntity<>(dto, headers);
		ResponseEntity> response = restTemplate.exchange(url, POST, request,
				new ParameterizedTypeReference>() {
				});

		ZabbixResponse result = response.getBody();
		this.zabbixToken = result.getResult();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy