#!/bin/sh
#
# A simple example script to alert Perforce admin should available license
# fall below a user specified threshold.
#
# Warning:
# If you run this from cron, make sure you don't overload your server
# running this too often...
#
# @author: Lester Cheung <lcheung@perforce.com>
#
# TODO: deal with user/client/login
#
#
# Copyright (c) Perforce Software, Inc., 1997-2009. All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE
# SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
P4=/usr/local/bin/p4
MAILX=/usr/bin/mail
LIC_THRESHOLD=1000
MAILTO=lcheung@perforce.com
function usage()
{
echo "Usage:"
echo " Do a 'p4 -p <P4PORT> -u <P4USER> login' then run..."
echo " $0 <P4PORT> <P4USER>"
exit 1
}
[ $# -eq 2 ] || usage
P4PORT=$1
P4USER=$2
# p4 info will work without an account/workspace...
OPTS="-p $P4PORT -u $P4USER"
LICENSE_AVA=`$P4 $OPTS info 2>/dev/null|grep '^Server license:'|sed 's| users.*$||'|sed 's|.* ||'`
LICENSE_USED=`$P4 $OPTS users 2>/dev/null|wc -l`
LIC_REMINDING=$(($LICENSE_AVA - $LICENSE_USED))
if [ $LIC_REMINDING -lt $LIC_THRESHOLD ] ; then
echo "$LIC_REMINDING license(s) reminding. Sending email to $MAILTO..."
echo "$LIC_REMINDING Perforce license(s) reminding!" | \
$MAILX -s "$LIC_THRESHOLD Perforce license(s) remaining" $MAILTO
if [ $? -ne 0 ]; then
echo "ERROR: Problem sending email!"
else
echo "Done!"
fi
fi