我正在尝试使用Zabbix API将模板导入我的Zabbix服务器,我使用了这个:
fileJSON=\""$(cat template_file)"\"
curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{
"jsonrpc": "2.0",
"method": "configuration.import",
"params": {
"format": "json",
"rules": {
"templates": {
"createMissing": true,
"updateExisting": true
}
},
"source": $fileJSON
},
"auth": "6a977cd94b26b6156698459ac4d0f769",
"id": 1
}' 'http://127.0.0.1/zabbix/api_jsonrpc.php' | jq '.'
这里是输出:
{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":"Invalid JSON. An error occurred on the server while parsing the JSON text."},"id":null}
我只是没有看到任何错误……我试图使用在线解析器网站,但他没有看到任何错误。该变量直接用Zabbix的导出方法填充(在JSON中)。这是文件的内容:
{
"jsonrpc": "2.0",
"result": "{\"zabbix_export\":{\"version\":\"4.4\",\"date\":\"2020-02-18T08:47:31Z\",\"groups\":[{\"name\":\"System\"}],\"templates\":[{\"template\":\"Template Systeme\",\"name\":\"Template Systeme\",\"groups\":[{\"name\":\"System\"}],\"discovery_rules\":[{\"name\":\"D\\u00e9couverte des services\",\"key\":\"service.discovery\",\"delay\":\"30s\"}]}]}}",
"id": 1
}
我认为错误是fileJSON的格式,如何将此变量编码为JSON格式?
您正在使用单引号JSON
提交'{...}'
,这不会导致$fileJSON
的内插-因此您会收到该错误。
改为使用双引号:
curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{
...
然后您还需要引用JSON
的内引号。
还有一种更简单的内插$fileJSON
的方法,像这样使用它:
curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{
"jsonrpc": "2.0",
"method": "configuration.import",
"params": {
"format": "json",
"rules": {
"templates": {
"createMissing": true,
"updateExisting": true
}
},
"source": '"$fileJSON"'
},
"auth": "6a977cd94b26b6156698459ac4d0f769",
"id": 1
}' ...
这种方式将使您省去引号内的麻烦
从template_file
输出的外观来看,result
键不是JSON,而是JSON字符串。您是否尝试过不使用JSON dump
?
source(必填)-字符串-包含配置数据。
您可以找到一个XML源示例:
"source": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><zabbix_export><version> [cut]
您正在将source
呼叫的完整输出作为configuration.export
发送:
{
"jsonrpc": "2.0",
"result": "{\"zabbix_export\":{\"version\":\"4.4\",\"date\":\"2020-02-18T08:47:31Z\",\"groups\":[{\"name\":\"System\"}],\"templates\":[{\"template\":\"Template Systeme\",\"name\":\"Template Systeme\",\"groups\":[{\"name\":\"System\"}],\"discovery_rules\":[{\"name\":\"D\\u00e9couverte des services\",\"key\":\"service.discovery\",\"delay\":\"30s\"}]}]}}",
"id": 1
}
当您只需要发送序列化的字符串时,该字符串应该是"result"
的fileJSON
字段的值:
"source": "{\"zabbix_export\":{\" [cut]