14 Replies Latest reply on Sep 28, 2017 7:10 AM by Matthew Ragland

    NSH Date Manipulation

      I have a need to perform some date manipulation within an NSH script in order to automate the staggered scheduling of phases in a deploy job using setAdvanceDeployJobPhaseScheduleByDBKey.  I need to schedule the commit phase for Today plus one day, and the Date function seems to be a bit lacking in NSH.  I've tried to use the some of the built-in bash commands like "date -d "+1 days" but NSH doesn't like this format.  Does anyone have a solution for this in NSH?


        • 1. Re: NSH Date Manipulation
          Barry McQuillan

          CURRENTDAY=`date +%d%`

          NEWDAY=`expr $CURRENTDAY + 1`

           

          This doesn't allow for handling month end rollover.

          • 2. Re: NSH Date Manipulation
            Bill Robinson

            something like:

             

            % zmodload zsh/datetime

            % date

            Mon Jul 29 08:04:43 2013

            % echo $(strftime '%Y-%m-%d %H:%M:%S' $((${EPOCHSECONDS}+3600)))

            2013-07-29 09:04:44

             

            use whatever date-formatting options you need there.

            • 3. Re: NSH Date Manipulation

              Thanks Barry, I am in need of a fool proof solution that will work during month and year end scenerios

              • 4. Re: NSH Date Manipulation

                Thanks Bill for the response.  I will give using zsh a try.  Do you know if this solution will handle month/year end rollover?

                • 5. Re: NSH Date Manipulation
                  Bill Robinson

                  you run the zmodload in nsh - nsh is zsh.

                  • 6. Re: NSH Date Manipulation

                    Got it!  Thanks Bill!

                    • 7. Re: NSH Date Manipulation
                      Sean Berry

                      Here are some sample scripts to schedule Patching Jobs....  Just for Seth D @ Country.  Hi Greg!

                       

                      Sample Scripts:

                       

                      #!/bin/nsh

                       

                      #    Requirements

                      #    Must have the following static groups:

                      #    Jobs - /Windows/Implementation/Tasks/Patching/Remediation

                      #    Jobs - /Windows/Implementation/Tasks/Patching/GMT-THURS_00:00-06:00

                      #    Depot - /Windows/Implementation/Tasks/Patching/Remediation

                      #    Depot - /Windows/Implementation/Tasks/Patching/GMT-THURS_00:00-06:00

                      #    Server - /Windows/Implementation/Tasks/Patching/GMT-THURS_00:00-06:00

                      #    Depot - /Patching-Win

                      #   

                      #    Must have the following Objects

                      #    Jobs - /Windows/Implementation/Tasks/Patching/PatchTemplate  (BLPackage Deploy Job)

                      #    Jobs - /Windows/Implementation/Tasks/Patching/GMT-THURS_00:00-06:00/Patch Analysis

                      #    Windows Patch Catalog - /Patching-Win/Windows

                       

                      CLIOPTIONS=" -v defaultProfile -r BLAdmins"

                       

                      TODAY_AS_INT=`blexpr 'show_date (get_date(), "%d")' | tr -d '[:cntrl:]'`

                      TODAY_HOURS=`blexpr 'show_date (get_date(), "%H")' | tr -d '[:cntrl:]'`

                      TODAY_MIN=`blexpr 'show_date (get_date(), "%M")' | tr -d '[:cntrl:]'`

                      TODAY_HOURS_EPOCH_OFFSET=$(( $TODAY_HOURS*60*60 ))

                      TODAY_EPOCH_OFFSET=$(( $TODAY_MIN*60+$TODAY_HOURS_EPOCH_OFFSET ))

                       

                      if [ $TODAY_AS_INT = 01 ]

                      then

                          P=0

                          P=$(( $P + $TODAY_EPOCH_OFFSET ))

                          FIRST_DAY_OF_MONTH=`blexpr 'show_date (get_date(), "%Y-%m-%d")' | tr -d '[:cntrl:]'`

                      else

                          P=0

                          TODAY_AS_INT=`blexpr 'show_date (get_date(), "%d")' | tr -d '[:cntrl:]'`

                          while [ $TODAY_AS_INT != 01 ]

                          do

                              P=$(( $P + 86400 ))

                              TODAY_AS_INT=`blexpr 'show_date (get_date()-'$P', "%d")' | tr -d '[:cntrl:]'`

                          done

                          P=$(( $P + $TODAY_EPOCH_OFFSET ))

                          # set 1st day of current calendar month property in both YYYY-MM-DD format and EPOCH

                          FIRST_DAY_OF_MONTH=`blexpr 'show_date (get_date()-'$P', "%Y-%m-%d")' | tr -d '[:cntrl:]'`

                          FIRST_DAY_OF_MONTH_EPOCH=`blexpr 'get_date()-'$P'' | tr -d '[:cntrl:]'`

                       

                      fi

                       

                      z=0

                      CURRENT_DOW=`blexpr 'show_date ('$FIRST_DAY_OF_MONTH_EPOCH',"%A")' | tr -d '[:cntrl:]'`

                      echo $CURRENT_DOW

                       

                      if [ "$CURRENT_DOW" = "Monday" ]; then

                          SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*8 ))

                      elif  [ "$CURRENT_DOW" = "Tuesday" ]; then

                          SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*7 ))

                      elif  [ "$CURRENT_DOW" = "Wednesday" ]; then

                          SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*13 ))

                      elif  [ "$CURRENT_DOW" = "Thursday" ]; then

                          SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*12 ))

                      elif  [ "$CURRENT_DOW" = "Friday" ]; then

                          SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*11 ))

                      elif  [ "$CURRENT_DOW" = "Saturday" ]; then

                          SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*10 ))

                      elif  [ "$CURRENT_DOW" = "Sunday" ]; then

                          SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*9 ))

                      fi

                       

                      TEST_EPOCH_OFFSET=$(( 86400*12 ))

                      SECOND_TUESDAY_EPOCH=$(( $SECOND_TUESDAY_EPOCH+$TEST_EPOCH_OFFSET ))

                       

                      STAGE_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400 ))

                      THU_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400*2 ))

                      FRI_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400*3 ))

                      SAT_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400*4 ))

                      SUN_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400*5 ))

                      MON_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400*6 ))

                      TUE_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400*7 ))

                      WED_EPOCH=$(( $SECOND_TUESDAY_EPOCH+86400*8 ))

                       

                      STAGE_DATE_STRING=`blexpr 'show_date ('$STAGE_EPOCH', "%Y-%m-%d 18:00:00")' | tr -d '[:cntrl:]'`

                       

                      echo StageString=$STAGE_DATE_STRING

                       

                      SERVER_GROUP=/Windows/Implementation/Tasks/Patching

                      PATCHWINDOW_LIST=`blcli $CLIOPTIONS ServerGroup listChildGroupsInGroup $SERVER_GROUP`

                      JOB_NAME=PatchTemplate

                       

                      for PATCHWINDOW_GROUP in $PATCHWINDOW_LIST; do

                          PATCHWINDOW=`echo $PATCHWINDOW_GROUP | cut -d\- -f2 -s`

                          if [ ! -z "$PATCHWINDOW" ]; then

                              echo $PATCHWINDOW

                              PATCH_DOW=`echo $PATCHWINDOW | cut -d_ -f1`

                              PATCH_TIME=`echo $PATCHWINDOW | cut -d_ -f2`

                              PATCH_TIME_START=`echo $PATCH_TIME | cut -d\- -f1`

                              if [ "$PATCH_TIME_START" = "00:00" ]; then

                                  EPOCH_START=0

                              elif [ "$PATCH_TIME_START" = "04:00" ]; then

                                  EPOCH_START=14400

                              elif [ "$PATCH_TIME_START" = "06:00" ]; then

                                  EPOCH_START=21600

                              elif [ "$PATCH_TIME_START" = "08:00" ]; then

                                  EPOCH_START=28800

                              elif [ "$PATCH_TIME_START" = "12:00" ]; then

                                  EPOCH_START=43200

                              elif [ "$PATCH_TIME_START" = "16:00" ]; then

                                  EPOCH_START=57600

                              elif [ "$PATCH_TIME_START" = "20:00" ]; then

                                  EPOCH_START=72000

                              fi

                             

                              if [ "$PATCH_DOW" = "THURS" ]; then

                                  SCHEDULE_DAY=$THU_EPOCH

                              elif [ "$PATCH_DOW" = "FRI" ]; then

                                  SCHEDULE_DAY=$FRI_EPOCH

                              elif [ "$PATCH_DOW" = "SAT" ]; then

                                  SCHEDULE_DAY=$SAT_EPOCH

                              elif [ "$PATCH_DOW" = "SUN" ]; then

                                  SCHEDULE_DAY=$SUN_EPOCH

                              elif [ "$PATCH_DOW" = "MON" ]; then

                                  SCHEDULE_DAY=$MON_EPOCH

                              elif [ "$PATCH_DOW" = "TUES" ]; then

                                  SCHEDULE_DAY=$TUE_EPOCH

                              elif [ "$PATCH_DOW" = "WED" ]; then

                                  SCHEDULE_DAY=$WED_EPOCH

                              fi

                             

                              SCHEDULE_EPOCH=$(( $SCHEDULE_DAY+$EPOCH_START ))

                             

                              SCHEDULE_STRING=`blexpr 'show_date ('$SCHEDULE_EPOCH', "%Y-%m-%d %H:%M:00")' | tr -d '[:cntrl:]'`

                              echo $SCHEDULE_STRING

                             

                              JOB_KEY=`blcli $CLIOPTIONS DeployJob getDBKeyByGroupAndName "$SERVER_GROUP/Templates" "$JOB_NAME"`

                              PATCH_JOB_KEY=`blcli $CLIOPTIONS PatchingJob getDBKeyByGroupAndName "$SERVER_GROUP/$PATCHWINDOW_GROUP" "Patch Analysis"`

                              PATCH_JOB_KEY=`blcli $CLIOPTIONS PatchingJob getDBKeyByGroupAndName "$SERVER_GROUP/Templates" "Remediate-NonProd"`

                      #        JOB_KEY=`blcli $CLIOPTIONS DeployJob setAdvanceDeployJobPhaseScheduleByDBKey $JOB_KEY AtTime "$STAGE_DATE_STRING" AfterPreviousPhase "$STAGE_DATE_STRING" AtTime "$SCHEDULE_STRING"`

                              JOB_KEY=`blcli $CLIOPTIONS DeployJob addOneTimeSchedule $JOB_KEY "$SCHEDULE_STRING"`

                              blcli $CLIOPTIONS PatchingJob setRemediationWithDeployOptions "$PATCH_JOB_KEY" "prefix" "$SERVER_GROUP/Remediation" "$SERVER_GROUP/Remediation" "$JOB_KEY"

                          fi

                         

                      done

                       

                      CLIOPTIONS=" -v defaultProfile -r BLAdmins"

                      SERVER_GROUP=/Windows/Implementation/Tasks/Patching

                      JOB_NAME=PatchTemplate

                      EPOCH_STAGE_DATE_STRING=`blexpr 'get_date()+86400' | tr -d '[:cntrl:]'`

                      STAGE_DATE_STRING=`blexpr 'show_date ('$EPOCH_STAGE_DATE_STRING', "%Y-%m-%d %H:%M:00")' | tr -d '[:cntrl:]'`

                      EPOCH_SCHEDULE_STRING=`blexpr 'get_date()+105400' | tr -d '[:cntrl:]'`

                      SCHEDULE_STRING=`blexpr 'show_date ('$EPOCH_SCHEDULE_STRING', "%Y-%m-%d %H:%M:00")' | tr -d '[:cntrl:]'`

                      JOB_KEY=`blcli $CLIOPTIONS DeployJob getDBKeyByGroupAndName "$SERVER_GROUP/Templates" "$JOB_NAME"`

                      PATCH_JOB_KEY=`blcli $CLIOPTIONS PatchingJob getDBKeyByGroupAndName "$SERVER_GROUP/Templates" "Remediate-NonProd"`

                      JOB_KEY=`blcli $CLIOPTIONS DeployJob clearDeployJobSchedulesByDBKey "$JOB_KEY"`

                      PATCH_JOB_KEY=`blcli $CLIOPTIONS PatchingJob setRemediationWithDeployOptions "$PATCH_JOB_KEY" "prefix" "$SERVER_GROUP/Remediation" "$SERVER_GROUP/Remediation" "$JOB_KEY"`

                      JOB_KEY=`blcli $CLIOPTIONS DeployJob setAdvanceDeployJobPhaseScheduleByDBKey $JOB_KEY AtTime "$STAGE_DATE_STRING" AfterPreviousPhase "$STAGE_DATE_STRING" AtTime "$SCHEDULE_STRING"`

                      blcli $CLIOPTIONS PatchingJob setRemediationWithDeployOptions "$PATCH_JOB_KEY" "prefix" "$SERVER_GROUP/Remediation" "$SERVER_GROUP/Remediation" "$JOB_KEY"

                       

                       

                      JOB_KEY=`blcli $CLIOPTIONS DeployJob addOneTimeSchedule $JOB_KEY "$SCHEDULE_STRING"`

                       

                      ------------------------

                       

                      #!/bin/nsh

                       

                      # v0.0.1

                      # This script is designed to find a patch analysis job, then for each

                      # target in the job, generate a patch deployment job.

                      #

                      # Use as is, free to redistribute, and no Warranty or support will be provided.

                      #

                      # Call this script with the following parameters:

                      #   $1  Region (ex: America)

                      #   $2  DataCenterName (ex: DC.FL_LAB)

                      #   $3  OS (ex: RHEL)

                      #

                       

                      if [ "$#" != 3 ]

                      then

                        echo "Usage: nsh $0 Region DataCenterName OS"

                        exit 1

                      fi

                       

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

                      DEBUG=true

                      # Folder (group) containing Patch Analysis job

                      JOB_FOLDER="/UNIX Patching/DATA CENTERS/$1/$2/Analysis Jobs/$3"

                      # Name of the Patch Analysis job

                      JOB_NAME="$3 Security Patch Analysis-$2"

                      # Name of the job group where the new job is going to be saved.

                      JOB_GROUP_NAME="/UNIX Patching/DATA CENTERS/$1/$2/Deploy Jobs/$3"

                      # Name of the job group where the generated Deploy Job(s) and Batch Job is going to be saved.

                      DEPLOY_JOB_GROUP_NAME="/UNIX Patching/DATA CENTERS/$1/$2/Deploy Jobs/$3"

                      # Name of the depot group where the new package is going to be saved.

                      DEPOT_GROUP_NAME="/UNIX Patching/Data Center Objects - Remediation Items (Deploy Jobs)/$2/$3"

                      # Remediation Template.  This Patch Remediation Job should contain the right

                      # ACL Policy and deploy options.

                      TEMPLATE_JOB_NAME="$2-Deploy Job Template"

                      TEMPLATE_JOB_GROUP="/UNIX Patching/DATA CENTERS/$1/$2/Deploy Jobs/$3/Deploy Job Template"

                      PWD=`pwd`

                      DEP_FILE="${PWD}/dep.out"

                      RUNNING_HOST=`echo $HOST`

                      TMP_FILE="c:\\Temp\\dep.out"

                      RUNNING_HOST_TMP_FILE="//${RUNNING_HOST}/Temp/dep.out"

                       

                      echo

                      echo "JOB_FOLDER is: "

                      echo $JOB_FOLDER

                      echo

                      echo "JOB_NAME is: "

                      echo $JOB_NAME

                      echo

                      echo "JOB_GROUP_NAME is: "

                      echo $JOB_GROUP_NAME

                      echo

                      echo "DEPLOY_JOB_GROUP_NAME is: "

                      echo $DEPLOY_JOB_GROUP_NAME

                      echo

                      echo "DEPOT_GROUP_NAME is: "

                      echo $DEPOT_GROUP_NAME

                      echo

                      echo "TEMPLATE_JOB_NAME is: "

                      echo $TEMPLATE_JOB_NAME

                      echo

                      echo "TEMPLATE_JOB_GROUP is: "

                      echo $TEMPLATE_JOB_GROUP

                      echo

                       

                      echo "RUNNING_HOST=$RUNNING_HOST"

                       

                       

                      print_debug()

                      {

                          if [ "${DEBUG}" = "true" ]

                              then

                              echo " "

                              echo "DEBUG: $@"

                          fi

                      }

                       

                      print_info()

                      {

                          echo " "

                          echo "INFO: $@"

                      }

                       

                      check_errs()

                      {

                        # Function. Parameter 1 is the return code

                        # Para. 2 is text to display on failure.

                        if [ "${1}" -ne "0" ]; then

                          echo "ERROR # ${1} : ${2}"

                          # as a bonus, make our script exit with the right error code.

                          exit ${1}

                        fi

                      }

                       

                      gen_patch_job()

                      {

                          TODAY=`date +%Y%m%d%H%M`

                          # Specify prefix to be used in package name.

                          PKG_PREFIX="${1}_${3}"

                          # Get Patching Job DBKey by job group name and job name.

                          PA_JOB_RUNKEY="$2"

                          # Name of the new job.

                          REMEDIATION_JOB_NAME="${1}_Remediation_${TODAY}"

                       

                          print_debug "blcli_execute PatchRemediationJob createRemediationJobWithDeployOptsForATarget" "$REMEDIATION_JOB_NAME" "$JOB_GROUP_NAME" "$PA_JOB_RUNKEY" "$1" "$PKG_PREFIX" "$DEPOT_GROUP_NAME" "$DEPLOY_JOB_GROUP_NAME" "$4"

                          blcli_execute PatchRemediationJob createRemediationJobWithDeployOptsForATarget "$REMEDIATION_JOB_NAME" "$JOB_GROUP_NAME" "$PA_JOB_RUNKEY" "$1" "$PKG_PREFIX" "$DEPOT_GROUP_NAME" "$DEPLOY_JOB_GROUP_NAME" "$4"

                          check_errs $? "BLCLI ERROR"

                          blcli_storeenv DB_KEY

                          print_info "Generated Patch Remediation Jobs for $1"

                         

                          blcli_execute PatchRemediationJob executeJobAndWait "$DB_KEY"

                          check_errs $? "BLCLI ERROR"

                          blcli_storeenv JRUN_KEY

                       

                          # Set the ACL on the DEPLOY_JOB_GROUP_NAME (where deploy jobs are stored)

                          blcli_execute JobGroup applyAclPolicy "$DEPLOY_JOB_GROUP_NAME" "$5"

                          check_errs $? "BLCLI ERROR"

                         

                          # Now check out the Job run result

                          blcli_execute JobRun jobRunKeyToJobRunId "$JRUN_KEY"

                          check_errs $? "BLCLI ERROR"

                          blcli_storeenv JRUN_ID

                       

                          `sleep 5`

                         

                          blcli_execute JobRun getLogItemsByJobRunId "$DB_KEY" "$JRUN_ID"

                          check_errs $? "BLCLI ERROR"

                          blcli_storeenv ITEMLOG

                          print_debug "$ITEMLOG"

                         

                          # parse the log to find the package and deploy job to apply acl policy

                          ITEMLOG=`echo $ITEMLOG | sed 's/Type: Info Date:/:/g'`

                          print_debug "$ITEMLOG"

                         

                          # find the pkg create (this is for Linux, need to test out for Solaris, HPUX, and and AIX

                          CREATED_PKG=`echo $ITEMLOG | awk '{split($0,a,":"); for (i in a) if (a[i] == " Created BlPackage") print a[i+1]}'`

                          #trim the front and end spaces

                          CREATED_PKG=`echo $CREATED_PKG | awk '{i=split($0,a,"/"); print a[i]}'`

                          # find the deploy job

                          CREATED_DPJOB=`echo $ITEMLOG | awk '{split($0,a,":"); for (i in a) if (a[i] == " Created deploy job") print a[i+1]}'`

                          CREATED_DPJOB=`echo $CREATED_DPJOB | awk '{i=split($0,a,"/"); print a[i]}'`

                          print_debug "PKG=$CREATED_PKG"

                          print_debug "DPJOB=$CREATED_DPJOB"

                         

                          # ONLY Proceed if deploy job created

                          if [ -n "$CREATED_DPJOB" ]

                          then

                              #Apply ACL policy on Package

                              blcli_execute BlPackage getDBKeyByGroupAndName "$DEPOT_GROUP_NAME" "$CREATED_PKG"

                              check_errs $? "BLCLI ERROR"

                              blcli_storeenv DEPOT_KEY

                       

                              blcli_execute DepotObject applyAclPolicy "$DEPOT_KEY" "$5"

                              check_errs $? "BLCLI ERROR"

                         

                              #Apply ACL to the deploy job, we'll not apply ACL to the batch job because the batch is useless

                              blcli_execute DeployJob getDBKeyByGroupAndName "$DEPLOY_JOB_GROUP_NAME" "$CREATED_DPJOB"

                              check_errs $? "BLCLI ERROR"

                              blcli_storeenv DPJOB_KEY

                         

                              blcli_execute Job applyAclPolicy "$DPJOB_KEY" "$5"

                              check_errs $? "BLCLI ERROR"

                             

                              # Find and REMOVE the Batch Job because it's useless

                              blcli_execute Utility exportDependencyGraph DEPLOY_JOB "$DEPLOY_JOB_GROUP_NAME" "$CREATED_DPJOB" "$TMP_FILE" true 0

                              check_errs $? "BLCLI ERROR"

                              `cp $RUNNING_HOST_TMP_FILE $DEP_FILE`

                              BATCH_JOB=`awk 'END{print}' ${DEP_FILE} | awk '{split($0,a,","); print a[1]}'`

                              BATCH_JOB=`echo $BATCH_JOB | sed 's/\"//g'`

                              echo "deleting=$BATCH_JOB"

                              blcli_execute BatchJob deleteJobByGroupAndName "$DEPLOY_JOB_GROUP_NAME" "$BATCH_JOB"       

                             

                              # if this is Linux, we need to apply ACL on the custom package as well.

                              blcli_execute Server printPropertyValue "${1}" "OS"

                              blcli_storeenv OS_TYPE

                              if [ "${OS_TYPE}" = "Linux" ]

                              then

                                  blcli_execute Utility exportDependencyGraph DEPLOY_JOB "$DEPLOY_JOB_GROUP_NAME" "$CREATED_DPJOB" "$TMP_FILE" false 0

                                  `cp $RUNNING_HOST_TMP_FILE $DEP_FILE`

                                  check_errs $? "BLCLI ERROR"

                                  blcli_storeenv CUSTOM_PKG

                                  CUSTOM_PKG=`grep "_Remediation_" ${DEP_FILE} | awk '{split($0,a,","); print a[1]}'`

                                  CUSTOM_PKG=`echo $CUSTOM_PKG | sed 's/\"//g'`

                                  echo "CustomPKG=$CUSTOM_PKG"   

                                  echo "GROUP=$DEPOT_GROUP_NAME"

                                 

                                  blcli_execute DepotObject getDBKeyByTypeStringGroupAndName "CUSTOM_SOFTWARE_INSTALLABLE" "$DEPOT_GROUP_NAME" "$CUSTOM_PKG"

                                  check_errs $? "BLCLI ERROR"

                                  blcli_storeenv DEPOT_KEY

                       

                                  blcli_execute DepotObject applyAclPolicy "$DEPOT_KEY" "$5"

                                  check_errs $? "BLCLI ERROR"                                       

                              fi

                              print_info "Patch Package and Deploy Job generated."

                          fi

                          return 0

                      }

                       

                       

                      #Main script

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

                      # Initialize BLCLI

                      print_info "$TODAY"

                      print_info "Initialize BLCLI"

                      print_debug "blcli_disconnect"

                      blcli_disconnect

                      check_errs $? "BLCLI ERROR"

                      print_debug "blcli_init"

                      blcli_init

                      check_errs $? "BLCLI ERROR"

                      print_debug "blcli_setoption roleName BLAdmins"

                      blcli_setoption roleName BLAdmins

                      blcli_setoption serviceProfileName defaultProfile

                      #blcli_setoption serviceProfileName SOTA_SRP

                      check_errs $? "BLCLI ERROR"

                      print_debug "blcli_connect"

                      blcli_connect

                      check_errs $? "BLCLI ERROR"

                       

                      # strip off the suffix as ACL Policy

                      ACL_POLICY=`echo $JOB_NAME | awk '{split($0,a,"-"); print a[2]}'`

                      print_debug "$ACL_POLICY"

                      # make the Remediation Job based on the analysis job

                      REM_JOB_NAME=`echo $JOB_NAME | sed 's/Analysis/Deploy/g'`

                      print_debug "$REM_JOB_NAME"

                       

                      # get the remediation template job key

                      print_debug "blcli_execute PatchRemediationJob getDBKeyByGroupAndName $TEMPLATE_JOB_GROUP $TEMPLATE_JOB_NAME"

                      blcli_execute DeployJob getDBKeyByGroupAndName "$TEMPLATE_JOB_GROUP" "$TEMPLATE_JOB_NAME"

                      check_errs $? "BLCLI ERROR"

                      blcli_storeenv TEMPLATE_JOB_KEY

                      #print_debug "JOB_KEY: $TEMPLATE_JOB_KEY"

                       

                       

                      # get the patch analysis job key

                      print_debug "blcli_execute PatchingJob getDBKeyByGroupAndName $JOB_FOLDER $JOB_NAME"

                      blcli_execute PatchingJob getDBKeyByGroupAndName "$JOB_FOLDER" "$JOB_NAME"

                      check_errs $? "BLCLI ERROR"

                      blcli_storeenv JOB_KEY

                      print_debug "JOB_KEY: $JOB_KEY"

                       

                      # get the last job run key

                      blcli_execute JobRun findLastRunKeyByJobKey $JOB_KEY

                      check_errs $? "BLCLI ERROR"

                      blcli_storeenv JOB_RUN_KEY

                      print_debug "JOB_RUN_KEY: $JOB_RUN_KEY"

                       

                      #get the target list

                      blcli_execute Job getTargetServers $JOB_KEY "ENROLLED"

                      check_errs $? "BLCLI ERROR"

                      blcli_storeenv TARGETS

                      # strip the bracket from [...]

                      TARGETS=`echo $TARGETS | awk  -F'.' '{print substr($0, 2, length()-2)}'`

                      TARGETS=`echo $TARGETS | sed 's/\,//g'`

                      print_debug "TARGETS: $TARGETS"

                       

                      # Iterate through the TARGET in TARGETS.

                      for TARGET in ${TARGETS}

                          do

                              print_debug "TARGET=$TARGET"

                              # make sure the target server exist, else don't bother

                              blcli_execute Server serverExists "$TARGET"

                              check_errs $? "BLCLI ERROR"

                              blcli_storeenv SERVER_EXIST

                       

                              if [ "${SERVER_EXIST}" = "true" ]

                              then

                                  gen_patch_job "$TARGET" "$JOB_RUN_KEY" "$REM_JOB_NAME" "$TEMPLATE_JOB_KEY" "$ACL_POLICY"

                              fi

                          done   

                      echo "Script Complete."

                      print_debug "blcli_destroy"

                      blcli_destroy

                      check_errs $? "BLCLI ERROR"

                       

                      exit 0

                      • 9. Re: NSH Date Manipulation
                        Bill Robinson

                        that's a lot of date math.  i wonder how much of it can be done w/ 'strftime' which is a zsh module (zmodload zsh/datetime)...

                         

                        also FOO=`blcli xx` is bad.  should be

                        blcli_execute blah

                        blcli_storeenv FOO

                        • 10. Re: NSH Date Manipulation
                          Matthew Ragland

                          How would I go about getting the 2nd Tuesday of last month? I am trying to programmatically update a smartgroup that contains all patches add since last catalog update, and we update the catalog every patch Tuesday. So far, using Sean's code, I am able to calculate the current month's patch Tuesday:

                           

                          CURRENT_MON=`blexpr 'show_date (get_date(), "%m")' | tr -d '[:cntrl:]'`

                          TODAY_AS_INT=`blexpr 'show_date (get_date(), "%d")' | tr -d '[:cntrl:]'`

                          if [ $TODAY_AS_INT = 01 ]; then

                           

                              P=0

                              P=$(( $P + $TODAY_EPOCH_OFFSET ))

                              FIRST_DAY_OF_MONTH=`blexpr 'show_date (get_date(), "%Y-%m-%d")' | tr -d '[:cntrl:]'`

                              FIRST_DAY_OF_MONTH_EPOCH=`blexpr 'get_date()' | tr -d '[:cntrl:]'`

                          else

                              P=0

                              TODAY_AS_INT=`blexpr 'show_date (get_date(), "%d")' | tr -d '[:cntrl:]'`

                              while [ $TODAY_AS_INT != 01 ]; do

                           

                                  P=$(( $P + 86400 ))

                                  TODAY_AS_INT=`blexpr 'show_date (get_date()-'$P', "%d")' | tr -d '[:cntrl:]'`

                              done

                           

                              # set 1st day of current calendar month property in both YYYY-MM-DD format and EPOCH

                              FIRST_DAY_OF_MONTH=`blexpr 'show_date (get_date()-'$P', "%Y-%m-%d")' | tr -d '[:cntrl:]'`

                              FIRST_DAY_OF_MONTH_EPOCH=`blexpr 'get_date()-'$P'' | tr -d '[:cntrl:]'`

                          fi

                           

                          z=0

                          CURRENT_DOW=`blexpr 'show_date ('$FIRST_DAY_OF_MONTH_EPOCH',"%A")' | tr -d '[:cntrl:]'`

                          echo $CURRENT_DOW

                           

                          if [ "$CURRENT_DOW" = "Monday" ]; then

                              SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*8 ))

                          elif  [ "$CURRENT_DOW" = "Tuesday" ]; then

                              SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*7 ))

                          elif  [ "$CURRENT_DOW" = "Wednesday" ]; then

                              SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*13 ))

                          elif  [ "$CURRENT_DOW" = "Thursday" ]; then

                              SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*12 ))

                          elif  [ "$CURRENT_DOW" = "Friday" ]; then

                              SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*11 ))

                          elif  [ "$CURRENT_DOW" = "Saturday" ]; then

                              SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*10 ))

                          elif  [ "$CURRENT_DOW" = "Sunday" ]; then

                              SECOND_TUESDAY_EPOCH=$(( $FIRST_DAY_OF_MONTH_EPOCH+86400*9 ))

                          fi

                           

                           

                          DATE_STRING=`blexpr 'show_date ('$SECOND_TUESDAY_EPOCH', "%Y-%m-%d 00:00:00")' | tr -d '[:cntrl:]'`

                          echo "PATCH TUESDAY:${DATE_STRING}"

                          • 11. Re: NSH Date Manipulation
                            Bill Robinson

                            i use the strftime instead of blquery (zmodload zsh/datetime) to do date maths.

                             

                            $EPOCHSECONDS is now

                            get the now month in numbers.  subtract 1 from the month, handle when it's jan

                            strftime %m $EPOCHSECONDS

                            flip that to epoch:

                            strftime -r "%m-%d-%Y" '08-01-2017'

                            get the day name

                            strftime "%a" 1501560000

                            if it's Tue, profit!

                            else

                            add one day to that epoch time (1501560000)

                            check if it's tue, do that until you find the 1st tues

                            then you should have the day number and month.

                            ?

                            • 12. Re: NSH Date Manipulation
                              Matthew Ragland

                              Bill,

                              I cheated, and I created a variable with all the 2nd Tuesdays for the next many months:

                               

                              # CREATE VARIABLE WITH CURRENT MONTH & PREVIOUS MONTH'S 2ND TUESDAY

                              SECONDTUESDAYS="201709 2017-08-08,201710 2017-09-12,201711 2017-10-10,201712 2017-11-14,201801 2017-12-12"

                               

                              # PARSE THE VARIABLE TO CAPTURE PREVIOUS MONTH'S 2ND TUESDAY

                              SGDATE=`echo "${SECONDTUESDAYS}" | tr ',' '\n' | grep "$(date +%Y%m)" | cut -d " " -f 2`

                               

                              In true Bill Robinson form:
                              2016-12-21 17_08_30.png

                              • 13. Re: NSH Date Manipulation
                                Bill Robinson

                                I cheated, and I created a variable with all the 2nd Tuesdays for the next many months:

                                so lame, boooo!

                                 

                                just remember to update it next year.

                                 

                                i did that whole date math thing for a customer before we had maintenance windows.  they had a window property w/ values like '2-SAT', '3-SUN'. we ended up having a nsh script figure out what day it was of the month then look for a smart job or server group that listed the corresponding jobs or targets in the window and then ran whatever needed to run.  not sure how much they ended up using it but date math is fun.

                                • 14. Re: NSH Date Manipulation
                                  Matthew Ragland

                                  My variable goes all the way until 2099  

                                   

                                  Sent from my iPhone