#! /bin/bash ############################################################################### # Copyright (c) Perforce Software, Inc., 2007-2016. 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. ############################################################################### # # Last Modified: $Date$ # Submitted by: $Author$ # Revision: $Revision$ # # Usage: # check_helix_p4d_health -p [--all] # check_helix_p4d_health -p [--licensecheck] [-l ] # check_helix_p4d_health -p [--pidcheck] [-p ] # check_helix_p4d_health -p [--moncheck] [-p ] # check_helix_p4d_health -p [--version] [--help] # # The '--all' flag runs all tests. # # The '--licensecheck' flag tests if the license file is nearing it's expiry date. # By default it checks for expiry within '30' days but this can be overriden with # the '-l' flag. # # The '--pidcheck' flag counts the number of connected p4d processes and warns if # the are over 500 processes running. This value can be overriden with the '-p' # flag. # # The '--moncheck' flag counts the number of commands in the 'p4 monitor' table and # warns if there are over 500 running. This value can be overriden with the '-m' # flag. # # # Description: # DESCRIPTION="P4D health checker - Example Nagios monitoring script" # # This plugin will run the following checks against your Helix P4D server: # - Online? # - Licensed and not expiring soon # - P4D process count in acceptable range # - P4D monitor count in acceptable range # - Optional - Replication OK? # - No significant errors in log file # # Output: # # Notes: # # Examples: # # Run all checks against server on localhost:1666 # # check_helix_p4d_health -p 1666 --all # # Check if license will expire in next 45 days # # check_helix_p4d_health -p 1666 --licensecheck -l 45 # ############################################################################### PROGNAME=$0 P4=/usr/sbin/p4 STATE_OK=0 STATE_WARNING=1 STATE_CRITICAL=2 STATE_UNKNOWN=3 exitstatus=$STATE_OK # Defaults - Can edit or override at command invocation LICEXPIRE=30 PIDCOUNT=100 MONCOUNT=100 set_status() { if [ $1 -gt $exitstatus ] then exitstatus=$1 fi } license_check() { LICENSE_EXPIRE_TIP="Tip: Please contact sales@perforce.com to request a new license file. " License_line=$(echo "$result" | grep 'Server license\:') if [ $(echo $License_line|grep ': none'|wc -l) -gt 0 ] then echo "WARNING: P4D server on $PORT is unlicensed." set_status $STATE_WARNING else Expires=$(echo $License_line| cut -f2 -d"(" | cut -f1 -d")"|rev|cut -f1 -d " "|rev) Expires_Sec=$(date -ud $Expires +'%s') Today=$(date +'%s') ((TIME_LEFT=($Expires_Sec - $Today)/60/60/24)) if [ $TIME_LEFT -lt $LICEXPIRE ] then echo "WARNING: License expires in $TIME_LEFT days." echo "$LICENSE_EXPIRE_TIP" set_status $STATE_WARNING fi fi } server_up() { SERVERDOWN_TIP="Tip: Check if the 'p4d' process is running on the box. Check the P4D log file for errors if it unexpectedly stopped. " result=`p4 -p $PORT info 2>&1` server_up=`echo $result | grep "Server license" | wc -l` case $server_up in [1]) ;; *) echo "CRITICAL: P4D server not responding!" echo "$result" echo "$SERVERDOWN_TIP" set_status $STATE_CRITICAL ;; esac } pid_check(){ PIDCHECK_TIP="Tip: This may be caused by a performance problem or by a script that has gone wild. Use 'netstat -anp' and 'p4 monitor show -ael' to find the culprits. " PIDS=$(netstat -anp | grep $PORT | wc -l) if [ $PIDS -gt $PIDCOUNT ] then echo "WARNING: $PIDS running p4d pids exceeded threshold $PIDCOUNT." echo "$PIDCHECK_TIP" set_status $STATE_WARNING fi } monitor_check() { echo TBD } replication_check() { echo TBD } print_usage() { echo " Usage: check_helix_p4d_health -p [--all] check_helix_p4d_health -p [--licensecheck] [-l ] check_helix_p4d_health -p [--pidcheck] [-p ] check_helix_p4d_health -p [--moncheck] [-p ] check_helix_p4d_health -p [--version] [--help] The '--all' flag runs all tests. The '--licensecheck' flag tests if the license file is nearing it's expiry date. By default it checks for expiry within '30' days but this can be overriden with the '-l' flag. The '--pidcheck' flag counts the number of connected p4d processes and warns if the are over 500 processes running. This value can be overriden with the '-p' flag. The '--moncheck' flag counts the number of commands in the 'p4 monitor' table and warns if there are over 500 running. This value can be overriden with the '-m' flag. " } print_help() { echo $PROGNAME $Revision$ echo "" echo " Description: $DESCRIPTION" echo "" print_usage echo "" } # Main() exitstatus=$STATE_OK #default while test -n "$1"; do case "$1" in --help|--version|-h|-V) print_help exit $STATE_OK ;; -p) PORT=$2 shift ;; -L|--licensecheck) LICCHECK=true ;; -l) LICEXPIRE=$2 shift ;; -C|--pidcheck) PIDCHECK=true ;; -c) PIDCOUNT=$2 shift ;; -M|--moncheck) MONCHECK=true ;; -m) MONCOUNT=$2 shift ;; --all) LICCHECK=true PIDCHECK=true MONCHECK=true ;; --exitstatus) exitstatus=$2 exit $exitstatus ;; --debug) set -x ;; *) echo "Unknown argument: $1" print_usage exit $STATE_UNKNOWN ;; esac shift done # Run tests server_up if [ $exitstatus -lt 2 ] then if [ "$LICCHECK" == "true" ]; then license_check; fi if [ "$PIDCHECK" == "true" ]; then pid_check; fi if [ "$MONCHECK" == "true" ]; then mon_check; fi echo "P4D OK" fi exit $exitstatus