Welcome to Netcordia Connection Sign in | Join | Help
in Search

CCS - Conditional Statements and Loops - Sample Script

Last post 06-12-2008 5:31 PM by mgriffin. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 06-12-2008 5:31 PM

    CCS - Conditional Statements and Loops - Sample Script

    Locked Reply Contact

    There isn't a direct way to write a conditional statement nor a loop; however, there is looping process that can occur within a "Trigger:" directive when a condition is met, and a "Commands:" directive can be written to act as a filter.  

    When a "Trigger-Template" matches multiple times within a given output stream, the "Trigger-Commands:" and "Output-Triggers:" directives, for that particular trigger, will run once for each individual template match.

    For the rest of the explanation, I'm going to write a sample script.


    Consider the following command output.

    Command:
      sh cdp neigh det

    Output:
      Device ID: SAMPLE-9999-01-iface1.my.domain.com
      Entry address(es):
        IP address: 10.1.1.1
      Platform: cisco AIR,  Capabilities: Trans-Bridge IGMP
      Interface: FastEthernet0/1,  Port ID (outgoing port): FastEthernet0
      Holdtime : 150 sec
     
      Device ID: SAMPLE-9999-02-iface2.my.domain.com
      Entry address(es):
       IP address: 10.1.1.2
      Platform: cisco AIR,  Capabilities: Trans-Bridge IGMP
      Interface: FastEthernet0/2,  Port ID (outgoing port): FastEthernet0
      Holdtime : 160 sec

      Device ID: SAMPLE-9999-03-iface3.my.domain.com
      Entry address(es):
        IP address: 10.1.1.3
      Platform: cisco AIR,  Capabilities: Trans-Bridge IGMP
      Interface: FastEthernet0/3,  Port ID (outgoing port): FastEthernet0
      Holdtime : 170 sec
     


    I want my CCS script to extract the CDP deviceID out of each interface and set the description for the interface to match the CPD DeviceID.  Throw an error if the "Holdtime" is more than 160 seconds.  

    Things to consider:
    - Commands are done on all interfaces
    - Enter config mode only once
    - Perform a "write mem" only once
    - Throw an error only if the value of holdtime is more than 160




    The pseudo-code logic of the script is as follows:

     

    --- Start Pseudo Logic ---

    first match = yes

    match found = no

    hold time = 0

     

    loop (for every interface the template matches, do this...)

      if (Template match) then

        set: (match found = yes)
     

        if (first match = yes) then

          enter config mode

          set: (first match = no)

        endif
        
        
          # run these commands against every interface that matches the template
          interface $ifname
          description $deviceid - $porttype $portidd
          exit
          
          # And throw an issue if holdtime > 160
          if (hold time more than 160)
            
            inject an issue into the health report, with details
            
          endif
        
        
      endif


    end loop

     

    if (match found = yes)

      write mem

    endif

    --- End Pseudo Logic ---







    --- Start Pseudo CCS Script Logic ---

    To start off our script, we're going to decide which sections we need and what each section does.

    Script Section - Define device types to run against, and schedule a time to run (if needed)
      - run against all Cisco IOS devices
        
    Action Section - Needed to perform our first command, and can use output triggers from here
      - run our initial command: sh cdp neigh det - this will create the output stream
      - output to a trigger
     
    Trigger Section - work with the output stream from our action command.
      - match the output to a specific template
      - what matches our template, execute the command set against
      - evaluate if holdtime value meets our critera
      - send that evaluation to an issue
     
    Issue Section - Check the eval passed from the trigger,
      - inject an issue if the condition is met
     
    Action Section - Determine if "write mem" needs to be run
      - Run "write mem" only if a change was made.

    --- End Pseudo CCS Script Logic ---






    -- Start CCS Script ---

    Script:
        Change CDP Interface

    Script-Description:
        This script sets the interface description for any interface on a Cisco IOS device
        that has a CDP entry.  And will inject an error into the health report any CDP
        interface that the value of "Holdtime" is greater than 160 seconds

        The processing will proceed as follows:

            - Get cdp interfaces
            - Process Interfaces (for each interface)
            - Set description for each interface
            - Inject Issue (if necessary)
            - Write Memory (if at least one change was made)

    Script-Filter:
        $Vendor eq "Cisco"
        and $sysDescr like /IOS/

    #######################################################################
    Action:
        Get CDP Interfaces

    Action-Description:
        Execute the "show cdp neighbor details" command and process the output
        using the "Process CDP Interfaces" trigger.
        $updateMade - indicate if at least one interface has
          been updated by the output trigger.
        $firstMatch - used to enter config mode only once

    Action-Commands:
        SET: $updateMade = "no"
        SET: $firstMatch = "yes"
        sh cdp neigh det

    #  Example output from show cdp neighbor detail
    #
    # xxxxx-sw1-1#sho cdp nei det
    # -------------------------
    # Device ID: cisco-6513-rtr
    # Entry address(es):
    #   IP address: 10.1.1.1
    # Platform: cisco WS-C6513,  Capabilities: Router Switch IGMP
    # Interface: GigabitEthernet0/1,  Port ID (outgoing port): GigabitEthernet10/11
    # Holdtime : 150 sec
    #

    Output-Triggers:
        Process CDP Interfaces

    #######################################################################
    Trigger:
        Process CDP Interfaces

    Trigger-Description:
        For each CDP deviceID, extract out the interface and set the description for the interface
        to match the cdp device id.

    Trigger-Variables:
        $deviceid    /[\w\?\-\.]+/
        $ifname        string
        $mismatch    string
        $porttype    /\D/
        $portidd    /\d[0-9\/]*/
        $holdTime    /\d+/

    Trigger-Template:
        Device ID\x3A [[$deviceid]]
        ...
        Interface\x3A [[$ifname]]\,  Port ID [[$mismatch]]\x3A [[$porttype]]\D+[[$portidd]]
        Holdtime \x3A [[$holdTime]] sec
        

    #--------------------------------------------------------------------------
    #
    #    "Trigger-Commands:" Directives
    #
    #
    #    DEBUG: Shows what would happen.
    #    To actually run the commands, DEBUG: needs to be removed!.
    #--------------------------------------------------------------------------

    #--------------------------------------------------------------------------
    #    Enter config mode the first time an interface matches our template
    #--------------------------------------------------------------------------
    Trigger-Commands: { $firstMatch eq "yes" }
        DEBUG: config terminal

    #--------------------------------------------------------------------------
    #    Always change the interface
    #--------------------------------------------------------------------------
    Trigger-Commands:
        DEBUG: interface $ifname
        DEBUG: description $deviceid - $porttype $portidd
        DEBUG: exit
        
    #--------------------------------------------------------------------------
    #    Avoid updating the global variables on every template match
    #--------------------------------------------------------------------------
    Trigger-Commands: {$firstMatch eq "yes" }
        SET: $firstMatch eq "no"
        
    Trigger-Commands: {$updateMade eq "no"}
        SET: $updateMade eq "yes"


    Output-Triggers:
        Holdtime Value Exceeded
        

    #######################################################################
    Issue:
        Holdtime Value Exceeded

    Issue-ID:
        holdTimeExceeded
        
    Issue-Severity:
        error
        
    Issue-Description:
        If the above matched our criteria, display an error with device info

    Issue-Filter:
        $holdTime > 160
        
    Issue-Details:
        Host        $IPAddress
        Name        $Name
        Interface    $ifname
        PortID        $portidd
        Holdtime    $holdTime
        
    #######################################################################
    Action:
        Write Memory

    Action-Description:
        If the $updateMade variable was set to "yes" by at least one
        instance of the "Process CDP Interfaces" trigger, then execute the
        "end" command to exit config mode and the "write memory"
        command to save the current configuration.

    Action-Filter:
        $updateMade eq "yes"

    Action-Commands:
        DEBUG: end
        DEBUG: write memory

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

    --- End CCS Script ---
    Mike Griffin
    Netcordia TAC
    support@netcordia.com
    Filed under: ,
Page 1 of 1 (1 items)