8 Replies Latest reply on Oct 22, 2015 1:40 PM by Bill Robinson

    NSH Scripting command(s) for listing Server Groups

    Donald Butts

      I have this situation:

       

      BL_Path_To_Server_Groups

      + Server_Group_1

                                      + Env _SmartServer_Group_1

                                                      Server_1

                                      + Env_SmartServer_Group_2

                                                      Server_1

                                                      Server_2

                                                      Server_3

                                      + Env_SmartServer_Group_3

                                                      Server_1

                                                      Server_2

                      - Server_Group_2

                                                      …

                      - Server_Group_3

                                                      …

       

      I want to be able to write the NSH script code that points to the BL_Path_To_Server_Groups directory and then dynamically, recursively, lists all of the Server_Groups, Env_SmartServer_Goups,  and servers. I am completely fine with nested for-loops, I ultimately just want to list all of the servers underneath each Env_SmartServer_Group.  I have the code to dynamically list all of the Servers within a given SartServer goup,  I just can’t seem to find the correct command(s) to get the dynamic list of Server_Groups underneath the BL_Path_To_Server_Groups directory, and underneath each one of those, the dynamic list of Env_SmartServer_Groups directories.  Any help towards accomplishing this goal would be greatly appreciated.

        • 1. Re: NSH Scripting command(s) for listing Server Groups
          Bill Robinson

          so you mean blcli not nsh... i didn't see a released command for this so:

           

          blcli_execute ServerGroup groupNameToId "/Workspace"

          blcli_storeenv groupId

          # child static groups

          blcli_execute Group findAllByParentGroup 5003 ${groupId}

          blcli_execute Group getGroupId

          blcli_execute Utility setTargetObject

          blcli_execute Utility listPrint

          blcli_storeenv staticGroupIds

          # child smart groups

          blcli_execute Group findAllByParentGroup 5007 ${groupId}

          blcli_execute Group getGroupId

          blcli_execute Utility setTargetObject

          blcli_execute Utility listPrint

          blcli_storeenv smartGroupIds

           

          then just call the bit to recurse into the child groups.

          • 2. Re: NSH Scripting command(s) for listing Server Groups
            Donald Butts

            Thank you very much, Bill, I will give this a try.

             

                Don Butts

            • 3. Re: NSH Scripting command(s) for listing Server Groups
              Yanick Girouard

              Bill Robinson wrote:

               

              so you mean blcli not nsh... i didn't see a released command for this so:

               

              blcli_execute ServerGroup groupNameToId "/Workspace"

              blcli_storeenv groupId

              # child static groups

              blcli_execute Group findAllByParentGroup 5003 ${groupId}

              blcli_execute Group getGroupId

              blcli_execute Utility setTargetObject

              blcli_execute Utility listPrint

              blcli_storeenv staticGroupIds

              # child smart groups

              blcli_execute Group findAllByParentGroup 5007 ${groupId}

              blcli_execute Group getGroupId

              blcli_execute Utility setTargetObject

              blcli_execute Utility listPrint

              blcli_storeenv smartGroupIds

               

              then just call the bit to recurse into the child groups.

               

              The last sentence is the most complicated part ! blcli_storeenv stores variables as globals and this screws up recursion if you want to put this into a function to call the code recursively. Recursive functions were always my bane in programming, and I just couldn't get this one to work well in NSH. The scoping in zsh/nsh is hellish compared to other languages. I tried messing around with it without having to rely on temp files to dump lists of groups in sorted order that I can then loop through recursively, but I gave up and couldn't get it to work.

               

              Basically, a tree command to list the BSA structure (depot, servers, jobs, etc...).

               

              If you can figure it out, it would really help the community to have a working script. Ideally, it would reproduce the same layout and output as you see it in the GUI: that is, in alphabetical order with both group types mixed in, all expanded. I would prefix static groups with "[-]" and smartgroups with "[+]", properly indented.

              • 4. Re: NSH Scripting command(s) for listing Server Groups
                Bill Robinson

                There’s a blcli_setlocal in 8.7 i think now ☺

                 

                You can do the storeenv and then set a local variable in the function to the value from the storeenv

                • 5. Re: NSH Scripting command(s) for listing Server Groups
                  Yanick Girouard

                  We are several months away from 8.7 at best over here... so in the mean time, it's back to SQL !

                  • 6. Re: NSH Scripting command(s) for listing Server Groups
                    Bill Robinson

                    Local vars aren’t that hard to use

                    • 7. Re: NSH Scripting command(s) for listing Server Groups
                      Yanick Girouard

                      Not that simple on my end... This is what I got so far:

                       

                      blcli_setjvmoption -Dcom.bladelogic.cli.execute.quietmode.enabled=true
                      PARENT_GROUP_NAME=$1
                      blcli_execute ServerGroup groupNameToId "${PARENT_GROUP_NAME}"
                      blcli_storeenv PARENT_GROUP_ID
                      
                      
                      OLD_IFS=$IFS
                      
                      
                      # $1 = groupId
                      function get_child_groups() {
                        
                          local childGroups=""
                        
                          blcli_execute Group findAllByParentGroup 5003 ${1}
                          blcli_execute Group getGroupId
                          blcli_execute Utility setTargetObject
                          blcli_execute Utility listPrint
                          blcli_storeenv staticGroupIds
                          staticGroupIds=$(sed '$d' <<< "$staticGroupIds" | paste -sd "," -)
                        
                          IFS=,
                          for gid in $staticGroupIds; do
                              blcli_execute Group getAQualifiedGroupName 5003 "${gid}"
                              blcli_storeenv childGroupName
                              childGroupName="$(awk -F/ '{print $NF}' <<< "${childGroupName}")"
                              childGroups+="${gid},5003,${childGroupName}\n"
                          done
                        
                          IFS=$OLD_IFS
                          blcli_execute Group findAllByParentGroup 5007 ${1}
                          blcli_execute Group getGroupId
                          blcli_execute Utility setTargetObject
                          blcli_execute Utility listPrint
                          blcli_storeenv smartGroupIds
                          smartGroupIds=$(sed '$d' <<< "$smartGroupIds" | paste -sd "," -)
                        
                          IFS=,
                          for gid in $smartGroupIds; do
                              blcli_execute Group getAQualifiedGroupName 5007 "${gid}"
                              blcli_storeenv childGroupName
                              childGroupName="$(awk -F/ '{print $NF}' <<< "${childGroupName}")"
                              childGroups+="${gid},5007,${childGroupName}\n"
                          done
                        
                          echo "${childGroups}" | sort -t, -k3
                      }
                      
                      
                      function main() {
                        get_child_groups $PARENT_GROUP_ID
                      }
                      
                      
                      main
                      
                      

                       

                      This gives an output like so:

                       

                      2002127,5003,Group 1

                      2001806,5007,Group 2

                      2001505,5003,Group 3

                      etc...

                       

                       

                      This only gives me the first level in a sorted manner, but it's in a format that gives me the group_id, type_id, and name. I need to loop through that recursively (when I find a type 5003), incrementing the indent every level, to present it into a tree format.

                       

                      I can't get this to work using only variable and parameter passing between functions. If you can see how go ahead and show me