下面例子我们通过调用Zabbix的API获取到我们Zabbix所产生的告警信息。
Zabbix Api官方手册:https://www.zabbix.com/documentation/3.0/en/manual/api
要想获取到Zabbix上的其他信息,可以参考我们的Zabbix Api进行配置!
导入相关maven
复制代码
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5-20081211</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
复制代码
实现类
复制代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zabbix.dao.ZabbixDao;
import com.zabbix.dao.ZabbixDaoImpl;
import com.zabbix.utils.EditTool;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
/**
* @author
* @since
*/
public class ZabbixUtil {
private static String URL = "http://192.168.111.131/zabbix/api_jsonrpc.php"; //Zabbix访问地址
private static String AUTH = null;
private static final String USERNAME = "Admin"; //登录Zabbix的用户名
private static final String PASSWORD = "zabbix"; //密码
/**
* 向Zabbix发送Post请求,并返回json格式字符串
*
* @return
* @throws Exception
*/
private static String sendPost(Map map) {
String param = JSON.toJSONString(map);
HttpURLConnection connection = null;
DataOutputStream out = null;
BufferedReader reader = null;
StringBuffer sb = null;
try {
//创建连接
URL url = new URL(URL);
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();
//POST请求
out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(param);
//中文用户名需设置UTF-8,out.write(param.getBytes("UTF-8"));
out.flush();
//读取响应
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return sb.toString();
}
/**
* 通过用户名和密码设置AUTH,获得权限
*/
private static void setAuth() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("user", USERNAME);
params.put("password", PASSWORD);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "user.login");
map.put("params", params);
map.put("id", 0);
map.put("auth", null);
String response = sendPost(map);
JSONObject json = JSON.parseObject(response);
AUTH = json.getString("result");
}
private static String getAuth() {
if (AUTH == null) {
setAuth();
}
return AUTH;
}
/**
* 通过IP获得主机hostid
*
* @return 返回hostid @
*/
private static String getHostIdByIp(String ip) {
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("ip", ip);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "host.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
String hostId = json.getString("hostid");
return hostId;
} else {
return null;
}
}
/**
* 通过IP获取主机名称
*
* @return 返回hostname @
*/
private static String getHostname(String ip) {
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("ip", ip);
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("filter", filter);
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "host.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 0);
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
JSONObject json = result.getJSONObject(0);
String hostname = json.getString("host");
return hostname;
} else {
return null;
}
}
/**
* 通过主机IP获取告警信息
*
* @return 返回mailMap @
*/
private static List actionById(String ip) {
List list=new ArrayList();
Map<String, Object> params = new HashMap<String, Object>();
params.put("output", "extend");
params.put("hostids", getHostIdByIp(ip));
params.put("selectFunctions","extend");
Map<String, Object> map = new HashMap<String, Object>();
map.put("jsonrpc", "2.0");
map.put("method", "trigger.get");
map.put("params", params);
map.put("auth", getAuth());
map.put("id", 1);
JSONObject json = null;
String response = sendPost(map);
JSONArray result = JSON.parseObject(response).getJSONArray("result");
if (result.size() > 0) {
for (int i = 0; i < result.size(); i++) {
json = result.getJSONObject(i);
Map warnMap=new HashMap();
warnMap.put("description",json.getString("description"));
warnMap.put("value",json.getString("value"));
warnMap.put("priority",json.getString("priority"));
list.add(warnMap);
}
return list;
} else {
return null;
}
}
@Override
public void run(){
setAuth();
String ip = "192.168.111.130"; //要获取信息的主机IP地址
//获取数据库接口
ZabbixDao dBtest = new ZabbixDaoImpl();
//获取主机名称
String hostName = getHostname(ip);
//通过主机id获取告警信息
List list = actionById(ip);
//获取告警具体某字段信息
if(list != null && !list.isEmpty()){
for(int i=0;i<list.size();i++){
Map warnMap=(Map)list.get(i);
String warn_content= String.valueOf(warnMap.get("description"));
String warn_type= String.valueOf(warnMap.get("priority"));
String warn_status= String.valueOf(warnMap.get("value"));
}
}
}
}
复制代码
分享到:
相关推荐
Java调用zabbix API获取监控信息相关jar包,包括zabbix4j-0.1.jar、httpclient-4.3.1.jar、httpcore-4.3.jar、lombok.jar、slf4j-api-1.6.1.jar、slf4j-log4j12-1.6.1.jar、commons-codec-1.6.jar、commons-logging-...
Java-gateway通过配置文件中START_POLLERS参数设置启动多个线程,在zabbix服务器这边如果一个连接所用时间超过Timeout,将会被中断,但是Java-gateway将继续从JMXcounter取数据。所以 StartJavaPollers设置的值要...
zabbix自动发现JAVA进程,并进行自动添加监控JAVA 进程的JVM数据,统计分析告警监控。 支持多种中间件,tomcat、weblogic、微服务方式部署JAVA应用监控。 自带发现脚本和监控脚本,以及zabbix导入模板。
java中zabbix常用参数与指令_.docx
zabbix-java-gateway.tar.zip
zabbix, Go包提供对 Zabbix API的访问 zabbix 这个包提供了对 Zabbix API的访问。 在 Zabbix 2.0,2.2,2.4上测试。安装:go get github.com/AlekSi/zabbix在使用这个包之前,你不能运行测试- Zabb
深信服行为管理是一款强大的网络安全管理系统,为了更好地监控和管理深信服行为管理设备,使用Zabbix提供的监控模板是一个很好的选择。 本文将详细介绍如何配置和使用Zabbix模板来监控深信服行为管理。以下是文章的...
zabbix api java源码,适合zabbix 二次开发人员
abbix自动发现linux系统开启端口,并进行自动添加监控端口与进程,统计分析告警监控。 支持多种业务端口监控以及应用使用情况监控。 自带发现脚本和监控脚本,以及zabbix导入模板。
配置zabbix与oracle连接的配置
监控原始Java Zabbix Java网关 描述 这是Zabbix Java Gateway的源代码,已更新为允许 jmx.discovery项(使用Mbeans自动发现) JBoss JMX Weblogic T3协议 它是如何工作的 您必须在JMX项目中指定一个用户名,例如“ ...
Zabbix Java 客户端 该项目是 Zabbix API 的 Java 客户端。 zabbix java 客户端使用 jsonrpc4j ( ) 库。 Maven 本项目使用 [Maven] 构建,并使用 Java 8 编译。 在<dependencies> : <!-- zabbix java client ...
zabbix_get是zabbix获取监控项的可执行文件,通过指定监控的机器以及端口、监控key,可以获取相应的监控value。 我们可以通过java-proxy对java进行jmx监控,获取java-proxy的监控项比较特殊,所以需要具体的脚本。
该项目是Zabbix监视系统的数据收集器客户端。 它可以下载监视值列表,并将其发送回zabbix服务器。
zabbix3.0的Lenovo XCC 监控脚本,需要开启SNMP协议,定义COMMUNITY,自己设置一下
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
这是关于java中zabbix常用命令的参数的描述,很适合初学java的人使用。
Zabbix通过JMX监控java中间件.docx
zabbix教程:zabbix从放弃到入门(1):zabbix概念 zabbix教程:zabbix从放弃到入门(2):安装zabbix zabbix教程:zabbix从放弃到入门(3):在zabbix中添加主机 zabbix教程:zabbix从放弃到入门(4):在zabbix中...
zabbix内部培训资料.ppt Zabbix简介及安装 Zabbix架构介绍 功能流程介绍,包括 自动发现,Mysql数据库监控,拓扑图,触发器等 zabbix内部培训资料.ppt Zabbix简介及安装 Zabbix架构介绍 功能流程介绍,包括 自动...