JAVA访问Zabbix API

Zabbix

一、Zabbix 概述

zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。

zabbix由2部分构成,zabbix server与可选组件zabbix agent。

(1)zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。
(2)zabbix server可以单独监视远程服务器的服务状态;同时也可以与zabbix agent配合,可以轮询zabbix agent主动接收监视数据(agent方式),同时还可被动接收zabbix agent发送的数据(trapping方式)。

二、Zabbix API

Zabbix的功能虽然很强大,能将数据以图表形式展现在Web中,但是,一个监控系统的使用者如果不了解Zabbix,或者其非维护人员需要通过监控了解各个服务器大致运行状况时,Zabbix所提供的界面就不是很友好了。
Zabbix API恰恰解决了这一问题。我们可以从API接口中读取想要了解的数据,用自己的方式展现在Web中。

三、java程序以HTTP方式向API请求数据

zabbix监控中的几个概念:

  1. hostid:每个被监控服务器唯一标识。可以根据ip/hostname获得,具体方法为代码中的getHostIdByIp/getHostIdByHostName。
  2. key:监控项名字,可以在zabbix平台自行查到。例如"system.cpu.util[,idle]","system.cpu.load[all,avg1]","system.swap.size[,free]"等等。
  3. itemid:根据hostid和key唯一缺确定,监控项的唯一标识。具体方法为代码中的getItemId(hostId, key)。
  4. value_type:数据类型,可以通过官方API item.get获取,history.get方法中就是传value_type给history参数。获取这个参数详见我代码中的方法getValueType(hostId, key),使用这个参数参考getHistoryDataByItemId(itemId, valueType, time_from, time_till)。具体参考:
    https://www.zabbix.com/documentation/2.4/manual/api/reference/item/object
    https://www.zabbix.com/documentation/2.4/manual/api/reference/history/get
package test.test;

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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author xueyuan@meizu.com
 * @since 创建时间:2016年8月10日 上午11:43:49
 */
public class ZabbixUtil {

    private static String       URL      = "http://IP:port/api_jsonrpc.php";

    private static String       AUTH     = null;

    private static final String USERNAME = "u";

    private static final String PASSWORD = "p";


