7 Replies Latest reply on Jun 18, 2018 7:43 AM by Stephen Galvin

    blcli_execute Server getServerDBKeyByName $HOST

    Stephen Galvin

      Guys,

       

      I'm running an NSH script against all my servers to check disk space and set a property to LOW if space is below a certain level. However, on certain servers the script does not run (Although it runs fine on 80% of them). The 20% that dont run, all have the latest RSCD Agent.

       

      The commands in the NSH script are as follows:

       

      HOST=$NSH_RUNCMD_HOST

      SERVER_ID=`blcli_execute Server getServerDBKeyByName $HOST`

      RESULT=`blcli_execute Server setPropertyValue $SERVER_ID $PROP_NAME $PROP_VALUE`

       

      Results of a debug show this:

       

      If I go into blcli and execute a manual command blcli Server getServerDBKeyByName myservername.domain.local it returns a key: DBKey:SDeviceModelKeyImpl:56-2149800 whereas if I print_debug RESULT it shows its value as 139 and the Server_ID is <blank>

       

      Has anyone come across this before?

       

      Appreciate any insight you can provide.

       

      Thanks,

      Stephen

        • 1. Re: blcli_execute Server getServerDBKeyByName $HOST
          Bill Robinson

          don't do FOO=`blcli_execute xxx`.  that spawns off a child process and defeats the purpose of using the 'performance commands'.

           

          try:

          HOST=$NSH_RUNCMD_HOST

          blcli_execute Server getServerDBKeyByName ${HOST}

          blcli_storeenv SERVER_ID

          blcli_execute Server setPropertyValue ${SERVER_ID} "${PROP_NAME}" "${PROP_VALUE}"

          blcli_storeenv RESULT

           

          or simply:

          blcli_execute Server setPropertyValueByName ${NSH_RUNCMD_HOST} "${PROP_NAME}" "${PROP_VALUE}"

          blcli_storeenv RESULT

           

          can you attach the script ?

          • 2. Re: blcli_execute Server getServerDBKeyByName $HOST
            Stephen Galvin

            Hi Bill

             

            Thanks a lot for coming back to me. I haven't tested your recommendations as yet but please see full script below.

             

            Appreciate the guidance, thank you again.

             

            Stephen

             

            # Collect servers with less than X freespace on C

            # Craig Williams

            # 04/07/2008 - Created

             

            ### This is a NSH Script ###

             

            # Declare Variables

            _LOW_SPACE_ON_C="FALSE"

            DEBUG="TRUE"

            HOST=$NSH_RUNCMD_HOST

            OS=`uname -s`

             

            echo "Host is $HOST, OS is $OS"

             

            # Set the DEBUG variable to 1 to print out all debug statements

            sub print_debug()

            {

            if [ "$DEBUG" = "TRUE" -o "$DEBUG" = "True" -o "$DEBUG" = " true" ]

            then

            echo $@

            fi

            }

             

            # Subroutine takes two arguments: The property name and the value to which

            # the property should be set. It is assumed that the global server variable

            # defines the server for which the property should be set.

            sub set_server_property()

            {

            PROP_NAME=$1

            PROP_VALUE=$2

             

            print_debug "Executing command: blcli Server setPropertyValue $SERVER_ID $PROP_NAME $PROP_VALUE"

            RESULT=`blcli_execute Server setPropertyValue $SERVER_ID $PROP_NAME $PROP_VALUE`

            sleep 10

            if test "$?" = "0"

            then

            print_debug "$PROP_NAME property set to $PROP_VALUE on $HOST."

            else

                echo "ERROR: Failed to set $PROP_NAME property for $HOST: $RESULT"

                exit 1

            fi

            }

             

            # If the host isn't windows then we don't need to do anything

            if [ "$OS" = "WindowsNT" ]

            then

                echo "in WindowsNT block"

            # Get the server's ID

            print_debug "Executing command blcli Server getServerDBKeyByName $HOST"

            SERVER_ID=`blcli_execute Server getServerDBKeyByName $HOST`

            print_debug "Result is $?"

            print_debug "Server ID is $SERVER_ID"

             

            #for FQDN in $1

            #do

                # We use cut here to remove the DNS suffix from the server name, makes log easier to read

                SERVER=`echo $HOST | cut -d . -f 1`

                FREE=`blquery -h $HOST -e 'df_free("/C")'/1024`

                echo $FREE

                # If the length of $FREE is less than 6, assume it is a number

                if [ "$#FREE" -lt "6" ]

                then

                    if [ "$FREE" -lt "10000" ]

                    then

                        echo "$SERVER -- $FREE MB Free on C:"

                        echo Setting _LOW_SPACE_ON_C to TRUE

                        set_server_property '_LOW_SPACE_ON_C' 'true'

                        echo "($HOST) Exit Code 1"

                    else

                        echo "($HOST) $FREE MB Free on C:"

                        echo Setting _LOW_SPACE_ON_C to FALSE

                        set_server_property '_LOW_SPACE_ON_C' 'false'

                        echo "($HOST) Exit Code 0"

                    fi

                else

                    # If the length of $FREE is greater than 6, assume it is an error string

                    echo "$SERVER blquery did not return a number"

                    echo "($HOST) Exit Code 1"

                fi

            #done

            else

                echo "OS != Windows, nothing will happen"

            fi

             

            exit 0

            • 3. Re: blcli_execute Server getServerDBKeyByName $HOST
              Stephen Galvin

              Bill I just tried to execute those commands manually via NSH and I get the following. It looks like HOST is not being set? I haven't tried via NSH script yet.

               

              MYSERVER% HOST=$NSH_RUNCMD_HOST

              MYSERVER% blcli_execute Server getServerDBKeyByName ${HOST}

              Command execution failed. com.bladelogic.om.infra.cli.factory.CommandNotFoundExc

              eption: Found command 'Server.getServerDBKeyByName' but with mismatching argumen

              ts. : java.lang.IllegalArgumentException: Wrong number of arguments provided to

              method : getServerDBKeyByName expected : 1 received : 0

              MYSERVER%

              MYSERVER% echo $HOST

              MYSERVER%

              • 4. Re: blcli_execute Server getServerDBKeyByName $HOST
                Bill Robinson

                i would not use HOST since that's a reserved variable:

                blapp892# echo $HOST

                blapp892.example.com

                blapp892#

                 

                NSH_RUNCMD_HOST is only available from w/in a runscript nsh script run.  eg the 'type 1' nsh job or if you use 'runscript' to run the script.

                • 5. Re: blcli_execute Server getServerDBKeyByName $HOST
                  Stephen Galvin

                  Noted thank you! What are your thoughts on that script?

                  • 6. Re: blcli_execute Server getServerDBKeyByName $HOST
                    Bill Robinson

                    i would take out all the FOO=`blcli_execute xxx` stuff and stop using HOST as a variable name.  there's some other stuff to change.  in the 'set_server_property' function the test is testing the success of the 'sleep' command, not the success of the blcli call.  i'd expect you'd want to check the success of the blcli call.  you also don't need to get the server id/key since you can use a different blcli command to set the property w/ the server name.

                     

                    the exit 139 should be a connection loss somewhere - i'm not sure why that would come out of the blcli call to set the server property though.  maybe forking off the child process to run the blcli in RESULT=`blcli_execute xxx` is causing that.

                     

                    then in the if block after you set the value to low space to true or false there's stuff like:

                             echo "($HOST) $FREE MB Free on C:"

                    the 'echo (servername)' is for a 'type 2' nsh script (pass targets as args).  and there was a for loop that looks like it processed a loop.  but it seems like this got converted to a 'type 1' (runscript) nsh script.  in the type 1 you get 1 instance of the script per target so you have concurrent runs and NSH_RUNCMD_HOST evaluates to the target of the instance of the script.  NSH_RUNCMD_HOST is not available in 'type 2' or if you run it from the commandline w/o using 'runscript'.  so you can take that stuff out.

                     

                    you also don't need to get the server key since you can use the setPropertyValueByName w/ the server name.  and you don't need to get the alias for the server since that was used once....

                     

                    also in that if block, if the space is low it echos 'exit code 1' but it will still exit w/ a 0.  so i'm not sure how useful that is.  if you want the job to show an error because the space is too low you need to do an 'exit 1' right there.

                     

                    # turn off blcli stdout
                    blcli_setjvmoption -Dcom.bladelogic.cli.execute.quietmode.enabled=true
                    # Declare Variables
                    _LOW_SPACE_ON_C="FALSE"
                    DEBUG="TRUE"
                    TARGET=${NSH_RUNCMD_HOST}
                    OS="$(uname -s)"
                    echo "Host is ${TARGET}, OS is ${OS}"
                    # Set the DEBUG variable to 1 to print out all debug statements
                    
                    sub print_debug()
                    {
                    if [[ "${DEBUG}" = "TRUE" -o "${DEBUG}" = "True" -o "${DEBUG}" = " true" ]]
                    then
                    echo "${@}"
                    fi
                    }
                    
                    
                    # Subroutine takes three arguments: The property name, value and server.
                    sub set_server_property()
                    {
                    local PROP_NAME="${1}"
                    local PROP_VALUE="${2}"
                    local SERVER="${3}"
                    local RESULT
                    local NEWVAL
                    print_debug "Executing command: blcli Server setPropertyValue ${SERVER} ${PROP_NAME} ${PROP_VALUE}"
                    blcli_execute Server setPropertyValueByName ${SERVER} "${PROP_NAME}" "${PROP_VALUE}"
                    blcli_storelocal RESULT
                    # get the value, did it get set ?
                    blcli_execute Server printPropertyValue ${SERVER} "${PROP_NAME}"
                    blcli_storelocal NEWVAL
                    
                    if [[ "${NEWVAL}" != "${PROP_VALUE}" ]]
                    then
                    echo "ERROR: Failed to set ${PROP_NAME} property for ${TARGET} to ${PROP_VALUE}: ${RESULT}"
                    exit 1
                    else
                    print_debug "${PROP_NAME} property set to $PROP_VALUE on ${TARGET}."
                    fi
                    }
                    
                    # If the host isn't windows then we don't need to do anything
                    if [[ "$OS" = "WindowsNT" ]]
                    then
                    echo "in WindowsNT block"
                    FREE="$(blquery -h ${TARGET} -e 'df_free("/C")'/1024)"
                    # If the length of $FREE is less than 6, assume it is a number
                    if [[ "${#FREE}" -lt "6" ]]
                    then
                    if [ "${FREE}" -lt "10000" ]
                    then
                    echo "${TARGET} -- ${FREE} MB Free on C:"
                    echo "Setting _LOW_SPACE_ON_C to TRUE"
                    set_server_property '_LOW_SPACE_ON_C' 'true' "${TARGET}"
                    echo "${TARGET} Exit Code 1"
                    else
                    echo "${TARGET} $FREE MB Free on C:"
                    echo "Setting _LOW_SPACE_ON_C to FALSE"
                    set_server_property '_LOW_SPACE_ON_C' 'false' "${TARGET}"
                    echo "${TARGET} Exit Code 0"
                    fi
                        else
                    # If the length of $FREE is greater than 6, assume it is an error string
                    echo "$SERVER blquery did not return a number"
                    echo "${TARGET} Exit Code 1"
                    fi
                    else
                    echo "OS != Windows, nothing will happen"
                    fi
                    exit 0
                    
                    1 of 1 people found this helpful
                    • 7. Re: blcli_execute Server getServerDBKeyByName $HOST
                      Stephen Galvin

                      Thank you Bill, that did the trick!