#!/bin/sh

#%# family=auto
#%# capabilities=autoconf

case "$1" in
    autoconf|detect)
	if [ -z "${pid_file}" ]; then
            echo "no (PID file isn't specified by env.pid_file)"
	    exit 1
        elif [ -f "${pid_file}" ]; then
            echo "yes"
            exit 0
        else
            echo "no (PID file doesn't exist: ${pid_file})"
            exit 1
        fi
        ;;
    config)
	if [ -z "${label}" ]; then
	    title="groonga: memory usage"
	else
	    title="groonga: ${label}: memory usage"
	fi
        cat <<EOF
graph_title ${title}
graph_vlabel memory usage
graph_category groonga
graph_info groonga memory usage

rss.label resident set size
rss.type GAUGE
vsz.label virtual memory size
vsz.type GAUGE
EOF
        exit 0
        ;;
    *)
esac

if [ -z "${pid_file}" ]; then
    echo "PID file isn't specified by env.pid_file"
    exit 1
fi

groonga_pid=$(cat ${pid_file})
read rss_in_kb vsz_in_kb <<EOC
$(ps h -o rss,vsz -p ${groonga_pid})
EOC
echo "rss.value ${rss_in_kb}000"
echo "vsz.value ${vsz_in_kb}000"