    /**
     * 向Zabbix发送Post请求,并返回json格式字符串
     * 
     * @param param 请求参数
     * @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);
            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("auth", null);
        map.put("id", 0);

        String response = sendPost(map);
        JSONObject json = JSON.parseObject(response);
        AUTH = json.getString("result");
    }


    private static String getAuth() {
        if (AUTH == null) {
            setAuth();
        }
        return AUTH;
    }


    /**
     * 获得主机hostid
     * 
     * @param host Zabbix中配置的主机hosts
     * @return 返回hostid @
     */
    private static String getHostIdByHostName(String host) {
        Map<String, Object> filter = new HashMap<String, Object>();
        filter.put("host", host);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("output", "hostid");
        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;
        }

    }


    /**
     * 获得主机hostid
     * 
     * @param host Zabbix中配置的主机hosts
     * @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;
        }

    }


    /**
     * 获得某主机的某个监控项id
     * 
     * @param host 主机
     * @param item 监控项
     * @return 监控项itemid
     * @throws Exception
     */
    private static String getItemId(String hostId, String key) throws Exception {
        JSONArray result = getItemByHostAndKey(hostId, key);
        if (result.size() > 0) {
            JSONObject json = result.getJSONObject(0);
            if (json != null) {
                return json.getString("itemid");
            }
        }
        return null;

    }


    /**
     * 获取某主机某监控项的value_type
     * 
     * @param host
     * @param key
     * @return
     * @throws Exception
     * @author xueyuan@meizu.com
     * @since 创建时间:2016年8月12日 上午10:38:10
     */
    private static int getValueType(String hostId, String key) throws Exception {
        JSONArray result = getItemByHostAndKey(hostId, key);
        if (result.size() > 0) {
            JSONObject json = result.getJSONObject(0);
            if (json != null) {
                return json.getIntValue("value_type");
            }
        }
        return 3;

    }


    /**
     * 获取某主机某监控项在一段时间内的数据
     * 
     * @param itemId
     * @param valueType
     * @param from
     * @param to
     * @return
     * @author xueyuan@meizu.com
     * @since 创建时间:2016年8月18日 上午11:42:48
     */
    public static JSONArray getHistoryDataByItemId(String itemId, int valueType, long from, long to) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("output", "extend");
        params.put("history", valueType);
        params.put("itemids", itemId);
        params.put("sortfield", "clock");
        params.put("sortorder", "DESC");//时间降序
        params.put("time_from", from / 1000);
        params.put("time_till", to / 1000);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jsonrpc", "2.0");
        map.put("method", "history.get");
        map.put("params", params);
        map.put("auth", getAuth());
        map.put("id", 0);

        String response = sendPost(map);
        JSONArray result = JSON.parseObject(response).getJSONArray("result");

        return result;

    }


    /**
     * 获取某主机某监控项最近的数据
     * 
     * @param hostId
     * @param key
     * @return @
     * @author xueyuan@meizu.com
     * @since 创建时间:2016年8月12日 上午10:31:49
     */
    public static JSONArray getItemByHostAndKey(String hostId, String key) {
        if (hostId == null) {
            return null;
        }
        Date start = new Date();

        Map<String, Object> search = new HashMap<String, Object>();
        search.put("key_", key);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("output", "extend");
        params.put("hostids", hostId);
        params.put("search", search);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jsonrpc", "2.0");
        map.put("method", "item.get");
        map.put("params", params);
        map.put("auth", getAuth());
        map.put("id", 0);

        String response = sendPost(map);
        JSONArray result = JSON.parseObject(response).getJSONArray("result");

        return result;
    }


    /**
     * 多个监控项的当前数据
     * 
     * @param itemIds
     * @return
     * @author xueyuan@meizu.com
     * @since 创建时间:2016年8月18日 下午4:33:14
     */
    public static JSONArray getDataByItems(List<String> itemIds) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("output", "extend");
        params.put("itemids", itemIds);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jsonrpc", "2.0");
        map.put("method", "item.get");
        map.put("params", params);
        map.put("auth", getAuth());
        map.put("id", 0);
        String response = sendPost(map);
        JSONArray result = JSON.parseObject(response).getJSONArray("result");

        return result;

    }


    /**
     * 多个监控项的历史数据
     * 
     * @param itemIds
     * @param valueType
     * @param from
     * @param to
     * @return
     * @author xueyuan@meizu.com
     * @since 创建时间:2016年8月18日 下午5:12:53
     */
    public static JSONArray getHistoryDataByItems(List<String> itemIds, int valueType, long from,
                                                  long to) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("output", "extend");
        params.put("history", valueType);
        params.put("itemids", itemIds);
        params.put("sortfield", "clock");
        params.put("sortorder", "DESC");//时间降序
        params.put("time_from", from / 1000);
        params.put("time_till", to / 1000);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jsonrpc", "2.0");
        map.put("method", "history.get");
        map.put("params", params);
        map.put("auth", getAuth());
        map.put("id", 0);
        String response = sendPost(map);
        JSONArray result = JSON.parseObject(response).getJSONArray("result");

        return result;

    }


    public static void main(String[] args) throws Exception {
        setAuth();
        long time_till = new Date().getTime();
        long time_from = time_till - 5 * 60000;
        String ip = "10.1.1.13";
        String key = "system.cpu.util[,idle]";
        String hostId = getHostIdByIp(ip);
        String itemId = getItemId(hostId, key);
        int valueType = getValueType(hostId, key);
        getHistoryDataByItemId(itemId, valueType, time_from, time_till);
    }

}

原文地址:https://www.cnblogs.com/seven7seven/p/4170317.html