zabbix 是很强大的监控运维工具,但是无奈界面实在是又丑又不人性化,所以上面的大神决定要包装一下zabbix api获取数据自己画界面(其实还一种方法就是直接访问zabbix 的mysql数据库,但是这种逻辑要自己写了),但是就是苦了我们下面搬砖的码农了。
zabbix api利用json-rpc协议发送一些json数据来获取数据,格式如下
Request
{
"jsonrpc":"2.0"
"method":"user.login", //zabbix api里面提供的方法
"params":{ //方法里面传递的参数
"user":"admin",
"password":"zabbix"
},
"id":1
}
Response
{
"jsonrpc":"2.0",
"result":"0424bd59b807674191e7d77572075f33", //这就是我们需要的滑板鞋了
"id":1
}
看到这里,我们要用java(其他语言也一样)调用zabbix api就很简单了,只需要构建请求发送json字符创就行了,直接贴代码
public String HttpRequest(String params) {
try {
//提供zabbix api的url
String strURL = "http://172.19.0.127/zabbix/frontends/php/api_jsonrpc.php";
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8");
out.append(params);
out.flush();
out.close();
int length = (int) connection.getContentLength();
InputStream is = connection.getInputStream();
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8");
is.close();
return result;
}
} catch (IOException e) {
e.printStackTrace();
}
return "{\"error\":\"failed to connect server\"}";
}
//调用zabbix api必须要经过认证
public String Auth() {
if (auth == null) {
String user = "admin";
String password = "zabbix";
String params = "{\"jsonrpc\":\"2.0\",\"method\":\"user.login\",\"params\":{\"user\":"
+ "\""
+ user
+ "\""
+ ",\"password\":"
+ "\""
+ password
+ "\"" + "},\"auth\":null,\"id\":0}";
auth = new JSONObject(HttpRequest(params)).getString("result");
}
return auth;
}
//构建要发送的json字符创,省的每次都写这么一大堆
public String buildParams(String method, String params) {
String result = "{\"jsonrpc\":\"2.0\",\"method\":\"" + method
+ "\",\"params\":{" + params +"},\"auth\":\"" + Auth()
+ "\",\"id\":0}";
return result;
}
到这,调用zabbix api基本就结束了,剩下就是拼凑参数了,给个例子
//获取item的值
public String getItemValue(String itemId, int limit) {
String params = base.buildParams("history.get",
"\"sortfield\":\"clock\",\"sortorder\":\"DESC\",\"itemids\":\""
+ itemId + "\",\"limit\":" + limit);
return HttpRequest(params);
}
zabbix api详细文档:https://www.zabbix.com/documentation/2.4/manual/api/reference
大家注意自己使用的zabbix版本,不同版本参数很可能不一样(坑爹!)
—————————————————就是想使用分割线试试—————————————————
注意:
如果要创建zabbix 中的caculate 项目,请现在zabbix 中建立caculate要依赖的项目,不然汇报找不到XXX项目的,真是太不智能了:)逃
如果创建的是caculate项目,请务必加上很多转义符号 \ ,如
String key="\"last(\\\"vm.memory.size[used]\\\",0)/last(\\\"vm.memory.size[total]\\\",0)\"";
因为zabbix api接收的是字符串所以。。。。。。。我们发送过去的时候除了头尾的双引号,其他的都是要你转义一下的
还有就是,zabbix好像不支持max(last(xx))这种双重计算(一点都不强大!!)