RabbitMQ(9)-REST API监控RabbitMQ

无人久伴 提交于 2020-01-07 07:33:54

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

一.介绍

Nagios

Nagios健康检测是一个独立的程序,它在运行时监控服务并在程序终止运行时退出代码来指示服务的健康状态,可以用任何语言来编写,检测程序可以将可读状态打印到stdout上,返回四种整型退出代码:

0----OK:接收检测的服务正常运行

1----WARNING:服务运行遇到问题,但并不紧急,比如内存到了监控阀值或者检测的队列属性错误等

2----CRITICAL:服务关闭无响应,并且超过受监控的临界值

3----UNKONWN:服务的状态或者监控的度量值无法确定

二.实战

1.工具类:

import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQUtils extends ConnectionFactory{
    //定义Nagios的健康状态值
    public static final Integer EXIT_OK=0;
    public static final Integer EXIT_WARNING=1;
    public static final Integer EXIT_CRITICAL=2;
    public static final Integer EXIT_UNKNOWN=3;
    public static ConnectionFactory getFactory(){
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPort(5670);
        factory.setHost("192.168.111.131");
        factory.setUsername("admin");
        factory.setPassword("admin");
        return factory;
    }
}

2.非REST API方式检测:

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.demo.inaction.utils.RabbitMQUtils;

public class AMQPPingCheck {

    public static void main(String[] args) {
        ConnectionFactory factory = RabbitMQUtils.getFactory();
        Connection connection = null;
        try {
            connection = factory.newConnection();
            Channel channel = connection.createChannel();
        } catch (Exception e) {
            System.out.println("Could not connect to "+factory.getHost()+":"+factory.getPort());
            System.exit(RabbitMQUtils.EXIT_CRITICAL);
            e.printStackTrace();
        }
        System.out.println("OK! Connect to "+factory.getHost()+":"+factory.getPort()+" successful ");
        System.exit(RabbitMQUtils.EXIT_OK);
    }
}

3.REST API方式检测:

import com.rabbitmq.demo.inaction.utils.RabbitMQUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import sun.misc.BASE64Encoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class RESTAMQPPingCheck {
    private static final String USERNAME="admin";
    private static final String PASSWORD="admin";
    //%2F代表"/"(默认的vhost)
    private static final String URL="http://192.168.111.128:15672/api/aliveness-test/%2F";
    public static void main(String[] args) throws UnsupportedEncodingException {
        HttpResponse response = null;
        int statusCode = 0;
        try {
            DefaultHttpClient Client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(URL);
            String up = USERNAME+":"+PASSWORD;
            //设置凭证
            String credentials = new BASE64Encoder().encode(up.getBytes("UTF-8"));
            httpGet.setHeader("Authorization","Basic "+credentials);
            response = Client.execute(httpGet);
            //读取响应内容
            BufferedReader breader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder responseString = new StringBuilder();
            String line = null;
            while ((line = breader.readLine()) !=null) {
                responseString.append(line);
            }
            breader.close();
            String repsonseStr =responseString.toString();
            statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode="+statusCode+" repsonseStr =" + repsonseStr);
        } catch (Exception e) {
            System.out.println("Could not connect to "+URL);
            System.exit(RabbitMQUtils.EXIT_CRITICAL);
            e.printStackTrace();
        }
        //响应码大于299要么代表错误,要么就是发送给客户端额外的指令
        if(statusCode>299){
            System.out.println("Critical:Broker not alive:"+statusCode);
            System.exit(RabbitMQUtils.EXIT_CRITICAL);
        }
        System.out.println("OK! Connect to "+URL+" successful ");
        System.exit(RabbitMQUtils.EXIT_OK);
    }
}

4.检测配置文件的修改:

import com.alibaba.fastjson.JSON;
import com.rabbitmq.demo.inaction.utils.RabbitMQUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import sun.misc.BASE64Encoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class RESTTestQueueConfiguration {
    private static final String USERNAME="admin";
    private static final String PASSWORD="admin";
    private static final String QUEUE="myqueue";
    //%2F代表"/"(默认的vhost)
    private static final String URL="http://192.168.111.128:15672/api/queues/%2F/"+QUEUE;
    public static void main(String[] args) throws UnsupportedEncodingException {
        HttpResponse response = null;
        int statusCode = 0;
        String auto_delete = null, repsonseStr=null,durable = null;
        try {
            DefaultHttpClient Client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(URL);
            String up = USERNAME+":"+PASSWORD;
            //设置凭证
            String credentials = new BASE64Encoder().encode(up.getBytes("UTF-8"));
            httpGet.setHeader("Authorization","Basic "+credentials);
            response = Client.execute(httpGet);
            //读取响应内容
            BufferedReader breader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder responseString = new StringBuilder();
            String line = null;
            while ((line = breader.readLine()) !=null) {
                responseString.append(line);
            }
            breader.close();
            repsonseStr = responseString.toString();
            auto_delete = JSON.parseObject(repsonseStr).getString("auto_delete");
            durable = JSON.parseObject(repsonseStr).getString("durable");
            statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode="+statusCode+" repsonseStr =" + repsonseStr);
        } catch (Exception e) {
            System.out.println("Could not connect to "+URL);
            System.exit(RabbitMQUtils.EXIT_CRITICAL);
            e.printStackTrace();
        }
        //队列不存在的情况
        if(statusCode==404){
            System.out.println("Critical:Queue "+QUEUE+" does not exist");
            System.exit(RabbitMQUtils.EXIT_CRITICAL);
        }
        if(statusCode>299){
            System.out.println("Unexpected API error :"+repsonseStr);
            System.exit(RabbitMQUtils.EXIT_UNKNOWN);
        }
        //检测auto_delete或者durable属性不对应则警告
        if(auto_delete!="false"){
            System.out.println("WARN: Queue "+QUEUE+"-auto_delete flag is not false");
            System.exit(RabbitMQUtils.EXIT_WARNING);
        }if(durable!="true"){
            System.out.println("WARN: Queue "+QUEUE+"-durable flag is not true");
            System.exit(RabbitMQUtils.EXIT_WARNING);
        }
        System.out.println("OK! Connect to "+URL+" successful ");
        System.exit(RabbitMQUtils.EXIT_OK);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!