#!/bin/bash Virtual Thom # AUTEUR : SCH Thom # VERSION : 1.0 # DATE : 10/08/2014 # # set -n # Decommenter pour debug syntax sans exéon # NOTE: Ne pas oublier de recommenter ! # set -x # Decommenter pour debug # ########################################################## # VARIABLES ########################################################## OK=0 KOCRIT=1 KOWARN=2 CR=${OK} date_du_jour=`date +"%d_%m_%Y"` # Le nom des arrays doit pouvoir être résolu (/etc/hosts ou dns) ou mettre l'adresse IP iparrays="VNX5700 VNX5300 CX4-960" tmpfile="${date_du_jour}_`uuidgen`_etat-capacitaire-shemc.xml" ftp_ip=ftp.adresse.intradef.gouv.fr ftp_user=xxxx ftp_mdp=xxxx rep_script='/SANSCRIPTS/etat-capacitaire' # pour naviseccli export PATH=$PATH:/opt/Navisphere/bin ########################################################## # FONCTIONS ########################################################## date_heure(){ date_heure_var=`date +"%d/%m/%Y - %H:%M:%S : "` echo "${date_heure_var}$1" } fonc_sortie(){ numero_exit=$1 message_exit=$2 date_heure "${message_exit}" date_heure "Fin du script. Exit : ${numero_exit}" exit ${numero_exit} } usage(){ # exemple d'usage script=${0##*/} ; #script=${script%.*} ; echo "" ; echo "Genere un fichier xml de l'etat capacitaire SHEM-C" echo "Usage :" ; echo " ${script}" echo " Pas de parametre." echo "" exit ${KOWARN}; } ########################################################## # SCRIPT ########################################################## date_heure 'Début du script' cat ${rep_script}/etat-capacitaire-en-tete-xml.txt > ${rep_script}/$tmpfile echo " " >> ${rep_script}/$tmpfile # Requete sur les baies de stockage en naviseccli (les clés doivent être mise en place) # naviseccli -h ipbaie -addusersecurity -scope 0 -password password -user userName for iparray in $(echo $iparrays) do # un seul naviseccli pour toues les storages d'une baie (ça fait moins de requêtes) naviseccli -h $iparray storagepool -list -userCap -availableCap > ${rep_script}/${tmpfile}_$iparray # Le nom et les ip des array sont identiques ici mais on peut mettre autre chose # si le résultat contient des lignes, on traite if test $(cat ${rep_script}/${tmpfile}_$iparray | wc -l) -gt 1 then # si les baies sont aussi des têtes NAS, je récupère l'état du "Pool NAS" (il s'appelle comme ça chez moi) if test "$iparray" = "VNX5700" then namearray="VNX5700" # le nom du NAS doit pouvoir être résolu (/etc/hosts ou dns) ou mettre l'adresse IP ipnas="NAS5700" ssh nasadmin@$ipnas '. .bash_profile ; nas_pool -size "Pool NAS"' > ${rep_script}/pool_nas_size_$ipnas else if test "$iparray" = "VNX5300" then namearray="VNX5300" # le nom du NAS doit pouvoir être résolu (/etc/hosts ou dns) ou mettre l'adresse IP ipnas="NAS5300" ssh nasadmin@$ipnas '. .bash_profile ; nas_pool -size "Pool NAS"' > ${rep_script}/pool_nas_size_$ipnas else if test "$iparray" = "CX4-960" then namearray="CX4-960" else # par défaut on met l'IP ou le nom dns en tant que nom de baie namearray=$iparray fi fi fi echo "" >> ${rep_script}/$tmpfile # line de la sortie naviseccli < ${rep_script}/${tmpfile}_$iparray # Ne pas oublier que l'entrée ssh dans un boucle while remplace l'entrée standard # solution ssh -n while read line do if test -n "$(echo $line | grep 'Pool Name')" then if test -n "$(echo $line | grep 'NAS')" then NAS=1 else NAS=0 fi echo "" >> ${rep_script}/$tmpfile echo "" >> ${rep_script}/$tmpfile # awk -F: '$1 ~ /Pool Name/ {print $2}' => délimiteur ":", 1er champ contient "Pool Name" # affiche le deuxième champs echo $line | awk -F: '$1 ~ /Pool Name/ {print $2}' | sed 's/^ //g' >> ${rep_script}/$tmpfile echo "" >> ${rep_script}/$tmpfile # FIN name else if test -n "$(echo $line | grep 'User Capacity (GBs)')" then echo "" >> ${rep_script}/$tmpfile if test "$NAS" = "1" then usercap=`cat ${rep_script}/pool_nas_size_${ipnas} | awk -F"=" '$1 ~ /total_mb/ {print $2}' | sed 's/^ //g'` if test "$usercap" != "0" then usercap=`expr $usercap \/ 1024` else usercap=`cat ${rep_script}/pool_nas_size_${ipnas} | awk -F"=" '$1 ~ /potential_mb/ {print $2}' | sed 's/^ //g'` usercap=`expr $usercap \/ 1024` fi echo $usercap >> ${rep_script}/$tmpfile # si Pool différent du NAS else echo $line | awk -F: '$1 ~ /User Capacity \(GBs\)/ {print $2}' | sed 's/^ //g' >> ${rep_script}/$tmpfile fi echo "" >> ${rep_script}/$tmpfile # FIN usercap else if test -n "$(echo $line | grep 'Available Capacity (GBs)')" then echo "" >> ${rep_script}/$tmpfile if test "$NAS" = "1" then availablecap=`cat ${rep_script}/pool_nas_size_${ipnas} | awk -F"=" '$1 ~ /avail_mb/ {print $2}' | sed 's/^ //g'` if test "$availablecap" != "0" then availablecap=`expr $availablecap \/ 1024` else availablecap=`cat ${rep_script}/pool_nas_size_${ipnas} | awk -F"=" '$1 ~ /potential_mb/ {print $2}' | sed 's/^ //g'` availablecap=`expr $availablecap \/ 1024` fi echo $availablecap >> ${rep_script}/$tmpfile # si pas pool NAS else echo $line | awk -F: '$1 ~ /Available Capacity \(GBs\)/ {print $2}' | sed 's/^ //g' >> $tmpfile fi echo "" >> ${rep_script}/$tmpfile # FIN availablecap else # Je prends les lignes vides comme séparateur des pools if test -z "$line" then echo "" >> ${rep_script}/$tmpfile fi fi fi fi done < ${rep_script}/${tmpfile}_$iparray echo "" >> ${rep_script}/$tmpfile fi done echo " " >> ${rep_script}/$tmpfile date_heure "Transfert FTP" # Transfert FTP sur le site de la SHEM ftp -v -n -i > /dev/null << EOF open ${ftp_ip} user ${ftp_user} ${ftp_mdp} binary put ${rep_script}/$tmpfile index.xml EOF # TODO : prévoir d'envoyer un e-mail en compte rendu avec le xml attaché # Nettoyage des fichiers temporaires for iparray in $(echo $iparrays) do rm -f ${rep_script}/${tmpfile}_$iparray done rm -f ${rep_script}/pool_nas_size* rm -f ${rep_script}/$tmpfile date_heure 'Fin du script'