Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 04/02/2011, à 09:08

dranakan

Surveillance Disque

Hello,

Je désire faire un script (lancé avec Crontab) pour surveiller un serveur. Pour commencer je voulais vérifier l'espace disque et envoyer un mail si valeur définie est dépassé.

Je cherche à pouvoir :
->Définir plusieurs répertoires/partitions et leur attribuer une limite en % ou un espace en taille.

Avez-vous connaissance d'un tel Shell Script ?

J'ai fait plusieurs recherche et ce qui se rapproche le plus est : http://www.cyberciti.biz/tips/shell-scr … space.html

Merci bien.

Hors ligne

#2 Le 04/02/2011, à 09:21

Brunod

Re : Surveillance Disque

J'utilise d'autres systèmes qui ne correspondent pas directement, mais je te les donne comme pistes : conky (en local car serveur graphique) et/ou gkrellm en distant.
Ça me permet une surveillance visuelle en temps réel.

Dernière modification par Brunod (Le 04/02/2011, à 09:23)


Windows est un système d'exploitation de l'homme par l'ordinateur. Linux, c'est le contraire...
39 pc linux convertis

Hors ligne

#3 Le 04/02/2011, à 15:28

dranakan

Re : Surveillance Disque

Merci des liens Brunod.

Je pense définir en début de script des couples "partitions"="Max utilisation disque" comme : /dirA=40G, /dirB=50%, ...

Comment puis-je faire ceci en Shell Script ? Avez-vous un exemple ?
Ensuite je pense extraire (avecle signe smile le répertoire avec sa valeur...

Hors ligne

#4 Le 04/02/2011, à 15:38

dranakan

Re : Surveillance Disque

C'est bon... j'avance...

partitions=("/dirA=10G" "dirB=45%")
for value in ${partitions[@]}
do
        echo $value

done

Hors ligne

#5 Le 07/02/2011, à 14:10

dranakan

Re : Surveillance Disque

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

Dernière modification par dranakan (Le 07/02/2011, à 15:17)

Hors ligne

#6 Le 07/02/2011, à 18:55

wsc

Re : Surveillance Disque

franchement ça fait beaucoup de lignes pour pas grand chose hmm
et je comprend pas trop ce que tu veux..

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"

Utilise une variable non ?

DATE=`date +'%b %e %H:%M:%S'`
echo $DATE

#split
###plein de lignes de code
#split
echo -e "$DATE , disque utilisé à `df -H | grep -m1 -o "..[0-9].%" `\n_____________"

Dernière modification par wsc (Le 07/02/2011, à 18:57)


#!/bin/crash
SYS="Arch Linux avec FVWM Crystal"
echo -e "\nMon site internet et le weB-log ... \nMon système: $SYS \n\n"
sleep 2 && exit 0

Hors ligne

#7 Le 07/02/2011, à 19:12

dranakan

Re : Surveillance Disque

Merci d'avoir répondu wsc.

wsc a écrit :

Utilise une variable non ?

J'aimerais relancer la date pour avoir le temps exacte au moment où c'est exécuté.... J'essaie d'avoir une méthode qui me retourne ceci mais j'ai des soucis pour l'utiliser...

Hors ligne

#8 Le 08/02/2011, à 00:44

wsc

Re : Surveillance Disque

#!/bin/bash
function madate
    {
date +'%b %e %H:%M:%S'
    }

echo "c'est quoi la date ? `madate`"
echo "on patiente"
sleep 1m
echo "c'est quoi la date ? `madate`"

Résultat:

c'est quoi la date ? Feb  6 17:54:08
on patiente
c'est quoi la date ? Feb  6 17:55:08

17:54:08
17:55:08

Dernière modification par wsc (Le 08/02/2011, à 00:46)


#!/bin/crash
SYS="Arch Linux avec FVWM Crystal"
echo -e "\nMon site internet et le weB-log ... \nMon système: $SYS \n\n"
sleep 2 && exit 0

Hors ligne

#9 Le 08/02/2011, à 00:49

wsc

Re : Surveillance Disque

Mais elle est déjà présente en plus la fonction dans ton script hmm

#!/bin/bash
getTime(){
        date="$(date +'%b %e %H:%M:%S')"
        echo "$date"
}

echo "c'est quoi la date ? `getTime`"
echo "on patiente"
sleep 1m
echo "c'est quoi la date ? `getTime`"

#!/bin/crash
SYS="Arch Linux avec FVWM Crystal"
echo -e "\nMon site internet et le weB-log ... \nMon système: $SYS \n\n"
sleep 2 && exit 0

Hors ligne

#10 Le 30/03/2011, à 15:08

dranakan

Re : Surveillance Disque

Merci bien... je ne connaissais pas l'utilisation de ces caractères : `

Tout fonctionne à merveille :-)

Hors ligne