top of page

Scripting

Whilst it is possible to get everything you need from the NBU Java console, Aptare and Opscenter, it may be quicker to generate the information you required via the command line. Once you are familiar with the commands required to achieve certain tasks then you will be in position to automate repetitive tasks or reporting. Below are some examples to get you started using standard UNIX or Linux utilities. In the examaples where gawk is used, awk or or any variant will work also.

The following line will enumerate the fields from bpdbjobs output for a job. As the fields are numbered the fields can be cross referenced against the bpdbjobs command details in the NetBackup  command reference to confirm which fields is which e.g.

Field 1: jobid

Field 4 : exit status

Field 5 : policy

Field 9 : start time (number of seconds since the epoch)

Field 10: duration

Field 11: end time (number of seconds since the epoch)

Field 15 : kilobytes written

# bpdbjobs -jobid <jobid> | gawk '{ split($0,ARRAY,",") ; size = length(ARRAY) ; for ( i = 1; i <= size; i++) print i, ARRAY[i] }'

In the above example, the output of the bpdbjobs command is piped into gawk after which teh follwing happens :

 

split($0,ARRAY,",") : The output of the command is split into individual fields, based on commas, which are then added to the array called ARRAY.

 

size = length(ARRAY) : The number of elements in ARRAY is assigned to the variable size

for (i = 1; i <= size ; i++ )  : A for loop which initialises the counter i to 1 ; will loop through until i is equal to the number of elements in ARRAY  as defined by the variable size. Each time through the loop the counter i is incremented by 1 as per i++.

 

print i, ARRAY[i] : During each iteration of the loop, this statement will print the value of the counter i, and the value held in the relevant array index for that value.

To sum up the amount of data backed up over the past 24 hours :

# bpimagelist -hoursago 24 -U | gawk 'NR > 2 { TOTAL += $5 } END { print TOTAL / ( 1024 * 1024 ) }'

In the above example, the output of the bpdbjobs command is piped into gawk after which the follwing happens :

 

NR > 2 :  This statement tells gawk to skip the first 2 records, i.e. the first 2 lines of the input, which is the columns headings in this example

{TOTAL += $5 } : As gawk loops through the output the variable TOTAL will be incremented with the value held in field 5

END : The actions which follow are to be performed once all the input has been processed, i.e. there are no more records or lines of input to be read.

{ print TOTAL / ( 1024 * 1024 ) } : The variable TOTAL will hold the amount of KB backed up as per the bpimagelist output, that figure is divided by  (1024*1024) to get amount in GB.

The gawk statement below will sum the amount of data written over the past 7 days by day

# bpimagelist -hoursago 168  -U | gawk 'NR > 2 { COUNT[$1] += $5 } END { for ( DATE in COUNT ) print DATE, COUNT[DATE] / (1024 * 1024 ) }' 

In the above example, the output of the bpdbjobs command is piped into gawk after which the follwing happens :

 

NR > 2 :  This statement tells gawk to skip the first 2 records, i.e. the first 2 lines of the input, which is the columns headings in this example

{ count[$1] += $5 } : This statement creates an array called count which uses field1, the date, as the index and field 5, the amount backed up as the value. The += operator tells gawk to sum the values for each date.

END : The actions which follow are to be performed once all the input has been processed, i.e. there are no more records or lines of input to be read.

{ for ( DATE in COUNT ) print DATE, COUNT[DATE] / ( 1024 * 1024 ) } : The for loop will loop over the indices of the array COUNT and for each index, defined by the variable date, it will print the index which is the date and the value associated with the date 

 It is possible to create arrays from substrstrings  of fields. The following example will give a count of jobs started per hour over the past 24 hours 

# bpimagelist -hoursago 24  -U | gawk 'NR > 2 { ++COUNT[substr($2,1,2)] END { for ( HOUR in COUNT ) print HOUR, COUNT[HOUR]  }' | sort -n -k1

In the above example, the output of the bpdbjobs command is piped into gawk after which the follwing happens :

 

NR > 2 :  This statement tells gawk to skip the first 2 records, i.e. the first 2 lines of the input, which is the columns headings in this example

{ count[$1] += $5 } : This statement creates an array called count which uses field1, the date, as the index and field 5, the amount backed up as the value. The += operator tells gawk to sum the values for each date.

END : The actions which follow are to be performed once all the input has been processed, i.e. there are no more records or lines of input to be read.

{ for ( DATE in COUNT ) print DATE, COUNT[DATE] / ( 1024 * 1024 ) } : The for loop will loop over the indices of the array COUNT and for each index, defined by the variable date, it will print the index which is the date and the value associated with the date 

The following piece of code will print out the smallest and largest backup from the past 24 hours

bpimagelist -hoursago 24 -U | gawk '
BEGIN { counter = 1 }
NR > 2 {
 if ( counter = 1 ) {
  min = $5
  minline = $0
  max = $5
  maxline = $0
 }
 if ( min > 5 ) {
  min = $5
  minline = $0
 }
 if ( max < $5 ) {
  max = $5
  maxline = $0
 }
 counter++
} END {
 printf "%-10d\t:\t%s\n", min, minline
 printf "%-10d\t:\t%s\n", max, maxline
}'

In the above example, the output of the bpdbjobs command is piped into gawk after which the follwing happens :

 

BEGIN { counter = 1 } :  The variable counter is initialised to 1 before any data is processed

NR > 2 :  This statement tells gawk to skip the first 2 records, i.e. the first 2 lines of the input, which is the columns headings in this example

if ( counter =  1 ) { : As the counter has already been set to one before any data has been processed, this statement will be true when awk reads the first line of data, line number 3, as the first 2 lines have already been skipped due to the NR statement above. As this statement is true, the variables contained within the {} will be  initialised to the values held in field 5 for the min and max variables and to entire line for minline and maxline.

if ( min > 5 ) { : Thesecond time around the loop, so when parsing record or line 4 of the output, tis conditional statement checks if the value held in the min variable is greater than the value in field 5 on the current line. If it is greater then the value in field 5 from line 4 is assigned to the min variable ans the entire line is assigned tothe minline variable. This check will be performed on every line until all input is read.

if ( max < 5 ) { : The next time around the loop, so when parsing record or line 4 of the output, tis conditional statement checks if the value held in the min variable is greater than the value in field 5 on the current line. If it is greater then the value in field 5 from line 4 is assigned to the min variable ans the entire line is assigned tothe minline variable. This check will be performed on every line until all input is read.

sort -n -k1 : sort the output on column 1, the hour, in ascending order

bottom of page