Code Monkey home page Code Monkey logo

zabbix-api-rs's People

Contributors

lebe-dev avatar tjyang avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

learnzabbix

zabbix-api-rs's Issues

RFE: Support more Host objects/parameters like psk related objects

@lebe-dev
I am hoping to create the host with PSK enabled on zabbix server side.

  • Existing host objects doesn't support PSK related objects.
    image
  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>,

Doc-tests zabbix-api has error messages.

  • start zabbix-mysql container.
$  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
$

  • Doc-tests zabbix-api has error messages.
$ 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`
$

login demo code in README.md has "unexpected error"

@lebe-dev
Thanks for creating this framework, I am interested to learn use it.

  • dir layout and create test01.
$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

1 directory, 3 files
$
$ cargo install --path . -f

  • error message, I am expected to see error message saying that remote certificate not valid.
$ 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
$
  • Cargo.toml
$ 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"
$
  • main.rs with masked private info.
$ 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")
    }
  }
$

  • user curl to verify login
$ 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
$

  • remote zabbix was able return bearer string after using --insecure to ignore certificate check
$  ~/zbxc-login.sh
{"jsonrpc":"2.0","result":"6b47c2a57d09b187f511d40369xxxxxx","id":1}
$

Please add create_host example

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 ?

  • Searching the real EXAMPLE code
[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]$
  • this my attempt, still have lots of TBC.
// 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)
    }
}

Q: What is syntax for specify attributes for CreateHostReqest ?

@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(),
            };

ERROR zabbix_api::tests::builder] item create error: zabbix api call error

  • latest git commit
[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
  • start up the zabbix+mysql containers.
[me@rocky9t01a zabbix-api-rs]$ docker-compose up -d
  • run the test
[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]$

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.