Said above, I was to Zabbix do the second development of small white, since to Zabbix to do the second development is small white, then you have to study Zabbix to provide the relevant API.
So I found a variety of Zabbix site, and finally find the following URL:
Http://zabbix.org/wiki/Docs/api/libraries: There are APIs for each development language, so you can go and see them.
Next, continue with the Java API, as follows:
The first and third are the APIs that connect to Zabbix server, and the first one needs to construct its own JSON format parameters. The second sender is the development of an API that transports data to the server.
The third is the object of my study, the API, the further encapsulation, also provides some test code. This is the time to study it from here. (Study together (⊙o⊙)! )
To use this API, you can import zabbix4j This package is good, the relevant package will be imported into, (I took the first detour, are a guide package, and then want to Zabbix since the API should be provided Zabbix package AH? Try it, sure enough, Roar roar ~ ~)
To test we also import additional packages, as follows:
The following is the formal start of the code:
In the Zabbix Web interface first started from the creation of the host, then, here we also start from the creation of the host, the code to create the host is as follows:
1 Import Staticorg.junit.assert.*;2 3 Importjava.util.ArrayList;4 Importjava.util.Date;5 Importjava.util.List;6 7 Importorg.junit.Test;8 9 Importcom.zabbix4j.host.HostCreateRequest;Ten ImportCom.zabbix4j.host.HostCreateResponse; One ImportCom.zabbix4j.hostinteface.HostInterfaceObject; A ImportCom.zabbix4j.usermacro.Macro; - - Public classTestcreatehostextendszabbixapitestbase{ the PublicTestcreatehost () { - Super(); - } - + @Test - Public voidCreateHost () { + //These two variables must exist in the database (the first time I've been here for a while, thinking that creating a host is nothing new, A //This is caused by the Zabbix's own misunderstanding) at FinalInteger groupId = 4; - FinalInteger TemplateID = 10081; - - Try { -Hostcreaterequest request =Newhostcreaterequest (); -Hostcreaterequest.params Params =request.getparams (); in - Params.addtemplateid (TemplateID); to params.addgroupid (groupId); + //Add a macro -list<macro> macros =NewArraylist<macro>(); theMacro Macro1 =NewMacro (); *Macro1.setmacro ("{$MACRO 1}"); $Macro1.setvalue ("value1");Panax Notoginseng Macros.add (Macro1); - Params.setmacros (macros); the //Add Host Interface +Hostinterfaceobject Hostinterface =NewHostinterfaceobject (); AHostinterface.setip ("192.168.106.133"); the Params.addhostinterfaceobject (hostinterface); + //add host, and host name -Params.sethost ("Test host created1." +NewDate (). GetTime ()); $Params.setname ("Test host created1 name" +NewDate (). GetTime ()); $ -Hostcreateresponse response =zabbixapi.host (). Create (request); - Assertnotnull (response); the Logger.debug (Getgson (). ToJson (response)); - Wuyi}Catch(Exception e) { the fail (e.getmessage ()); - } Wu } - About}
To create a host in Zabbix (and do other things as well), first through the authentication, first you have to pass your own Zabbix interface address: http://IP/zabbix/api_jsonrpc.php;
I put this premise work in a class with the following code:
1 Import StaticOrg.junit.Assert.fail;2 3 4 ImportOrg.slf4j.Logger;5 Importorg.slf4j.LoggerFactory;6 7 ImportCom.google.gson.Gson;8 ImportCom.google.gson.GsonBuilder;9 ImportCom.zabbix4j.ZabbixApi;Ten Importcom.zabbix4j.ZabbixApiException; One A - - Public classZabbixapitestbase { the - protected StaticLogger Logger = Loggerfactory.getlogger (zabbixapitestbase.class); - protected StaticString user = "Admin";//Zabbix Login Account - protected StaticString password = "Zabbix";//Zabbix Login Password + - protectedZabbixapi Zabbixapi; + A Publiczabbixapitestbase () { at Login (user, password); - } - - protected voidLogin (string user, string password) { - Try { -Zabbixapi =NewZabbixapi ("http://192.168.106.136/zabbix/api_jsonrpc.php"); in zabbixapi.login (user, password); -}Catch(zabbixapiexception e) { to fail (e.getmessage ()); + } - } the * $ protectedGson Getgson () {Panax Notoginseng - return NewGsonbuilder (). setprettyprinting (). Create (); the } + A}
Small white I was learning on the way, I hope you guys a lot of advice, thank you, thank you!
Reprint Please specify address: http://www.cnblogs.com/dbdd/p/7161723.html
Java API for Zabbix (i)