GithubHelp home page GithubHelp logo

zabbix-api's Introduction

zabbix-api

zabbix-api for java

https://www.zabbix.com/wiki/doc/api

https://www.zabbix.com/documentation/2.4/manual/api/reference/user/login

Based on zabbix api version 2.4.

Zabbix api version 2.2 will throw a exception.

##Info API is simple, beacuse java can not process json like dynamic language.

You can build you own Request Object.

public interface ZabbixApi {

	public void init();

	public void destory();

	public String apiVersion();

	public JSONObject call(Request request);

	public boolean login(String user, String password);
}

##Example

		String url = "http://192.168.90.102/zabbix/api_jsonrpc.php";
		zabbixApi = new DefaultZabbixApi(url);
		zabbixApi.init();

		boolean login = zabbixApi.login("zabbix.dev", "goK0Loqua4Eipoe");
		System.err.println("login:" + login);

		String host = "192.168.66.29";
		JSONObject filter = new JSONObject();

		filter.put("host", new String[] { host });
		Request getRequest = RequestBuilder.newBuilder()
				.method("host.get").paramEntry("filter", filter)
				.build();
		JSONObject getResponse = zabbixApi.call(getRequest);
		System.err.println(getResponse);
		String hostid = getResponse.getJSONArray("result")
				.getJSONObject(0).getString("hostid");
		System.err.println(hostid);

You can set your own HttpClient.

		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectTimeout(5 * 1000).setConnectionRequestTimeout(5 * 1000)
				.setSocketTimeout(5 * 1000).build();
		PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

		CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connManager)
				.setDefaultRequestConfig(requestConfig).build();

		ZabbixApi zabbixApi = new DefaultZabbixApi(
				"http://localhost:10051/zabbix/api_jsonrpc.php", httpclient);
		zabbixApi.init();

		String apiVersion = zabbixApi.apiVersion();

		System.out.println("api version:" + apiVersion);

		zabbixApi.destory();

##Maven dependency

<dependency>
    <groupId>io.github.hengyunabc</groupId>
    <artifactId>zabbix-api</artifactId>
    <version>0.0.2</version>
</dependency>

##Links

https://github.com/hengyunabc/zabbix-sender

##Licence Apache License V2

zabbix-api's People

Contributors

hengyunabc avatar unik avatar v-zhuravlev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zabbix-api's Issues

Refactor project and resolve some problems

Hi, hengyunabc,
I've refactored the project (https://github.com/tinawenqiao/zabbix3api) and do some things as following:
1. Design uniform API return result. ZabbixAPIResult has 3 fields: code, data, message.
2. Refactor Request class and change "params" from Map to Object. It resolve the main problem
of original project that it can’t support multiple params. The issue url:#9
3. Replace alibaba fastjson with jackson lib.
4. Use JDK 1.8.

info

how can i check if exist a item ?

中文乱码的问题

您好,我调用一个script,在一台windows主机上执行了net user Administrator命令,由于该windows主机是中文操作系统,所以返回的JSONObject中的value是乱码,请问可以通过转码解决吗?
以下是接收到的json内容:
{
"id":2,
"jsonrpc":"2.0",
"result":{
"response":"success",
"value":"?û??? Administrator\r\nȫ?? \r\nע?? ????????(??)???????ʻ?\r\n?û???ע?? \r\n???/??????? 000 (ϵͳĬ??ֵ)\r\n?ʻ????? No\r\n?ʻ????? ?Ӳ?\r\n\r\n?ϴ????????? 2010/11/21 5:56:34\r\n???뵽?? ?Ӳ?\r\n????ɸ?? 2010/11/21 5:56:34\r\n??Ҫ???? Yes\r\n?û????Ը?????? Yes\r\n\r\n????Ĺ???վ All\r\n??¼?ű? \r\n?û??????ļ? \r\n??Ŀ¼ \r\n?ϴε?¼ 2010/11/21 5:48:12\r\n\r\n??????ĵ?¼Сʱ?? All\r\n\r\n???????Ա *Administrators *HomeUsers \r\nȫ?????Ա *None \r\n????ɹ???ɡ?"
}
}

How to optimize the execution time??

Hi @hengyunabc ,

I'm trying to get a particular trigger by searching for a specific description for a given time frame.

I have configured to get trigger for a particular description and Getting these triggers for a given time, the time limit in trigger function is not giving desired output. So to get the trigger for a given time, we have to use event.get
When we pass these json array to each function and the logic I defined using java, it takes time to get the data and performance is also affected.

Could you please help or guide me, how to optimize the code to reduce the execution time?

CloseableHttpResponse needs to be fully consumed and closed

