SyslogAction.java 27.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
package com.sitech.ismp.ice.syslogd;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;

import com.sitech.database.ConnectionOraclePool;
import com.sitech.ismp.ice.net.SimpleSMTP;
import com.sitech.ismp.ice.syslog.SyslogDefs;
import com.sitech.ismp.ice.util.ClassUtilities;
import com.sitech.ismp.ice.util.StringUtilities;
import com.sitech.util.ShellExecutor;

public class SyslogAction implements SyslogActionInterface {
	public static final String RCS_ID = "$Id: SyslogAction.java,v 1.1.1.1 1998/02/22 05:47:54 time Exp $";
	public static final String RCS_REV = "$Revision: 1.1.1.1 $";
	public static final String RCS_NAME = "$Name:  $";

	// TYPES
	public static final int FILE = 1;
	public static final int DISPLAY = 2;
	public static final int FORWARD = 3;
	public static final int SOUND = 4;
	public static final int SYSTEM = 5;
	public static final int CLASS = 6;
	public static final int EMAIL = 7;

	private int type;
	private boolean isOpen;
	private boolean useThread;
	private String[] parameters;

	private PrintWriter writer;

	private int forwardPort;
	private InetAddress forwardAddr;
	private DatagramSocket forwardSocket;

	private SyslogActionInterface action;
	private SyslogDisplayInterface display;

	private static Hashtable storeHostNetInfoTab;
	private static Hashtable matchRoleTable;
	private boolean is_swap = false;
	private String DEVICE_ID = "";
	
	//zhangdj add
	DecideSaveTableManager saveManager = null;

	public SyslogAction(String typeStr, String[] parameters) {
		this(SyslogAction.getActionType(typeStr), parameters);
	}

	public SyslogAction(int type, String[] parameters) {
		this.type = type;
		this.isOpen = false;
		this.useThread = false;
		this.parameters = parameters;

		this.writer = null;
		this.forwardAddr = null;
	}

	public boolean isOpen() {
		return this.isOpen;
	}

	public boolean isThreaded() {
		return this.useThread;
	}

	public void setParameters(String[] parameters) {
		this.parameters = parameters;
	}

