Zoneshare

From pressy's brainbackup
Jump to: navigation, search

zoneshare

root@venus # ./zoneshare -? 
Usage:
	Report:
		./zoneshare -l [-z <zonename>]
			-l list all zones
			-z list only that one
	Manipulate:
		./zoneshare [-l] -z <zonename> -v <value> [-p]
			-l list the changed config
			-z zone to change
			-v new cpu-share value
			-p make the config permanent
root@venus # ./zoneshare -l 
 ZONENAME	   SHARES	   STATUS	   CONFIG	   POOL
 global                 1         running               -             -
  testzone              1         running               -             -
  OPScenter             2         running               2         POOL1
root@venus # ./zoneshare -l -z OPScenter -v 4 
 ZONENAME	   SHARES	   STATUS	   CONFIG	   POOL
  OPScenter             4         running               2         POOL1
root@venus #
#!/usr/bin/ksh
#set -x
######################################################################
#
# zoneshare v0.4
#
# small script to see and change CPU shares for zones in Solaris.
#
# 17.09.2010, written by Martin Presslaber
#
#
# NOTES:
# Tested on Solaris10 u8 and u9. It won't work with Sol10 releases
# which do not have a cpu-shares property in zonecfg.
# If you have questions or comments feel free to contact me via mail.
#
# Tried to improve that script to run on solaris 11. 
# v0.4 - 29.11.12 Tested and small changes for 11.1
#
######################################################################

########################### SOME VARs ################################
ZONEADM=/usr/sbin/zoneadm
PRCTL=/usr/bin/prctl
ZONECFG=/usr/sbin/zonecfg
DISPADMIN=/usr/sbin/dispadmin
UNAME=/usr/bin/uname
######################################################################

########################### SOME HELP ################################
help ()
        {
                print "Usage:"
                print "\tReport:"
                print "\t\t$0 -l [-z <zonename>]"
                print "\t\t\t-l list all zones"
                print "\t\t\t-z list only that one"
                print "\tManipulate:"
                print "\t\t$0 [-l] -z <zonename> -v <value> [-p]"
                print "\t\t\t-l list the changed config"
                print "\t\t\t-z zone to change"
                print "\t\t\t-v new cpu-share value"
                print "\t\t\t-p make the config permanent"
        }
######################################################################

######################### SOME PRECHECKS #############################
OS=$($UNAME -r)
# no more /usr/ucb in sol11, and yes, only works on solaris
[[ $($UNAME -s) == SunOS ]] || (echo "This script will work on Solaris only" && exit 1)
if [ ${OS#*.} -gt 10 ] ; 
then 
	ID=$(/usr/bin/whoami)
else
	ID=$(/usr/ucb/whoami)
fi
####
if [ $ID != "root" ]; then
        echo "$ID, you must be root to run this program."
        exit 1
fi
if [ $# -lt 1 ]
        then
                help && exit 1
        fi
######################################################################

######################## WHICH OPTIONS ###############################
while getopts ":z:lv:ph" args
do
        case $args in
        z)
                zone=$OPTARG
                if [[ $zone != $($ZONEADM list -c | grep -- $zone) ]]
                then
                        print "ERROR: Wrong zone name or missing argument" && help && exit 5
                fi
                ;;
        l)
                list=yes
                ;;
        v)
                # chance
                value=$OPTARG
                [[ $value == +([0-9]) ]] && [[ $value -gt 0 && $value -lt 65535 ]] || value=WRONG
                ;;
        p)
                permanent=yes
                ;;

        h|*)
                help && exit 0
                ;;
        esac
done
shift $(($OPTIND -1))
######################################################################


