i have created an snmp trap with 3 varbinds.
i have created an event with those 3 varbinds and i have created a rule to display this alarm.
i have tested it and the result was numbers.
now...
i want to convert those numbers to text and i have found the %[name] which is used to get value from script and what i do not understand is how i use the script to convert the varbinds to text
varbind 1 = %2 in event and the output is "1" and i want it to show "test" instead of the number.
10x,
lindeamon
let me clear things up.
i want to have some kind of transformation script in the event.
If I understand you need correctly, you need script which will convert numeric codes into strings, right? Then you can do the following:
Create script in script library, with some name, let's say "convert" (here I assume that you need to convert event's parameter %2 into text - otherwise change index in parameters[] accordingly):
switch($event->parameters[2])
{
case 1: return "test";
case 2: return "test2";
// and so on...
}
return "unknown"; // default value
and then use it in a message like %[convert]
Best regards,
Victor
hi victor,
this is what i was looking for.when i thought about the solution myself i have remembered that you helped me once with the $node parameters but i would have never guessed the $event parameter.
sorry to be a nag but this is another good example for why the documentation has to be updated.
thank u and best Regards,
Lindeamon
hi victor,
i have another question on the subject.
i receive an alarm with 3 parameters where all of them are numbers that i want to convert to text.
as a test for the 1st one i have used your script and it worked fine so now i want to upgrade it.
i want to write 2 sub's where the 1st one will receive event parameter 1 (and this i know how to do with the script you gave me) and the 2nd one will check parameters 2 and 3.
i have tried it myself but i do not know the correct syntax and parameter placing to subroutines.
i want the above in 1 script file.
Best Regards,
lindeamon
Hi!
Currently it is not possible - server always calls main() subroutine when expanding %[] macro. I can add an option to specify parameters and/or entry points to these scripts - in 1.1.8 or 1.1.9, depending on how fast I will fix other things from to-do list.
Best regards,
Victor
hi victor,
thank you very much.this is not urgent but it would make things more tidy.
how do i transfer parameters to a sub function ?
my last 2 parameters of the event can use the same function so i can use the same script but as you figured out the $event-parameters will be differnt and will have to be transfered as variables,how do i do that ?
Best Regards,
Lindeamon
Parameters can be passed to subroutines like that:
sub main()
{
f(1, 2);
f(3, 4);
}
sub f(p1, p2)
{
println "parameter 1 is " . p1;
println "parameter 2 is " . p2;
}
If you have multiple parameters which needs to be converted using same function, you can create separate script in the library containing this function, and call it from different small scripts. For example, create script called "convert" with function "convert":
convert(p)
{
// do conversion here
return converted_value;
}
then create scripts like that for each parameter:
convert_p1:
use convert;
return convert($event->parameters[1]);
convert_p2:
use convert;
return convert($event->parameters[2]);
and so on.
Best regards,
Victor
hi victor,
i think i understand.i will give it a try.
b.t.w - a feature request regarding this subject - the ability to copy/paste scripts.
Best Regards,
Lindeamon
hi victor,
no luck.
i have created 2 scripts:
1. the main script for converting called "CISCO_Config_Event_Config" and it's syntax is:
sub main(e)
{
switch(e)
{
case 3: return "running";
case 4: return "startup";
}
}
2. the event #3 param script called "CISCO_Config_Event_Source" and it's syntax is:
use CISCO_Config_Event_Config;
return CISCO_Config_Event_Config($event->parameters[3]);
in the event i have configured like this:
%[CISCO_Config_Event_Source]
and nothing works.where is my problem ?
Best Regards,
Lindeamon
and i forgot to mention that once i have tried the above configuration the server crashed.
You should not have function main() in library script. Try to change it like this:
sub CISCO_Config_Event_Config(e)
{
switch(e)
{
case 3: return "running";
case 4: return "startup";
}
}
It is strange that server crashes. I'll try to reproduce it myself.
Best regards,
Victor
hi victor,
it worked.
now, i do not understand why i had to do it like this,can you break down the syntax for me ?
for exm., i understand that "use XXXXX" will use the XXXXX script and the "return XXXX" is transferring what ever is in the () to the script but what i do not understand is why i had to do the last change you recommended ?
when i do this configuration the used script become the name of the function ?
Best Regards,
Lindeamon
Hi!
There are some confusion caused primarily by giving script itself and a function inside this script the same name. In "use" operator you should use script name (you can think of it as of virtual file name) - the one you see in script list in the library. Then, you can call functions from that script using their respective names - name of the function may or may not be identical to script name. Note that each script has it's own main() function, even if you do not declare it explicitly, so you cannot call main() function of another script. One script may provide multiple functions, like in this example:
Script in the library called "MyScript":
/* function 1 */
sub f1(p)
{
println "f1 called with parameter " . p;
}
/* function 2 */
sub f2(p)
{
println "f2 called with parameter " . p;
}
and then you use it in some other script:
use MyScript; /* load script from library by it's name */
f1(1); /* call function f1 */
f2(2); /* call function f2 */
Script above will produce output
f1 called with parameter 1
f2 called with parameter 2
Best regards,
Victor
hi victor,
thank you for the clarification although there is one thing i do not understand.
do i have to begin the script with:
sub main()
{
}
and then write the other functions i want to use ?
Best Regards,
Lindeamon
No, it is not necessary. Empty main() will be created by script compiler if it was not defined explicitly.
Best regards,
Victor
thank you very much
Best Regards,
Lindeamon