pub struct ZabbixInstance { /* private fields */ }Expand description
Represents an active connection to a Zabbix server.
Implementations§
Source§impl ZabbixInstance
impl ZabbixInstance
Sourcepub fn builder(url: &str) -> ZabbixInstanceBuilder
pub fn builder(url: &str) -> ZabbixInstanceBuilder
Creates a new ZabbixInstanceBuilder to configure the connection.
The URL should be the base URL of the Zabbix server, without the /api_jsonrpc.php suffix.
§Examples
use http_request_zabbix::ZabbixInstance;
let builder = ZabbixInstance::builder("http://localhost/zabbix");Source§impl ZabbixInstance
impl ZabbixInstance
Sourcepub fn id(&self) -> &str
pub fn id(&self) -> &str
Returns the internally generated UUID for this instance.
You can use it to identify the instance in your logs.
§Examples
use http_request_zabbix::{ZabbixInstance, AuthType};
let zabbix = ZabbixInstance::builder("http://zabbix.example.com/zabbix")
.build()
.unwrap()
.login(AuthType::UsernamePassword("Admin".to_string(), "zabbix".to_string()))
.unwrap();
println!("Instance ID: {}", zabbix.id());Sourcepub fn url(&self) -> &str
pub fn url(&self) -> &str
Returns the Zabbix API URL this instance connects to (without the /api_jsonrpc.php suffix).
§Examples
use http_request_zabbix::{ZabbixInstance, AuthType};
let zabbix = ZabbixInstance::builder("http://zabbix.example.com/zabbix")
.build()
.unwrap()
.login(AuthType::UsernamePassword("Admin".to_string(), "zabbix".to_string()))
.unwrap();
println!("Instance URL: {}", zabbix.url());Sourcepub fn logout(&mut self) -> Result<&mut Self, ZabbixError>
pub fn logout(&mut self) -> Result<&mut Self, ZabbixError>
Logs out of the Zabbix server and invalidates the current token.
Call automatically when the instance is dropped.
§Examples
use http_request_zabbix::{ZabbixInstance, AuthType};
let mut zabbix = ZabbixInstance::builder("http://zabbix.example.com/zabbix")
.build()
.unwrap()
.login(AuthType::UsernamePassword("Admin".to_string(), "zabbix".to_string()))
.unwrap();
zabbix.logout().unwrap();Sourcepub fn get_version(&self) -> Result<String, ZabbixError>
pub fn get_version(&self) -> Result<String, ZabbixError>
Retrieves the Zabbix server API version.
Use this method to check the version instead of directly calling zabbix_request with apiinfo.version,
because this method requires no authentication.
§Examples
use http_request_zabbix::{ZabbixInstance, AuthType};
let zabbix = ZabbixInstance::builder("http://zabbix.example.com/zabbix")
.build()
.unwrap()
.login(AuthType::UsernamePassword("Admin".to_string(), "zabbix".to_string()))
.unwrap();
println!("Zabbix Version: {}", zabbix.get_version().unwrap());Sourcepub fn check_version(&self, version_req: &str) -> Result<bool, ZabbixError>
pub fn check_version(&self, version_req: &str) -> Result<bool, ZabbixError>
Checks if the connected Zabbix server’s version matches a semantic version requirement.
Example requirement: >=6.4, <7.0
You can use this method to quickly check if the connected Zabbix server’s version satisfies your requirements.
§Examples
use http_request_zabbix::{ZabbixInstance, AuthType};
let zabbix = ZabbixInstance::builder("http://zabbix.example.com/zabbix")
.build()
.unwrap()
.login(AuthType::UsernamePassword("Admin".to_string(), "zabbix".to_string()))
.unwrap();
println!("Is >= 6.4: {}", zabbix.check_version(">=6.4").unwrap());Sourcepub fn zabbix_request<P: Into<ApiRequestParams>>(
&self,
method: &str,
params: P,
) -> Result<String, ZabbixError>
pub fn zabbix_request<P: Into<ApiRequestParams>>( &self, method: &str, params: P, ) -> Result<String, ZabbixError>
Makes a raw JSON-RPC request to the Zabbix API.
params can be either a serde_json::Value (like json!({...})), a string slice &str, or a String.
§Examples
use http_request_zabbix::{ApiRequestParams, AuthType, ZabbixInstance};
let zabbix = ZabbixInstance::builder("http://zabbix.example.com/zabbix").build().unwrap()
.login(AuthType::UsernamePassword("Admin".to_string(), "zabbix".to_string())).unwrap();
let params_json = ApiRequestParams::from(serde_json::json!({"output": ["host", "name"], "limit": 1}));
let params_string = ApiRequestParams::from("{\"output\": [\"host\", \"name\"], \"limit\": 1}");
let result_json = zabbix.zabbix_request("host.get", params_json).unwrap();
let result_string = zabbix.zabbix_request("host.get", params_string).unwrap();