J'ai fait une première version... Pourriez-vous svp m'aider à rendre un peu plus propre. J'aimerais améliorer :
- CHECK_DISKSPACE_ENABLE : Comment puis-je facilement mettre true ou false une variable et ensuite comparer ? Faire par exemple
CHECK_DISKSPACE_ENABLE=true
if [ $CHECK_DISKSPACE_ENABLE]; then
...
- Utiliser une fonction qui retourne une date pour permettre d'écrire
message="getTime CheckDiskSpace"
à la place de
message="$(date +'%b %e %H:%M:%S') CheckDiskSpace"
Merci bien :-)
Monitoring.sh
#!/bin/sh
####################
# CONFIGURATION USER
####################
### Check Disk Space
CHECK_DISKSPACE_ENABLE="true" #"true" or "false"
# Write the partition with the limit minimum size(if the limit is a size, put the same unit than the command "df -h" show the partition
CHECK_DISKSPACE_PARTITIONS_LIMITS=("/dev/sda3=56%" "/dev/sda2=24G")
### Mail
MAIL_NOTIFICATION_ENABLE="true" # true or false
MAIL_NOTIFICATION_SENDER="10.10.10.10@company.ch"
MAIL_NOTIFICATION_DESTINATION="user@company.ch"
MAIL_NOTIFICATION_LOCATION="Company" # Where the script is running
MAIL_NOTIFICATION_TYPE_SERVER="GED" # Type of the server
MAIL_NOTIFICATION_TYPE_SCRIPT="Monitoring" # Type of the script like Backup, Monitoring...
### LogFile
LOGFILE="/var/log/companyMonitoring.log"
### Message
SUCCESS="SUCCESS"
ERROR="ERROR"
WARNING="WARNING"
TRUE="TRUE"
FALSE="FALSE"
######
# Init
######
### Check Disk Space
# Get the list of partition in a compact format
partitionDetails=$( df -Ph | sed -r "s/ {1,}/@/g" )
# Position elements in the output of the command "df"
POSITION_PARTITION=1
POSITION_USEDSPACE_PERCENT=5
POSITION_AVAILABLE_SPACE_SIZE=4
###############################################
# Get the date of the day
###############################################
getTime(){
date="$(date +'%b %e %H:%M:%S')"
echo "$date"
}
############################################################
# Check the disk space (configure in the partitionsLimites).
############################################################
checkDiskSpace(){
message="$(date +'%b %e %H:%M:%S') CheckDiskSpace"
echo "$message"
echo "$message" >> $LOGFILE
# Read all partitions with the limit
for partitionsToCheck in ${CHECK_DISKSPACE_PARTITIONS_LIMITS[@]}
do
# Get the values of the parameters in the list (partition, limit)
partitionInList=${partitionsToCheck%%=*}
limitInList=${partitionsToCheck##*=}
# Read all partition on the disk
checkPartition="false" # The partition in the liste has been compared
for partitionOnDisk in $partitionDetails
do
# Check if it's the good partition
if [ "$partitionInList" != "$( echo "$partitionOnDisk" | awk -F@ '{print$1}')" ]; then
continue
fi
# The partition in the list has been found on the system
checkPartition="true"
# Read the unit (G, M, %) of the limit
typLimit=${limitInList:(-1)}
valueLimit=$(echo "$limitInList" | tr -d $typLimit)
echo "Check $partitionInList with the limit fixed with $limitInList"
# Search if the limit is fixed in "%" or unit like G, M...
if [ "$typLimit" = "%" ]; then
# The size is in %
valueToCheck=$POSITION_USEDSPACE_PERCENT
else
# The size is in unit G, M ...
valueToCheck=$POSITION_AVAILABLE_SPACE_SIZE
fi
usedSpace=$( echo "$partitionOnDisk" | awk -F@ -vposition=$valueToCheck '{print$position}' | tr -d $typLimit)
# Comparaison
if [ $usedSpace -le $valueLimit ]; then
# Alert !!!
message "$WARNING : Not enough space on $partitionOnDisk.The minimum is fixed on $limitInList."
else
echo "All is ok with $partitionOnDisk with the minimum limit fixed on $limitInList"
fi
done
# Check if the partition in the list has been found in the system
if [ $checkPartition == "false" ]; then
message "$ERROR : The partition $partitionInList has not been found !!! Please check the configuration of the script."
fi
done
}
#############################################################################
# Send a mail. Do not use it directly... Use function message()
# Use it like this :
# message="I am a message"
# sendMail "%I - $MAIL_NOTIFICATION_LOCATION - GED - Backup" "$message"
#############################################################################
sendMail(){
echo "Send mail notification to : $MAIL_NOTIFICATION_DESTINATION"
echo "$2" | mail -s "$1" "$MAIL_NOTIFICATION_DESTINATION" -- -r "$MAIL_NOTIFICATION_SENDER"
if [ $? -gt 0 ] ; then
message="$(date +'%b %e %H:%M:%S') Unable to send mail notification"
echo "$message"
echo $MESSAGE_SEE_LOG
echo "$message" >> $LOGFILE
fi
}
#############################################################################################
# Write a message in the console and send a mail
# Use it like this :
# message "$ERROR : Unable to give permissions on the directory to link to the share"
# message "$WARNING : Unable to remove mysql file : $BACKUP_FILE_MYSQL_SLQ"
# message "End backup ($SUCCESS)"
############################################################################################
message(){
message="$(date +'%b %e %H:%M:%S') $1"
echo "$message"
echo $MESSAGE_SEE_LOG
echo "$message" >> $LOGFILE
# Sendmail ?
if [ "$MAIL_NOTIFICATION_ENABLE" = "true" ]; then
# Success
echo "$message" | grep "$SUCCESS" 1>/dev/null
if [ $? = 0 ] ; then
typeMessage="%I"
fi
# WARNING
echo "$message" | grep "$WARNING" 1>/dev/null
if [ $? = 0 ] ; then
typeMessage="%W"
fi
# ERROR
echo "$message" | grep "$ERROR" 1>/dev/null
if [ $? = 0 ] ; then
typeMessage="%E"
fi
# SendMail
sendMail "$typeMessage - $MAIL_NOTIFICATION_LOCATION - $MAIL_NOTIFICATION_TYPE_SERVER - $MAIL_NOTIFICATION_TYPE_SCRIPT" "$message"
fi
}
######
# Main
######
# Information startup
message="$(date +'%b %e %H:%M:%S') Start Monitoring"
echo "$message"
echo "The log file is here : $LOGFILE"
echo ""
echo "$message" >> $LOGFILE
# CheckDiskSpace
if [ "$TRUE" = "$(echo $CHECK_DISKSPACE_ENABLE | tr "[:lower:]" "[:upper:]")" ]; then
checkDiskSpace
fi
# Information end
message="$(date +'%b %e %H:%M:%S') End of monitoring"
echo "$message"
echo "$message" >> $LOGFILE