Method call() here needs to properly deal with CloseableHttpResponse.

It needs to make sure that:

  • Response content is fully consumed
    This will allow the connection manager to safely re-use the connection. This is easily achievable by calling EntityUtils.consume(entity); before returning the data;

  • CloseableHttpResponse is actually closed, in order to ensure correct deallocation of system resources.
    This MUST be called from a finally clause. So it would just a question of adding response.close(); in a finally block.

set loglevel to WARN

Hello.
Thank you for that lib!
I'd like to set loglevel for org.apache.http.*, but i don't know how - i'm far from programming in java(
Can you help me?

Runtime Exception!!

HI @hengyunabc ,

I'm getting below exception? how to resolve this?
Exception in thread "main" java.lang.RuntimeException: DefaultZabbixApi call exception!
at com.java.emc.zabbixpoc.DefaultZabbixApi.call(DefaultZabbixApi.java:147)
at com.java.emc.zabbixpoc.ZabbixTriggerModelVer2.main(ZabbixTriggerModelVer2.java:143)

Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, pos 1

Error coming at

Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, pos 1
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1248)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1158)
at com.alibaba.fastjson.JSON.parse(JSON.java:141)
at com.alibaba.fastjson.JSON.parse(JSON.java:125)
at com.alibaba.fastjson.JSON.parse(JSON.java:112)
at zabbixjavaapi.call(zabbixjavaapi.java:116)
at zabbixjavaapi.apiVersion(zabbixjavaapi.java:93)
at main.main(main.java:25)

Code

public JSONObject call(request request)
{

    if(request.getAuthentication()==null)
    {
        request.setAuthentication(auth);
    }

    try 
    {
        HttpUriRequest httpRequest = org.apache.http.client.methods.RequestBuilder
                .post().setUri(uri)
                .addHeader("Content-Type", "application/json")
                .setEntity(new StringEntity(JSON.toJSONString(request)))
                .build();
        CloseableHttpResponse response = httpClient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        byte[] data = EntityUtils.toByteArray(entity);
        return (JSONObject) JSON.parse(data);
    } 
    catch (IOException e) 
    {
        throw new RuntimeException("DefaultZabbixApi call exception!", e);
    }




}

Could you help me out regarding this?

Versions of the libraries

Hi, I'm trying to use the Zabbix-api but I'm having some problem.

I think that the problem is the versions of the libraries, especially the org.log4j.

So, Could you tell me which version I should use?

Thank you.

Follow the console error:

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:140)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:119)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:328)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:280)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:301)
at br.jus.tjrr.zabbixAPI.DefaultZabbixApi.(DefaultZabbixApi.java:22)
at br.jus.tjrr.javaclasses.ConexaoZabbixTeste.main(ConexaoZabbixTeste.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:140)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:119)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:328)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:280)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:301)
at br.jus.tjrr.zabbixAPI.DefaultZabbixApi.(DefaultZabbixApi.java:22)
at br.jus.tjrr.javaclasses.ConexaoZabbixTeste.main(ConexaoZabbixTeste.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

hostgroupCreate() 可以用吗?

我看过你的kafka的代码,里面引用的是0.01的版本,里面也有创建hostgroup的方法,和你这个里面的差距还蛮大,这里的可以使用吗?

NullPointerException

您好:
在调用您zabbix-api-0.0.2去查询监控项Item是否存在时,会报出如下的错误信息:
java.lang.NullPointerException:at io.github.hengyunabc.zabbix.api.DefaultZabbixApi.java136
以下是我自己封装的接口:
public boolean queryItem(String hostid, String key){
JSONObject jsonObject = new JSONObject();
jsonObject.put("key_",new String[]{key});
Request request = RequestBuilder.newBuilder().method("item.get")
.paramEntry("hostids",hostid)
.paramEntry("search",jsonObject)
.paramEntry("sortfield","name")
.build();
com.alibaba.fastjson.JSONObject response = new com.alibaba.fastjson.JSONObject();
try{
response = zabbixApi.call(request);
}catch(RuntimeException e){
e.printStackTrace();
}
return response.getJSONArray("result").size() == 0 ? false : true;
}
try中的代码报出的错误,想请教下您怎么解决,谢谢!

timestamp problem with zabbix 3.0

Hi!

I've discovered that Zabbix server 3.01 doesn't accept the data from ZabbixSender correctly.
The problem is that ZabbixSender always send timestamp for the whole request as System.currentTimeMillis(), though Unix timestamp (time in seconds) is required for zabbix items. Older versions of server accepted this, the new one does not.

The fix is simple - I replaced with System.currentTimeMillis()/1000;, and now it works fine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.