News:

We really need your input in this questionnaire

Main Menu

Script - Some generic questions

Started by jdl, June 30, 2009, 11:21:04 AM

Previous topic - Next topic

jdl

Dear all,

Some generic questions related to script definition:
- Any possible way to insert comments? Using for example # as in Perl...
- Is the operator ~= similar to the one in Perl? I.e. calling for regular expression?

As far as reg.ex. are concerned, is the syntax the same?
If for example, I need a script parsing a message text to return different elements and use them to defined return argument(s)...
Message test is "TCP Socket is open on host xyz.domain.com (172.16.0.1) using port 3333" and my regular expression is:
$event->message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port (\d)$"
How can I put the matched info in different variables: $status, $host, $ipaddr and $port?
How do you define those variables?

What about functions in script?

How do we return an array as argument?
Is syntax return ($val_1, $val_2); correct?
Will this array be returned as an array?

Would you have a complex example to post that would illustrate as many things as possible about defining NXSL script?

Regards,
JDamien

Victor Kirhenshtein

Hello!

Quote from: jdl on June 30, 2009, 11:21:04 AM
- Any possible way to insert comments? Using for example # as in Perl...

Yes. Comments is like in C or Java - i.e. you can have multiline comments enclosed in /* */, and comments till end of line started from //.

Quote from: jdl on June 30, 2009, 11:21:04 AM
- Is the operator ~= similar to the one in Perl? I.e. calling for regular expression?

Yes, it's calling regular expression specified at right on left operand, and result is true (1) if operand matches regexp, and false (0) otherwise. But unlike perl, you should specify regular expression as string, not inside // characters.

Quote from: jdl on June 30, 2009, 11:21:04 AM
As far as reg.ex. are concerned, is the syntax the same?
If for example, I need a script parsing a message text to return different elements and use them to defined return argument(s)...
Message test is "TCP Socket is open on host xyz.domain.com (172.16.0.1) using port 3333" and my regular expression is:
$event->message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port (\d)$"

Syntax is not completely the same - NXSL uses POSIX extended regular expressions (http://en.wikipedia.org/wiki/Regular_expression#POSIX_Extended_Regular_Expressions), and as far as I know Perl, there are some differences. Your example expression for NXSL should looks like

$event->message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port ([0-9]+)$"

Quote from: jdl on June 30, 2009, 11:21:04 AM
How can I put the matched info in different variables: $status, $host, $ipaddr and $port?
How do you define those variables?

Similar to Perl, NXSL places matched parts enclosed in brackets into variables $1, $2, and so on. If you wish to place these results into your own variables, you have to assign them after match. Please note that  variable should not start with $ sign, it's often used as indicator of system/special variable. You may or may not use $ sign in your variables. Variables declared implicitly on first use. For example, to assign status, host, ipaddr and port variables on successful match, code can looks like this:


if ($event->message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port ([0-9]+)$")
{
   status = $1;
   host = $2;
   ipaddr = $3;
   port = $4;
}



Quote from: jdl on June 30, 2009, 11:21:04 AM
What about functions in script?

You can define as many functions as you wish. Functions defined with sub keyword. For example:


/* script entry point */
sub main()
{
   // Call functions
   result = my_function_1();
   result2 = my_function_2(1, 2);
   println "result1=" . result . " result2=" . result2;
}

/* additional function */
sub my_function()
{
   return 1;
}

/* function with parameters */
sub my_function_2(param1, param2)
{
   return param1 + param2;
}


Function order within script is not important.

Quote from: jdl on June 30, 2009, 11:21:04 AM
How do we return an array as argument?
Is syntax return ($val_1, $val_2); correct?
Will this array be returned as an array?

To return an array, you should explicitly create it with array operator, assign array's elements, and return it. For example:


sub fx()
{
   array a;  // Create empty array and store reference to it in variable a
   a[1] = "value1";
   a[2] = "value2";
   return a;   // return reference to array
}

sub main()
{
   x = fx();  // get array result from function
   println "second element is " . x[2];
}


Quote from: jdl on June 30, 2009, 11:21:04 AM
Would you have a complex example to post that would illustrate as many things as possible about defining NXSL script?

We don't have one, but looks like it worth creating one :) I'll do this later.

Best regards,
Victor

jdl

Great - wonderful  :)
Your answers just clean off all my questions
many thanks for this comprehensive feedback


Best regards,
JDamien

jdl

Hi again,

Seems I managed to crash my server while returning an array...
Is the following supported?

- Action which calls an action on remote agent having as parameter the output of %[script] (I only have '$1' as parameter to agent script in agent config file)
- Output of %[script] is an array of 2 elements
- Immediate effect: console connection broken and server process not running anymore!

Nothing in server log!
No trace of EXEC request for action related script on agent in agent log!

Regards,
JDamien






Victor Kirhenshtein

Server should not crash - it's a bug. But anyway, when script returns something to be used as macro expansion result, it should be string.

Best regards,
Victor