zabbix java api
zabbix官方的api文檔地址:https://www.zabbix.com/documentation/3.0/manual/api
Zabbix功能
概觀
- Zabbix是一個(gè)高度集成的網(wǎng)絡(luò)監(jiān)控解決方案炎滞,在單個(gè)軟件包中提供了多種功能班巩。
數(shù)據(jù)采集
- 可用性和性能檢查
- 支持SNMP(捕獲和輪詢)甚带,IPMI,JMX,VMware監(jiān)控
- 定制檢查
- 以定制的間隔收集所需的數(shù)據(jù)
- 由服務(wù)器/代理和代理執(zhí)行
靈活的閾值定義
- 您可以定義非常靈活的問題閾值跨释,稱為觸發(fā)器,從后端數(shù)據(jù)庫引用值
高度可配置的警報(bào)
- 可以為升級(jí)計(jì)劃,收件人疗绣,媒體類型定制發(fā)送通知
- 使用宏變量可以使通知變得有意義和有用
- 自動(dòng)操作包括遠(yuǎn)程命令
實(shí)時(shí)繪圖
- 使用內(nèi)置的圖形功能立即繪制被監(jiān)視的項(xiàng)目
Web監(jiān)控功能
- Zabbix可以按照網(wǎng)站上模擬鼠標(biāo)點(diǎn)擊的路徑,并檢查功能和響應(yīng)時(shí)間
廣泛的可視化選項(xiàng)
- 能夠創(chuàng)建可以將多個(gè)項(xiàng)目組合成單個(gè)視圖的自定義圖形
- 網(wǎng)絡(luò)地圖
- 自定義屏幕和幻燈片铺韧,以顯示儀表板風(fēng)格的概述
- 報(bào)告
- 監(jiān)控資源的高級(jí)(業(yè)務(wù))視圖
歷史數(shù)據(jù)存儲(chǔ)
- 存儲(chǔ)在數(shù)據(jù)庫中的數(shù)據(jù)
- 可配置歷史
- 內(nèi)置內(nèi)務(wù)程序
輕松配置
- 將監(jiān)控的設(shè)備添加為主機(jī)
- 主機(jī)被拾取用于監(jiān)視多矮,一次在數(shù)據(jù)庫中
- 將模板應(yīng)用于受監(jiān)控設(shè)備
使用模板
- 在模板中分組檢查
- 模板可以繼承其他模板
網(wǎng)絡(luò)發(fā)現(xiàn)
- 自動(dòng)發(fā)現(xiàn)網(wǎng)絡(luò)設(shè)備
- 代理商自動(dòng)注冊
- 發(fā)現(xiàn)文件系統(tǒng),網(wǎng)絡(luò)接口和SNMP OID
快速的Web界面
- PHP中的基于Web的前端
- 可從任何地方訪問
- 你可以點(diǎn)擊你的方式
- 審核日志
Zabbix API
- Zabbix API為Zabbix 提供了可編程接口哈打,用于大規(guī)模操作塔逃,第三方軟件集成和其他目的。
權(quán)限系統(tǒng)
- 安全的用戶認(rèn)證
- 某些用戶可以限于某些視圖
全功能和易于擴(kuò)展的代理
- 部署在監(jiān)測目標(biāo)上
- 可以部署在Linux和Windows上
二進(jìn)制程序
- 寫在C中料仗,用于性能和小內(nèi)存占用
- 容易攜帶
準(zhǔn)備復(fù)雜的環(huán)境
- 通過使用Zabbix代理湾盗,遠(yuǎn)程監(jiān)控變得容易
zabbix最近問題列表
pom.xml
<dependency>
<groupId>io.github.hengyunabc</groupId>
<artifactId>zabbix-api</artifactId>
<version>0.0.1</version>
</dependency>
zabbix獲取最近問題列表
JSONObject jo = new JSONObject();
jo.put("value", 1);
jo.put("priority", new String[]{"2", "3", "4", "5"});
Request request = RequestBuilder.newBuilder().method("trigger.get")
.paramEntry("output", new String[]{"description", "priority", "lastchange"})
.paramEntry("selectHosts", new String[]{"host", "name", "hostid"})
.paramEntry("selectDependencies", "extend")
.paramEntry("expandData", "host")
.paramEntry("skipDependent", "1")
.paramEntry("monitored", "1")
.paramEntry("active", "1")
.paramEntry("expandDescription", "1")
.paramEntry("sortfield", "priority")
.paramEntry("sortorder", "DESC")
.paramEntry("filter", jo)
.build();
zabbix Api
import io.github.hengyunabc.zabbix.api.DefaultZabbixApi;
import io.github.hengyunabc.zabbix.api.Request;
import io.github.hengyunabc.zabbix.api.RequestBuilder;
import io.github.hengyunabc.zabbix.api.ZabbixApi;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* zabbix Api
* @author can
*/
public class ZabbixUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ZabbixUtil.class);
private ZabbixApi zabbixApi;
public ZabbixUtil(String username, String password, String url) throws Exception {
if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(url)){
throw new Exception("ZabbixApi初始化失敗立轧!參數(shù)不全格粪!");
}
login(username, password, url);
}
private ZabbixApi login(String username, String password, String url) throws Exception {
zabbixApi = new DefaultZabbixApi(url);
zabbixApi.init();
boolean login = zabbixApi.login(username, password);
if(!login){
LOGGER.info(username + " login in Zabbix " + (login ? "SUCCESS" : "FALURE") + " !");
}
return zabbixApi;
}
/**
* 獲取zabbix中所以的主機(jī)群組列表
* @return 主機(jī)群組列表json
*/
public String getHostGroupList() throws Exception {
Request request = RequestBuilder.newBuilder().method("hostgroup.get")
.paramEntry("output", "extend")
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
public String getHostList() throws Exception {
Request request = RequestBuilder.newBuilder().method("host.get")
.paramEntry("output", new String[]{"host", "name", "description", "hostid"})
.paramEntry("selectGroups", "extend")
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
/**
* 獲取主機(jī)ID
* @param hostIp
* @return 主機(jī)ID
*/
public String getHostByGroupid(Integer groupid) throws Exception {
Request request = RequestBuilder.newBuilder().method("host.get")
.paramEntry("groupids", groupid)
.paramEntry("output", new String[]{"host", "name", "description", "hostid"})
.paramEntry("selectGroups", "extend")
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
/**
* 獲取zabbix報(bào)警列表
* @param timeFrom 僅返回在給定時(shí)間之后生成的警報(bào)。
* @return
*/
public String getAlertList(Long timeFrom) throws Exception {
Request request = RequestBuilder.newBuilder().method("alert.get")
.paramEntry("output", new String[]{"sendto", "subject", "clock", "message"})
.paramEntry("selectHosts", new String[]{"host"})
.paramEntry("time_from", timeFrom)
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
/**
* 獲取zabbix報(bào)警列表
* @param timeFrom 僅返回在給定時(shí)間之后生成的警報(bào)氛改。
* @return
*/
public String getAlertListByGroupids(Integer groupid, Long timeFrom) throws Exception {
Request request = RequestBuilder.newBuilder().method("alert.get")
.paramEntry("time_from", timeFrom)
.paramEntry("groupids", groupid)
.paramEntry("output", new String[]{"sendto", "subject", "clock", "message","triggerid"})
.paramEntry("selectHosts", new String[]{"host"})
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
/**
* 獲取zabbix最近問題列表
* @return
* @throws Exception
*/
public String getTriggerInfoList() throws Exception {
JSONObject jo = new JSONObject();
jo.put("value", 1);
jo.put("priority", new String[]{"2", "3", "4", "5"});
Request request = RequestBuilder.newBuilder().method("trigger.get")
.paramEntry("output", new String[]{"description", "priority", "lastchange"})
.paramEntry("selectHosts", new String[]{"host", "name", "hostid"})
.paramEntry("selectDependencies", "extend")
.paramEntry("expandData", "host")
.paramEntry("skipDependent", "1")
.paramEntry("monitored", "1")
.paramEntry("active", "1")
.paramEntry("expandDescription", "1")
.paramEntry("sortfield", "priority")
.paramEntry("sortorder", "DESC")
.paramEntry("filter", jo)
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
/**
* 根據(jù)群組id和機(jī)器host獲取觸發(fā)器信息列表
* @param groupid
* @param host
* @return
* @throws Exception
*/
public String getTrigger(Integer groupid, String host) throws Exception {
Request request = RequestBuilder.newBuilder().method("trigger.get")
.paramEntry("groupids", groupid)
.paramEntry("host", host)
.paramEntry("monitored", 1)
.paramEntry("output", new String[]{"expression","description", "priority", "lastchange","status"})
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
/**
* 根據(jù)觸發(fā)器id獲取觸發(fā)器信息
* @param triggerId
* @return
* @throws Exception
*/
public String getTriggerByTriggerId(Integer triggerId) throws Exception {
Request request = RequestBuilder.newBuilder().method("trigger.get")
.paramEntry("triggerids", triggerId)
.paramEntry("output", "extend")
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
public String getItemList() throws Exception {
Request request = RequestBuilder.newBuilder().method("item.get").paramEntry("output", "extend").paramEntry("monitored", "true").build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
public String getTriggerPrototypeByGroupid(Integer groupid) throws Exception {
Request request = RequestBuilder.newBuilder().method("triggerprototype.get")
.paramEntry("groupids", groupid)
.paramEntry("selectHosts", new String[]{"host", "hostid"})
.paramEntry("selectGroups", "extend")
.paramEntry("output", new String[]{"expression", "triggerid", "description", "priority", "status"})
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
public String getTriggerPrototypeByTriggerids(Integer triggerid) throws Exception {
Request request = RequestBuilder.newBuilder().method("triggerprototype.get")
.paramEntry("triggerids", triggerid)
.paramEntry("output", "extend")
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
public String getTriggerInfo(Integer groupid,Long lastChangeSince) throws Exception {
Request request = RequestBuilder.newBuilder().method("trigger.get")
.paramEntry("groupids", groupid)
.paramEntry("lastChangeSince", lastChangeSince)
.paramEntry("output", new String[]{"description", "priority", "lastchange"})
.paramEntry("selectHosts", new String[]{"host", "name", "hostid"})
.paramEntry("skipDependent", "1")
.paramEntry("monitored", "1")
.paramEntry("active", "1")
.paramEntry("expandDescription", "1")
.paramEntry("sortfield", "priority")
.build();
JSONObject response = zabbixRequest(request);
zabbixError(response);
JSONArray result = response.getJSONArray("result");
return result.toJSONString();
}
private JSONObject zabbixRequest(Request request) throws Exception {
JSONObject response = zabbixApi.call(request);
return response;
}
private void zabbixError(JSONObject response) throws Exception {
if (!StringUtils.isBlank(response.getString("error")))
throw new Exception("向Zabbix請(qǐng)求出錯(cuò)了帐萎!" + JSON.parseObject(response.getString("error")).getString("data"));
}