From b17a1b5014eec1791d0298fbea16f4a576dd5e96 Mon Sep 17 00:00:00 2001 From: Yvan GODARD Date: Sun, 26 Jul 2015 16:28:54 +0200 Subject: [PATCH 1/8] enhanced support of older OS X systems Add support to 10.6 and older systems (changeip command isn't located in the same folder) --- check_osx_hostname.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/check_osx_hostname.sh b/check_osx_hostname.sh index b78f8c9..cd3c393 100755 --- a/check_osx_hostname.sh +++ b/check_osx_hostname.sh @@ -4,6 +4,9 @@ # by Jedda Wignall # http://jedda.me +# v1.2 - 26 Jul 2015 by Yvan GODARD / godardyvan@gmail.com / yvangodard.me +# Add support to multi OS X versions (changeip isn't located in the same folder) + # v1.1 - 12 Aug 2013 # Significant re-work. Now also does a forward and reverse lookup to ensure server DNS is healthy. @@ -13,9 +16,16 @@ # Simple script that makes sure the infamous changeip -checkhostname command returns a happy status. # It then does a forward and reverse lookup of the returned hostname and IP adress to make sure that DNS is healthy. -checkHostname=`sudo /Applications/Server.app/Contents/ServerRoot/usr/sbin/changeip -checkhostname` + +versionOSX=$(sw_vers -productVersion | awk -F '.' '{print $(NF-1)}') regex="s.+=.([0-9].+)..Cu.+=.([a-z0-9.-]+).D" +if [[ ${versionOSX} -ge 7 ]]; then + checkHostname=`sudo /Applications/Server.app/Contents/ServerRoot/usr/sbin/changeip -checkhostname` +elif [[ ${versionOSX} -le 6 ]]; then + checkHostname=`sudo /usr/sbin/changeip -checkhostname` +fi + if echo $checkHostname | grep -q "The names match."; then [[ $checkHostname =~ $regex ]] if [ "${BASH_REMATCH[0]}" != "" ]; then From c80adfaa5a9740667de059c9d81be913cd595e5d Mon Sep 17 00:00:00 2001 From: Yvan GODARD Date: Fri, 18 Sep 2015 08:28:38 +0200 Subject: [PATCH 2/8] version 2.0 v2.0 - 18 Sep 2015 Modded to work both on Mac & Linux. Complete refactoring --- check_certificate_expiry.sh | 196 ++++++++++++++++++++++++++++-------- 1 file changed, 156 insertions(+), 40 deletions(-) diff --git a/check_certificate_expiry.sh b/check_certificate_expiry.sh index fcb42e9..56efdff 100755 --- a/check_certificate_expiry.sh +++ b/check_certificate_expiry.sh @@ -1,51 +1,167 @@ #!/bin/bash -# Check Mac OS X Server Certificate Expiry -# by Jedda Wignall -# http://jedda.me +# Check Certificate Expiry +# Original script by Jedda Wignall - http://jedda.me +# Modded to work both on Mac & Linux by Yvan GODARD - godardyvan@gmail.com - http://www.yvangodard.me -# v1.1 - 17 Sep 2012 -# Fixed script to throw proper critical error if a cert cannot be loaded by openssl. +# v2.0 - 18 Sep 2015 +# Modded to work both on Mac & Linux. +# Complete refactoring -# v1.0 - 20 Mar 2012 -# Initial release. +# v1.1 - 17 Sep 2012 +# Fixed script to throw proper critical error if a cert cannot be loaded by openssl. -# This script checks the expiry dates of all certificates in the /etc/certificates directory, and returns a warning if needed based on your defined number of days. -# Takes 1 argument - the minimum number of days between today and cert expiry to throw a warning: +# v1.0 - 20 Mar 2012 +# Initial release. + +# This script checks the expiry dates of all certificates in the /etc/certificates directory, and returns a warning if needed based on your defined number of days. +# Takes 1 argument - the minimum number of days between today and cert expiry to throw a warning: # -# check_certificate_expiry.sh 7 -# Warns if a certificate is set to expire in the next 7 days. +# check_certificate_expiry.sh -d 7 -p /etc/apache2/ssl +# Warns if a certificate is set to expire in the next 7 days. + +version="check_certificate_expiry v2.0 - 2015, Yvan Godard [godardyvan@gmail.com] - http://www.yvangodard.me" +system=$(uname -a) +currentDate=$(date "+%s") +critical=0 +warning=0 +defaultPathToCheck=1 +recursivity=0 +scriptDir=$(dirname "${0}") +scriptName=$(basename "${0}") +scriptNameWithoutExt=$(echo "${scriptName}" | cut -f1 -d '.') +pathToCheck=$(mktemp /tmp/${scriptNameWithoutExt}_pathToCheck.XXXXX) +warningFile=$(mktemp /tmp/${scriptNameWithoutExt}_warningFile.XXXXX) +criticalFile=$(mktemp /tmp/${scriptNameWithoutExt}_criticalFile.XXXXX) +certificatesList=$(mktemp /tmp/${scriptNameWithoutExt}_certificatesList.XXXXX) + +cat ${system} | grep "Darwin" > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + systemOs="Mac" + certPath="/etc/certificates" + extension=".cert.pem" + recursivity=0 +fi +cat ${system} | grep "Linux" > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + systemOs="Linux" + certPath="/etc/apache2/ssl" + extension=".pem" + recursivity=0 +fi + +[[ ${systemOs} -ne "Linux" ]] && [[ ${systemOs} -ne "Mac" ]] && error 2 "CRITICAL - This tool doesn't works well on tis OS System!" + +help () { + printf "\n${version}\n" + printf "\nThis script checks the expiry dates of all certificates in a path.\n" + printf "\nDisclamer:\n" + printf "\nThis tool is provide without any support and guarantee.\n" + printf "\nSynopsis:\n" + printf "./$scriptName [-h] | -d \n" + printf " [-p ] [-r] [-e ]\n" + printf "\nTo print this help:\n" + printf "\t-h: prints this help then exit\n" + printf "\nMandatory options:\n" + printf "\t-d : number of days within expiration to warn\n" + printf "\nOptional options:\n" + printf "\t-p : the full path of the directory to check (e.g.: '/etc/apache2/ssl/certs')\n" + printf "\t if you want to check more than one directory, separate path with '%'\n" + printf "\t (e.g.: '-p /etc/certs%/etc/certificates'\n" + printf "\t-r: check the path with recursivity\n" + printf "\t-e : extension of certificats to check (e.g.: '.certifs.pem', default: '${extension}')\n" + alldone 0 +} + +function alldone () { + [[ -e ${criticalFile} ]] && rm ${criticalFile} + [[ -e ${warningFile} ]] && rm ${warningFile} + [[ -e ${pathToCheck} ]] && rm ${pathToCheck} + [[ -e ${certificatesList} ]] && rm ${certificatesList} + exit ${1} +} + +function error () { + [[ ! -z ${2} ]] && printf ${2} + alldone ${1} +} + +# Parameters tests +optsCount=0 +while getopts "hrd:p:e:" option +do + case "$option" in + h) help="yes" + ;; + d) days=${OPTARG} + let optsCount=$optsCount+1 + ;; + p) [[ ! -z ${OPTARG} ]] && defaultPathToCheck=0 && echo ${OPTARG} | perl -p -e 's/%/\n/g' | perl -p -e 's/ //g' | awk '!x[$0]++' >> ${pathToCheck} + ;; + e) extension=${OPTARG} + ;; + r) recursivity=1 + ;; + esac +done + +if [[ ${optsCount} != "1" ]]; then + help + error 3 "CRITICAL - Mandatory parameters needed!" +fi + +[[ ${help} = "yes" ]] && help -CERTS=/etc/certificates/* -currentDate=`date "+%s"` +echo ${days} | grep "^[ [:digit:] ]*$" > /dev/null 2>&1 +[[ $? -ne 0 ]] && error 4 "CRITICAL - Parameter '-d ${days}' is not coorect. Must be an interger." -for c in $CERTS +[[ ${defaultPathToCheck} -eq 1 ]] && printf "${certPath}" > ${pathToCheck} + +for directoryToCheck in $(cat ${pathToCheck}) do - fileType=`echo $c | awk -F . '{print $(NF-1)}'` - if [ $fileType == 'cert' ]; then - # read the dates on each certificate - certDates=`openssl x509 -noout -in "$c" -dates 2>/dev/null` - if [ -z "$certDates" ]; then - # this cert could not be read. - printf "CRITICAL - $c could not be loaded by openssl\n" - exit 2 - fi - notAfter=`echo $certDates | awk -F notAfter= '{print $NF}'` - expiryDate=$(date -j -f "%b %e %T %Y %Z" "$notAfter" "+%s") - diff=$(( $expiryDate - $currentDate )) - warnSeconds=$(($1 * 86400)) - if [ "$diff" -lt "0" ]; then - # this cert is has already expired! return critical status. - printf "CRITICAL - $c has expired!\n" - exit 2 - elif [ "$diff" -lt "$warnSeconds" ]; then - # this cert is expiring within the warning threshold. return warning status. - printf "WARNING - $c will expire within the next $1 days.\n" - exit 1 - fi - fi + if [[ -d ${directoryToCheck} ]]; then + [[ ${recursivity} -eq 1 ]] && find ${directoryToCheck%/} -type f -name "${extension}" >> ${certificatesList} + [[ ${recursivity} -eq 0 ]] && find ${directoryToCheck%/} -type f -name "${extension}" -maxdepth 1 >> ${certificatesList} + fi done -# all certificates passed testing. return OK status. -printf "OK - Certificates are valid.\n" -exit 0 +[[ -z $(cat ${certificatesList}) ]] && error 5 "CRITICAL - No certificate to check. Please be sure your parameters are OK." + +for certificate in $(cat ${certificatesList}) +do + # read the dates on each certificate + certDates=$(openssl x509 -noout -in "${certificate}" -dates 2>/dev/null) + if [[ -z "$certDates" ]]; then + # this cert could not be read. + printf "> ${certificate} could not be loaded by openssl\n" >> ${criticalFile} + critical=1 + fi + notAfter=$(echo ${certDates} | awk -F notAfter= '{print $NF}') + expiryDate=$(date --date="${notAfter}" "+%s") + diff=$(( ${expiryDate} - ${currentDate} )) + warnSeconds=$((${days} * 86400)) + if [[ "${diff}" -lt "0" ]]; then + # this cert is has already expired! return critical status. + printf "> ${certificate} has expired!\n" >> ${criticalFile} + critical=1 + elif [[ "${diff}" -lt "${warnSeconds}" ]]; then + # this cert is expiring within the warning threshold. return warning status. + delay=$((${diff} / 86400)) + printf "> ${certificate} will expire within the next ${days} days.\n" >> ${warningFile} + printf " delay until expiration : ${delay} day(s)\n" >> ${warningFile} + warning=1 + fi +done + +if [[ ${critical} -eq "1" ]]; then + [[ ! -z $(cat ${criticalFile}) ]] && printf "\n-- CRITICAL --\n" && cat ${criticalFile} + [[ ! -z $(cat ${warningFile}) ]] && printf "\n-- WARNING --\n" && cat ${warningFile} + alldone 2 +elif [[ ${warning} -eq "1" ]]; then + [[ ! -z $(cat ${warningFile}) ]] && printf "\n-- WARNING --\n" && cat ${warningFile} + alldone 1 +else + alldone 0 "OK - Certificates are valid.\n" +fi + +alldone 0 \ No newline at end of file From 7d1fc641dc5bb0c2796e1a8b18d4ac8bbb05d6ad Mon Sep 17 00:00:00 2001 From: Yvan GODARD Date: Fri, 18 Sep 2015 08:30:26 +0200 Subject: [PATCH 3/8] version 2.0 v2.0 - 18 Sep 2015 Modded to work both on Mac & Linux. Complete refactoring --- check_certificate_expiry.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/check_certificate_expiry.sh b/check_certificate_expiry.sh index 56efdff..7000436 100755 --- a/check_certificate_expiry.sh +++ b/check_certificate_expiry.sh @@ -65,7 +65,8 @@ help () { printf "\nMandatory options:\n" printf "\t-d : number of days within expiration to warn\n" printf "\nOptional options:\n" - printf "\t-p : the full path of the directory to check (e.g.: '/etc/apache2/ssl/certs')\n" + printf "\t-p : the full path of the directory to check\n" + printf "\t (e.g.: '/etc/apache2/ssl/certs', default '${certPath}')\n" printf "\t if you want to check more than one directory, separate path with '%'\n" printf "\t (e.g.: '-p /etc/certs%/etc/certificates'\n" printf "\t-r: check the path with recursivity\n" From 943a1caeddf7cded2e0d831fe43b74ff22ad34cc Mon Sep 17 00:00:00 2001 From: Yvan GODARD Date: Fri, 18 Sep 2015 09:22:52 +0200 Subject: [PATCH 4/8] version 2.0 Modded to work both on Mac & Linux. Complete refactoring --- check_certificate_expiry.sh | 85 +++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/check_certificate_expiry.sh b/check_certificate_expiry.sh index 7000436..3d6429f 100755 --- a/check_certificate_expiry.sh +++ b/check_certificate_expiry.sh @@ -14,11 +14,7 @@ # v1.0 - 20 Mar 2012 # Initial release. -# This script checks the expiry dates of all certificates in the /etc/certificates directory, and returns a warning if needed based on your defined number of days. -# Takes 1 argument - the minimum number of days between today and cert expiry to throw a warning: -# -# check_certificate_expiry.sh -d 7 -p /etc/apache2/ssl -# Warns if a certificate is set to expire in the next 7 days. +# This script checks the expiry dates of all certificates in a path and returns a warning if needed based on your defined number of days. version="check_certificate_expiry v2.0 - 2015, Yvan Godard [godardyvan@gmail.com] - http://www.yvangodard.me" system=$(uname -a) @@ -27,6 +23,7 @@ critical=0 warning=0 defaultPathToCheck=1 recursivity=0 +systemOs="" scriptDir=$(dirname "${0}") scriptName=$(basename "${0}") scriptNameWithoutExt=$(echo "${scriptName}" | cut -f1 -d '.') @@ -35,14 +32,14 @@ warningFile=$(mktemp /tmp/${scriptNameWithoutExt}_warningFile.XXXXX) criticalFile=$(mktemp /tmp/${scriptNameWithoutExt}_criticalFile.XXXXX) certificatesList=$(mktemp /tmp/${scriptNameWithoutExt}_certificatesList.XXXXX) -cat ${system} | grep "Darwin" > /dev/null 2>&1 +echo ${system} | grep "Darwin" > /dev/null 2>&1 if [[ $? -eq 0 ]]; then systemOs="Mac" certPath="/etc/certificates" extension=".cert.pem" recursivity=0 fi -cat ${system} | grep "Linux" > /dev/null 2>&1 +echo ${system} | grep "Linux" > /dev/null 2>&1 if [[ $? -eq 0 ]]; then systemOs="Linux" certPath="/etc/apache2/ssl" @@ -53,24 +50,30 @@ fi [[ ${systemOs} -ne "Linux" ]] && [[ ${systemOs} -ne "Mac" ]] && error 2 "CRITICAL - This tool doesn't works well on tis OS System!" help () { - printf "\n${version}\n" - printf "\nThis script checks the expiry dates of all certificates in a path.\n" - printf "\nDisclamer:\n" - printf "\nThis tool is provide without any support and guarantee.\n" - printf "\nSynopsis:\n" - printf "./$scriptName [-h] | -d \n" - printf " [-p ] [-r] [-e ]\n" - printf "\nTo print this help:\n" - printf "\t-h: prints this help then exit\n" - printf "\nMandatory options:\n" - printf "\t-d : number of days within expiration to warn\n" - printf "\nOptional options:\n" - printf "\t-p : the full path of the directory to check\n" - printf "\t (e.g.: '/etc/apache2/ssl/certs', default '${certPath}')\n" - printf "\t if you want to check more than one directory, separate path with '%'\n" - printf "\t (e.g.: '-p /etc/certs%/etc/certificates'\n" - printf "\t-r: check the path with recursivity\n" - printf "\t-e : extension of certificats to check (e.g.: '.certifs.pem', default: '${extension}')\n" + echo "" + echo "${version}" + echo "This script checks the expiry dates of all certificates in a path." + echo "" + echo "Disclamer:" + echo "This tool is provide without any support and guarantee." + echo "" + echo "Synopsis:" + echo "./${scriptName} [-h] | -d " + echo " [-p ] [-r] [-e ]" + echo "" + echo "To print this help:" + echo " -h: prints this help then exit" + echo "" + echo "Mandatory options:" + echo " -d : number of days within expiration to warn" + echo "" + echo "Optional options:" + echo " -p : the full path of the directory to check" + echo " (e.g.: '/etc/apache2/ssl/certs', default '${certPath}')" + echo " if you want to check more than one directory, separate path with '%'" + echo " (e.g.: '-p /etc/certs\%/etc/certificates'" + echo " -r: check the path with recursivity" + echo " -e : extension of certificats to check (e.g.: '-e .certifs.pem', default: '${extension}')" alldone 0 } @@ -83,7 +86,7 @@ function alldone () { } function error () { - [[ ! -z ${2} ]] && printf ${2} + [[ ! -z ${2} ]] && echo ${2} alldone ${1} } @@ -121,8 +124,8 @@ echo ${days} | grep "^[ [:digit:] ]*$" > /dev/null 2>&1 for directoryToCheck in $(cat ${pathToCheck}) do if [[ -d ${directoryToCheck} ]]; then - [[ ${recursivity} -eq 1 ]] && find ${directoryToCheck%/} -type f -name "${extension}" >> ${certificatesList} - [[ ${recursivity} -eq 0 ]] && find ${directoryToCheck%/} -type f -name "${extension}" -maxdepth 1 >> ${certificatesList} + [[ ${recursivity} -eq 1 ]] && find ${directoryToCheck%/} -type f -name "*${extension}" >> ${certificatesList} + [[ ${recursivity} -eq 0 ]] && find ${directoryToCheck%/} -maxdepth 1 -type f -name "*${extension}" >> ${certificatesList} fi done @@ -137,8 +140,24 @@ do printf "> ${certificate} could not be loaded by openssl\n" >> ${criticalFile} critical=1 fi - notAfter=$(echo ${certDates} | awk -F notAfter= '{print $NF}') - expiryDate=$(date --date="${notAfter}" "+%s") + notAfter=$(echo ${certDates} | awk -F notAfter= '{print $NF}') + if [[ ${systemOs} == "Mac" ]]; then + date -j -f "%b %e %T %Y %Z" "${notAfter}" "+%s" > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + printf "> ${certificate} - expiry date could not be found by openssl\n" >> ${warningFile} + warning=1 + else + expiryDate=$(date -j -f "%b %e %T %Y %Z" "${notAfter}" "+%s") + fi + elif [[ ${systemOs} == "Linux" ]]; then + date --date="${notAfter}" "+%s" > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + printf "> ${certificate} - expiry date could not be found by openssl\n" >> ${warningFile} + warning=1 + else + expiryDate=$(date --date="${notAfter}" "+%s") + fi + fi diff=$(( ${expiryDate} - ${currentDate} )) warnSeconds=$((${days} * 86400)) if [[ "${diff}" -lt "0" ]]; then @@ -155,14 +174,14 @@ do done if [[ ${critical} -eq "1" ]]; then - [[ ! -z $(cat ${criticalFile}) ]] && printf "\n-- CRITICAL --\n" && cat ${criticalFile} + [[ ! -z $(cat ${criticalFile}) ]] && printf "CRITICAL - See informations below" && printf "\n-- CRITICAL --\n" && cat ${criticalFile} [[ ! -z $(cat ${warningFile}) ]] && printf "\n-- WARNING --\n" && cat ${warningFile} alldone 2 elif [[ ${warning} -eq "1" ]]; then - [[ ! -z $(cat ${warningFile}) ]] && printf "\n-- WARNING --\n" && cat ${warningFile} + [[ ! -z $(cat ${warningFile}) ]] && printf "WARNING - See informations below" && printf "\n-- WARNING --\n" && cat ${warningFile} alldone 1 else - alldone 0 "OK - Certificates are valid.\n" + error 0 "OK - Certificates are valid." fi alldone 0 \ No newline at end of file From 195365e532e652d6a34d2a3c95c9f1347b7ebdae Mon Sep 17 00:00:00 2001 From: Yvan GODARD Date: Mon, 21 Sep 2015 07:37:20 +0200 Subject: [PATCH 5/8] enhanced output message for Centreon --- check_certificate_expiry.sh | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/check_certificate_expiry.sh b/check_certificate_expiry.sh index 3d6429f..d19afb0 100755 --- a/check_certificate_expiry.sh +++ b/check_certificate_expiry.sh @@ -31,6 +31,10 @@ pathToCheck=$(mktemp /tmp/${scriptNameWithoutExt}_pathToCheck.XXXXX) warningFile=$(mktemp /tmp/${scriptNameWithoutExt}_warningFile.XXXXX) criticalFile=$(mktemp /tmp/${scriptNameWithoutExt}_criticalFile.XXXXX) certificatesList=$(mktemp /tmp/${scriptNameWithoutExt}_certificatesList.XXXXX) +messageContent=$(mktemp /tmp/${scriptNameWithoutExt}_messageContent.XXXXX) +numberExpiredCertificates=0 +numberWarningCertificates=0 +numberProblemCertificates=0 echo ${system} | grep "Darwin" > /dev/null 2>&1 if [[ $? -eq 0 ]]; then @@ -82,6 +86,7 @@ function alldone () { [[ -e ${warningFile} ]] && rm ${warningFile} [[ -e ${pathToCheck} ]] && rm ${pathToCheck} [[ -e ${certificatesList} ]] && rm ${certificatesList} + [[ -e ${messageContent} ]] && rm ${messageContent} exit ${1} } @@ -98,7 +103,7 @@ do h) help="yes" ;; d) days=${OPTARG} - let optsCount=$optsCount+1 + let optsCount=${optsCount}+1 ;; p) [[ ! -z ${OPTARG} ]] && defaultPathToCheck=0 && echo ${OPTARG} | perl -p -e 's/%/\n/g' | perl -p -e 's/ //g' | awk '!x[$0]++' >> ${pathToCheck} ;; @@ -138,6 +143,7 @@ do if [[ -z "$certDates" ]]; then # this cert could not be read. printf "> ${certificate} could not be loaded by openssl\n" >> ${criticalFile} + let numberProblemCertificates=${numberProblemCertificates}+1 critical=1 fi notAfter=$(echo ${certDates} | awk -F notAfter= '{print $NF}') @@ -145,6 +151,7 @@ do date -j -f "%b %e %T %Y %Z" "${notAfter}" "+%s" > /dev/null 2>&1 if [[ $? -ne 0 ]]; then printf "> ${certificate} - expiry date could not be found by openssl\n" >> ${warningFile} + let numberProblemCertificates=${numberProblemCertificates}+1 warning=1 else expiryDate=$(date -j -f "%b %e %T %Y %Z" "${notAfter}" "+%s") @@ -153,6 +160,7 @@ do date --date="${notAfter}" "+%s" > /dev/null 2>&1 if [[ $? -ne 0 ]]; then printf "> ${certificate} - expiry date could not be found by openssl\n" >> ${warningFile} + let numberProblemCertificates=${numberProblemCertificates}+1 warning=1 else expiryDate=$(date --date="${notAfter}" "+%s") @@ -163,22 +171,37 @@ do if [[ "${diff}" -lt "0" ]]; then # this cert is has already expired! return critical status. printf "> ${certificate} has expired!\n" >> ${criticalFile} + let numberExpiredCertificates=${numberExpiredCertificates}+1 critical=1 + elif [[ "${diff}" -lt "${warnSeconds}" ]]; then # this cert is expiring within the warning threshold. return warning status. delay=$((${diff} / 86400)) printf "> ${certificate} will expire within the next ${days} days.\n" >> ${warningFile} printf " delay until expiration : ${delay} day(s)\n" >> ${warningFile} + let numberWarningCertificates=${numberWarningCertificates}+1 warning=1 fi done +# Generate first line message for Centreon +[[ ${numberExpiredCertificates} -eq 1 ]] && echo "1 certificate has expired" >> ${messageContent} +[[ ${numberExpiredCertificates} -gt 1 ]] && echo "${numberExpiredCertificates} ceertificates had expired" >> ${messageContent} + +[[ ${numberProblemCertificates} -eq 1 ]] && echo "Problem to read 1 certificate" >> ${messageContent} +[[ ${numberProblemCertificates} -gt 1 ]] && echo "Problem to read ${numberProblemCertificates} certificates" >> ${messageContent} + +[[ ${numberWarningCertificates} -eq 1 ]] && echo "1 certificate will expire within the next ${days} days" >> ${messageContent} +[[ ${numberWarningCertificates} -gt 1 ]] && echo "${numberWarningCertificates} certificates will expire within the next ${days} days" >> ${messageContent} + +messageContentLine=$(cat ${messageContent} | perl -p -e 's/\n/ - /g' | awk 'sub( "...$", "" )') + if [[ ${critical} -eq "1" ]]; then - [[ ! -z $(cat ${criticalFile}) ]] && printf "CRITICAL - See informations below" && printf "\n-- CRITICAL --\n" && cat ${criticalFile} + [[ ! -z $(cat ${criticalFile}) ]] && printf "CRITICAL - ${messageContentLine}" && printf "\n-- CRITICAL --\n" && cat ${criticalFile} [[ ! -z $(cat ${warningFile}) ]] && printf "\n-- WARNING --\n" && cat ${warningFile} alldone 2 elif [[ ${warning} -eq "1" ]]; then - [[ ! -z $(cat ${warningFile}) ]] && printf "WARNING - See informations below" && printf "\n-- WARNING --\n" && cat ${warningFile} + [[ ! -z $(cat ${warningFile}) ]] && printf "WARNING - ${messageContentLine}" && printf "\n-- WARNING --\n" && cat ${warningFile} alldone 1 else error 0 "OK - Certificates are valid." From 10bd8b1fece29590a930bd3770f7a00063a8ea21 Mon Sep 17 00:00:00 2001 From: Yvan GODARD Date: Sun, 15 Nov 2015 21:28:55 +0100 Subject: [PATCH 6/8] v1.2 v1.2 - 31 Octobre 2015 Add options to check write outpout in a specific file for very large folder. Complete refactoring --- check_folder_size.sh | 331 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 273 insertions(+), 58 deletions(-) mode change 100644 => 100755 check_folder_size.sh diff --git a/check_folder_size.sh b/check_folder_size.sh old mode 100644 new mode 100755 index eb1286b..fbcb73c --- a/check_folder_size.sh +++ b/check_folder_size.sh @@ -1,8 +1,12 @@ #!/bin/bash -# Check Folder Size -# by Dan Barrett -# http://yesdevnull.net +# Check Folder Size - Nagios Probe for OSX +# Original by Dan Barrett - http://yesdevnull.net +# Modded by Yvan GODARD - godardyvan@gmail.com - http://www.yvangodard.me + +# v1.2 - 31 Octobre 2015 +# Add options to check write outpout in a specific file for very large folder. +# Complete refactoring # v1.1 - 28 October 2013 # Added OS X 10.9 Support and fixes a bug where folders with spaces in their name would fail with du. @@ -10,79 +14,290 @@ # v1.0 - 9 August 2013 # Initial release. -# Checks to see how large the folder is and warns or crits if over a specified size. -# Defaults to MB - -# Arguments: -# -f Path to folder -# -b Block size (i.e. data returned in MB, KB or GB - enter as m, k or g) -# -w Warning threshold for storage used -# -c Critical threshold for storage used - -# Example: -# ./check_folder_size.sh -f /Library/Application\ Support/ -w 2048 -c 4096 - -# Supports: -# Untested but I'm sure it works fine on OS X 10.6 and 10.7 -# * OS X 10.8.x -# * OS X 10.9 - +# Options +version="check_folder_size v1.2 - 2015 - by Yvan Godard http://www.yvangodard.me & Dan Barrett http://yesdevnull.net" +scriptDir=$(dirname "${0}") +scriptName=$(basename "${0}") +scriptNameWithoutExt=$(echo "${scriptName}" | cut -f1 -d '.') +help="no" folderPath="" blockSize="m" -blockSizeFriendly="MB" warnThresh="" critThresh="" +withTimeLimit=0 +timeLimit="" +thisTime=0 +actualSizeK="" +previousSizeK="" +previousSizeM="" +previousSizeG="" +previousDate="" +previousLineBufferFile="" +newLineBufferFile="" +optsCount=0 +bufferFolder="/var/${scriptNameWithoutExt}" +bufferFile="${bufferFolder%/}/bufferFile.txt" +messageContent=$(mktemp /tmp/${scriptNameWithoutExt}_messageContent.XXXXX) +duTempScript=$(mktemp /tmp/${scriptNameWithoutExt}_duTempScript.XXXXX) + +help () { + echo "" + echo "${version}" + echo "" + echo "This tool is a Nagios probe for Mac OS X System." + echo "It's designed to check how large a folder is and to warn or crit if it's over a specified size." + echo "" + echo "Disclamer:" + echo "This tool is provide without any support and guarantee." + echo "" + echo "Synopsis:" + echo "./${scriptName} [-h] | -f -w -c " + echo " [-b ] [-t