	public void openAction() {
		this.isOpen = true;

		switch (this.type) {
		case SyslogAction.FILE:
			System.out.println("zhangdj print syslog is :" + SyslogAction.FILE
					+ "start initial");
			/**
			 * zhangdj add 2010-10-22 * ��ѯ��ݿ⣬��ȡ�������������豸��Ϣ��
			 * ֻ�ڳ�����ĵ�һ�ν��в�ѯ�����ں�4�ij��������в��ٲ�ѯ��ݿ�
			 */
			if (this.storeHostNetInfoTab == null) {
				this.storeHostNetInfoTab = new Hashtable();
				if (this.matchRoleTable == null)
					this.matchRoleTable = new Hashtable();

				// ����ݿ���в�ѯ����
				System.out.println("select v_asset_all table info !");
				String SQL = "select unit_id,kbp_class,device_id,device_name,ip_addr from v_asset_all where ip_addr is not null";
				java.sql.Statement oracleStatement = null;
				java.sql.ResultSet rsOracle = null;
				java.sql.Connection con = null;
				try {

					ConnectionOraclePool conPool = ConnectionOraclePool
							.getInstance();
					con = conPool.createConnection();
					oracleStatement = con.createStatement();
					boolean resultsOracle = true;
					oracleStatement.execute(SQL);

					if (resultsOracle) {
						rsOracle = oracleStatement.getResultSet();
						while (rsOracle.next()) {
							/*
							 * System.out.println("ip address :" +
							 * rsOracle.getString("IP_ADDR"));
							 */
							Map resultMap = new HashMap();
							resultMap.put("UNIT_ID", rsOracle
									.getString("UNIT_ID"));
							resultMap.put("KBP_CLASS", rsOracle
									.getString("KBP_CLASS"));
							resultMap.put("DEVICE_ID", rsOracle
									.getString("DEVICE_ID"));
							resultMap.put("DEVICE_NAME", rsOracle
									.getString("DEVICE_NAME"));
							resultMap.put("IP_ADDR", rsOracle
									.getString("IP_ADDR"));
							this.storeHostNetInfoTab.put(rsOracle
									.getString("DEVICE_ID"), resultMap);
						}

					}
					/**
					 * HOST_IP VARCHAR2(18 BYTE), ALERT NUMBER(10), CRITICAL
					 * NUMBER(10), ERROR NUMBER(10), WARNING NUMBER(10), NOTICE
					 * NUMBER(10), INFO NUMBER(10), DEBUG NUMBER(10), IS_SWAP
					 * NUMBER(10)
					 */
					SQL = "select HOST_IP,DEVICE_ID,EMERGENCY,ALERT,CRITICAL,ERROR,WARNING,NOTICE,INFO,DEBUG,IS_SWAP from tb_cfg_syslog";
					oracleStatement.execute(SQL);
					if (resultsOracle) {
						rsOracle = oracleStatement.getResultSet();
						while (rsOracle.next()) {
							System.out.println("ip address :"
									+ rsOracle.getString("HOST_IP"));
							Map resultMap = new HashMap();
							resultMap.put("HOST_IP", rsOracle
									.getString("HOST_IP"));
							resultMap.put("DEVICE_ID", rsOracle
									.getString("DEVICE_ID"));
							resultMap.put("EMERGENCY", rsOracle
									.getString("EMERGENCY"));
							resultMap.put("ALERT", rsOracle.getString("ALERT"));
							resultMap.put("CRITICAL", rsOracle
									.getString("CRITICAL"));
							resultMap.put("ERROR", rsOracle.getString("ERROR"));
							resultMap.put("WARNING", rsOracle
									.getString("WARNING"));
							resultMap.put("NOTICE", rsOracle
									.getString("NOTICE"));
							resultMap.put("INFO", rsOracle.getString("INFO"));
							resultMap.put("DEBUG", rsOracle.getString("DEBUG"));
							resultMap.put("IS_SWAP", rsOracle
									.getString("IS_SWAP"));
							this.matchRoleTable.put(rsOracle
									.getString("HOST_IP"), resultMap);
						}

					}
					rsOracle.close();
				} catch (SQLException e) {
					System.out.println("SQLException has display :" + e);
				}
				try {
					oracleStatement.close();
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}
			this.useThread = false;
			try {
				this.writer = new PrintWriter(new FileWriter(
						this.parameters[0], true));
			} catch (IOException ex) {
				this.isOpen = false;
				this.writer = null;
				System.err.println("ERROR opening file writer for '"
						+ this.parameters[0] + "'\n\t" + ex.getMessage());
			}
			break;

		case SyslogAction.FORWARD:
			this.useThread = false;
			this.openForwardAction();
			break;

		case SyslogAction.EMAIL:
			this.useThread = true;
			break;

		case SyslogAction.CLASS:
			this.useThread = true;
			this.openClassAction();
			break;

		case SyslogAction.SYSTEM:
			this.useThread = true;
			if (false)
				for (int i = 0; i < this.parameters.length; ++i)
					System.err.println("SYSTEM argv[" + i + "] '"
							+ this.parameters[i] + "'");
			break;

		case SyslogAction.SOUND:
			this.useThread = true;
			SyslogMedia.loadAudioClip(this.parameters[0], this.parameters[1]);
			break;
		}
	}

	public void closeAction() {
		this.isOpen = false;
		this.storeHostNetInfoTab = null;
		switch (this.type) {
		case SyslogAction.FILE:
			if (this.writer != null) {
				this.writer.close();
				this.writer = null;
			}
			break;

		case SyslogAction.FORWARD:
			this.isOpen = false;
			if (this.forwardSocket != null) {
				this.forwardSocket.close();
				this.forwardSocket = null;
			}
			break;

		case SyslogAction.CLASS:
			this.isOpen = false;
			this.closeClassAction();
			break;
		}
	}

	public void registerActionDisplay(String name,
			SyslogDisplayInterface display) {
		if (this.type == SyslogAction.DISPLAY) {
			if (name.equalsIgnoreCase(this.parameters[0])) {
				this.display = display;
			}
		}
	}

	public void finalize() {
		this.closeAction();
	}

	public void restart() {
	}

	/**
	 * TB_ATO_SYSLOGMSG HOST_IP VARCHAR2(18 BYTE), UNIT_ID VARCHAR2(60 BYTE),
	 * DEVICE_ID VARCHAR2(60 BYTE), MESSAGE VARCHAR2(600 BYTE), TIMESTRAMP DATE,
	 * ALARM_LEVEL VARCHAR2(30 BYTE)
	 */

	public void processMessage(SyslogMessage logMsg) {
		if (!this.isOpen)
			return;
		String KBP_CLASS = "10-10-22-18";
		String UNIT_ID;
		String device_id = "";
		String coll_time;
		switch (this.type) {
		case SyslogAction.FILE:

			// zhangdj add 2010-10-22
			//System.out.println("##########process ip : " + logMsg.hostName);
			/*System.out.println("##########process time : " + logMsg.timestamp);*/
		/*	System.out.println("##########process message : " + logMsg.message
					+ ",size :" + this.storeHostNetInfoTab.size());*/
			/*System.out.println("##########process priority : "
					+ logMsg.priority);*/
			// ��ȡ�澯�ȼ�
			String alarmLevel = SyslogDefs.getPriorityName(logMsg.priority);
			// �ɼ�ʱ��
			coll_time = this.getLongFormatTime("" + logMsg.timestamp);
			// �жϸõȼ��Ƿ����û����ƵIJɼ���Χ֮��,����ڣ������κδ���
			//����msg repeated 1 times in 0 seconds.
			if(!this.filterMsg(logMsg.message))
			if (judgeIsColl(this.matchRoleTable, alarmLevel,logMsg.hostName)) {
			//if (judgeIsColl(this.matchRoleTable, alarmLevel,"192.168.3.252")) {
				// ��������豸Id4ȷ����Ϣ
				Map infoMap = (Map) this.storeHostNetInfoTab
						.get(this.DEVICE_ID);
				if (infoMap != null) {
					// �ж���������������豸,����Ϊ10-10�������豸��Ϊ10-13
					KBP_CLASS = infoMap.get("KBP_CLASS").toString();
					device_id = infoMap.get("DEVICE_ID").toString();
					/*System.out.print("KBP_CLASS:" + KBP_CLASS + ",DEVICE_ID:"
							+ device_id);*/
					if (KBP_CLASS != null && KBP_CLASS.startsWith("10-13")) {
						KBP_CLASS = KBP_CLASS+"-12";
					}else{
						//-18���ľ�������syslog��־������syslog��־�����ǵ����Ŀ¼��������18��β��
						KBP_CLASS = KBP_CLASS+"-18";
					}
					String splitMsg = logMsg.message;
					System.out.println("process message : "+ logMsg.message);
					splitMsg = splitMsg.substring(splitMsg.lastIndexOf("]") == -1 ? 0 : splitMsg.lastIndexOf("]")+1);
					splitMsg = splitMsg.substring(0,splitMsg.length() > 6?6:splitMsg.length()-1);
					splitMsg = splitMsg.trim().replaceAll(":", "").replaceAll("\\ ", "_").replaceAll("-", "_").replaceAll("\t", "_");
					
					UNIT_ID = KBP_CLASS + ":" + device_id +"-"+alarmLevel+"_"+splitMsg+"-syslog";
					// ��ʼ���
					System.out.println("process unit id : "+ UNIT_ID);
				//10-13-10-10-12:APP_2_2960-warning-5w1d: -syslog
					Map data = new HashMap();
					data.put("HOST_IP", logMsg.hostName.toString());
					data.put("UNIT_ID", UNIT_ID);
					data.put("DEVICE_ID", device_id);
					data.put("MESSAGE", logMsg.message);
					data.put("COLL_TIME", coll_time);
					data.put("ALARMLEVEL", alarmLevel);
					data.put("DEVICE_NAME", infoMap.get("DEVICE_NAME")
							.toString());
					data.put("KPI_ID", judgeKPI(KBP_CLASS, logMsg.priority));
					data.put("PROCESS_ID",String.valueOf(logMsg.message));
					// ������ݿ�
					putInStorage(data, is_swap);
				}
			}

			/**
			 * if ( this.writer != null ) { this.writer.print( logMsg.timestamp );
			 * this.writer.print( " " ); this.writer.print( logMsg.hostName );
			 * this.writer.print( " " ); this.writer.print( logMsg.processName );
			 * this.writer.print( "[" ); this.writer.print( logMsg.processId );
			 * this.writer.print( "]: " ); this.writer.print( logMsg.message );
			 * this.writer.println(); this.writer.flush(); }
			 */

			break;

		case SyslogAction.FORWARD:
			// this.forwardMessage(logMsg);
			break;

		case SyslogAction.EMAIL:
			// this.emailMessage(logMsg);
			break;

		case SyslogAction.CLASS:
			if (this.action != null) {
				// this.action.processMessage(logMsg);
			}
			break;

		case SyslogAction.SOUND:
			// SyslogMedia.playAudioClip(this.parameters[0],
			// this.parameters[1]);
			break;

		case SyslogAction.SYSTEM:
			if (this.parameters != null && this.parameters.length > 0) {
				try {
					// Process proc =
					// Runtime.getRuntime().exec(this.parameters);
				} catch (Exception ex) {
					this.parameters = null;
					System.err.println("ERROR exec-ing '" + this.parameters[0]
							+ "'\n\t" + "   " + ex.getMessage());
				}
			}
			break;

		case SyslogAction.DISPLAY:
			if (this.display != null) {
				if (!logMsg.isRepeat && this.parameters.length > 1) {
					SyslogMessage cMsg = (SyslogMessage) logMsg.clone();

					cMsg.message = this.formatMessage(this.parameters[1],
							logMsg);

					logMsg = cMsg;
				}

				this.display.processMessage(logMsg);
			}
			break;
		}
	}

	public String formatMessage(String format, SyslogMessage logMsg) {
		Hashtable vars = new Hashtable();

		if (logMsg.matchVars != null) {
			for (int i = 0; i < logMsg.matchVars.length; ++i) {
				vars.put(("" + i), logMsg.matchVars[i]);
			}
		}

		vars.put("facility", SyslogDefs.getFacilityName(logMsg.facility));
		vars.put("priority", SyslogDefs.getPriorityName(logMsg.priority));

		vars.put("timestamp", logMsg.timestamp);
		vars.put("hostname", logMsg.hostName);
		vars.put("processname", logMsg.processName);
		vars.put("processid", ("" + logMsg.processId));
		vars.put("message", logMsg.message);

		return StringUtilities.stringSubstitution(format, vars);
	}

	private void emailMessage(SyslogMessage logMsg) {
		SimpleSMTP smtp;

		try {
			smtp = new com.sitech.ismp.ice.net.SimpleSMTP(this.parameters[0]);

			String body = logMsg.timestamp + SimpleSMTP.EOL + logMsg.hostName
					+ SimpleSMTP.EOL
					+ SyslogDefs.getFacilityName(logMsg.facility)
					+ SimpleSMTP.EOL
					+ SyslogDefs.getPriorityName(logMsg.priority)
					+ SimpleSMTP.EOL + logMsg.processName + SimpleSMTP.EOL
					+ logMsg.processId + SimpleSMTP.EOL + logMsg.message
					+ SimpleSMTP.EOL + SimpleSMTP.EOL + logMsg.timestamp + " "
					+ logMsg.hostName + " " + logMsg.processName + "["
					+ logMsg.processId + "] " + logMsg.message;

			smtp.sendMailMsg("syslogd", this.parameters[1], this.parameters[2],
					body);
		} catch (UnknownHostException ex) {
			this.closeAction();
			System.err.println("ERROR unknown SMTP host '" + this.parameters[0]
					+ "':\n\t" + ex.getMessage());
		} catch (ProtocolException ex) {
			System.err.println("ERROR SMTP protocol error:\n\t"
					+ ex.getMessage());
		} catch (IOException ex) {
			System.err.println("ERROR SMTP io error:\n\t" + ex.getMessage());
		}
	}

	public void openClassAction() {
		this.action = null;
		String className = this.parameters[0];

		try {
			Class actClass = Class.forName(className);

			String interfaceName = "com.sitech.ismp.ice.syslogd.SyslogActionInterface";

			if (!ClassUtilities.implementsInterface(actClass, interfaceName)) {
				System.err.println("ERROR class '" + className
						+ "' does not implement the interface '"
						+ interfaceName + "'.");
			} else {
				this.action = (SyslogActionInterface) actClass.newInstance();

				this.action.setParameters(this.parameters);

				this.action.openAction();
			}
		} catch (ClassNotFoundException ex) {
			this.isOpen = false;
			System.err.println("ERROR could not load action class '"
					+ className + "'\n\t" + ex.getMessage());
		} catch (InstantiationException ex) {
			this.isOpen = false;
			System.err.println("ERROR could not instantiate action class '"
					+ className + "'\n\t" + ex.getMessage());
		} catch (IllegalAccessException ex) {
			this.isOpen = false;
			System.err.println("ERROR could not load action class '"
					+ className + "'\n\t" + ex.getMessage());
		}
	}

	public void closeClassAction() {
		if (this.action != null) {
			this.action.closeAction();
			this.action = null;
		}
	}

	public void openForwardAction() {
		String hostName = this.parameters[0];

		this.forwardPort = SyslogDefs.DEFAULT_PORT;

		if (this.parameters.length > 1) {
			try {
				this.forwardPort = Integer.parseInt(this.parameters[1]);
			} catch (NumberFormatException ex) {
				this.forwardPort = SyslogDefs.DEFAULT_PORT;
				System.err.println("ERROR bad forward port '"
						+ this.parameters[1] + "' using '" + this.forwardPort
						+ "'\n\t" + ex.getMessage());
			}
		}

		try {
			this.forwardAddr = InetAddress.getByName(hostName);

			this.forwardSocket = new DatagramSocket();

			// Check for a forwarding loop....
			InetAddress localAddr = null;
			try {
				localAddr = InetAddress.getLocalHost();
			} catch (UnknownHostException ex) {
				localAddr = null;
			}

			if (localAddr != null) {
				if (localAddr.getHostAddress().equalsIgnoreCase(
						this.forwardAddr.getHostAddress())) {
					System.err.println("WARNING forward host '" + hostName
							+ "' is the local host.\n\t"
							+ " A forwarding loop is highly likely.");
				}
			}
		} catch (UnknownHostException ex) {
			this.isOpen = false;
			this.forwardSocket = null;
			System.err.println("ERROR getting forward host '" + hostName
					+ "' address:\n\t" + ex.getMessage());
		} catch (SocketException ex) {
			this.isOpen = false;
			this.forwardSocket = null;
			System.err.println("ERROR creating forward udp socket '"
					+ this.forwardPort + "@" + hostName + "':\n\t"
					+ ex.getMessage());
		}
	}

	private void forwardMessage(SyslogMessage logMsg) {
		int idx;
		byte[] data;
		byte[] strBytes;
		String strObj = null;
		Integer intObj = new Integer(0);

		if (this.forwardSocket != null) {
			int length = logMsg.message.length() + 5 + 16
					+ logMsg.processName.length() + 8 + 2;

			data = new byte[length + 32];

			idx = 0;
			data[idx++] = '<';

			int pricode = SyslogDefs.computeCode(logMsg.facility,
					logMsg.priority);
			
			strBytes = intObj.toString(pricode).getBytes();
			System.arraycopy(strBytes, 0, data, idx, strBytes.length);
			idx += strBytes.length;

			data[idx++] = '>';

			strBytes = logMsg.timestamp.getBytes();
			System.arraycopy(strBytes, 0, data, idx, strBytes.length);
			idx += strBytes.length;

			data[idx++] = ' ';

			strBytes = logMsg.processName.getBytes();
			System.arraycopy(strBytes, 0, data, idx, strBytes.length);
			idx += strBytes.length;

			data[idx++] = '[';

			strBytes = intObj.toString(logMsg.processId).getBytes();
			System.arraycopy(strBytes, 0, data, idx, strBytes.length);
			idx += strBytes.length;

			data[idx++] = ']';

			data[idx++] = ':';
			data[idx++] = ' ';

			strBytes = logMsg.message.getBytes();
			System.arraycopy(strBytes, 0, data, idx, strBytes.length);
			idx += strBytes.length;

			data[idx] = 0;

			DatagramPacket packet = new DatagramPacket(data, idx,
					this.forwardAddr, this.forwardPort);

			try {
				this.forwardSocket.send(packet);
			} catch (IOException ex) {
				this.closeAction();
				System.err.println("ERROR forwarding message:\n\t"
						+ ex.getMessage());
			}
		}
	}

	static public int getActionType(String typeStr) {
		if (typeStr.equalsIgnoreCase("DISPLAY")) {
			return SyslogAction.DISPLAY;
		} else if (typeStr.equalsIgnoreCase("FILE")) {
			return SyslogAction.FILE;
		} else if (typeStr.equalsIgnoreCase("SOUND")) {
			return SyslogAction.SOUND;
		} else if (typeStr.equalsIgnoreCase("SYSTEM")) {
			return SyslogAction.SYSTEM;
		} else if (typeStr.equalsIgnoreCase("EMAIL")) {
			return SyslogAction.EMAIL;
		} else if (typeStr.equalsIgnoreCase("FORWARD")) {
			return SyslogAction.FORWARD;
		}

		return SyslogAction.DISPLAY;
	}

	static public String actionName(int type) {
		switch (type) {
		case SyslogAction.DISPLAY:
			return "DISPLAY";
		case SyslogAction.FILE:
			return "FILE";
		case SyslogAction.FORWARD:
			return "FORWARD";
		case SyslogAction.EMAIL:
			return "EMAIL";
		case SyslogAction.SOUND:
			return "SOUND";
		case SyslogAction.SYSTEM:
			return "SYSTEM";
		}

		return "unknwon action type '" + type + "'";
	}

	public String toString() {
		StringBuffer result = new StringBuffer();

		result.append(SyslogAction.actionName(this.type));
		result.append("(");
		if (this.parameters != null)
			for (int i = 0; i < this.parameters.length; ++i) {
				result.append(this.parameters[i]);
				if (i < (this.parameters.length - 1))
					result.append(", ");
			}
		result.append(")");

		return result.toString();
	}

	private String getLongFormatTime(String date) {
		long longTime = 0;
		StringTokenizer st = new StringTokenizer(date, " ");
		String month = st.nextToken();
		String day = st.nextToken();
		String time = st.nextToken();
		int mon = 0;
		if ("Jan".equals(month))
			mon = 1;
		else if ("Feb".equals(month))
			mon = 2;
		else if ("Mar".equals(month))
			mon = 3;
		else if ("Apr".equals(month))
			mon = 4;
		else if ("May".equals(month))
			mon = 5;
		else if ("Jun".equals(month))
			mon = 6;
		else if ("Jul".equals(month))
			mon = 7;
		else if ("Aug".equals(month))
			mon = 8;
		else if ("Sep".equals(month))
			mon = 9;
		else if ("Oct".equals(month))
			mon = 10;
		else if ("Nov".equals(month))
			mon = 11;
		else if ("Dec".equals(month))
			mon = 12;
		else
			mon = -1;
		Date year = new Date();
		/*
		 * st = new StringTokenizer(time, ":"); String hour = st.nextToken();
		 * String min = st.nextToken(); String sec = st.nextToken();
		 * 
		 * Calendar theCalendar = Calendar.getInstance();
		 * System.out.println(theCalendar.); if (mon>-1)
		 * theCalendar.set(Calendar.MONTH, mon);
		 * theCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
		 * theCalendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hour));
		 * theCalendar.set(Calendar.MINUTE,Integer.parseInt(min));
		 * theCalendar.set(Calendar.SECOND,Integer.parseInt(sec));
		 * longTime=theCalendar.getTimeInMillis();
		 */
		return "" + year.toString().split(" ")[5].toString() + "-" + mon + "-"
				+ day + " " + time;
	}

	// �жϸ澯�����ĸ���
	public String judgeAlarmLevel(String content, String[] step) {
		String alarmLevel = null;
		if (content != null)
			for (int i = 0; i < step.length; i++) {
				if (content.indexOf(step[i]) != -1) {
					alarmLevel = step[i];
					break;
				}
			}
		return alarmLevel;
	}

	// �жϸ澯�ȼ��Ƿ����û����ƵIJɼ���Χ֮��
	public boolean judgeIsColl(Map roleMap, String roleSingle,String ip_addr) {
		boolean isColl = false;
		Map role = (HashMap)roleMap.get(ip_addr);
		
		if(role != null){
			if (role.get(roleSingle.toUpperCase()) != null && "1".equals(role.get(roleSingle.toUpperCase()).toString())) {
				isColl = true;
				is_swap = role.get("IS_SWAP")!=null?(role.get("IS_SWAP").equals("1")?true:false):false;
				DEVICE_ID = (String)role.get("DEVICE_ID");
			}
		}
		return isColl;
	}

	// �ж�ʹ�õ�KPI
	public String judgeKPI(String KBP, int alarmLevel) {
		switch (alarmLevel) {
		case 1:
			if (judgeKBPScope(KBP)) {
				return "FM-07-07-007-001";
			} else {
				return "FM-07-07-008-001";
			}
		case 2:
			if (judgeKBPScope(KBP)) {
				return "FM-07-07-007-002";
			} else {
				return "FM-07-07-008-002";
			}
		case 3:
			if (judgeKBPScope(KBP)) {
				return "FM-07-07-007-003";
			} else {
				return "FM-07-07-008-003";
			}
		case 4:
			if (judgeKBPScope(KBP)) {
				return "FM-07-07-007-004";
			} else {
				return "FM-07-07-008-004";
			}
		case 5:
			if (judgeKBPScope(KBP)) {
				return "FM-07-07-007-005";
			} else {
				return "FM-07-07-008-005";
			}
		case 6:
			if (judgeKBPScope(KBP)) {
				return "FM-07-07-007-006";
			} else {
				return "FM-07-07-008-006";
			}
		case 7:
			if (judgeKBPScope(KBP)) {
				return "FM-07-07-007-006";
			} else {
				return "FM-07-07-008-006";
			}
		default:
			return "";
		}
	}

	// ���������true����֮����false
	public boolean judgeKBPScope(String KBP) {
		if (KBP.startsWith("10-10")) {
			return true;
		} else {
			return false;
		}
	}

	public boolean putInStorage(Map data, boolean is_swap) {
		ConnectionOraclePool conPool = ConnectionOraclePool.getInstance();
		java.sql.Statement oracleStatement = null;
		// java.sql.ResultSet rsOracle = null;
		System.out.println("begin storage operation!~");
		java.sql.Connection con = null;
		boolean isSave = true;
		if (is_swap) {
			saveManager = new DecideSaveTableManager();
			try {
				Vector saveResult = saveManager.search(data);
				System.out.println("SEARCH RESULT SIZE : "+saveResult);
				if (saveResult != null && !saveResult.toString().equals("[]")){
					for (int idx = 0; idx < saveResult.size(); idx++) {
						HashMap map = (HashMap) saveResult.get(idx);
						if (map != null
								&& map.get("PROCESS_ID").toString().equals(
										data.get("PROCESS_ID").toString())) {
							System.out.println("this syslog exist!"+data.get("PROCESS_ID"));
							isSave = false;
							break;
						}else{
							System.out.println("this syslog has modify !"+data.get("PROCESS_ID")+" --->"+map.get("PROCESS_ID"));
							saveManager.modify(data);
						}
					}
				}else{
					saveManager.add(data);
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (isSave) {
				String SQL = "insert into tb_ato_syslogmsg(host_ip,unit_id,device_id,message,timestramp,alarm_level,device_name) "
						+ "values('"
						+ data.get("HOST_IP")
						+ "','"
						+ data.get("UNIT_ID")
						+ "','"
						+ data.get("DEVICE_ID")
						+ "',"
						+ "'"
						+ data.get("MESSAGE")
						+ "',to_date('"
						+ data.get("COLL_TIME")
						+ "','yyyy-mm-dd hh24:mi:ss'),'"
						+ data.get("ALARMLEVEL")
						+ "','"
						+ data.get("DEVICE_NAME") + "')";
				try {
					con = conPool.createConnection();
					oracleStatement = con.createStatement();
					// boolean resultsOracle = true;
					oracleStatement.execute(SQL);

				} catch (SQLException e) {
					e.printStackTrace();
				}
				try {

					if (oracleStatement != null)
						oracleStatement.close();
					if (con != null)
						con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		// ��ʼд��swapĿ¼
		// �澯��Ϣд��
		String command1 = "ksh /bnms/syslogdAgent/masteragent/bin/WriteSwap.sh "
				+ data.get("UNIT_ID")
				+ " "
				+ data.get("KPI_ID")
				+ " "
				+ ""
				+ data.get("MESSAGE").toString().replaceAll(" ", "#") + "";
		// String changeChmod ="chmod -R 777 /bnms/syslogagent/swap/";
		try {
			ShellExecutor.executeCmd(command1);
			// ShellExecutor.executeCmd(changeChmod);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String formateMessage(String message){
		if(message != null){
			String[] messageList = message.split(" ");
			message = "";
			for(int i = 0 ; i < messageList.length ; i++){
				message += messageList[i]+"\\"+"";
			}
		}
		return message;
	}
	
	private boolean filterMsg(String message){
		if(message != null && message.indexOf("msg repeated") == 0){
			System.out.println("filter msg is : "+ message +",filter date :"+new Date());
			return true;
		}else{
			return false;
		}
	}
	
	
	public static void main(String[] args) {
		// System.out.println(
		// SyslogAction.getLongFormatTime("Oct 25 10:51:48"));
		/*String message = "123 456 789 100 101 102  103";
		message = message.replace(' ', '\\');
		String[] messageList = message.split(" ");
		message = "";
		for(int i = 0 ; i < messageList.length ; i++){
			message += messageList[i]+"\\"+" ";
		}
		
		System.out.println("message : "+message);*/
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date updateTime = null;
		try {
			updateTime = format.parse("20101009233010");
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*updateTime = format.fo*/
		Date nowTime = null;
		try {
			nowTime = format.parse("20101010232010");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.print("compare result :"+(Integer.parseInt(format.format(nowTime))-Integer.parseInt(format.format(updateTime))));
		/*if(nowTime.compareTo(updateTime) == -1){
			//s
		}*/
	}
	
	
}