#! /bin/bash

#
# Copyright (c) 2002 Compaq Computer Corporation.  All Rights Reserved.
#
# Permission to use, copy, and modify this software and its documentation
# is hereby granted under the following terms and conditions.  The above
# copyright notice, this permission notice, and the following disclaimer
# must appear in all copies of the software, derivative works or modified
# versions.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND COMPAQ COMPUTER CORP. DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL COMPAQ COMPUTER
# CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#

# default libexecdir used to bootstrap scripts
libexecdir=${GLOBUS_LOCATION}/libexec

# load GRIS CPU common code and initialization
. ${libexecdir}/grid-info-mem-common

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

probe_resource_mem ()
{
	( /usr/bin/vmstat -s ; /usr/sbin/swapon -s ) 2>/dev/null |	\
	{
		eval `awk '						\
		   BEGIN { pgsz=8192; totswap=0 }			\
		      $1 == "Virtual" { pgsz=$6 }			\
		      $2 == "active" && $3 == "pages" { active=$1 }	\
		      $2 == "inactive" && $3 == "pages" { inactive=$1 }	\
		      $2 == "free" && $3 == "pages" { free=$1 }		\
		      $2 == "wired" && $3 == "pages" { wired=$1 }	\
		      $1 == "Total" && $2 == "swap" { totswap=1 }	\
		      totswap == 1 && $1 == "Allocated" { swalloc=$3 }	\
		      totswap == 1 && $1 == "Available" { swavail=$3 }	\
		   END { pgsz=pgsz/1024;				\
		      printf "_mem_size_mbytes=%u ",			\
		         (active+inactive+free+wired) * pgsz / 1024;	\
		      printf "_mem_free_mbytes=%u ",			\
		         (inactive+free) * pgsz / 1024;			\
		      printf "_vm_size_mbytes=%u ", swalloc * pgsz / 1024; \
		      printf "_vm_free_mbytes=%u", swavail * pgsz / 1024; \
		}'`

		# run this in initialized subshell environment
		emit_resource_mem_descriptions
	}
}

#############
# do the work
probe_resource_mem

