Blame view

src/com/sitech/ismp/ice/syslogd/SyslogServer.java 7.53 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

package com.sitech.ismp.ice.syslogd;

import com.sitech.ismp.ice.syslog.SyslogDefs;

import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;


public class
SyslogServer extends Thread
	{
	public static final String		RCS_ID = "$Id: SyslogServer.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:  $";

	private static final int	IN_BUF_SZ = (8 * 1024);
	private static final int	SYSLOG_PORT = 514;

	private boolean				socketOpen;

	private int					port;

	private DatagramSocket		inSock;
	private DatagramPacket		inGram;

	private byte[]				inBuffer;
	
	private boolean				debugPacketsReceived;
	private boolean				debugMessagesReceived;

	private SyslogConfig		configuration;
	private ConfigEntryVector	configEntries;

	private FeedbackDisplayer	feedback;


	public
	SyslogServer()
		{
		this( SyslogDefs.DEFAULT_PORT );
		}

	public
	SyslogServer( int port )
		{
		super();

		this.port = port;
		this.socketOpen = false;
		this.inBuffer = null;
		this.inGram = null;

		this.debugPacketsReceived = false;
		this.debugMessagesReceived = false;

		this.feedback = null;
		this.configuration = null;
		this.configEntries = null;
		}

	public SyslogConfig
	getConfiguration()
		{
		return this.configuration;
		}

	public void
	setConfiguration( SyslogConfig configuration )
		{
		this.configuration = configuration;
		this.configEntries = configuration.getConfigEntries();
		}

	public void
	startupServices()
		{
		try {
			this.inSock =
				new DatagramSocket( this.port );

			this.inBuffer =
				new byte[ SyslogServer.IN_BUF_SZ ];

			this.socketOpen = true;
			}
		catch ( SocketException ex )
			{
			System.err.println
				( "FATAL could not create input socket on port '"
					+ this.port + "'\n\t" + ex.getMessage() );
			this.stop();
			}
		}

	public void
	shutdownServices()
		{
		if ( this.socketOpen )
			{
			this.inSock.close();
			this.socketOpen = false;
			}
		}

	public void
	finalize()
		{
		this.shutdownServices();
		this.closeAllActions();
		}

	public void
	openAllActions()
		{
		for ( int eIdx = 0
				; eIdx < this.configEntries.size()
					; ++eIdx )
			{
			ConfigEntry entry =
				this.configEntries.entryAt( eIdx );

			if ( entry != null )
				{
				entry.openAction();
				}
			}
		}

	public void
	closeAllActions()
		{
		for ( int eIdx = 0
				; eIdx < this.configEntries.size()
					; ++eIdx )
			{
			ConfigEntry entry =
				this.configEntries.entryAt( eIdx );

			if ( entry != null )
				{
				entry.closeAction();
				}
			}
		}

	public void
	registerActionDisplay( String name, SyslogDisplayInterface display )
		{
		for ( int eIdx = 0
				; eIdx < this.configEntries.size()
					; ++eIdx )
			{
			ConfigEntry entry =
				this.configEntries.entryAt( eIdx );

			if ( entry != null )
				{
				entry.registerActionDisplay( name, display );
				}
			}
		}

	public void
	setFeedbackDisplayer( FeedbackDisplayer feedback )
		{
		this.feedback = feedback;
		}

	public void
	displayFeedback( String message )
		{
		if ( this.feedback != null )
			{
			this.feedback.displayFeedback( message );
			}
		}

	public void
	run()
		{
		int		packetCount = 0;

		this.displayFeedback
			( "Establishing communications..." );

		this.startupServices();

		this.displayFeedback
			( "Opening all configuration entries..." );

		this.openAllActions();

		this.displayFeedback
			( "Listening for incoming packets..." );

		for ( ; ; )
			{
			try {
				this.inGram =
					new DatagramPacket
						( this.inBuffer, this.inBuffer.length );
			
				this.inSock.receive( this.inGram );
				}
			catch ( IOException ex )
				{
				System.err.println
					( "ERROR reading input socket:\n\t"
						+ ex.getMessage() );
				break;
				}

			String msgBuf =
				new String( this.inGram.getData(), 0,
							this.inGram.getLength() );

			if ( this.debugPacketsReceived )
				System.err.println
					( "[" + this.inGram.getLength()
						+ "] {" + msgBuf + "}" );

			++packetCount;
			this.displayFeedback
				( "Packets received: " + packetCount + "." );

			String hostName =
				this.inGram.getAddress().getHostName();

			if ( hostName == null )
				hostName = "localhost";

			this.processMessage( msgBuf, hostName );
			}

		this.displayFeedback
			( "Closing all configuration entries..." );

		this.closeAllActions();

		this.displayFeedback
			( "Shutting down communications..." );

		this.shutdownServices();
		}

	public void
	processMessage( String message, String hostName )
		{
		//System.out.println("##################receive message is :"+message+",host name is :"+hostName);
		int lbIdx = message.indexOf( '<' );
		int rbIdx = message.indexOf( '>' );

		if ( lbIdx < 0 || rbIdx < 0 
				|| lbIdx >= (rbIdx - 1) )
			{
			System.err.println
				( "BAD MSG {" + message + "}" );
			return;
			}
		
		int priCode = 0;
		String priStr =
			message.substring( lbIdx + 1, rbIdx );

		try { priCode = Integer.parseInt( priStr ); }
		catch ( NumberFormatException ex )
			{
			System.err.println
				( "ERROR Bad priority code '" + priStr + "'" );
			return;
			}

		int facility = SyslogDefs.extractFacility( priCode );
		int priority = SyslogDefs.extractPriority( priCode );

	
		
		message =
			message.substring
				( rbIdx + 1, (message.length()) );
		//System.out.println(" message process : "+message);
		//
		// Check to see if msg looks non-standard.
		// In this case, it means that there is not a standard
		// date in the front of the message text.
		//
		boolean stdMsg = true;

		if ( message.length() < 16 )
			{
			stdMsg = false;
			}
		else if (	   message.charAt(3)	!= ' '
					|| message.charAt(6)	!= ' '
					|| message.charAt(9)	!= ':'
					|| message.charAt(12)	!= ':'
					|| message.charAt(15)	!= ' ' )
			{
			stdMsg = false;
			}

		String timestamp;

		if ( ! stdMsg )
			{
			try {
				timestamp =
					TimestampFormat.getInstance().format
						( new Date() );
				}
			catch ( IllegalArgumentException ex )
				{
				System.err.println( "ERROR INTERNAL DATE ERROR!" );
				timestamp = "";
				}
			}
		else
			{
			timestamp = message.substring( 0, 15 );
			message = message.substring( 16 );
			}

		lbIdx = message.indexOf( '[' );
		rbIdx = message.indexOf( ']' );
		int colonIdx = message.indexOf( ':' );
		int spaceIdx = message.indexOf( ' ' );
		
		int		processId = 0;
		String	processName = "";
		String	processIdStr = "";

		if ( lbIdx < (rbIdx - 1)
				&& colonIdx == (rbIdx + 1)
				&& spaceIdx == (colonIdx + 1) )
			{
			processName = message.substring( 0, lbIdx );
			processIdStr = message.substring( lbIdx + 1, rbIdx );
			message = message.substring( colonIdx + 2 );

			try { processId = Integer.parseInt( processIdStr ); }
			catch ( NumberFormatException ex )
				{
				System.err.println
					( "ERROR Bad process id '" + processIdStr + "'" );
				processId = 0;
				}
			}
		else if ( lbIdx < 0 && rbIdx < 0
					&& colonIdx > 0 && spaceIdx == (colonIdx + 1) )
			{
			processName = message.substring( 0, colonIdx );
			message = message.substring( colonIdx + 2 );
			}

		if ( this.debugMessagesReceived )
			System.err.println
				( "[" + facility + ":" + SyslogDefs.getFacilityName(facility)
					+ "] ["
					+ priority + ":" + SyslogDefs.getPriorityName(priority)
					+ "] '"
					+ processName + "' '" + processId + "' "
					+ timestamp + " " + message );

		SyslogMessage logMessage =
			new SyslogMessage(
				facility, priority, timestamp, hostName,
					processName, processId, message );

		for ( int eIdx = 0 ; eIdx < this.configEntries.size() ; ++eIdx )
			{
			ConfigEntry entry =
				this.configEntries.entryAt( eIdx );

			entry.processMessage( logMessage );
			}
		}
	
	}