Automation

 View Only
last person joined: 6 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Slax script for rpd

    Posted 05-23-2024 19:23

    I created a script to retrieve the WCPU of RPD in OIDs, but it's not functioning as expected. Could someone assist me with this?

    version 1.0;
     
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
     
    match / {
        <op-script-results> {
            var $cmd = <command> "show system processes extensive";
            var $out = jcs:invoke($cmd);
     
            var $lines = jcs:break-lines($out);
            for-each ($lines) {
                if (contains(., "rpd{rpd}")) {
                    var $pattern = "[0-9]+\s+root\s+[0-9]+\s+[0-9]+\s+[0-9]+[MKG]\s+[0-9]+[MKG]\s+CPU1\s+[0-9]+\s+[0-9]+:[0-9]+\s+([0-9]+\.[0-9]+%)\s+rpd\{rpd\}";
                    var $match = jcs:regex($pattern, .);
                    var $cpu_usage = $match[1];
     
                    if ($cpu_usage) {
                        var $rpc = <request-snmp-utility-mib-set> {
                            <object-type> "integer";
                            <instance> "RPD_normalized_cpu";
                            <object-value> $cpu_usage;
                        };
                        var $res = jcs:invoke($rpc);
                    }
                }
            }
        }
    }



    ------------------------------
    VIGNESH KANNAN
    ------------------------------


  • 2.  RE: Slax script for rpd

     
    Posted 05-24-2024 04:52
    Edited by asharp 05-24-2024 15:34

    My first guess would be that the following is required:

    var $cpu_usage = $match[2];

    Node sets start at 1, so jcs:regex() will return the entire string in node 1, and the matches in subsequent nodes. 

    You will also need to double escape the last part of your regex.  Special characters need to be double escaped as SLAX to XSLT conversion will remove one.

    rpd\\{rpd\\}

    Finally, I think that \s doesn't work in SLAX, so change to [[.space.]] instead.

    Last thing which was more of an observation than a particular issue, is that using jcs:invoke() is fine when just used once as it opens a connection, executes the RPC and then closes the connection.

    However, if you are using it multiple times there is an overhead, as each time a connection is being opened and closed.  For just a few times this is fine, but I prefer to open the connection once using jcs:open(), then execute as many RPC as needed using jcs:execute(), and then finally close the connection, when it is no longer needed using jcs:close().

    For scripts that perform a number of RPCs, especially when they're in a foreach loop, this can improve execution times significantly, as only a single connection is opened/closed rather than in each iteration.

    I did run your script on a virtual device I had access to, and made a few changes, more to match the output that I was getting which was slightly different to your own regex.

    Script and results are shown below:

    version 1.0;
     
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
     
    match / {
        <op-script-results> {
            <output> {
                var $conn = jcs:open();
                var $cmd = <command> "show system processes extensive";
                var $out = jcs:execute($conn, $cmd);
        
                var $lines = jcs:break-lines($out);
                for-each ($lines) {
                    if (contains(., "rpd{rpd}")) {
                        var $pattern = "[0-9]+[[.space.]]+root[[.space.]]+[0-9]+[[.space.]]+[0-9]+[[.space.]]+[0-9]+[MKG][[.space.]]+[0-9]+[MKG][[.space.]]+[[:alnum:]]+[[.space.]]+[0-9]+[[.space.]]+[0-9]+:[0-9]+[[.space.]]+([0-9]+\.[0-9]+%)[[.space.]]+rpd\\{rpd\\}";
                        var $match = jcs:regex($pattern, .);
                        var $cpu_usage = $match[2];
      
                        if ($cpu_usage) {
                            var $rpc = <request-snmp-utility-mib-set> {
                                <object-type> "integer";
                                <instance> "RPD_normalized_cpu";
                                <object-value> $cpu_usage;
                            };
                            var $res = jcs:execute($conn, $rpc);
                        }
                    }
                }
                expr jcs:close($conn);
            }
        }
    }

    show snmp mib walk jnxUtil ascii
    jnxUtilIntegerValue."RPD_normalized_cpu" = 0
    jnxUtilIntegerTime."RPD_normalized_cpu" = 07 e8 05 18  02 02 2c 00  2d 07 00
    
    op cpu_script
    show snmp mib walk jnxUtil ascii
    jnxUtilIntegerValue."RPD_normalized_cpu" = 0
    jnxUtilIntegerTime."RPD_normalized_cpu" = 07 e8 05 18  02 03 04 00  2d 07 00

    As I mentioned the output for rpd was slightly different in my environment to your own regex, so you may need to tweak it a little further, the match that I was getting based on the string was as follows:

    10957 root         20    0   962M   164M kqread   1 163:19   0.00% rpd{rpd}
    
    And the regex was returning
    0.00%

    Regards,

    ------------------------------
    Andy Sharp
    ------------------------------



  • 3.  RE: Slax script for rpd

    Posted 28 days ago

    I didn't get any output from the OIDs. I copied and pasted the script but can't figure out what I did wrong.

    set event-options generate-event collect_rpd_data_event time-interval 300
    set event-options policy collect_rpd_data_policy events collect_rpd_data_event
    set event-options policy collect_rpd_data_policy then event-script collect_rpd_cpu.slax
    set event-options event-script file collect_rpd_cpu.slax

    This is my output for show system process extensive command output

    show system processes extensive | match rpd | except 0.00%
    18892 root 37 0 1773M 896M CPU1 1 382:11 21.19% rpd{rpd}
    18892 root 23 0 1773M 896M wait 4 108:57 4.98% rpd{TraceThread}
    18892 root 20 0 1773M 896M kqread 1 13:20 0.88% rpd{rsvp-io}



    ------------------------------
    VIGNESH KANNAN
    ------------------------------



  • 4.  RE: Slax script for rpd

     
    Posted 27 days ago

    Maybe add some output into the script, so that when you run it manually as an OP script you can check to see if you actually get data.

    e.g.

    <snip>
                        var $pattern = "[0-9]+[[.space.]]+root[[.space.]]+[0-9]+[[.space.]]+[0-9]+[[.space.]]+[0-9]+[MKG][[.space.]]+[0-9]+[MKG][[.space.]]+[[:alnum:]]+[[.space.]]+[0-9]+[[.space.]]+[0-9]+:[0-9]+[[.space.]]+([0-9]+\.[0-9]+%)[[.space.]]+rpd\\{rpd\\}";
                        var $match = jcs:regex($pattern, .);
                        var $cpu_usage = $match[2];
                        expr "\nMatch: " _ $match[1] _ "\n";
                        expr "CPU Usage: " _ $cpu_usage;
    <snip>

    With these two additional lines added to the script I posted you should at least be able to see what string is being used for regex, and what the match is.



    ------------------------------
    Andy Sharp
    ------------------------------



  • 5.  RE: Slax script for rpd

     
    Posted 27 days ago
    Edited by asharp 27 days ago

    Or another option is to use the debugger to check your script and the data that it is getting.

    op collect_rpc_cpu.slax invoke-debugger cli
    sdb: The SLAX Debugger (version 0.22.1)
    Type 'help' for help
    (sdb)
    
    Then set some line breakpoints, e.g.
    
    (sdb) break 14
    Breakpoint 1 at file /var/db/scripts/op/cpu_script.slax, line 14
    (sdb) break 21
    Breakpoint 2 at file /var/db/scripts/op/cpu_script.slax, line 21
    
    (sdb) continue
    Reached breakpoint 1, at /var/db/scripts/op/cpu_script.slax:14
    cpu_script.slax: 14:             var $lines = jcs:break-lines($out);
    (sdb) print $out
    << just verify that you are seeing the show system processes extensive output>>
    </output>
    (sdb) continue
    Reached breakpoint 2, at /var/db/scripts/op/cpu_script.slax:21
    cpu_script.slax: 21:                     if ($cpu_usage) {
    (sdb) print $match[1]
    [node-set] (1)
    <match>10957 root         20    0   962M   164M kqread   0 168:27   0.00% rpd{rpd}</match>
    (sdb) print $match[2]
    [node-set] (1)
    <match>0.00%</match>
    (sdb)
    
    << again, just checking that you are getting the matches that you expect and the output that you expect>>
    (sdb) break 27
    Breakpoint 3 at file /var/db/scripts/op/cpu_script.slax, line 27
    (sdb) continue
    Reached breakpoint 3, at /var/db/scripts/op/cpu_script.slax:27
    cpu_script.slax: 27:                         var $res = jcs:execute($conn, $rpc);
    (sdb) print $rpc
    [rtf] (1)
    <request-snmp-utility-mib-set>
      <object-type>integer</object-type>
      <instance>RPD_normalized_cpu</instance>
      <object-value>0.00%</object-value>
    </request-snmp-utility-mib-set>
    (sdb) continue
    >

    I did a test using the data that you had sent, and the regex appears to work just fine in my test environment.

    (sdb) print $match[1]
    [node-set] (1)
    <match>18892    root   37   0   1773M   896M   CPU1   1   382:11   21.19%    rpd{rpd}</match>
    (sdb) print $match[2]
    [node-set] (1)
    <match>21.19%</match>

    As you can see from the debugger output, the regex is working fine with the string and is extracting a value of 21.19%,

    The data is being pushed into the utility mib,

    root@vmx101> show snmp mib walk jnxUtil ascii
    jnxUtilIntegerValue."RPD_normalized_cpu" = 21
    jnxUtilIntegerTime."RPD_normalized_cpu" = 07 e8 05 1c  09 09 0f 00  2d 07 00

    I guess, one observation here is that we are obtaining a float via the regex, and then this is being stored as integer within the utility mib.  Just be aware that the utility mib only supports a set number of data types: , (string, counter32, counter64, integer and unsigned integer).  So if you are expecting that a float value should be stored within the MIB, then you'll need to save it as a string value instead.

    If you continue to have issues, please post the script in full, and also the exact steps that you are performing,  also any debugger output would also be useful, because I am unsure why this appears to be working just fine for me but not within your environment.


    Regards,



    ------------------------------
    Andy Sharp
    ------------------------------



  • 6.  RE: Slax script for rpd

     
    Posted 27 days ago
    Edited by Jodi Meier 27 days ago

    When you ran the script just as an OP script, did you see data being pushed to the MIB?

    1. Run the OP script : op collect_rpd_cpu.slax
    
    2. Walk the utility mib and see if data is being recorded.
    show snmp mib walk jnxUtil ascii
    jnxUtilIntegerValue."RPD_normalized_cpu" = 0
    jnxUtilIntegerTime."RPD_normalized_cpu" = 07 e8 05 18  02 02 2c 00  2d 07 00
    
    3. Run the OP script again : op collect_rpd_cpu.slax
    
    4. Walk the utility mib again and verify that the IntegerTime reports a new value.
    show snmp mib walk jnxUtil ascii
    jnxUtilIntegerValue."RPD_normalized_cpu" = 0
    jnxUtilIntegerTime."RPD_normalized_cpu" = 07 e8 05 18  02 03 04 00  2d 07 00

    The script that I supplied was the version that worked for the vMX that I was using, the structure is different for your own.  Have you checked that the regex is correct for your data?

    If you can't get this part to work, then there is no point in putting it in as an event script, since there won't be any data to populate in the MIB.

    ------------------------------
    Andy Sharp
    ------------------------------