Java tutorial
/* Copyright (C) 2012 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.systemlog.bean; import java.io.Serializable; import java.text.MessageFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import java.util.Locale; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.maintenance.util.HinemosPropertyUtil; import com.clustercontrol.util.HinemosTime; /** * syslog??? */ public class SyslogMessage implements Serializable { private static final long serialVersionUID = 1L; // ?() private static final int SYSLOG_DEFAULT_PERIOD_HOUR = 24; // ? private static final int SYSLOG_FORMAT_PRIORITY = 0; // ? private static final int SYSLOG_FORMAT_MONTH = 1; // ? private static final int SYSLOG_FORMAT_DAY = 2; // ? private static final int SYSLOG_FORMAT_HH = 3; // ? private static final int SYSLOG_FORMAT_MM = 4; // ? private static final int SYSLOG_FORMAT_SS = 5; // ? private static final int SYSLOG_FORMAT_HOSTNAME = 6; // ? private static final int SYSLOG_FORMAT_MESSAGE = 7; // ?? private static final int LEAPYEAR_CHECK_COUNT = 7; private static Log log = LogFactory.getLog(SyslogMessage.class); public Facility facility; public Severity severity; public long date; public String hostname; public String message; public String rawSyslog; public enum Facility { KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP, NTP, SECURITY, CONSOLE, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7 }; public enum Severity { EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG }; public SyslogMessage() { } public SyslogMessage(Facility facility, Severity severity, long date, String hostname, String message, String rawSyslog) { this.facility = facility; this.severity = severity; this.date = date; this.hostname = hostname; this.message = message; this.rawSyslog = rawSyslog; } /** * syslog????????SyslogMessage????API<br> * <br> * <b>???</b><br> * 1. ???????????????<br> * 2. ???????????<br> * @param syslog syslog? * @return SyslogMessage * @throws ParseException syslog??????????? * @throws HinemosUnknown */ public static SyslogMessage parse(String syslog) throws ParseException, HinemosUnknown { if (log.isDebugEnabled()) { log.debug("parsing syslog : " + syslog); } // [0]:, [1]:(MMM), [2]:(dd), [3]:(HH), [4]:(mm), [5]:(ss), [6]:??, [7]: // ?{1,date,MMM dd HH:mm:ss}????????????1970???? // ????????2/29???3/1?????(1970???????) MessageFormat syslogFormat = new MessageFormat("<{0,number,integer}>{1} {2} {3}:{4}:{5} {6} {7}", Locale.ENGLISH); // ?????? Object[] syslogArgs = syslogFormat.parse(syslog); // RFC3164?????????????????? if ("".equals(syslogArgs[SYSLOG_FORMAT_DAY])) { syslogFormat = new MessageFormat("<{0,number,integer}>{1} {2} {3}:{4}:{5} {6} {7}", Locale.ENGLISH); syslogArgs = syslogFormat.parse(syslog); } if (syslogArgs == null) throw new HinemosUnknown("different syslog pattern"); if (log.isDebugEnabled()) { int i = 0; for (Object arg : syslogArgs) { log.debug(String.format("syslog args [%d] : %s", i++, arg.toString())); } } // ???????(H) Integer syslogEffectiveTime = HinemosPropertyUtil .getHinemosPropertyNum("monitor.systemlog.period.hour", Long.valueOf(SYSLOG_DEFAULT_PERIOD_HOUR)) .intValue(); // 0??????????? if (syslogEffectiveTime <= 0) { syslogEffectiveTime = SYSLOG_DEFAULT_PERIOD_HOUR; } Calendar nowCal = HinemosTime.getCalendarInstance(); // ??(?) int year = nowCal.get(Calendar.YEAR) + 1; int month = editCalendarMonth((String) syslogArgs[SYSLOG_FORMAT_MONTH]); int dayOfMonth = Integer.parseInt((String) syslogArgs[SYSLOG_FORMAT_DAY]); int hourOfDay = Integer.parseInt((String) syslogArgs[SYSLOG_FORMAT_HH]); int minute = Integer.parseInt((String) syslogArgs[SYSLOG_FORMAT_MM]); int second = Integer.parseInt((String) syslogArgs[SYSLOG_FORMAT_SS]); // ? List<Calendar> checkCalList = new ArrayList<Calendar>(); // (?? Calendar syslogEffectiveCal = (Calendar) nowCal.clone(); syslogEffectiveCal.add(Calendar.HOUR, syslogEffectiveTime); // ??? for (int i = 0; checkCalList.size() < 2; i++) { // ?????? if (LEAPYEAR_CHECK_COUNT <= i) { break; } Calendar syslogCheckCal = new GregorianCalendar(year, month, dayOfMonth, hourOfDay, minute, second); year--; // ???????(??) if (syslogEffectiveCal.compareTo(syslogCheckCal) < 0) { continue; } // ?????????????? if (dayOfMonth != syslogCheckCal.get(Calendar.DAY_OF_MONTH)) { continue; } // ???? checkCalList.add(syslogCheckCal); } Calendar editSyslogCal = null; // ????? if (checkCalList.size() > 0) { long absMinMillis = Long.MAX_VALUE; for (int i = 0; i < checkCalList.size(); i++) { long absDiff = Math.abs(checkCalList.get(i).getTimeInMillis() - nowCal.getTimeInMillis()); if (absDiff < absMinMillis) { // ?????? absMinMillis = absDiff; editSyslogCal = checkCalList.get(i); } } } else { // ?????? editSyslogCal = new GregorianCalendar(nowCal.get(Calendar.YEAR), month, dayOfMonth, hourOfDay, minute, second); log.warn("System log date is invalid : " + syslog); } int pri = ((Long) syslogArgs[SYSLOG_FORMAT_PRIORITY]).intValue(); String hostname = (String) syslogArgs[SYSLOG_FORMAT_HOSTNAME]; String msg = (String) syslogArgs[SYSLOG_FORMAT_MESSAGE]; Date date = editSyslogCal.getTime(); // ?? SyslogMessage instance = new SyslogMessage(getFacility((int) pri), getSeverity((int) pri), date.getTime(), hostname, msg, syslog); if (log.isDebugEnabled()) { log.debug("parsed syslog : " + instance); } return instance; } /** * ?? * ?(String)Calendar?(int)??? * * @param month * @return Calendar? */ private static int editCalendarMonth(String month) { switch (month.toUpperCase()) { case "JAN": return Calendar.JANUARY; case "FEB": return Calendar.FEBRUARY; case "MAR": return Calendar.MARCH; case "APR": return Calendar.APRIL; case "MAY": return Calendar.MAY; case "JUN": return Calendar.JUNE; case "JUL": return Calendar.JULY; case "AUG": return Calendar.AUGUST; case "SEP": return Calendar.SEPTEMBER; case "OCT": return Calendar.OCTOBER; case "NOV": return Calendar.NOVEMBER; case "DEC": return Calendar.DECEMBER; } return Calendar.UNDECIMBER; } @Override public String toString() { return String.format("%s [facility = %s, severity = %s, date = %s, hostname = %s, message = %s]", this.getClass().getSimpleName(), facility, severity, new Date(date), hostname, message); } /** * syslog?<PRI>??Facility?API * @param pri syslog?<PRI>? * @return Facility */ public static Facility getFacility(int pri) { int facility = (pri & 0xFFFFFFF8) >> 3; switch (facility) { case 0: return Facility.KERN; case 1: return Facility.USER; case 2: return Facility.MAIL; case 3: return Facility.DAEMON; case 4: return Facility.AUTH; case 5: return Facility.SYSLOG; case 6: return Facility.LPR; case 7: return Facility.NEWS; case 8: return Facility.UUCP; case 9: return Facility.CRON; case 10: return Facility.AUTHPRIV; case 11: return Facility.FTP; case 12: return Facility.NTP; case 13: return Facility.SECURITY; case 14: return Facility.CONSOLE; case 16: return Facility.LOCAL0; case 17: return Facility.LOCAL1; case 18: return Facility.LOCAL2; case 19: return Facility.LOCAL3; case 20: return Facility.LOCAL4; case 21: return Facility.LOCAL5; case 22: return Facility.LOCAL6; case 23: return Facility.LOCAL7; } return null; } /** * syslog?<PRI>??Severity?API * @param pri syslog?<PRI>? * @return Severity */ public static Severity getSeverity(int pri) { int severity = (pri & 0x00000007); switch (severity) { case 0: return Severity.EMERG; case 1: return Severity.ALERT; case 2: return Severity.CRIT; case 3: return Severity.ERR; case 4: return Severity.WARNING; case 5: return Severity.NOTICE; case 6: return Severity.INFO; case 7: return Severity.DEBUG; } return null; } public static void main(String args[]) throws Exception { String syslog1 = "<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8"; System.out.println(SyslogMessage.parse(syslog1)); } }