CFEngine 3 :: Read in system commands and use them to calculate new variables
By cyril on Monday 29 April 2013, 22:11 - CFEngine - Permalink
How to read in system commands and use them to calculate new variables ?
Bash script :
#!/bin/bash # # hugepages_settings.sh # # Linux bash script to compute values for the # recommended HugePages/HugeTLB configuration # # Note: This script does calculation for all shared memory # segments available when the script is run, no matter it # is an Oracle RDBMS shared memory segment or not. # Check for the kernel version KERN=`uname -r | awk -F. '{ printf("%d.%d\n",$1,$2); }'` # Find out the HugePage size HPG_SZ=`grep Hugepagesize /proc/meminfo | awk {'print $2'}` # Start from 1 pages to be on the safe side and guarantee 1 free HugePage NUM_PG=1 # Cumulative number of pages required to handle the running shared memory segments for SEG_BYTES in `ipcs -m | awk {'print $5'} | grep "[0-9][0-9]*"` do MIN_PG=`echo "$SEG_BYTES/($HPG_SZ*1024)" | bc -q` if [ $MIN_PG -gt 0 ]; then NUM_PG=`echo "$NUM_PG+$MIN_PG+1" | bc -q` fi done # Finish with results case $KERN in '2.4') HUGETLB_POOL=`echo "$NUM_PG*$HPG_SZ/1024" | bc -q`; echo "Recommended setting: vm.hugetlb_pool = $HUGETLB_POOL" ;; '2.6') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;; *) echo "Unrecognized kernel version $KERN. Exiting." ;; esac # End
"meminfo" string => execresult( "${paths.path[cat] /proc/meminfo", "noshell" ); memtotal:: "memtotal" string => "${mi_array[1]"; "myvar" string => execresult( "${paths.path[bc]} <<< '${SEG_BYTES / 2', "noshell" ); You might need useshell rather than noshell. You can pull data from ${meminfo} using regextract. classes: "memtotal" expression => regextract( "(?i).*?memtotal:\s*(\d*).*", "${meminfo}", "mi_array" );
Answered by Neil Watson