groovy syntax to display Smarts timestamps in human-readable format
iFountain Forum is no longer in use. Support functionality is moved to iFountain Issue Tracker.
You are not allowed to post a new topic on this forum, please create a new ticket on iFountain Issue Tracker.
You need to create a new account and login to use issue tracker.
Posted September 25th, 2007 by Anonymous
I'm new to grooovy, and I'm looking at the syntax to display a Smarts timestamp (FirstNotifiedAt) in a human-readable format.
1) A } is missing at the end of the script,
2) It is not possible to use ICEventType directly. It is an attribute of the dob. dob.ICEventType(like dob.LastClearedAt) is the correct access to it.
3) Java/groovy does not support ICEventType == "NOTIFY|CLEAR". Instead, you need to use either:
- if(ICEventType == "NOTIFY" || ICEventType == "CLEAR")
- if(ICEventType.matches("NOTIFY|CLEAR"))
No problem. Actually, there is no standalone closing bracket. When counted, they match. Because code is not formatted well here, eyes can fail sometimes.
Your severity code is OK with one little change. In groovy, accessing the array elements with indices requires integers. Severity seems to be an integer, however it is a string. So, calling the toInteger() method for Severity will solve the issue:
You can use the
import java.text.SimpleDateFormat;
def humanReadableText = convertToHumanReadableFormat(dob.LastNotifiedAt);
def convertToHumanReadableFormat(smartsTimestampValue)
{
smartsTimestampValue += "000";
def longVal = Long.parseLong(smartsTimestampValue);
if(longVal == 0)
{
return "never";
}
else
{
def timeStampFormat = "MMM d yyyy HH:mm:ss";
//http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
def format = new SimpleDateFormat(timeStampFormat);
def timestamp = new Timestamp(longVal);
return format.format(timestamp);
}
}
Here is the script that I
Here is the script that I am trying to use:
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
def convertToHumanReadableFormat(smartsTimestampValue)
{
smartsTimestampValue += "000";
def longVal = Long.parseLong(smartsTimestampValue);
if(longVal == 0)
{
return "never";
}
else
{
def timeStampFormat = "MMM d yyyy HH:mm:ss";
def format = new SimpleDateFormat(timeStampFormat);
def timestamp = new Timestamp(longVal);
return format.format(timestamp);
}
}
if(ICEventType == "NOTIFY|CLEAR")
{
dob.FirstNotifiedAt = convertToHumanReadableFormat(dob.FirstNotifiedAt);
dob.LastNotifiedAt = convertToHumanReadableFormat(dob.LastNotifiedAt);
dob.LastClearedAt = convertToHumanReadableFormat(dob.LastClearedAt);
dob.LastChangedAt = convertToHumanReadableFormat(dob.LastChangedAt);
dob.addAttributeValue("Action[0].ActionType", "SendMessage");
dob.addAttributeValue("Action[0].Text", dob.ClassDisplayName + "::" + dob.InstanceDisplayName + " is "
+ dob.EventDisplayName);
dob.each(){key,value ->
if(value instanceof String)
{
dob.addAttributeValue("Action[0]." + key, value);
}
}
but the connector fails with the following error:
2007-09-25 17:13:19,166 ERROR : ICNotificationReader Reader : AbstractSimpleSource - caught PipelineException [ScriptingTransf
ormer ScriptingEngine : startup failed, script1190733199129.groovy: 54: expecting '}', found 'null' @ line 54, column 1.
1 error
Severity: HANDLE]
2007-09-25 17:13:19,166 ERROR : ICNotificationReader Reader : AbstractSimpleSource - Source trapped HANDLE exception, source t
hread will exit. You can use a ExceptionHandler to manage these exceptions automatically.
2007-09-25 17:13:24,100 WARN : RunAdaptor ESIN-CSA_notifications complete - exiting
Paul, There are three errors
Paul,
There are three errors in the script:
1) A } is missing at the end of the script,
2) It is not possible to use ICEventType directly. It is an attribute of the dob. dob.ICEventType(like dob.LastClearedAt) is the correct access to it.
3) Java/groovy does not support ICEventType == "NOTIFY|CLEAR". Instead, you need to use either:
- if(ICEventType == "NOTIFY" || ICEventType == "CLEAR")
- if(ICEventType.matches("NOTIFY|CLEAR"))
The corrected script is:
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
def convertToHumanReadableFormat(smartsTimestampValue)
{
smartsTimestampValue += "000";
def longVal = Long.parseLong(smartsTimestampValue);
if(longVal == 0)
{
return "never";
}
else
{
def timeStampFormat = "MMM d yyyy HH:mm:ss";
def format = new SimpleDateFormat(timeStampFormat);
def timestamp = new Timestamp(longVal);
return format.format(timestamp);
}
}
if(dob.ICEventType == "NOTIFY" || dob.ICEventType == "CLEAR")
{
dob.FirstNotifiedAt = convertToHumanReadableFormat(dob.FirstNotifiedAt);
dob.LastNotifiedAt = convertToHumanReadableFormat(dob.LastNotifiedAt);
dob.LastClearedAt = convertToHumanReadableFormat(dob.LastClearedAt);
dob.LastChangedAt = convertToHumanReadableFormat(dob.LastChangedAt);
dob.addAttributeValue("Action[0].ActionType", "SendMessage");
dob.addAttributeValue("Action[0].Text", dob.ClassDisplayName + "::" + dob.InstanceDisplayName + " is " + dob.EventDisplayName);
dob.each(){key,value ->
if(value instanceof String)
{
dob.addAttributeValue("Action[0]." + key, value);
}
}
}
burak
thanks, it works. But weird
thanks, it works.
But weird that there is a standalone closing bracket at the end. why ?
How can I display the severity in textual form rather than numeric ? I have guessed something like:
def severities = ['', 'Critical', 'Major', 'Minor', 'Unknown', 'Normal']
emailBody += " Severity : " + severities [properties.Severity]
I'll post the entire script when completed.
Thanks,
Paul
No problem. Actually, there
No problem. Actually, there is no standalone closing bracket. When counted, they match. Because code is not formatted well here, eyes can fail sometimes.
Your severity code is OK with one little change. In groovy, accessing the array elements with indices requires integers. Severity seems to be an integer, however it is a string. So, calling the toInteger() method for Severity will solve the issue:
def severities = ['', 'Critical', 'Major', 'Minor', 'Unknown', 'Normal']
emailBody += " Severity : " + severities [properties.Severity.toInteger()]
burak