Directly receive and parse ITM events

Source: Internet
Author: User

Directly receive and parse ITM events

In the previous implementation of a monitoring project, the customer offered a set of TIVOLI system monitoring software because they bought IBM small machines, customers have also deployed ITM monitoring in their production environments. Since they have not purchased IBM netcool, they are unable to centrally manage alert events. Please ask us to directly handle the ITM alert, currently, I have studied an ITM and netcool interface eif, and found that it is implemented through socket and the data is in the file format. So at that time, I developed a program to directly receive events from ITM, I want to share some useful things with you for your reference.

TECSocketServer. java, the main method class of the program, start the local port to receive the ITM alarm events.

Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java.net. ServerSocket;
Import java.net. Socket;
Import java. util. concurrent. Define blockingqueue;

Import org. apache. log4j. Logger;

/**
* The TECSocketServer collects the event information forwarded by TEMS.
*
* @ Author James Gao
* @ Since iMon 2.0 2011-11-6
*/
Public class TECSocketServer extends Thread {

/**
* Bind the local network port to receive events sent by the TEC.
*/
Private ServerSocket ss;

/**
* Whether to run.
*/
Boolean runFlag = false;

/**
* The data structure of the cached data queue.
*/
LinkedBlockingQueue queue;
/**
* Log records.
*/
Static final Logger logger = Logger
. GetLogger (TECSocketServer. class );

TECEventParser eventParser;

Private TECEventCachedHandler handler;
Private int port;

/**
* By default, 1000000 events are cached.
*/
Private static final int EVNET_CACHE_SIZE = 1000000;

Public TECSocketServer (int port, int eventCacheSize ){
// NetcoolEventHandler = new TECEventCachedHandler ();
This. eventParser = new TECEventParser (",");
This. port = port;

If (eventCacheSize <= 1000 ){
This. queue = new LinkedBlockingQueue (EVNET_CACHE_SIZE );
} Else {
This. queue = new LinkedBlockingQueue (eventCacheSize );

}
}

/**
* The service bound to the local port needs to be started in the thread to facilitate running in the background.
*/
Public void run (){
Try {

Ss = new ServerSocket (port );

While (runFlag ){
// Obtain client connection
Socket socket = ss. accept ();
Socket. setKeepAlive (true );
// Start the receiving service
CollectorWork work = new CollectorWork (socket );
Work. start ();

}
} Catch (Exception e ){
Logger. error (e. getMessage (), e );
} Finally {
If (ss! = Null ){
Try {
Ss. close ();
} Catch (IOException e ){

}
}
}

}

/**
* Start the collection service.
*
* @ Param eventParser event Parsing
*/
Public void startCollectServer (){
RunFlag = true;

Handler = new TECEventCachedHandler (this, eventParser );
Handler. setDaemon (true );
Handler. start ();
Logger.info ("Start the TEC event processing thread. ");

// Bind the local network port to start receiving events forwarded by TEMS.
This. start ();
Logger.info ("TEC receives TEMS forwarding event service started. ");
}

Public void stopCollectServer (){
RunFlag = false;
Try {
Ss. close ();
} Catch (Exception ex ){
Logger. error (ex. getMessage (), ex );
}

Try {
Handler. interrupt ();
} Catch (Exception ex ){
}
Logger.info ("TEC receives TEMS forwarding event service closed, release the binding port. ");
}

/**
* Event receiving thread
*
*/
Class CollectorWork extends Thread {
Private BufferedReader in;
Private Socket socket;

Public CollectorWork (){

}

Public CollectorWork (Socket socket ){
This. socket = socket;
}

Public void run (){

Try {

// Close the client if there is no event within 15 minutes
Socket. setSoTimeout (15*60*1000 );
Logger.info ("************************************* ******************************");
Logger.info ("a tems client come in:" + socket );
Logger.info ("************************************* ******************************");
StringBuffer sb = new StringBuffer ();
// String event = null;
In = new BufferedReader (new InputStreamReader (
Socket. getInputStream ()));
String line = "";
Boolean endflag = false;
While (runFlag & (line = in. readLine ())! = Null ){
Line = line. trim ();
System. out. println (line );
System. out. println ();
If (line. equals (""))
Continue;

// Each event forwarded by tems starts with Start.
If (line. startsWith ("<START>") & line. endsWith ("END ")){
Endflag = true;

Sb = new StringBuffer ();
// Sb. append (line). append (";");
Sb. append (line );

}
//// Each event forwarded by SocketGateway starts with End.
// If (line. indexOf ("end") = 0 ){
// Endflag = true;
//
//}
// If (! Endflag ){
// If (event! = Null ){
// Sb. append (line );
//}
//}

If (endflag ){
Logger.info ("********** es data = *************"
+ Sb. toString ());
// Put it into the cache team.
Queue. put (sb. toString ());
}

}
} Catch (Exception e ){
Logger.info (e. getMessage (), e );
} Finally {
Try {
If (in! = Null ){
In. close ();
}
} Catch (IOException e ){
Logger.info (e. getMessage (), e );
}
Try {
If (socket! = Null ){
Socket. close ();
}
} Catch (IOException e ){
Logger.info (e. getMessage (), e );
}
}
}
}


Public static void main (String [] args ){
TECSocketServer server = new TECSocketServer (5529,100000 );
Server. startCollectServer ();
}
}

