【问题标题】:How to passing parameter from json-rpc to the API function in zabbix?如何将参数从 json-rpc 传递给 zabbix 中的 API 函数?
【发布时间】:2019-07-23 03:01:31
【问题描述】:

我正在编写如下的zabbix监控API。

{
    "jsonrpc": "2.0",
    "method": "data.delete",
    "params":{
            "startTimestamp": "1560725284",
            "endTimestamp": "1560725644",
            "deleteType": "0"
        },
    "auth": "6a810d97a79ef6d31ddde7bf93103299",
    "id": 1
}

我创建了 CData.php api 服务并编写了函数 delete() 如下:

    public function delete($startTimestamp, $endTimestamp, $deleteType) {

        $from = zbx_toArray($startTimestamp);

        $to = zbx_toArray($endTimestamp);

        $type = zbx_toArray($deleteType);

        // delete action conditions
        $sql = 'delete from history where clock >='.$from.' and clock <= '.$to;
        DBexecute($sql);

        return ['success' => '200', 'message' => 'Đã xóa thành công', 'startTimestamp' => $from, 'endTimestamp' => $to, 'deleteType' =>$type];
    }

当我以 get 方法调用 API 时出现问题: CLocalApiClient.php 第 123 行 $result = call_user_func_array([$this->serviceFactory->getObject($api), $method], [$params]);

和函数 CLocalApiClient->callMethod( ???, ???, ???, ??? )

CData->delete(???, ???, ???)上的CData服务错误

当我尝试以下代码时,它运行良好。

    public function delete() {

        $from = "1563229215";
        $to = "1563229275";
        $type = "0";

        // delete action conditions
        $sql = 'delete from history where clock >='.$from.' and clock <= '.$to;
        DBexecute($sql);

        return ['success' => '200', 'message' => 'Đã xóa thành công', 'startTimestamp' => $from, 'endTimestamp' => $to, 'deleteType' =>$type];
    }

请帮助我如何将参数传递给 api 函数?

非常感谢, BienHV

【问题讨论】:

    标签: php zabbix


    【解决方案1】:

    我找到了这个问题的解决方案。我将删除功能更改如下。

    public function delete(array $data) {
    
        $from = $data['startTimestamp'];
    
        $to = $data['endTimestamp'];
    
        $type = $data['deleteType'];
    
        // delete action conditions
        $deleteHistory = 'delete from history where clock >='.$from.' and clock <= '.$to;
    
        if (!DBexecute($deleteHistory)) {
            self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot delete history');
        }
    
        return ['success' => '200', 'message' => 'Delete success', 'startTimestamp' => $startTimestamp, 'endTimestamp' => $to, 'deleteType' =>$type];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2011-11-18
      • 2015-05-21
      相关资源
      最近更新 更多