LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Blogs > Joe2003
User Name
Password

Notices

Rate this Entry

Code Dump

Posted 01-20-2010 at 09:42 AM by Joe2003

Code:
public class Monitor extends Thread
{
	private int maxOccurance;
	private int totalOccurance;
	private int timePeriod;
	private double average;

	private final int HISTORY_LENGTH = 10;

	private int[] history = new int[HISTORY_LENGTH];
	private int historyPtr;

	public Monitor(int time, int frequency)
	{
		maxOccurance = frequency;
		timePeriod = time * 1000;
		totalOccurance = 0;
		historyPtr = 0;
		average = maxOccurance;

		this.start();
	}

	public void update()
	{
		totalOccurance++;
	}

	private double getAverage()
	{
		double total = 0.0;
		
		for(int i = 0; i < HISTORY_LENGTH; i++)
		{
			total += history[i];
		}

		total = total / HISTORY_LENGTH;

		return total;
	}

	public void run()
	{
		boolean loopCompleted = false;
		double currentAverage = 0.0;

		while(!loopCompleted)
		{
			if(historyPtr == HISTORY_LENGTH)
			{
				historyPtr = 0;
			}

			try
			{
				Thread.sleep(timePeriod);
			}
			catch(InterruptedException ex)
			{
				continue;
			}

			synchronized(this)
			{
				currentAverage = getAverage();
				
				if(totalOccurance > maxOccurance || currentAverage > average)
				{
					loopCompleted = true;

					history[historyPtr] = totalOccurance;
					historyPtr++;
					
					//At this point generate an event log and shutdown the server.
					//If we get here it means that a DOS is happening				 
				}
				else
				{
					history[historyPtr] = totalOccuance;
					historyPtr++;
					totalOccurance = 0;
				}
			}
		}
	}
}
Posted in Uncategorized
Views 481 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 03:18 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration