从日志中心获取数据并进行处理和预警

package com.ccservice.train.app.Reptile_mobile_warning.task;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccservice.Util.PropertyUtil;
import com.ccservice.Util.file.WriteLog;
import com.ccservice.Util.string.StringUtil;
import com.ccservice.train.buy_web_train.util.MsgDingDingUtil;

/**
 * 查询排队的代理IP
 * 
 * @author Administrator
 *
 */
public class CheckProxyIPOfFellIn {
	public static Map<String, Integer> ipCountMap = new HashMap<String, Integer>();// 每个代理ip的计数
	public static String collectedIPs = "";
	public static String collectedIDs = "";
	public static String earlyWarningIp = "";// 预警ip
	private static long cacheTime = Long.parseLong(PropertyUtil.getValue("cacheTime", "warning.properties").toString());//执行搜索日志任务的间隔时间
	private static long warnCacheTime = Long.parseLong(PropertyUtil.getValue("warnCacheTime", "warning.properties").toString());//执行报警任务的间隔时间
	private static Integer delay = Integer.parseInt((PropertyUtil.getValue("delay", "warning.properties").toString()));//开始执行任务延迟时间
	/**
	 * 重复执行收集日志的任务
	 */
	public static void repeatTask(){
		Timer timer = new Timer();
    	TimerTask task = new TimerTask() {
			@Override
			public void run() {
				generateEarlyWarningIP();
			}
		};
		timer.schedule(task, delay,cacheTime);
	}
	
	public static void setValueOfIpCountMap() {
		Date date=new Date();
		Calendar calendar = Calendar.getInstance();
		String toDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
		calendar.setTime(date);
		calendar.set(Calendar.HOUR,
				calendar.get(Calendar.HOUR) - 1);
		String fromDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
		getIP();
		String[] ipArr = collectedIPs.split(",");
		for (int i = 0; i < ipArr.length; i++) {
			String ip = ipArr[i];
			if (!ipCountMap.containsKey(ip)) {
				ipCountMap.put(ip, 1);
			} else {
				// 否则获得c的值并且加1
				ipCountMap.put(ip, ipCountMap.get(ip) + 1);
			}
		}
	}
	/**
	 * 生成报警ip
	 */
	public static void generateEarlyWarningIP() {
		setValueOfIpCountMap();
		for (java.util.Map.Entry<String, Integer> entry : ipCountMap.entrySet()) {
			String key = entry.getKey();
			int value = entry.getValue();
			if (!StringUtil.StringIsNull(value+"")&&value >= 3 && earlyWarningIp.indexOf(key) < 0) {
				earlyWarningIp = earlyWarningIp + key+";";
			}
		}
	}
	/**
	 * 钉钉报警
	 */
	public static void warning(){
		String url_dingding = PropertyUtil.getValue("zhenji_monitoring_url_dingding", "system.properties");
		if(!"".equals(earlyWarningIp)){
			MsgDingDingUtil.sendDingdingMsg_back("排队的代理IP为:"+earlyWarningIp, url_dingding);
		}
		//每天凌晨过后清空当天缓存数据
        if(isBelong()){
        ipCountMap = new HashMap<String, Integer>();// 每个代理ip的计数
    	earlyWarningIp = "";// 预警ip
    	collectedIDs="";
    	collectedIPs="";
        }
	}
	/**
	 * 重复执行报警任务
	 */
	public static void bjTask(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 1); //凌晨1点
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date date=calendar.getTime(); //第一次执行定时任务的时间
		//如果第一次执行定时任务的时间 小于当前的时间
		//此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
		if (date.before(new Date())) {
			date = addDay(date, 1);
		}
		Timer timer = new Timer();
    	TimerTask task = new TimerTask() {
			@Override
			public void run() {
				generateEarlyWarningIP();
				warning();
			}
		};
		//repeatTask();
		timer.schedule(task, 1000,warnCacheTime);
	}
	public static Date addDay(Date date, int num) {
		Calendar startDT = Calendar.getInstance();
		startDT.setTime(date);
		startDT.add(Calendar.DAY_OF_MONTH, num);
		return startDT.getTime();
	}
	public static void getIP(){
		collectedIPs="";
		String dateStr=new SimpleDateFormat("yyyy.MM.dd").format(new Date());
		String ipStr="";
		String uri="http://101.236.47.119:9200/redis-log-"+dateStr
				+ "/doc/_search?pretty";
		try {
			JSONObject obj=ESConfig.post(uri).getJSONObject("hits");
			JSONArray array=obj.getJSONArray("hits");
			for (int i = 0; i < array.size(); i++) {
				JSONObject ob=array.getJSONObject(i);
				String id=ob.getString("_id");
				JSONObject ob1=ob.getJSONObject("_source");
				String biztype=ob1.getString("biztype");
				//收集过的数据不再收集
				if(!"".equals(biztype)&&biztype!=null&&collectedIDs.indexOf(id)<0){
					String logs=ob1.getString("logString");
					collectedIDs+=id+";\r\n";
					//logs=logs.substring(0, logs.indexOf(":"));
					ipStr+=logs+",\r\n";
				}
			}
			ipStr="".equals(ipStr)?"":ipStr.substring(0, ipStr.lastIndexOf(","));
			collectedIPs+=ipStr;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			WriteLog.write("CheckProxyIPOfFellIn.getIP","收集排队的代理IP出错");
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
////		 ipCountMap = new HashMap<String, Integer>();// 每个代理ip的计数
////	    	earlyWarningIp = "";// 预警ip
		generateEarlyWarningIP();
		System.out.println(warnCacheTime);
		System.out.println("id:"+collectedIDs);
		System.out.println("ip:"+collectedIPs);
		System.out.println("排队ip:"+earlyWarningIp);
//		warning();
//		CheckProxyIPOfFellIn.bjTask();
		isBelong();
	}
	public static Boolean isBelong(){

	    SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
	    Date now =null;
	    Date beginTime = null;
	    Date endTime = null;
	    try {
	        now = df.parse(df.format(new Date()));
	        beginTime = df.parse("00:00");
	        endTime = df.parse("02:00");
	    } catch (Exception e) {
	        e.printStackTrace();
	    }

	    Boolean flag = belongCalendar(now, beginTime, endTime);
	   return flag;
	}


	/**
	     * 判断时间是否在时间段内
	     * @param nowTime
	     * @param beginTime
	     * @param endTime
	     * @return
	     */
	    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
	        Calendar date = Calendar.getInstance();
	        date.setTime(nowTime);

	        Calendar begin = Calendar.getInstance();
	        begin.setTime(beginTime);

	        Calendar end = Calendar.getInstance();
	        end.setTime(endTime);

	        if (date.after(begin) && date.before(end)) {
	            return true;
	        } else {
	            return false;
	        }
	    }
}

 

展开阅读全文

没有更多推荐了,返回首页