Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
Sep 03, 2015

I need REST api for list datagroup and check the irule is applied to which vs

I need REST api for list datagroup and check the irule is applied to which vs

 

7 Replies

  • Hi Robert,

    all commands below are one-liners. Here you go.

    To list datagroup names (internal):

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/data-group/internal?\$select=name | sed -r 's/$/\n/; s/,/$\n/g; s/(\[)/\1\n/g; s/(\])/\n]/g'

    To list datagroup links (internal):

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/data-group/internal?\$select=selfLink | sed -r 's/$/\n/; s/,/$\n/g; s/(\[)/\1\n/g; s/(\])/\n]/g'

    To list a specific internal datagroups records (link retrieved from command above; make sure to replace admin domain and name i.e. ~Common~datagroup_acl_data in sample below):

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/data-group/internal/~Common~datagroup_acl_data?\$select=records | sed -r 's/$/\n/; s/,/$\n/g; s/(\[)/\1\n/g; s/(\])/\n]/g'

    To list datagroup names (external):

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/data-group/external?\$select=name | sed -r 's/$/\n/; s/,/$\n/g; s/(\[)/\1\n/g; s/(\])/\n]/g'

    To list datagroup links (external):

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/data-group/external?\$select=selfLink | sed -r 's/$/\n/; s/,/$\n/g; s/(\[)/\1\n/g; s/(\])/\n]/g'

    To list virtual server names:

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/virtual?\$select=name | sed -r 's/$/\n/; s/,/$\n/g; s/(\[)/\1\n/g; s/(\])/\n]/g'

    To list virtual server links:

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/virtual?\$select=selfLink | sed -r 's/$/\n/; s/,/$\n/g; s/(\[)/\1\n/g; s/(\])/\n]/g'

    To list virtual server associated iRule(s) (sample virtual server link retrieved from output above; make sure to replace admin domain and name i.e. ~Common~vs_105 in sample below):

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET https://localhost/mgmt/tm/ltm/virtual/~Common~vs_105?\$select=rules | sed -r 's/$/\n/; s/,/\n/; s/(\[)/\1\n/g; s/(\])/\n]/g'

    Thanks, Stephan

  • wait one sec

     

    I want to know which VS the irule is applied to

     

    for example: in irule editor , I can right click one irule and check the porperty to see how many vips this irule is applied to

     

    Is there any simliar way in REST interface?

     

  • Child objects don't reference their parents, so you'd necessarily have to loop through each parent VIP to see if a specific rule was applied. So while I'm quite certain there's an easier way to do this, I cooked up a little Bash script to loop through all of the VIPs in /Common and find a given rule. Tweak as needed.

    !/bin/bash
    
     find and loop through all of the VIPs
    for vip in `curl -sk -u 'admin:admin' -H "Content-Type: application/json" -X GET "https://x.x.x.x/mgmt/tm/ltm/virtual" |grep -o '\"name\":\"[^"]*' |awk -F"\"" '{ print $4 }'`
    do
         find the rules associated with this VIP
        rule=`curl -sk -u 'admin:admin' -H "Content-Type: application/json" -X GET "https://x.x.x.x/mgmt/tm/ltm/virtual/$vip" |grep -o '\"rules\":[^,]*' |awk -F":" '{ print $2 }'`
    
         specify here which rule you're looking for (glob match)
        if [[ $rule == *"redirect"* ]]
        then    
             if rule is found echo some text
            echo "$vip contains $rule"
        fi
    done
    
  • This one-liner will dump all virtual servers and the associated iRules:

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET "https://localhost/mgmt/tm/ltm/virtual" | \
    grep -Eo '("name":"[^,]+|"rules":[^]]+"\])' | tr '\n' ';' | sed -r 's/$/\n/; s/;("name")/\n\1/g' | sed -r 's/;$//g'
    

    Output looks i.e. as follows:

    "name":"vs_51";"rules":["/Common/rule_remove_xff","/Common/rule_plaintext"]
    "name":"vs_52";"rules":["/Common/rule_remove_xff"]
    "name":"vs_53"
    "name":"vs_54";"rules":["/Common/rule_plaintext"]
    

    If you filter the output i.e. by using grep or awk for a specific iRule (i.e. "rule_remove_xff") you can expect the following output containing the relevant virtual server names only:

    curl -sk -u admin:admin -H "Content-Type: application/json" -X GET "https://localhost/mgmt/tm/ltm/virtual" | grep -Eo '("name":"[^,]+|"rules":[^]]+"\])' | tr '\n' ';' | sed -r 's/$/\n/; s/;("name")/\n\1/g' | sed -r 's/;$//g' | awk -F ';' '/rule_remove_xff/ {print $1}'
    "name":"vs_51"
    "name":"vs_52"
    
  • An alternative would be to use plain tmsh. The example below (one-liner again) does a recursive search to cover all admin partions and folders:

    $ tmsh -q -c "cd /; list ltm virtual recursive" | tr -d '\n' | sed -r 's/([^[:space:]])}/\1}\n/g' | \
    tr -s '[:space:]' | sed -r 's/([^[:space:]])\}/\1 }/' | \
    grep -Eow '(virtual [^ ]+|rules (none|\{[^\}]+\}))' | tr '\n' ';' | \
    sed -r 's/;$/\n/; s/;(virtual )/\n\1/g'
    virtual Common/vs_51;rules { Common/rule_remove_xff Common/rule_plaintext }
    virtual Common/vs_52;rules { Common/rule_remove_xff }
    virtual Common/vs_53
    virtual Common/vs_54;rules { Common/rule_plaintext }
    virtual Common/vs_55
    virtual myadminpartition/myiapp.app/myiapp_redir_vs;rules { myadminpartition/myownfolder/rule_myredirect }
    virtual myadminpartition/myiapp.app/myiapp_vs
    

    The output above lists as well the strange case of using folders (by iApp and own) in an administrative partition.