12 Replies Latest reply on Oct 6, 2017 5:53 AM by Kaushik KM

    how to query the cell and feed the slots of an events as parameter to a perl script?

    Kaushik KM

      Hi Everyone,

      I have a perl script which consumes slots of an event as the parameters and send it to the destination.

      I would like to know how to query the cell and retrieve the events and feed the script which we would like to execute by Remote action policy.

       

      the syntax of the command is like this:

       

      "perl simple_collector_submit.pl --host=devapp01.ipsoft.ny1 --state=CRITICAL --service="check log" --client=IPsoft --host_alias=devapp01 --host_address=74.54.1.191 --timestamp=1354660453 --attributes="criticality=P2,auto_correlate=false,escalation=\"8AM to 4PM_Mon to Fri_Always\",default_ipim_queue=codestreet-general" --info="test alert" --server=10.22.52.40:9997 --source_id=123456"

       

      We need to pass each event's slots to that perl script.

      Can some one please suggest how this can be achieved?

        • 1. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
          Brendan Murray

          Hi Kaushik,

           

          You say that you want to query the cell and you want to execute your script with a remote action policy. I have not used remote action policies, so I can't help you much with that part. If I were doing what you describe, I would use a simple MRL rule and use the execute primitive to call your Perl script. There is no need to "query" the cell. You would construct the rule such that it would only execute for the specific set of events you want to pass to your script. The execute primitive passes all event slots to the shell environment used by your script. It can consume the slot values by referencing them as either Windows or Unix environment variables.

           

          Also, as a general rule, it's best to wrap your Perl script in a Unix shell script or a Windows .cmd file, rather than calling it directly. Your shell script would reference the environment variables and construct the arguments required by your Perl script. Here's a link to a Communities post that describes how to do it on Windows.

           

          You could use either the 'new' or 'execute' rule phase, depending on how you want to trigger your script. The new phase fires only on the arrival of new events. The execute phase fires when any event slot is modified. If you use the new rule phase, your rule would look something like this:

           

          new MyTestRemoteExecution: EVENT ($EV)

          where [ your ECF criteria here]

           

          when [ $EV.status == OPEN ]

               {

               execute($EV, mc_myperlscript.cmd, [arguments], NO);   

               }

          END

           

          Regards,

           

          Brendan

          1 of 1 people found this helpful
          • 2. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
            Kaushik KM

            Hii Brendan,

            I really thank you so much for your valuable response and giving me idea about MRL.

            I will try and revert for any queries asap.

            • 3. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
              Kaushik KM

              Hi Brendan,

               

              I have written a rule to call a simple perl script consuming only one variable, wraped in ".cmd" and passing the value from mrl as shown below:

              My consern: how to pass more than 9 variables from batch file which is calling the perl script?

               

              ####my simple perl script for testing##### which will take a string from MRL and write it to a text file.

              use strict;

              use warnings;

              use Getopt::Long;

              my $in_host;

              my $getopt_result = GetOptions( "host=s"  => \$in_host );

              my $existingdir = 'D:\myperltest';

              mkdir $existingdir unless -d $existingdir;

              open my $fileHandle, ">>", "$existingdir/2ndfile.txt" or die "Can't open '$existingdir/2ndfile.txt'\n";

              print $fileHandle " $in_host : name of the host \n";

              print $fileHandle "Working!\n";

              close $fileHandle;

              ###########################

               

               

               

              *****my batch file calling the above perl script***** this will call the above script passing one variable from MRL

              @ECHO OFF

              call "C:\Dwimperl\perl\bin\perl.exe" "C:\Users\bmcpatrol\Desktop\new_modified.pl" --host=%1

              ******************************************

               

               

               

              &&&&&& My MRL in execute phase &&&&&

               

              execute IPSoft : EVENT($EV)

              where [$EV.mc_host == 'yourserver']

              when $EV.status == OPEN

                   {

              $path ="D:\Program Files\BMC Software\ProactiveNet\pw\server\etc\Development_BPPM\kb\bin\w4\callmyperl.cmd";

                   execute($EV, $path, [$EV.mc_host], NO); 

                   }

              END

              &&&&&&&&&&&&&&&&&&&&&&&&&&&&&

               

              Question: what if my perl script is consuming more than 9 variables? how do i pass the 10th and above variable from batch script?

              till 9th variable my batch would be like this:

               

              @ECHO OFF

              call "C:\Dwimperl\perl\bin\perl.exe" "C:\Users\bmcpatrol\Desktop\PERL_SCRIPT_IPSOFT\simple_collector_submit.pl"  --host=%1 --state=%2 --service=%3 --client=%4 --host_alias=%5 --timestamp=%6 --attributes=%7 --info=%8 --server=%9

              • 4. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
                Brendan Murray

                Hi Kaushik,

                 

                There should be no need to pass the event data as command line arguments. As I said in my original response, "The execute primitive passes all event slots to the shell environment used by your script. It can consume the slot values by referencing them as either Windows or Unix environment variables."

                 

                If your script is running on Windows, you should be able to reference any event slot by name as an environment variable. For example:

                 

                call "C:\Dwimperl\perl\bin\perl.exe" "C:\Users\bmcpatrol\Desktop\PERL_SCRIPT_IPSOFT\simple_collector_submit.pl"  --host=%mc_host%. -- state=%severity%

                 

                If for some reason you still want to pass the data as parameters, Windows provides the 'shift' command for shifting the position of command line arguments. Here are some links that explain how to use shift:

                 

                Microsoft Technet - Shift

                 

                Stack Overflow - How to pass more than 9 parameters to a batch file

                 

                Let me know if this helps.

                 

                Regards,

                 

                Brendan

                • 5. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
                  Kaushik KM

                  Heyy Brendan,

                   

                  I understood that  "The execute primitive passes all event slots to the shell environment used by your script. It can consume the slot values by referencing them as either Windows or Unix environment variables."

                   

                  So we can just use  :

                   

                  call "C:\Dwimperl\perl\bin\perl.exe" "C:\Users\bmcpatrol\Desktop\PERL_SCRIPT_IPSOFT\simple_collector_submit.pl"  --host=%mc_host%. -- state=%severity% and so on....

                   

                  My question:

                  Then how is the syntax going to be for the MRL in [arguments] section ?

                  i mean what value are to be put here?

                   

                  new MyTestRemoteExecution: EVENT ($EV)

                  where [ your ECF criteria here]

                   

                  when [ $EV.status == OPEN ]

                       {

                       execute($EV, mc_myperlscript.cmd, [arguments], NO);  

                       }

                  END

                   

                  #########################

                  execute IPSoft : EVENT($EV)

                  where [$EV.mc_host == 'yourserver']

                  when $EV.status == OPEN

                       {

                  $path ="D:\Program Files\BMC Software\ProactiveNet\pw\server\etc\Development_BPPM\kb\bin\w4\callmyperl.cmd";

                      execute($EV, $path, [$EV.mc_host,$EV.severity,......(11 slots here?)......], NO);

                       }

                  END

                  ############################

                   

                  I will be trying this.

                  Please correct me if the above is wrong...

                  • 6. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
                    Brendan Murray

                    Hi Kaushik,

                     

                    The $ARGS is for passing command line arguments required by your script. In this case, your script is the Windows command file wrapper. It does not require any arguments. You can access the slot values you need as environment variables. Therefore, you do not need to put anything in the $ARGS argument.

                     

                    I would try this:

                     

                    execute($EV, $path,,NO);

                     

                    If the cell forces you to put something there, you could pass an empty list or a single slot, e.g.

                     

                    execute($EV, $path,[],NO);

                     

                    or

                     

                    execute($EV, $path,[$EV.mc_host],NO);

                     

                    The point is, you don't really need the $ARGS argument since you are calling a wrapper, rather than calling a script or OS command directly. The wrapper takes care of the command line arguments required by your Perl script.

                     

                    Regards,

                     

                    Brendan

                    1 of 1 people found this helpful
                    • 7. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
                      Kaushik KM

                      Heyy Brendan

                      WooW!! Super cool! its working!

                      i really really thank you so much!!

                       

                      I have tested with my own small perl script which is accepting 11 parameters and writing to a file and its working.

                      I will perform the same steps with Original script which sends the data(these 11 parameters) to another server.

                      I will post step by step procedure here after i complete everything(soon)

                       

                      My Question:

                      1)when i passed the %msg% and %mc_long_msg% slot to perl script(which will print the slot values to a file), from batch file like below :

                       

                      @ECHO OFF

                      call "C:\Dwimperl\perl\bin\perl.exe" "D:\Program Files\BMC Software\ProactiveNet\pw\server\etc\Development_BPPM\kb\bin\w4\MultipleARGS.pl"  --host=%mc_host% --severity=%severity% --service=%mc_service% --client=IPSoft --host_alias=%mc_smc_alias% --host_address=%mc_tool_address% --timestamp=%mc_arrival_time% --attributes=%mc_long_msg% --info=%msg% --server=10.240.248.253 --source_id=%mc_ueid%

                       

                      2)and my 'msg' and 'mc_long_msg' slot in msend command is like this :

                      msend -n Development_BPPM -a EVENT -b "mc_host='myserver';mc_service='myservice';mc_smc_alias='nanserver';mc_tool_address=10.10.10.10;mc_long_msg='abcd efgh ijkl'" -r MAJOR -m "testing madta edivi"

                       

                      in the file which is created by perl script.. had only "abcd" in place of "abcd efgh ijkl" for mc_long_msg slot.

                      and had only "testing" in place of "testing madta edivi" for msg slot.

                      which means its taking only first word of the slot and ignoring everything after 1st space,

                      Do you think its because of perl script or any other reasons?

                      perl script is attached as image, do not have option for attaching file.PrintSlotValues.bmp

                       

                       

                      • 9. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
                        Oleg Protokolov

                        Hi, Kaushik!

                         

                        Why so complex?

                        In my projects I use %ENV hash to passing arguments from MCELL to my script...

                        Relating to your project you need something like that:

                         

                        1. Your batch file shold contains only that

                        @ECHO OFF

                        call "C:\Dwimperl\perl\bin\perl.exe" "D:\Program Files\BMC Software\ProactiveNet\pw\server\etc\Development_BPPM\kb\bin\w4\MultipleARGS.pl"

                         

                        2. Inside your MultipleARGS.pl

                        ...

                        my $class = $ENV{'CLASS'};

                        my $host = $ENV{'mc_host'};

                        my $msg = $ENV{'msg'};

                        my $mc_long_msg = $ENV{'mc_long_msg'};

                        # and so on...

                        ...

                         

                        3. Don't forget check contents of $msg, $mc_long_msg and other slots for characters like: (double quotes), (apostrophe) and other...

                        You should mask it before call msend utility.

                         

                        4. Common note for task. I don't understand - why you need call Perl-script and then call msend?

                        If you need create new event inside MCELL you can doing this via following code:

                         

                        new MyBestFunctionEverCreated: EVENT ($EV)

                        where [your ECF criteria here]

                        when [$EV.status == OPEN]

                        {

                          generate_event(

                            EVENT,

                            [ mc_host = $EV.mc_host

                            , mc_host_address = $EV.mc_host_address

                            , status = OPEN

                            , severity = CRITICAL

                            , msg = 'my custom msg'

                            , mc_long_msg = 'very long msg'

                            #... another mandatory slots for your event

                            ]

                          );

                        }

                        END

                         

                        --

                        WBR, Oleg

                        • 10. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
                          Kaushik KM

                          Hiii Oleg,

                          Thank you for your input here..

                          To answer your response:

                           

                          1)&2) The Perl script that i shared was written by me for testing. just to test 'how to pass the slots values from mcell to the script', the Original script which i need to use is written by someone else(complicated and has dependencies on other files) and i do not want to modify it. and so its necessary for me to pass the slot values to the script by batch file.

                           

                          3)I need to know how to mask the space and other characters in msg and long_msg slot while passing the values to batch file?

                          in my previous msend command as i mentioned above only first word of the slot value was printed, i want the whole slot to be printed.

                           

                          4)I dont need to create a new event, i just want the slot values to be passed to the script.(the script does its job i.e., sending slot values to other receiver server)

                          • 11. Re: how to query the cell and feed the slots of an events as parameter to a perl script?
                            Oleg Protokolov

                            Hi Kaushik,

                             

                            You need surround %msg% and %mc_long_msg% by double qoutes:

                            call "....\perl.exe" "....\MultipleARGS.pl" ... --attributes="%mc_long_msg%" --info="%msg%" ...

                             

                            --

                            WBR, Oleg

                            1 of 1 people found this helpful