lebe-dev / zabbix-api-rs Goto Github PK
View Code? Open in Web Editor NEWZabbix API for Rust
License: MIT License
Zabbix API for Rust
License: MIT License
@lebe-dev
I am hoping to create the host with PSK enabled on zabbix server side.
pub host: String,
pub groups: Vec<ZabbixHostGroup, Global>,
pub interfaces: Vec<ZabbixHostInterface, Global>,
pub tags: Vec<ZabbixHostTag, Global>,
pub templates: Vec<ZabbixTemplate, Global>,
pub macros: Vec<ZabbixHostMacro, Global>,
pub inventory_mode: u8,
pub inventory: HashMap<String, String, RandomState>,
$ docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}' | egrep 'zabbix|NAMES'
NAMES IMAGE STATUS
zabbix-api-rs-zabbix-web-nginx-mysql-1 zabbix/zabbix-web-nginx-mysql:ubuntu-6.2-latest Up 30 minutes
zabbix-api-rs-zabbix-server-1 zabbix/zabbix-server-mysql:ubuntu-6.2-latest Up 30 minutes
zabbix-api-rs-mysql-server-1 mysql:8.0-oracle Up 30 minutes
$
$ pwd
~learnzabbix/zabbix-api-rs
$ git log -1
commit d5fe2f41030a28459c814c365f958109e93b4d0f (HEAD -> main, origin/main, origin/HEAD)
Author: Eugene Lebedev <[email protected]>
Date: Fri Jan 26 13:58:18 2024 +0300
Add doc for get_hosts method
$ cargo test
Finished test [unoptimized + debuginfo] target(s) in 0.09s
Running unittests src/lib.rs (target/debug/deps/zabbix_api-4e0bec763865bf2f)
running 11 tests
test client::v6::tests::create_host_group_and_host ... ok
test client::v6::tests::get_api_info ... ok
test client::v6::tests::get_host_groups_test ... ok
test client::v6::tests::create_item ... ok
test client::v6::tests::create_web_scenario ... ok
test client::v6::tests::get_hosts_test ... ok
test client::v6::tests::get_triggers_test ... ok
test client::v6::tests::create_trigger ... ok
test client::v6::tests::session_should_be_returned ... ok
test client::v6::tests::get_webscenarios_test ... ok
test client::v6::tests::raw_api_call_test ... ok
test result: ok. 11 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s
Doc-tests zabbix-api
running 1 test
test src/client/v6/mod.rs - client::v6::ZabbixApiV6Client::get_hosts (line 225) ... FAILED
failures:
---- src/client/v6/mod.rs - client::v6::ZabbixApiV6Client::get_hosts (line 225) stdout ----
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/client/v6/mod.rs:248:57
|
4 | fn main() { #[allow(non_snake_case)] fn _doctest_main_src_client_v6_mod_rs_225_0() {
| --------------------------------------------- this function should return `Result` or `Option` to accept `?`
...
26 | let session = client.get_auth_session("Admin", "zabbix")?;
| ^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `FromResidual<Result<Infallible, ZabbixApiError>>` is not implemented for `()`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/client/v6/mod.rs:249:49
|
4 | fn main() { #[allow(non_snake_case)] fn _doctest_main_src_client_v6_mod_rs_225_0() {
| --------------------------------------------- this function should return `Result` or `Option` to accept `?`
...
27 | let hosts = client.get_hosts(&session, &request)?;
| ^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `FromResidual<Result<Infallible, ZabbixApiError>>` is not implemented for `()`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.
Couldn't compile the test.
failures:
src/client/v6/mod.rs - client::v6::ZabbixApiV6Client::get_hosts (line 225)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.33s
error: doctest failed, to rerun pass `--doc`
$
Hi @lebe-dev
I don't know the real difference between MIT and GNU license term.
WHAT: Since zabbix itself is using GNU. Would you consider re-license your work into GNU one
WHY: Better chance in the long run to compete with existing Zabbix API frameworks like zabbix-cli, and official python api ... etc.
@lebe-dev
Thanks for creating this framework, I am interested to learn use it.
$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
└── src
└── main.rs
1 directory, 3 files
$
$ cargo install --path . -f
$ which test01
~/.cargo/bin/test01
$ test01
thread 'main' panicked at src/main.rs:15:9:
unexpected error
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$
$ cat Cargo.toml
[package]
name = "test01"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4.20"
reqwest = { version = "0.11.23", features = ["blocking", "json"] }
zabbix-api = "0.1.0"
$
$ cat src/main.rs
use reqwest::blocking::Client;
use zabbix_api::client::v6::ZabbixApiV6Client;
use zabbix_api::client::ZabbixApiClient;
use log::error;
fn main() {
let http_client = Client::new();
let client = ZabbixApiV6Client::new(http_client, "https://zbbix03.test.com/zabbix/api_jsonrpc.php");
match client.get_auth_session("Admin", "zabbix") {
Ok(session) => println!("session: {session}"),
Err(e) => {
error!("error: {}", e);
panic!("unexpected error")
}
}
$
$ more ~/zbxc-login.sh
curl --request POST \
--insecure \
--url 'https://zabbix03.test.com/zabbix/api_jsonrpc.php' \
--header 'Content-Type: application/json-rpc' \
--data '{"jsonrpc":"2.0","method":"user.login","params":{"username":"Admin","password":"zabbix"},"id":1}'
echo
$
$ ~/zbxc-login.sh
{"jsonrpc":"2.0","result":"6b47c2a57d09b187f511d40369xxxxxx","id":1}
$
Hi @lebe-dev
I was able to follow get_host example code to really get a host's information.
Can you please add create_host example ?
[zabbix-api-rs]$ egrep -r "\/\/\/\s\*\*" src
src/client/mod.rs: /// **Example:**
[me@rocky9t01a zabbix-api-rs]$ egrep -r "\/\/\/\s\*\*Example" src
src/client/mod.rs: /// **Example:**
[zabbix-api-rs]$
// https://www.zabbix.com/documentation/6.0/en/manual/api/reference/host/create
// create requests json using json.
// "jsonrpc": "2.0",
// "method": "host.create",
// "params": {
// "host": "Linux server",
// "interfaces": [
// {
// "type": 1,
// "main": 1,
// "useip": 1,
// "ip": "192.168.3.1",
// "dns": "",
// "port": "10050"
// }
// ],
// "groups": [
// {
// "groupid": "50"
// }
// ],
// "tags": [
// {
// "tag": "Host name",
// "value": "Linux server"
// }
// ],
// "templates": [
// {
// "templateid": "20045"
// }
// ],
// "macros": [
// {
// "macro": "{$USER_ID}",
// "value": "123321"
// },
// {
// "macro": "{$USER_LOCATION}",
// "value": "0:0:0",
// "description": "latitude, longitude and altitude coordinates"
// }
// ],
// "inventory_mode": 0,
// "inventory": {
// "macaddress_a": "01234",
// "macaddress_b": "56768"
// }
// },
// "auth": "038e1d7b1735c6a5436ee9eae095879e",
// "id": 1
//}
#[derive(Serialize)]
struct Host {pub host: Vec<String>,}
struct Interfaces {pub interface: Vec<String>,}
struct Groups {pub groups: Vec<String>,}
struct Tags {pub tags: Vec<String>,}
struct Templates {pub templates: Vec<String>,}
struct Macros {pub macros: Vec<String>,}
struct InventoryMode {pub inventory_mode: Vec<String>,}
struct Inventory {pub inventory: Vec<String>,}
let request = CreateHostRequest {
host: "test01".to_owned(),
interface: Interfaces {vec!["Zabbix server".to_string()]},
groups: Vec<String>,
tags: Vec<String>,
templates: Vec<String>,
macros: Vec<String>,
inventory_mode: 0 ,
inventory: Inventory {vec!["macaddress_a","macaddressb"]}
};
let http_client = ClientBuilder::new()
.danger_accept_invalid_certs(false)
.build()
.unwrap();
let client = ZabbixApiV6Client::new(http_client, DEFAULT_URL);
let session = client.get_auth_session(DEFAULT_ADMIN, DEFAULT_PASSWORD).unwrap();
match client.create_host(&session,&request) {
Ok(hosts) => {
// println!("host.len()= {:?}",hosts.len());
// let host = hosts.first().unwrap();
// // assert_eq!(&host.host,"Zabbix server")
// println!("host: {:?}", host);
}
Err(e) => {
eprintln!("host get error: {}", e);
panic!("{}", e)
}
}
@lebe-dev
Would you please provide an example of passing interface attributes using vec![] ?
let request = CreateHostRequest {
host: host_name,
groups: vec![host_group],
interfaces: vec![
"type",
1,
"main",
1,
"useip",
1,
"ip",
"192.168.3.1",
"dns",
"",
"port",
"10050",
],
tags: vec![],
templates: vec![],
macros: vec![],
inventory_mode: 0,
inventory: Default::default(),
};
[me@rocky9t01a zabbix-api-rs]$ git log -1
commit be332ed7598dac5412952740272230a9f0949efb (HEAD -> main, origin/main, origin/HEAD)
Author: Eugene Lebedev <[email protected]>
Date: Sun Jan 28 10:24:40 2024 +0300
Bump crate version to 0.2.1; Bump deps versions
[me@rocky9t01a zabbix-api-rs]$ docker-compose up -d
[me@rocky9t01a zabbix-api-rs]$ ./run-integration-tests.sh
Finished test [unoptimized + debuginfo] target(s) in 0.09s
Running unittests src/lib.rs (target/debug/deps/zabbix_api-3cf3010a4bb68ef2)
running 12 tests
test client::v6::tests::get_api_info ... ok
test client::v6::tests::create_host_group_and_host ... ok
test client::v6::tests::get_host_groups_test ... ok
test client::v6::tests::create_trigger ... FAILED
test client::v6::tests::create_item ... ok
test client::v6::tests::create_web_scenario ... ok
test client::v6::tests::get_hosts_test ... ok
test client::v6::tests::get_items_test ... ok
test client::v6::tests::get_triggers_test ... ok
test client::v6::tests::session_should_be_returned ... ok
test client::v6::tests::raw_api_call_test ... ok
test client::v6::tests::get_webscenarios_test ... ok
failures:
---- client::v6::tests::create_trigger stdout ----
[2024-01-28T08:44:03Z INFO zabbix_api::client::v6] getting auth session for user 'Admin'..
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] send post request to 'http://localhost:3080/api_jsonrpc.php'
[2024-01-28T08:44:03Z DEBUG reqwest::connect] starting new connection: http://localhost:3080/
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[HTTP RESPONSE]----
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] {"jsonrpc":"2.0","result":"e1da90fc6d41b96971dd4028cf26e20a","id":1}
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[/HTTP RESPONSE]----
[2024-01-28T08:44:03Z INFO zabbix_api::client::v6] auth ok
[2024-01-28T08:44:03Z INFO zabbix_api::client::v6] creating host group '8gIkLuWjBT'..
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] send post request to 'http://localhost:3080/api_jsonrpc.php'
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[HTTP RESPONSE]----
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] {"jsonrpc":"2.0","result":{"groupids":["26"]},"id":1}
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[/HTTP RESPONSE]----
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] [response body]
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] {"jsonrpc":"2.0","result":{"groupids":["26"]},"id":1}
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] [/response body]
[2024-01-28T08:44:03Z INFO zabbix_api::client::v6] host group '8gIkLuWjBT' has been created
[2024-01-28T08:44:03Z INFO zabbix_api::client::v6] creating host 'kAY6LHLQLdpAqmdADGD'..
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] send post request to 'http://localhost:3080/api_jsonrpc.php'
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[HTTP RESPONSE]----
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] {"jsonrpc":"2.0","result":{"hostids":["10563"]},"id":1}
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[/HTTP RESPONSE]----
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] [response body]
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] {"jsonrpc":"2.0","result":{"hostids":["10563"]},"id":1}
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] [/response body]
[2024-01-28T08:44:03Z INFO zabbix_api::client::v6] host 'kAY6LHLQLdpAqmdADGD' has been created
[2024-01-28T08:44:03Z INFO zabbix_api::client::v6] creating item with key 'keyF5ra6' for host id 10563..
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] send post request to 'http://localhost:3080/api_jsonrpc.php'
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[HTTP RESPONSE]----
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] {"jsonrpc":"2.0","error":{"code":-32500,"message":"Application error.","data":"DBEXECUTE_ERROR"},"id":1}
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::post] ---[/HTTP RESPONSE]----
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] [response body]
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] {"jsonrpc":"2.0","error":{"code":-32500,"message":"Application error.","data":"DBEXECUTE_ERROR"},"id":1}
[2024-01-28T08:44:03Z DEBUG zabbix_api::client::v6] [/response body]
[2024-01-28T08:44:03Z ERROR zabbix_api::client::v6] ZabbixError { code: -32500, message: "Application error.", data: "DBEXECUTE_ERROR" }
Caused by: [zabbix error] code -32500, message 'Application error.', data: 'DBEXECUTE_ERROR' [/zabbix error]
[2024-01-28T08:44:03Z ERROR zabbix_api::tests::builder] item create error: zabbix api call error
thread 'client::v6::tests::create_trigger' panicked at src/tests/builder.rs:143:17:
zabbix api call error
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
client::v6::tests::create_trigger
test result: FAILED. 11 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.95s
error: test failed, to rerun pass `--lib`
[me@rocky9t01a zabbix-api-rs]$
https://crates.io/crates/zabbix-api still showing 0.1.0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.