Groups > IBM > IBM Tivoli Directory Integrator > Re: Another JS question.




Another JS question.

Another JS question.
Mon, 10 Dec 2007 15:48:21 -050
I have the following code in an SNMP Listener "AfterGetNext"

currentHB = conn.getObject("snmp.timestamp");
task.logmsg("snmp.timestamp is: " + currentHB);
task.logmsg("snmp.timestamp is: " + lastHB);
//task.logmsg("Max number is:     " + Number.MAX_VALUE);
var diff = (currentHB-lastHB);
task.logmsg("LAST SNMP TRAP WAS SENT " + diff + " seconds
ago.");
lastHB = currentHB;
currentHB = null;

Here's the output:

- snmp.timestamp is: 1197319177934
- snmp.timestamp is: 1197319157882
- LAST SNMP TRAP WAS SENT 0 seconds ago.
- snmp.timestamp is: 1197319197956
- snmp.timestamp is: 1197319177934
- LAST SNMP TRAP WAS SENT 0 seconds ago.

When I uncomment the MAX_NUMBER line (sanity checker), I get:

Max number is: 
17976931348623157000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000

The 0 seconds is not actually causing any harm, but it got me thinking - 
   is there anything wrong with the code? Is this a number/arithmetic 
Post Reply
Re: Another JS question.
Tue, 11 Dec 2007 00:36:04 +010
I think the connector returns the timestamp in a java.lang.String 
representation... and you then try to perform a substraction on strings 
which does not provide you with the expected results.

Try converting the object into a number, for instance

currentHB = java.lang.Integer(conn.getObject("snmp.timestamp"));

or

currentHB = system.toInt(conn.getObject("snmp.timestamp"));

(both should have the same effect)

etc.

HTH,

Christian

Derryck Lamptey wrote:
> I have the following code in an SNMP Listener "AfterGetNext"
> 
> currentHB = conn.getObject("snmp.timestamp");
> task.logmsg("snmp.timestamp is: " + currentHB);
> task.logmsg("snmp.timestamp is: " + lastHB);
> //task.logmsg("Max number is:     " + Number.MAX_VALUE);
> var diff = (currentHB-lastHB);
> task.logmsg("LAST SNMP TRAP WAS SENT " + diff + " seconds
ago.");
> lastHB = currentHB;
> currentHB = null;
> 
> Here's the output:
> 
> - snmp.timestamp is: 1197319177934
> - snmp.timestamp is: 1197319157882
> - LAST SNMP TRAP WAS SENT 0 seconds ago.
> - snmp.timestamp is: 1197319197956
> - snmp.timestamp is: 1197319177934
> - LAST SNMP TRAP WAS SENT 0 seconds ago.
> 
> When I uncomment the MAX_NUMBER line (sanity checker), I get:
> 
> Max number is: 
>
17976931348623157000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000 
> 
> 
> The 0 seconds is not actually causing any harm, but it got me thinking - 
>   is there anything wrong with the code? Is this a number/arithmetic 
Post Reply
Re: Another JS question.
Thu, 13 Dec 2007 02:15:51 -050
Thanks Christian. I forgot to mention - the original code worked OK in
ITDI 5.2. This was an issue that came out of our 5.2 -> 6.1.1 porting 
effort. We could not get the second suggestion to work, gonna try the 
first tomorrow.

Thanks a lot. Derryck.

Christian Chateauvieux wrote:
> I think the connector returns the timestamp in a java.lang.String 
> representation... and you then try to perform a substraction on strings 
> which does not provide you with the expected results.
> 
> Try converting the object into a number, for instance
> 
> currentHB = java.lang.Integer(conn.getObject("snmp.timestamp"));
> 
> or
> 
> currentHB = system.toInt(conn.getObject("snmp.timestamp"));
> 
> (both should have the same effect)
> 
> etc.
> 
> HTH,
> 
> Christian
> 
> Derryck Lamptey wrote:
> 
>> I have the following code in an SNMP Listener "AfterGetNext"
>>
>> currentHB = conn.getObject("snmp.timestamp");
>> task.logmsg("snmp.timestamp is: " + currentHB);
>> task.logmsg("snmp.timestamp is: " + lastHB);
>> //task.logmsg("Max number is:     " + Number.MAX_VALUE);
>> var diff = (currentHB-lastHB);
>> task.logmsg("LAST SNMP TRAP WAS SENT " + diff + "
seconds ago.");
>> lastHB = currentHB;
>> currentHB = null;
>>
>> Here's the output:
>>
>> - snmp.timestamp is: 1197319177934
>> - snmp.timestamp is: 1197319157882
>> - LAST SNMP TRAP WAS SENT 0 seconds ago.
>> - snmp.timestamp is: 1197319197956
>> - snmp.timestamp is: 1197319177934
>> - LAST SNMP TRAP WAS SENT 0 seconds ago.
>>
>> When I uncomment the MAX_NUMBER line (sanity checker), I get:
>>
>> Max number is: 
>>
17976931348623157000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000 
>>
>>
>> The 0 seconds is not actually causing any harm, but it got me thinking

>> -   is there anything wrong with the code? Is this a number/arithmetic

>> anomaly? Overflow? Any ideas?
Post Reply
Re: Another JS question.
Thu, 13 Dec 2007 11:29:44 +010
Hi Derryck,

As far as fixpacks/upgrades and how they can solutions can
be affected, please note that TDI has been using IBM's own
JSEngine in place of Rhino since version 6.1. This can result
in some changes in script behavior, although much has been
addressed in the current release -- But when you discover
anything, Please register a PMR on it (thanks).

You can see that the value of snmp.timestamp is not
being interpreted as numeric by the JSEngine simply by the
way the log message prints it:

    snmp.timestamp is: 1197319177934

If it had been recognized as a Number by the script engine,
then there would have been a ".0" tagged after the number.

    snmp.timestamp is: 1197319177934.0

Now I wrote a little snmp test myself and in my getSNMP AL
I added a script to display the types of objects being
returned:

    attnames = work.getAttributeNames();

    for (name in attnames) {
	   task.logmsg("--> " + name + // print name
		       " (" + work.getObject(name).getClass() + // print class
		       "): " + work.getObject(name)); // print value
    }

When I caught a trap, the above code showed me that the snmp.timestamp
attribute value is of type com.tivoli.snmp.data.TimeTicks.

    ...
10:08:31  --> message (class java.lang.String): 42000
10:08:31  --> timestamp (class com.tivoli.snmp.data.TimeTicks):
1197536911892
    ...

Not finding the TimeTicks class def in our JavaDocs (or even with Google), I
decided to let TDI do some detective work for me. I dropped in
this code to display all the methods of this object:

    tt = work.getObject("timestamp");

    for (m in tt.getClass().getMethods())
       task.logmsg("     " + m);

and got this:

10:16:54       public boolean 
com.tivoli.snmp.data.TimeTicks.equals(java.lang.Object)
10:16:54       public native int java.lang.Object.hashCode()
10:16:54       public java.lang.String
com.tivoli.snmp.data.TimeTicks.toString()
10:16:54       public void 
com.tivoli.snmp.data.TimeTicks.fromString(java.lang.String)
10:16:54       public long com.tivoli.snmp.data.TimeTicks.getValue()
10:16:54       public void com.tivoli.snmp.data.TimeTicks.setValue(long)
10:16:54       public final native java.lang.Class java.lang.Object.getClass()
10:16:54       public final native void java.lang.Object.notify()
10:16:54       public final native void java.lang.Object.notifyAll()
10:16:54       public final void java.lang.Object.wait() throws 
java.lang.InterruptedException
10:16:54       public final void java.lang.Object.wait(long) throws 
java.lang.InterruptedException
10:16:54       public final native void java.lang.Object.wait(long,int) throws 
java.lang.InterruptedException

Reflection is handy :) This is the kind of thing I do all the time from the 
debugger, using the interactive JavaScript command line found to the right
of the Step buttons.

Returning to snmp again, from the above I see that the setValue() method
takes a Java long argument, so I figure I should be able to construct
this object with a long too. Good place to start anyway. Armed with this 
knowledge, I tried a paraphrasing of your code:

    currentHB = new com.tivoli.snmp.data.TimeTicks(15);
    lastHB = new com.tivoli.snmp.data.TimeTicks(45);
    task.logmsg("snmp.timestamp is: " + currentHB);
    task.logmsg("snmp.timestamp is: " + lastHB);
    //task.logmsg("Max number is:     " + Number.MAX_VALUE);
    var diff = (currentHB-lastHB);
    task.logmsg("LAST SNMP TRAP WAS SENT " + diff + " seconds
ago.");

and my results in 6.1.1 are:

10:21:15  snmp.timestamp is: 15
10:21:15  snmp.timestamp is: 45
10:21:15  LAST SNMP TRAP WAS SENT 0 seconds ago.

while 5.2 give me:

10:25:02  snmp.timestamp is: 15
10:25:02  snmp.timestamp is: 45
10:25:02  LAST SNMP TRAP WAS SENT -30 seconds ago.

So either the object itself has changed in such a way since the
amber days of 5.2 so that handling by a script engine in general is
affected; Or the JSEngine is just doing this differently from how
Rhino did/would. I know that they both handle Java Strings in a
similar fashion, so I changed the first two lines to return
String objects and it worked. This means that you should be able
to fix your code by doing this:

    currentHB = conn.getObject("snmp.timestamp").toString();

Please tell me if that helps.

-Eddie

Derryck Lamptey wrote:
> Thanks Christian. I forgot to mention - the original code worked OK in
> ITDI 5.2. This was an issue that came out of our 5.2 -> 6.1.1 porting 
> effort. We could not get the second suggestion to work, gonna try the 
> first tomorrow.
> 
> Thanks a lot. Derryck.
> 
> Christian Chateauvieux wrote:
>> I think the connector returns the timestamp in a java.lang.String 
>> representation... and you then try to perform a substraction on 
>> strings which does not provide you with the expected results.
>>
>> Try converting the object into a number, for instance
>>
>> currentHB =
java.lang.Integer(conn.getObject("snmp.timestamp"));
>>
>> or
>>
>> currentHB = system.toInt(conn.getObject("snmp.timestamp"));
>>
>> (both should have the same effect)
>>
>> etc.
>>
>> HTH,
>>
>> Christian
>>
>> Derryck Lamptey wrote:
>>
>>> I have the following code in an SNMP Listener
"AfterGetNext"
>>>
>>> currentHB = conn.getObject("snmp.timestamp");
>>> task.logmsg("snmp.timestamp is: " + currentHB);
>>> task.logmsg("snmp.timestamp is: " + lastHB);
>>> //task.logmsg("Max number is:     " + Number.MAX_VALUE);
>>> var diff = (currentHB-lastHB);
>>> task.logmsg("LAST SNMP TRAP WAS SENT " + diff + "
seconds ago.");
>>> lastHB = currentHB;
>>> currentHB = null;
>>>
>>> Here's the output:
>>>
>>> - snmp.timestamp is: 1197319177934
>>> - snmp.timestamp is: 1197319157882
>>> - LAST SNMP TRAP WAS SENT 0 seconds ago.
>>> - snmp.timestamp is: 1197319197956
>>> - snmp.timestamp is: 1197319177934
>>> - LAST SNMP TRAP WAS SENT 0 seconds ago.
>>>
>>> When I uncomment the MAX_NUMBER line (sanity checker), I get:
>>>
>>> Max number is: 
>>>
17976931348623157000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000 
>>>
>>>
>>> The 0 seconds is not actually causing any harm, but it got me 
>>> thinking -   is there anything wrong with the code? Is this a 
>>> number/arithmetic anomaly? Overflow? Any ideas?
Post Reply
about | contact