This is a C# library designed to simplify interactions with the Zabbix API (version 6.0). It provides an intuitive way to perform operations (synchronously and asynchronously) such as creating, updating, deleting and getting entties for Zabbix.
Please note that this library is still under active development.
Currently, the library can be used by either cloning this repository or by installing the NuGet package https://www.nuget.org/packages/Zabbix.Api.NET/
First, let's establish a connection to the Zabbix server using your credentials:
string url = "http://ZABBIX_SERVER/api_jsonrpc.php";
string username = "Admin";
string password = "zabbix";
ZabbixCore core = new ZabbixCore(url, username, password);
Creating an Entity returns its remote Zabbix id, Zabbix will set every unset value to its default value.
It's important to note that each entity has a set of required properties that must be configured, as indicated by the constructor.
Host serverHost = new Host("Server", new List<HostGroup>() { linuxServers });
string createdServerHostId = core.Hosts.Create(serverHost);
Updating an entity also returns its Zabbix ID. During the update process, only the properties that are explicitly set (not null) will be sent to Zabbix.
serverHost.Status = 1;
string updatedServerHostId = core.Hosts.Update(serverHost);
Deleting a Entity returns its former remote Zabbix id.
When deleting a Entity you need to know the remote Zabbix id.
//By Entity
string updatedServerHostId = core.Hosts.Delete(serverHost);
//Or also by Id
string updatedServerHostId = core.Hosts.Delete(createdServerHostId);
This is a simple Get, it will return every Host that is configured on Zabbix.
List<Host> hosts = core.Hosts.Get().ToList();
This will return the amount of configured Hosts on Zabbix:
int hostAmount = core.Hosts.CountOutput();
This is will return every Host that is configured on Zabbix as a Dictionary where their Id is the Key.
Dictionary<string, Host> hosts = core.Hosts.PreserveKeys();
However, the Get, CountOutput and PreserveKeys method can also accept a FilterOptions object as its parameter. Every entity has its own FilterOptions object named EntitynameFilterOptions.
The FilterOptions can be initialized directly via the constructor like so:
var hosts = core.Hosts.Get(new()
{
HostIds = new List<string>(){"12312","32323"}
});
Every query (every FilterOption Prop that has a 'Select' in its name) can be used in multiple ways:
The Default Constructor will set it to "extend"
var hosts = core.Hosts.Get(new()
{
SelectItems = new()
});
Return every Host and its Items but with only their 'Delay', 'Hostid' and 'ItemId' property set
var hosts = core.Hosts.Get(new()
{
SelectItems = new("delay", "hostid")
});
The exact meaning and usage of each FilterOption can be found on the Zabbix Docs under the Get section of the entity: https://www.zabbix.com/documentation/6.0/en/manual/api/reference
The 'count' option for queries is currently not supported
Most entities support the CRUD operations and some include some extra Operations.
Every Crud operation can be called with either a single Entity or a List of Entities.
Every operation has a Async Variant
❗ The Integration Tests are not meant to be run on a Productive System ❗
The reason for that is because they can potentially change the state of Zabbix. For example, during the HostTests, if the creation is successful but the deletion fails, Zabbix will now have a stray host that will need to be deleted manually.
The integration tests are mostly there for development purposes, so I can make changes and ensure that the base functionality of the Library is intact.
A automated set up for the test enviroment is planned.
I welcome contributions and improvements to this library. Please feel free to open issues and submit pull requests.
Lots of Code has been reused from https://github.com/HenriqueCaires/ZabbixApi/tree/master