TECEventCachedHandler. java

Import java. util. HashMap;

/**
*
* Cache the events sent by TEC, retrieve and resolve the events from the cache queue, and process the events as standardized events.
*
* @ Author James Gao, create on 2010-2-9
* @ Version v1.0
*/
Class TECEventCachedHandler extends Thread {
/**
*
*/
Private final TECSocketServer tecSocketServer;
Private TECEventParser parseHandler;

Public TECEventCachedHandler (TECSocketServer tecSocketServer, TECEventParser parseHandler ){
This. tecSocketServer = tecSocketServer;
This. parseHandler = parseHandler;
}

Public void run (){

While (this. tecSocketServer. runFlag ){

String data = null;
Try {
// Retrieve from the cache queue for processing.
Data = (String) this. tecSocketServer. queue. take ();

If (TECSocketServer. logger. isDebugEnabled ()){
TECSocketServer. logger. debug ("Take a event data from CacheQueue, data = ["
+ Data. toString () + "]");
}

HashMap eventMap = parseHandler
. ParserStr (data );
If (TECSocketServer. logger. isDebugEnabled ()){
TECSocketServer. logger. debug ("Process completely .");
}
} Catch (InterruptedException ex ){
TECSocketServer. logger. warn ("Read data from cache queue error, cause :"
+ Ex. getMessage (), ex );
} Catch (Exception ex ){
TECSocketServer. logger. warn ("Process event data fail, data = [" + data
+ "], Cause by:" + ex. getMessage (), ex );
}
}
}

}

TECEventParser. java is mainly used to parse text as an array.

Import java. util. HashMap;

Import org. apache. log4j. Logger;

/**
* Event Parsing
*
* @ Author James Gao, 2011-11-6
* @ Since SOP iMon 2.0
*/
Public class TECEventParser {

Private static final Logger logger = Logger
. GetLogger (TECEventParser. class );

Private String separator;

Public TECEventParser (String separator ){
This. separator = separator;
}

/**
* Parse the composite string
*
* @ Param initStr
*/
HashMap mapForData = new HashMap ();
Public HashMap parserStr (String initStr ){
Long t1 = System. currentTimeMillis ();
String event = initStr;
HashMap mapForValue = new HashMap ();
If (initStr! = Null &&! InitStr. equals ("")){
For (int a, B = 0, c, d, I = 0; I <initStr. length (); I = d ){

A = initStr. indexOf ("= '");
B = initStr. indexOf ("';");
C = initStr. indexOf (";");
String x = initStr. substring (c + 1, B + 1 );
System. out. println (x );
If (! "". Equals (x) & (x! = Null )){
Int e = x. indexOf ("= '");
String key = x. substring (0, e );
System. out. println (key );
String firstValue = x. substring (e + 2, x. length ());
String lastValue = firstValue. substring (0, firstValue. length ()-1 );
If (lastValue. equals ("")){
LastValue = "no value ";
}
System. out. println (lastValue );
If (key! = Null &&! "". Equals (key) & key. equals ("source ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("sub_source ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("severity ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("cms_hostname ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("situation_name ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("situation_fullname ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("situation_displayitem ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("situation_origin ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("situation_time ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("situation_group ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("situation_status ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("origin ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("hostname ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("master_reset_flag ")){
MapForValue. put (key, lastValue );
}
Else if (key! = Null &&! "". Equals (key) & key. equals ("integration_type ")){
MapForValue. put (key, lastValue );
}
Else {
MapForValue. put (key, lastValue );
}



}
System. out. println ("~~~~~~~~~~ ");
String x1 = initStr. substring (a + 2, B );
// System. out. println (x1 );
String x2 = initStr. substring (B + 1, initStr. length ());
If (x2.indexOf ("END") = 1 ){
Break;
}
C = x2.indexOf ("';");
String x3 = x2.substring (1, c + 1 );
InitStr = x2;
D = (initStr. length ()-B;

}
} Else {
Logger.info ("received raw data initStr is NULL ");
}
Long t4 = System. currentTimeMillis ();
Logger.info ("Resolution alert" + event + "Time consumed:" + (t4-t1) + "ms ");
Return mapForValue;
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.