实时监控DNS状态,并向MQ发送消息

实现:实时监控DNS状态,一旦发现DNS域名故障或DNS域名第一次恢复正常,即向ActiveMQ中发送消息。

监控实体:

package com.syxp.dns.entity;

public class Alarm {

	/**
	 * 告警实体to MQ status: dns状态1正常0故障 instatu: 记录是否入库,1入库,0不入库
	 */

	private int status; // 监控状态 1为正常,0为故障
	private int instatu; // 入库状态 1为需要入库,0为不需要入库
	private String alarmtype; // 告警类型
	private String alarmaddress; // 告警设备地址
	private String alarmcontent; // 告警内容
	private String alarmoccurtime; // 告警发生时间
	private int monitorstatus; // 监控状态
	private String sms; // 短信提醒
	private String phone; // 手机号
	private String alarmdevname; // 告警对象名称

	public String getSms() {
		return sms;
	}

	public void setSms(String sms) {
		this.sms = sms;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getMonitorstatus() {
		return monitorstatus;
	}

	public void setMonitorstatus(int monitorstatus) {
		this.monitorstatus = monitorstatus;
	}

	public String getAlarmtype() {
		return alarmtype;
	}

	public void setAlarmtype(String alarmtype) {
		this.alarmtype = alarmtype;
	}

	public String getAlarmaddress() {
		return alarmaddress;
	}

	public void setAlarmaddress(String alarmaddress) {
		this.alarmaddress = alarmaddress;
	}

	public String getAlarmcontent() {
		return alarmcontent;
	}

	public void setAlarmcontent(String alarmcontent) {
		this.alarmcontent = alarmcontent;
	}

	public String getAlarmoccurtime() {
		return alarmoccurtime;
	}

	public void setAlarmoccurtime(String alarmoccurtime) {
		this.alarmoccurtime = alarmoccurtime;
	}

	public String getAlarmdevname() {
		return alarmdevname;
	}

	public void setAlarmdevname(String alarmdevname) {
		this.alarmdevname = alarmdevname;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getInstatu() {
		return instatu;
	}

	public void setInstatu(int instatu) {
		this.instatu = instatu;
	}
}

 检测DNS并判断状态后向MQ发送消息

package com.syxp.dns.send;

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ResourceBundle;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

import com.syxp.dns.entity.Alarm;

public class SendMessageToMQ {

	private static Alarm alarm;
	private static ResourceBundle rb;
	private static Logger logger = Logger.getLogger(SendMessageToMQ.class);
//private static String subject = "integratedalarm.subject";										//消息队列:QUEUE		
	private String subject = "";
	private static String user = "";
	private static String password = "";
	private String url = "";
	private String phone = "";
//private static String url = "tcp://192.168.1.126:61616";
//private static String phone = "13940572922";
	private static int[] error = { 1, 1, 1 };

	/**
	 * 检测DNS并判断状态后向MQ发送消息
	 * @param alarm
	 * @author 
	 */

	public Alarm alarmOfMq() {
		int i = 1;
		int status = 1; // DNS状态,1正常,0故障
		String alarmdevname = "";
		String alarmadress = "";
		String dom = "";
		//暂定两个DNS域名,循环两次
		for (i = 1; i <= 2; i++) {
			alarm = new Alarm();
			rb = ResourceBundle.getBundle("conf/dnsinfo");
			dom = rb.getString("dom" + i);										// dns1的域名,用于检测dns
			status = this.testDns(dom);
			if (status == 1) {
				alarmdevname = rb.getString("alarmdevname" + i);
				alarmadress = rb.getString("ip" + i);
				alarm.setStatus(1);
				if (error[i] == 1) {
					alarm.setInstatu(0);
					//logger.info("数据采集监控线程正在运行,状态正常");
				} else {
					alarm.setAlarmdevname(alarmdevname);
					alarm.setInstatu(1);
					alarm.setAlarmtype("DNS");
					alarm.setAlarmaddress(alarmadress);
					alarm.setAlarmcontent(dom + "已恢复正常!");
					Calendar cal = Calendar.getInstance();
					SimpleDateFormat df = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");// 设置日期格式
					String time = null;
					time = df.format(cal.getTime());
					alarm.setAlarmoccurtime(time);
					alarm.setMonitorstatus(1);
					phone = rb.getString("phone");
					alarm.setPhone(phone);
					alarm.setSms("【告警时间:"+time+"】" +"【告警类型:DNS】"+alarmdevname+"("+alarmadress+ ")" + alarm.getAlarmcontent());
					error[i] = 1;
					logger.info("检测到DNS域名"+dom+"已恢复正常");
				}
			} else {
				alarmadress = rb.getString("ip" + i);
				alarmdevname = rb.getString("alarmdevname" + i);
				alarm.setAlarmdevname(alarmdevname);
				alarm.setStatus(0);
				alarm.setInstatu(1);
				alarm.setAlarmtype("DNS");
				alarm.setAlarmaddress(alarmadress);
				alarm.setAlarmcontent(dom + "发生故障!");
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				String time = null;
				time = df.format(cal.getTime());
				alarm.setAlarmoccurtime(time);
				alarm.setMonitorstatus(0);
				phone = rb.getString("phone");
				alarm.setPhone(phone);
				if(status==error[i]){
					alarm.setSms("");
				}else{
					alarm.setSms("【告警时间:"+time+"】" +"【告警类型:DNS】"+alarmdevname+"("+alarmadress+ ")" + alarm.getAlarmcontent());
					error[i] = 0;
					logger.info("检测到DNS域名"+dom+"出现异常");
				}
			}
			if (alarm.getInstatu() == 1) {
				url = rb.getString("url");
				// 创建连接工厂
				ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
						user, password, url);
				try {
					// 创建连接
					Connection connection = connectionFactory
							.createConnection();
					connection.start();
					// 创建会话Session
					/**
					 * 参数解释:
					 * 第一个参数是否使用事务:当消息发送者向消息提供者(即消息代理)发送消息时,消息发送者等待消息代理的确认,
					 * 没有回应则抛出异常,消息发送程序负责处理这个错误。 第二个参数消息的确认模式: AUTO_ACKNOWLEDGE
					 * : 指定消息提供者在每次收到消息时自动发送确认。消息只向目标发送一次,但传输过程中可能因为错误而丢失消息。
					 * CLIENT_ACKNOWLEDGE :
					 * 由消息接收者确认收到消息,通过调用消息的acknowledge()方法(会通知消息提供者收到了消息)
					 * DUPS_OK_ACKNOWLEDGE :
					 * 指定消息提供者在消息接收者没有确认发送时重新发送消息(这种确认模式不在乎接收者收到重复的消息)。
					 */
					Session session = connection.createSession(false,
							Session.AUTO_ACKNOWLEDGE);
					// 创建队列/主题
					subject = rb.getString("subject");
					Queue queue = session.createQueue(subject);
					// 创建消息生产者
					MessageProducer messageProducer = session
							.createProducer(queue);
					// 创建消息
					MapMessage message = session.createMapMessage();
					// 构造消息体
					try {
						message.setBytes("ALARMTYPE", alarm.getAlarmtype()
								.getBytes("GB2312"));
						// logger.info("告警类型:"+alarm.getAlarmtype());
						message.setBytes("ALARMADDRESS", alarm
								.getAlarmaddress().getBytes("GB2312"));
						// logger.info("告警设备地址:"+alarm.getAlarmaddress());
						message.setBytes("ALARMCONTENT", alarm
								.getAlarmcontent().getBytes("GB2312"));
						// logger.info("告警内容:"+alarm.getAlarmcontent());
						message.setBytes("ALARMOCCURTIME", alarm
								.getAlarmoccurtime().getBytes("GB2312"));
						// logger.info("告警发生时间:"+alarm.getAlarmoccurtime());
						message.setBytes("MONITORSTATUS",
								String.valueOf(alarm.getMonitorstatus())
										.getBytes("GB2312"));
						// logger.info("监控状态:"+alarm.getMonitorstatus());
						message.setBytes("ALARMDEVNAME", alarm
								.getAlarmdevname().getBytes("GB2312"));
						// logger.info("设备名称:"+alarm.getAlarmdevname());
						message.setBytes("SMS",
								alarm.getSms().getBytes("GB2312"));
						// logger.info("短信提醒:"+alarm.getSms());
						message.setBytes("PHONE",
								alarm.getPhone().getBytes("GB2312"));
						// logger.info("手机号码:"+alarm.getPhone());
					} catch (UnsupportedEncodingException e) {
						logger.error("---------------------------------无法找到字符编码方式GB2312---------------------------");
						e.printStackTrace();
					}
					// 发送消息到MQ
					messageProducer.send(message);
					logger.info("--------------------------------------成功发送消息----------------------------------------");
					// 关闭资源
					session.close();
					connection.close();
				} catch (JMSException e) {
					logger.error("--------------------------------------发送消息失败----------------------------------------");
					e.printStackTrace();
				}
			}
		}
		return alarm;
	}

	/**
	 * 通过域名dom检测dns是否正常
	 * 
	 * @param dom
	 * @return Dns的状态:1——正常formal,0——不正常stopped
	 */
	@SuppressWarnings("unused")
	public int testDns(String dom) {
		int status = 1;
		try {
			InetAddress[] addresses = InetAddress.getAllByName(dom);
			return status;
		} catch (UnknownHostException e) {
			// e.printStackTrace();
			status = 0;
			return status;
		}
	}

	public static void main(String[] args) {
		SendMessageToMQ sendMessageToMQ = new SendMessageToMQ();
		sendMessageToMQ.alarmOfMq();
	}

	public static Alarm getAlarm() {
		return alarm;
	}

	public static void setAlarm(Alarm alarm) {
		SendMessageToMQ.alarm = alarm;
	}

	public static ResourceBundle getRb() {
		return rb;
	}

	public static void setRb(ResourceBundle rb) {
		SendMessageToMQ.rb = rb;
	}

}

配置文件:dnsinfo.properties

subject=integratedalarm.subject
url=tcp://192.168.1.126:61616
phone=13940572922

ip1=192.168.1.12
dom1=www.baidu.com
status1=1
alarmdevname1=\u8BBE\u5907\u4E00

ip2=192.168.1.15
dom2=www.sina.com
status2=1
alarmdevname2=\u8BBE\u5907\u4E8C

线程

package com.syxp.dns.send.thread;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.syxp.dns.send.SendMessageToMQ;


public class SendMessageThread extends Thread{

	private static Logger logger = Logger.getLogger(SendMessageThread.class);
	SendMessageToMQ sendMessageToMQ = new SendMessageToMQ();
	
	/**
	 * 运行线程
	 */
	public void run(){
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");
		logger.info("数据采集监控线程开始运行"+sdf.format(now));
		//死循环,设置线程休眠
		while(true){
			sendMessageToMQ.alarmOfMq();
			try {
				Thread.sleep(60000);
			} catch (InterruptedException e) {
				logger.error("", e);
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 停止线程
	 */
	public void stopT(){
		this.stop();
	}
}

测试方法:

package com.syxp.dns.send.test;

import com.syxp.dns.send.thread.SendMessageThread;


public class SendMessageTest {

	public static void main(String[] args) {
		SendMessageThread sendMessageThread = new SendMessageThread();
		sendMessageThread.start(); 
//		sendMessageThread.stop();
	}
}

相关推荐