<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Script - Some generic questions]]></title><description><![CDATA[<p dir="auto">Dear all,</p>
<p dir="auto">Some generic questions related to script definition:</p>
<ul>
<li>Any possible way to insert comments? Using for example <strong>#</strong> as in <strong>Perl</strong>...</li>
<li>Is the operator <strong>~=</strong> similar to the one in <strong>Perl</strong>? I.e. calling for regular expression?</li>
</ul>
<p dir="auto">As far as reg.ex. are concerned, is the syntax the same?<br />
If for example, I need a script parsing a message text to return different elements and use them to defined return argument(s)...<br />
Message test is <em>"TCP Socket is open on host <a href="http://xyz.domain.com" rel="nofollow ugc">xyz.domain.com</a> (172.16.0.1) using port 3333"</em> and my regular expression is:<br />
<strong>$event-&gt;message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port (\d)$"</strong><br />
How can I put the matched info in different variables: <strong>$status</strong>, <strong>$host</strong>, <strong>$ipaddr</strong> and <strong>$port</strong>?<br />
How do you define those variables?</p>
<p dir="auto">What about functions in script?</p>
<p dir="auto">How do we return an array as argument?<br />
Is syntax <strong>return ($val_1, $val_2);</strong> correct?<br />
Will this array be returned as an array?</p>
<p dir="auto">Would you have a complex example to post that would illustrate as many things as possible about defining NXSL script?</p>
<p dir="auto">Regards,<br />
JDamien</p>
]]></description><link>https://forum.netxms.org/topic/674/script-some-generic-questions</link><generator>RSS for Node</generator><lastBuildDate>Fri, 13 Nov 2026 09:21:49 GMT</lastBuildDate><atom:link href="https://forum.netxms.org/topic/674.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 30 Jun 2009 08:21:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Script - Some generic questions on Tue, 30 Jun 2009 10:43:55 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">Best regards,<br />
Victor</p>
]]></description><link>https://forum.netxms.org/post/3189</link><guid isPermaLink="true">https://forum.netxms.org/post/3189</guid><dc:creator><![CDATA[victor]]></dc:creator><pubDate>Tue, 30 Jun 2009 10:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to Script - Some generic questions on Tue, 30 Jun 2009 10:13:56 GMT]]></title><description><![CDATA[<p dir="auto">Hi again,</p>
<p dir="auto">Seems I managed to crash my server while returning an array...<br />
Is the following supported?</p>
<ul>
<li>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)</li>
<li>Output of %[script] is an array of 2 elements</li>
<li>Immediate effect: console connection broken and server process not running anymore!</li>
</ul>
<p dir="auto">Nothing in server log!<br />
No trace of EXEC request for action related script on agent in agent log!</p>
<p dir="auto">Regards,<br />
JDamien</p>
]]></description><link>https://forum.netxms.org/post/3188</link><guid isPermaLink="true">https://forum.netxms.org/post/3188</guid><dc:creator><![CDATA[jdl]]></dc:creator><pubDate>Tue, 30 Jun 2009 10:13:56 GMT</pubDate></item><item><title><![CDATA[Reply to Script - Some generic questions on Tue, 30 Jun 2009 09:08:38 GMT]]></title><description><![CDATA[<p dir="auto">Great - wonderful  <img src="https://forum.netxms.org/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=72b86a538fc" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:1.4em;width:auto;vertical-align:middle" title=":)" alt="🙂" /><br />
Your answers just clean off all my questions<br />
many thanks for this comprehensive feedback</p>
<p dir="auto">Best regards,<br />
JDamien</p>
]]></description><link>https://forum.netxms.org/post/3187</link><guid isPermaLink="true">https://forum.netxms.org/post/3187</guid><dc:creator><![CDATA[jdl]]></dc:creator><pubDate>Tue, 30 Jun 2009 09:08:38 GMT</pubDate></item><item><title><![CDATA[Reply to Script - Some generic questions on Tue, 30 Jun 2009 08:59:50 GMT]]></title><description><![CDATA[<p dir="auto">Hello!</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jdl" aria-label="Profile: jdl">@<bdi>jdl</bdi></a> said in <a href="/post/3185">Script - Some generic questions</a>:</p>
<blockquote>
<ul>
<li>Any possible way to insert comments? Using for example <strong>#</strong> as in <strong>Perl</strong>...</li>
</ul>
</blockquote>
<p dir="auto">Yes. Comments is like in C or Java - i.e. you can have multiline comments enclosed in <strong>/* */</strong>, and comments till end of line started from <strong>//</strong>.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jdl" aria-label="Profile: jdl">@<bdi>jdl</bdi></a> said in <a href="/post/3185">Script - Some generic questions</a>:</p>
<blockquote>
<ul>
<li>Is the operator <strong>~=</strong> similar to the one in <strong>Perl</strong>? I.e. calling for regular expression?</li>
</ul>
</blockquote>
<p dir="auto">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.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jdl" aria-label="Profile: jdl">@<bdi>jdl</bdi></a> said in <a href="/post/3185">Script - Some generic questions</a>:</p>
<blockquote>
<p dir="auto">As far as reg.ex. are concerned, is the syntax the same?<br />
If for example, I need a script parsing a message text to return different elements and use them to defined return argument(s)...<br />
Message test is <em>"TCP Socket is open on host <a href="http://xyz.domain.com" rel="nofollow ugc">xyz.domain.com</a> (172.16.0.1) using port 3333"</em> and my regular expression is:<br />
<strong>$event-&gt;message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port (\d)$"</strong></p>
</blockquote>
<p dir="auto">Syntax is not completely the same - NXSL uses POSIX extended regular expressions (<a href="http://en.wikipedia.org/wiki/Regular_expression#POSIX_Extended_Regular_Expressions" rel="nofollow ugc">http://en.wikipedia.org/wiki/Regular_expression#POSIX_Extended_Regular_Expressions</a>), and as far as I know Perl, there are some differences. Your example expression for NXSL should looks like</p>
<p dir="auto"><strong>$event-&gt;message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port ([0-9]+)$"</strong></p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jdl" aria-label="Profile: jdl">@<bdi>jdl</bdi></a> said in <a href="/post/3185">Script - Some generic questions</a>:</p>
<blockquote>
<p dir="auto">How can I put the matched info in different variables: <strong>$status</strong>, <strong>$host</strong>, <strong>$ipaddr</strong> and <strong>$port</strong>?<br />
How do you define those variables?</p>
</blockquote>
<p dir="auto">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:</p>
<pre><code>if ($event-&gt;message ~= "^TCP Socket is (.*) on host (.*) \((.*)\) using port ([0-9]+)$")
{
   status = $1;
   host = $2;
   ipaddr = $3;
   port = $4;
}
</code></pre>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jdl" aria-label="Profile: jdl">@<bdi>jdl</bdi></a> said in <a href="/post/3185">Script - Some generic questions</a>:</p>
<blockquote>
<p dir="auto">What about functions in script?</p>
</blockquote>
<p dir="auto">You can define as many functions as you wish. Functions defined with <strong>sub</strong> keyword. For example:</p>
<pre><code>/* 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;
}
</code></pre>
<p dir="auto">Function order within script is not important.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jdl" aria-label="Profile: jdl">@<bdi>jdl</bdi></a> said in <a href="/post/3185">Script - Some generic questions</a>:</p>
<blockquote>
<p dir="auto">How do we return an array as argument?<br />
Is syntax <strong>return ($val_1, $val_2);</strong> correct?<br />
Will this array be returned as an array?</p>
</blockquote>
<p dir="auto">To return an array, you should explicitly create it with <strong>array</strong> operator, assign array's elements, and return it. For example:</p>
<pre><code>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];
}
</code></pre>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jdl" aria-label="Profile: jdl">@<bdi>jdl</bdi></a> said in <a href="/post/3185">Script - Some generic questions</a>:</p>
<blockquote>
<p dir="auto">Would you have a complex example to post that would illustrate as many things as possible about defining NXSL script?</p>
</blockquote>
<p dir="auto">We don't have one, but looks like it worth creating one <img src="https://forum.netxms.org/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=72b86a538fc" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:1.4em;width:auto;vertical-align:middle" title=":)" alt="🙂" /> I'll do this later.</p>
<p dir="auto">Best regards,<br />
Victor</p>
]]></description><link>https://forum.netxms.org/post/3186</link><guid isPermaLink="true">https://forum.netxms.org/post/3186</guid><dc:creator><![CDATA[victor]]></dc:creator><pubDate>Tue, 30 Jun 2009 08:59:50 GMT</pubDate></item></channel></rss>