############################ LET's GO ################################
listZ ()
        {
                # OUTPUT
                print " ZONENAME\t   SHARES\t   STATUS\t   CONFIG\t   POOL"
                if [[ $zone == "" ]]
                then
                        for zonelist in $($ZONEADM list -c);
                        do
                                # Zone-State
                                zonestatus=$($ZONEADM list -cp | nawk  -F: -v zname="$zonelist" '$2~zname {print $3}')
                                configvalue=$($ZONECFG -z $zonelist info cpu-shares)
                                configvalue=${configvalue#*:}; configvalue=${configvalue%*]}; configvalue=${configvalue:="-"}
                                configpool=$($ZONECFG -z $zonelist info pool)
                                configpool=${configpool#* }; configpool=${configpool%*]}; configpool=${configpool:="-"}
                                if [[ $zonestatus == "running" ]]
                                then
                                        testv=$($PRCTL -P -t privileged -n zone.cpu-shares -i zone $zonelist | awk '$2 ~ /priv/ {print $3}')
                                        [[ $zonelist == "global" ]] && \
					printf " %-12s %11s %15s %15s %13s\n" $zonelist $testv $zonestatus $configvalue $configpool || \
                                        printf "  %-12s %10s %15s %15s %13s\n" $zonelist $testv $zonestatus $configvalue $configpool
                                else
                                        printf "  %-12s %10s %15s %15s %13s\n" $zonelist "-" $zonestatus $configvalue $configpool
                                fi
                        done
                else
                        zonestatus=$($ZONEADM list -cp | nawk  -F: -v zname="$zone" '$2~zname {print $3}')
                        configvalue=$($ZONECFG -z $zone info cpu-shares)
                        configvalue=${configvalue#*:}; configvalue=${configvalue%*]}; configvalue=${configvalue:="-"}
                        configpool=$($ZONECFG -z $zone info pool)
                        configpool=${configpool#* }; configpool=${configpool%*]}; configpool=${configpool:="-"}
                        if [[ $zonestatus == "running" ]]
                        then
                                testv=$($PRCTL -P -t privileged -n zone.cpu-shares -i zone $zone | awk '$2 ~ /priv/ {print $3}')
                                printf "  %-12s %10s %15s %15s %13s\n" $zone $testv $zonestatus $configvalue $configpool
                        else
                                printf "  %-12s %10s %15s %15s %13s\n" $zone "-" $zonestatus $configvalue $configpool
                        fi
                fi
        }

if [[ $zone == "" ]]
 then
        [[ $list == "yes" ]] && listZ || help
 else
        if [[ $zone == $($ZONEADM list -c | grep $zone) ]]
         then
                if [[ $value != "" ]]
                then
                         if [[ $value == "WRONG" ]]
                         then
                                print "ERROR:\tValue must be an integer [1-65534]!" && exit 3
                         else
                                zonestatus=$($ZONEADM list -cp | nawk  -F: -v zname="$zone" '$2~zname {print $3}')
                                if [[ $zonestatus != "running" ]]
                                then
                                        [[ $permanent == "yes" ]] && $ZONECFG -z $zone set cpu-shares=$value || \
                                        print "ERROR: Can't change live-value for zone $zone when state is \"$zonestatus\""
                                else
                                        $PRCTL -n zone.cpu-shares -r -v $value -i zone $zone
                                        [[ $permanent == "yes" ]] && $ZONECFG -z $zone set cpu-shares=$value
                                fi
                         fi
                fi

                [[ $list == "yes" ]] && listZ
         else
                print "ERROR:\tZone $zone not found!" && exit 1
         fi
 fi
######################################################################

########################## OPTIONS OK? ###############################
[[ $zone != "" && $value == "" && $list == "" ]] && print "$0 syntax error" && help
[[ $value != "" && $zone == "" ]] && print "INFO:\tIgnoring value, no zone specified"
######################################################################

############################# EXTRA ##################################
FSS=$($DISPADMIN -l | grep FSS)
[[ $FSS == "" ]] && print "\nINFO:\t It makes no sence to configure CPU shares if you don't use the fair share scheduler\n"
############################# EOF ####################################