1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use reqwest::blocking::Client;
use serde::Serialize;
use std::env;
use zabbix_api::client::client::{ZabbixApiClient, ZabbixApiClientImpl};
use zabbix_api::error::ZabbixApiError;
// The ZabbixWebScenario type will be inferred from its usage in client.get_webscenarios
// but it's good practice to import it if you know the type.
// use zabbix_api::webscenario::model::ZabbixWebScenario;
// Define a structure for your API call's parameters for "httptest.get"
// API Reference: https://www.zabbix.com/documentation/current/en/manual/api/reference/httptest/get
#[derive(Serialize)]
struct GetWebScenariosParams {
output: String,
#[serde(rename = "selectSteps")] // Zabbix API expects "selectSteps"
select_steps: String,
// You can add more parameters like 'limit', 'filter', 'search', 'hostids', etc.
// For example, to limit results:
// limit: Option<u32>,
}
fn main() -> Result<(), ZabbixApiError> {
// Initialize logger (optional, but helpful for debugging)
// Ensure env_logger is in your [dev-dependencies] in Cargo.toml
// and you call init_logging or env_logger::init()
// For this example, we'll assume logging is initialized if needed elsewhere or skip for simplicity.
// env_logger::init();
let zabbix_api_url = env::var("ZABBIX_API_URL").expect(
"ZABBIX_API_URL environment variable not set (e.g., http://localhost:3080/api_jsonrpc.php)",
);
let zabbix_api_user = env::var("ZABBIX_API_USER")
.expect("ZABBIX_API_USER environment variable not set (e.g., Admin)");
let zabbix_api_password = env::var("ZABBIX_API_PASSWORD")
.expect("ZABBIX_API_PASSWORD environment variable not set (e.g., zabbix)");
let http_client = Client::new();
let client = ZabbixApiClientImpl::new(http_client, &zabbix_api_url);
let session = client.get_auth_session(&zabbix_api_user, &zabbix_api_password)?;
println!("Authenticated successfully. Session ID: {}", session);
// Prepare parameters for the "httptest.get" method
// This example fetches all web scenarios with their steps.
let request_params = GetWebScenariosParams {
output: "extend".to_string(),
select_steps: "extend".to_string(),
// limit: Some(5), // Example: limit to 5 scenarios
};
println!("\nCalling client.get_webscenarios()...");
match client.get_webscenarios(&session, &request_params) {
Ok(webscenarios) => {
if webscenarios.is_empty() {
println!("No web scenarios found matching the criteria.");
} else {
println!(
"Successfully fetched {} web scenario(s):",
webscenarios.len()
);
for scenario in webscenarios {
println!(
"\n Scenario Name: '{}', Host ID: {}",
scenario.name, scenario.host_id
);
// The ZabbixWebScenario model includes 'steps' Vec<ZabbixWebScenarioStep>
if scenario.steps.is_empty() {
println!(" No steps defined for this scenario.");
} else {
println!(" Steps ({}):", scenario.steps.len());
for step in scenario.steps {
println!(
" - Step No: {}, Name: '{}', URL: '{}', Expected Status Codes: '{}'",
step.no, step.name, step.url, step.status_codes
);
}
}
}
}
}
Err(e) => {
eprintln!("Error fetching web scenarios: {}", e);
return Err(e);
}
}
Ok(())
}