Files
syno_scripts/syno/tps
2018-03-13 08:04:28 +01:00

603 lines
18 KiB
Bash
Raw Permalink Blame History

#!/bin/sh
#==================================
# Script par Fredlime
# English Translation by Ikeke
#==================================
# Variables courantes du programme
#
model=`/bin/get_key_value /etc/synoinfo.conf upnpmodelname`
heure=`date +%Hh%M`
jour=`date +%d-%m-%Y`
script=`ps | grep $$ | grep -v grep | sed -e 's/^..*sh //' | awk '{print $1}'`
echo $script
#
#=====================================
# Creation du fichier de configuration
# si il n'existe pas => /etc/tps.conf
#=====================================
#
if test -f /etc/tps.conf
then
config="no"
else
config="yes"
printf "#\n# Script configuration of "$script"\n#\n" > /etc/tps.conf
#
if test -f /opt/bin/esmtp
then
echo "tps-mail=\"yes\"" >> /etc/tps.conf
else
echo "tps-mail=\"no\"" >> /etc/tps.conf
fi
#
if test -f /opt/bin/rrdtool
then
echo "tps-graph=\"yes\"" >> /etc/tps.conf
else
echo "tps-graph=\"no\"" >> /etc/tps.conf
fi
if test -d /volume1/web
then
echo "tps-image=\"/volume1/web\"" >> /etc/tps.conf
else
echo "tps-image=\"/tmp\"" >> /etc/tps.conf
fi
#
printf "#\ntps-temps=\"5\"\n#\n# Variables courantes\n#\ntps-jour=\""$jour"\"\n#\n" >> /etc/tps.conf
#
> /etc/tps.log
fi
#===================================
# Menu de configuration du script
# Cas ou on lance 'tps config'
# ou si premiere execution su script
#===================================
#
if test "$1" = "config" -o "$config" = "yes" -o "$1" = "info"
then
#-----------------------------
# recup des valeurs par defaut
#-----------------------------
tpsmail=`/bin/get_key_value /etc/tps.conf tps-mail`
tpsgraph=`/bin/get_key_value /etc/tps.conf tps-graph`
tpsimage=`/bin/get_key_value /etc/tps.conf tps-image`
tpstemps=`/bin/get_key_value /etc/tps.conf tps-temps`
jourlog=`/bin/get_key_value /etc/tps.conf tps-jour`
#
smtp=`/bin/get_key_value /etc/synoinfo.conf eventsmtp`
mail=`/bin/get_key_value /etc/synoinfo.conf eventmail1`
user=`/bin/get_key_value /etc/synoinfo.conf eventuser`
pass=`/bin/get_key_value /etc/synoinfo.conf eventpass`
#
#
if test "$1" = "info"
then
status=`cat /etc/crontab | grep $script | sed -e "s/^.*root sh //"`
if test "$status" = "$script"
then
status="Enabled"
else
status="Disabled"
fi
clear
echo "##################################################################"
echo " How to use the SCRIPT and informations about parameters to use "
echo ""
echo $script" config => Script configuration"
echo $script" stop => Disable this script"
echo $script" start => Enable this script"
echo $script" info => Script status"
echo $script" mail => Immediatly send an email"
echo $script" => Generate a temperature statement in database"
echo ""
echo "Send email => "$tpsmail
echo " MAIL => "$mail
echo " SMTP => "$smtp
echo " USER => "$user" useful with some I.S.P"
echo " PASS => "$pass" useful with some I.S.P"
echo ""
echo "Graph creation => "$tpsgraph
echo "Image folder => "$tpsimage
echo ""
echo "Time interval = "$tpstemps" minutes"
echo "Current day = "$jourlog
echo "Script status = "$status
echo ""
echo ""
exit
fi
#
if test -f /opt/bin/esmtp
then
echo
printf "Send eMail yes/no : ("$tpsmail") \c" ; read q
if test "$q" != ""
then
tpsmail=$q
cat /etc/tps.conf | sed -e 's/tps-mail="..*"/tps-mail="'$tpsmail'"/' > /etc/tps.conf
fi
echo
echo "Email address => "$mail
echo "Smtp Server => "$smtp
echo "Login => "$user
echo "Password => "$pass
fi
#
if test -f /opt/bin/rrdtool
then
printf "\nGraphical function yes/no : ("$tpsgraph") \c" ; read q
if test "$q" != ""
then
tpsgraph=$q
fi
if test "$tpsgraph" = "yes"
then
while test "$ok" != "ok"
do
printf "\nImage Folder, by default => "$tpsimage" : \c" ; read q
if test "$q" != ""
then
tpsimage=$q
fi
if test -d $tpsimage
then
ok="ok"
else
echo ""
echo "Error, entered path to store the "
echo "graph file doesn't exist !!"
echo ""
fi
done
fi
cat /etc/tps.conf | sed -e 's/tps-graph="..*"/tps-graph="'$tpsgraph'"/' > /etc/tps.conf
tpsimage="tps-image=\""`echo $tpsimage | sed -e "s/\//slash/g"`
cat /etc/tps.conf | sed -e 's/tps-image="..*/'$tpsimage'"/' | sed -e "s/slash/\//g" > /etc/tps.conf
fi
#
#
echo
printf "\nTime between each statements (minutes) : ("$tpstemps") \c" ; read q
if test "$q" != ""
then
tpstemps=$q
cat /etc/tps.conf | sed -e 's/tps-temps="..*"/tps-temps="'$tpstemps'"/' > /etc/tps.conf
fi
fi
#================================
# Cas ou on lance le script avec
# 'config' ou 'stop' ou 'start'
#================================
#
if test "$1" = "stop" -o "$1" = "start" -o "$config" = "yes"
then
/bin/killall crond
echo "Script halted"
#------------------------------------------------------
# Supression des lignes qui lancent le script dans
# le fichier /etc/rc.local => particularite du CS-406
# le fichier /etc/crontab => particularite des SYNO
#------------------------------------------------------
if test "$model" = "CS-406"
then
/bin/cat /etc/rc.local | grep -v $script > /etc/rc.local
fi
/bin/cat /etc/crontab | grep -v $script > /etc/crontab
#-------------------------------
# Cas ou on lance le script avec
# 'config' ou 'start'
#-------------------------------
if test "$1" = "start" -o "$config" = "yes"
then
echo "Script is starting !!"
tpstemps=`/bin/get_key_value /etc/tps.conf tps-temps`
if test "$model" = "CS-406"
then
echo "killall crond # "$script >> /etc/rc.local
echo "echo \"*/"$tpstemps" * * * * root sh "$script"\" >> /etc/crontab" >> /etc/rc.local
echo "/usr/sbin/crond # "$script >> /etc/rc.local
fi
echo "*/"$tpstemps" * * * * root sh "$script >> /etc/crontab
fi
#--------------------
# On sort du script
# et on relance crond
#--------------------
/usr/sbin/crond
exit
fi
#=================================================================================================
# Releve la temperature des disques
#=================================================================================================
#
cd /dev
nbrdisk=0
for bo in `/bin/ls hd?`
do
t=`/opt/sbin/smartctl -A $bo | grep "Temperature_Celsius" | awk '{print $10}'`
if test "$t" != ""
then
h=`/opt/sbin/smartctl -A $bo | grep "Power_On_Hours" | awk '{print $10}'`
nbrdisk=`expr $nbrdisk + 1`
temp=$temp" "$t
dure=$dure" disk"$nbrdisk"= "$h" "
fi
done
temp=`echo $temp | sed -e "s/^ //"`
Tmax=`echo $temp | awk '{print $1"\n"$2"\n"$3"\n"$4}' | sort | tail -1`
Tmin=`echo $temp | awk '{print $1"\n"$2"\n"$3"\n"$4}' | grep -v "^$" | sort -r | tail -1`
delta=`expr $Tmax - $Tmin`
Tmoy=`echo $temp | awk '{print ($1+$2+$3+$4)/'$nbrdisk' }'`
#
#=====================
# Traitement graphique
#=====================
tpsgraph=`/bin/get_key_value /etc/tps.conf tps-graph`
if test "$tpsgraph" = "yes"
then
tpsimage=`/bin/get_key_value /etc/tps.conf tps-image`"/tps-"`hostname`".png"
tempsrdd=`/opt/bin/rrdtool info /etc/tps.rrd 2> /dev/null | grep -i step | awk '{print $3 / 60}'`
tpstemps=`/bin/get_key_value /etc/tps.conf tps-temps`
#
#-------------------------------
# Test si la base existe avec
# le temps des releves, sinon
# creation de la base de donnees
#-------------------------------
if test "$tpstemps" = "$tempsrdd"
then
/opt/bin/rrdtool update /etc/tps.rrd N:$Tmoy:$delta
printf "Creation of the Temperature Graph \c"
#
#---------------------------------------------
# Generation du Graphique special Cube Station
# avec 1 ou plusieurs disques
#---------------------------------------------
if test "$model" = "CS-406"
then
#
/opt/bin/rrdtool graph $tpsimage -w 500 -h 100 \
--title `hostname`" => SYNOLOGY "$model" on "$jour" "$heure \
--vertical-label "Degrees C<>" \
--x-grid MINUTE:15:HOUR:1:HOUR:2:0:%Hh00 \
--imgformat PNG \
--color MGRID#337fbf \
--color FRAME#808080 \
--color ARROW#FF0099 \
DEF:disque=/etc/tps.rrd:disque:AVERAGE \
DEF:deltad=/etc/tps.rrd:deltad:AVERAGE \
VDEF:max=disque,MAXIMUM \
LINE2:max#ff0000 \
CDEF:flaming1=disque,40,*,100,/ \
CDEF:flaming2=disque,5,*,100,/ \
AREA:flaming1#ffff5f \
STACK:flaming2#fffc51 \
STACK:flaming2#fffc51 \
STACK:flaming2#fff046 \
STACK:flaming2#ffe95f \
STACK:flaming2#ffd237 \
STACK:flaming2#ffc832 \
STACK:flaming2#ffbe2d \
STACK:flaming2#ffaa23 \
STACK:flaming2#ff9619 \
STACK:flaming2#ff841e \
STACK:flaming2#ff841e \
STACK:flaming2#ff6600:"Disks Temperatures" \
GPRINT:disque:MAX:"Maxi %2.0lf<EFBFBD>" \
GPRINT:disque:MIN:"Mini %2.0lf<EFBFBD>" \
GPRINT:disque:AVERAGE:"Aver. %2.1lf<EFBFBD>" \
GPRINT:deltad:AVERAGE:"Max Diff. %2.1lf<EFBFBD>" \
GPRINT:disque:LAST:"Last measure %2.0lf<EFBFBD>" \
COMMENT:"\n" \
COMMENT:" ---------------------------- \n" \
COMMENT:" Time Counter $dure"
#
#
#---------------------------------------------
# Generation du Graphique pour les autres SYNO
# avec 1 disque uniquement
#---------------------------------------------
else
dure=`echo $dure | awk '{print $2}'`
/opt/bin/rrdtool graph $tpsimage -w 500 -h 100 \
--title `hostname`" => SYNOLOGY "$model" on "$jour" "$heure \
--vertical-label "Degrees C<>" \
--x-grid MINUTE:15:HOUR:1:HOUR:2:0:%Hh00 \
--imgformat PNG \
--color MGRID#337fbf \
--color FRAME#808080 \
--color ARROW#FF0099 \
DEF:disque=/etc/tps.rrd:disque:AVERAGE \
VDEF:max=disque,MAXIMUM \
LINE2:max#ff0000 \
CDEF:flaming1=disque,40,*,100,/ \
CDEF:flaming2=disque,5,*,100,/ \
AREA:flaming1#ffff5f \
STACK:flaming2#fffc51 \
STACK:flaming2#fffc51 \
STACK:flaming2#fff046 \
STACK:flaming2#ffe95f \
STACK:flaming2#ffd237 \
STACK:flaming2#ffc832 \
STACK:flaming2#ffbe2d \
STACK:flaming2#ffaa23 \
STACK:flaming2#ff9619 \
STACK:flaming2#ff841e \
STACK:flaming2#ff841e \
STACK:flaming2#ff6600:"Disk Temperature" \
GPRINT:disque:MAX:"Maxi %2.0lf<EFBFBD>" \
GPRINT:disque:MIN:"Mini %2.0lf<EFBFBD>" \
GPRINT:disque:AVERAGE:"Aver. %2.2lf<EFBFBD>" \
GPRINT:disque:LAST:"Last measure %2.0lf<EFBFBD> ("$dure" hours)"
#
fi
#
else
echo "Initialization of rrdtool database"
step=`expr $tpstemps \* 60`
inte=`expr $step \* 2`
/opt/bin/rrdtool create /etc/tps.rrd -s $step \
DS:disque:GAUGE:$inte:U:U \
DS:deltad:GAUGE:$inte:U:U \
RRA:AVERAGE:0.5:1:288
fi
fi
#
#=====================
# Traitement par Mail
#=====================
#
tpsmail=`/bin/get_key_value /etc/tps.conf tps-mail`
if test "$tpsmail" = "yes"
then
#
jourlog=`/bin/get_key_value /etc/tps.conf tps-jour`
tpstemps=`/bin/get_key_value /etc/tps.conf tps-temps`
#
if test -f /etc/tps.log
then
#
# test si le jour a change ou bien
# si on a lance le script avec l'option mail
#
if test "$jourlog" != "$jour" -o "$1" = "mail"
then
echo "Preparing email..."
#-----------------------------------------------
# Dans ce cas, on prepare le mail et on l'envoie
#-----------------------------------------------
/bin/cat /etc/tps.log | sed -e 's/ Delta.*//' | awk -F= '{print $2}' > /tmp/tps-moy.log
/bin/cat /etc/tps.log | sed -e 's/ Disks.*//' | awk -F= '{print $3}' > /tmp/tps-del.log
/bin/cat /etc/tps.log | sed -e 's/Ave.*Disks=//' | sed -e 's/ /<2F>/g' | sed -e "s/[0-9][0-9]h/~&/" > /tmp/tps-disk0.log
#
if test "$model" = "CS-406"
then
tab="4"
else
tab="6"
fi
col="1"
printf "Temperatures list of day "$jourlog" \n \n" > /tmp/tps-disk.log
for bo in `/bin/cat /tmp/tps-disk0.log`
do
if test "$col" != "$tab"
then
printf " "$bo" \c" >> /tmp/tps-disk.log
else
echo " "$bo >> /tmp/tps-disk.log
col="0"
fi
col=`expr $col + 1`
done
#
# Extraction des infos email, nom et domaine
#
smtp=`/bin/get_key_value /etc/synoinfo.conf eventsmtp`
mail=`/bin/get_key_value /etc/synoinfo.conf eventmail1`
user=`/bin/get_key_value /etc/synoinfo.conf eventuser`
pass=`/bin/get_key_value /etc/synoinfo.conf eventpass`
smtp=`/bin/get_key_value /etc/synoinfo.conf eventsmtp`
echo "hostname = "$smtp":25" > /opt/etc/esmtprc
echo "username = \""$user"\"" >> /opt/etc/esmtprc
echo "password = \""$pass"\"" >> /opt/etc/esmtprc
echo "mda = \"/opt/bin/procmail -d %T\"" >> /opt/etc/esmtprc
#
#-----------------------------
# Prepa entete du fichier mail
#-----------------------------
echo "FROM:SYNO "`hostname`"<"$mail">" > /tmp/ficmail
echo "TO:"`echo $mail | awk -F@ '{print $1}'`"<"$mail">" >> /tmp/ficmail
echo "SUBJECT: Disk diagnostic of "$jourlog >> /tmp/ficmail
echo "X-Priority: 1" >> /tmp/ficmail
echo "" >> /tmp/ficmail
#---------------------------------
# Infos SMART + Modele des disques
#---------------------------------
cd /dev
disque=0
> /tmp/tps-smart
> /tmp/tps-disk
# pour le CS-406
if test "$model" = "CS-406"
then
for bo in `/bin/ls hd?`
do
ph=`/opt/sbin/smartctl -A $bo | grep "Power_On_Hours" | awk '{ print $10 }'`
if test "$ph" != ""
then
disque=`expr $disque + 1`
pc=`/opt/sbin/smartctl -A $bo | grep "Power_Cycle_Count" | awk '{ print $10 }'`
echo "S.M.A.R.T values of DISQK number : "$disque" (/dev/"$bo")" >> /tmp/tps-smart
echo " " >> /tmp/tps-smart
/opt/sbin/smartctl -i $bo | grep -i "^[MDSU]" | grep -vi "smart" >> /tmp/tps-smart
echo " " >> /tmp/tps-smart
/opt/sbin/smartctl -A $bo | grep -i "ID#" >> /tmp/tps-smart
/opt/sbin/smartctl -A $bo | grep -i "^ *[0-9][0-9]* ..* 0x0" >> /tmp/tps-smart
echo "------------------------------------------------------------------------------------------------" >> /tmp/tps-smart
echo " " >> /tmp/tps-smart
echo "Disk n<>"$disque" : "$ph" hours / "$pc" starts." >> /tmp/tps-disk
fi
done
echo "Information of "`hostname`" with "$disque" disk(s) on "$jourlog >> /tmp/ficmail
# pour les autres syno
else
ph=`/opt/sbin/smartctl -A /dev/hda | grep "Power_On_Hours" | awk '{ print $10 }'`
pc=`/opt/sbin/smartctl -A /dev/hda | grep "Power_Cycle_Count" | awk '{ print $10 }'`
printf "S.M.A.R.T values of the DISK \n \n" >> /tmp/tps-smart
/opt/sbin/smartctl -i /dev/hda | grep -i "^[MDSU]" | grep -vi "smart" >> /tmp/tps-smart
echo " " >> /tmp/tps-smart
/opt/sbin/smartctl -A /dev/hda | grep -i "ID#" >> /tmp/tps-smart
/opt/sbin/smartctl -A /dev/hda | grep -i "^ *[0-9][0-9]* ..* 0x0" >> /tmp/tps-smart
echo "------------------------------------------------------------------------------------------------" >> /tmp/tps-smart
echo " " >> /tmp/tps-smart
echo "Internal hard disk : "$ph" hours / "$pc" starts." >> /tmp/tps-disk
echo "Information of "`hostname`" on "$jourlog >> /tmp/ficmail
fi
echo " " >> /tmp/ficmail
/bin/cat /tmp/tps-disk >> /tmp/ficmail
echo " " >> /tmp/ficmail
#----------------------
# Infos sur les volumes
#----------------------
cd /
> /tmp/tps-volume
for bo in `/bin/ls | grep volume`
do
echo `df -h $bo | grep $bo | awk '{print "Disk Usage. : "$2" - "$5" , "$4" available on "$6}'` >> /tmp/tps-volume
done
#----------------------------------------------------------------
# Temperature moyenne, mini, maxi, Heure les plus chaudes du jour
# Ecriture des infos
#----------------------------------------------------------------
nbr=`wc -l /tmp/tps-moy.log | awk '{print $1}'`
h=`echo $nbr | awk '{ print $1 / 60 * '$tpstemps'}' | awk -F. '{print $1}'`
m=`echo $nbr | awk '{ print (( $1 / 60 * '$tpstemps' ) - '$h') * 60 }' | awk -F. '{print $1}'`
Tmax=`cat /tmp/tps-moy.log | sort | tail -1`
Tmin=`cat /tmp/tps-moy.log | sort -r | tail -1`
HTmax=`cat /etc/tps.log | grep "Moyenne=$Tmax" | sed -e 's/^..* //'`
echo "Uptime : "$h" hour(s) "$m" minute(s)" >> /tmp/ficmail
#
# pour le CS-406
#
if test "$model" = "CS-406"
then
Delt=`cat /tmp/tps-del.log | sort | tail -1`
for bou in `/bin/cat /tmp/tps-moy.log`
do
t=`echo $bou | awk -F. '{print $1}'`
echo $t >> /tmp/tps.$t
done
echo "Min Average Temp. : "$Tmin"<22>" >> /tmp/ficmail
echo "Max Average Temp. : "$Tmax"<22>" >> /tmp/ficmail
echo "Max Diff./Disk : "$Delt"<22>" >> /tmp/ficmail
#
# pour les autres
#
else
Tmoy=0
for bou in `/bin/cat /tmp/tps-moy.log`
do
t=`echo $bou | awk -F. '{print $1}'`
Tmoy=`expr $Tmoy + $t`
echo $t >> /tmp/tps.$t
done
Tmoy=`echo $Tmoy | awk '{print $1 * 100 / '$nbr'}' | awk -F. '{print $1 / 100}'`
echo "Lowest Temperature : "$Tmin"<22>" >> /tmp/ficmail
echo "Highest Temperature : "$Tmax"<22>" >> /tmp/ficmail
echo "Average Temperature : "$Tmoy"<22>" >> /tmp/ficmail
fi
#
echo " " >> /tmp/ficmail
echo "Hottest ++ Hours : "$HTmax >> /tmp/ficmail
echo " " >> /tmp/ficmail
/bin/cat /tmp/tps-volume >> /tmp/ficmail
#-----------------------------------
# Tri du fichier de releve du jour
# et Stat temperature en % du temps
#-----------------------------------
#
echo " " >> /tmp/ficmail
echo "==== Distribution of reported temperature ====" >> /tmp/ficmail
echo " " >> /tmp/ficmail
for bou in `/bin/ls /tmp/tps.*`
do
t=`echo $bou | awk -F. '{ print $2 }'`
p=`wc -l $bou | awk '{print $1 * 10000 / '$nbr'}' | awk -F. '{print $1 / 100}'`
nbcar=`echo $p | awk -F. '{print $1}' | wc -c | awk '{print $1 - 1}'`
virgu=`echo $p | awk -F. '{print $2}' | wc -c | awk '{print $1 - 1}'`
if test "$virgu" = "0"
then
p=$p".00"
fi
if test "$nbcar" = "1"
then
echo $t"<22> ~~> " $p"% " >> /tmp/ficmail
else
echo $t"<22> ~~> " $p"% " >> /tmp/ficmail
fi
done
#
echo " " >> /tmp/ficmail
echo "=====================================================" >> /tmp/ficmail
echo " " >> /tmp/ficmail
/bin/cat /tmp/tps-smart >> /tmp/ficmail
#
/bin/cat /tmp/tps-disk.log | sed -e "s/~/ ~ /g" >> /tmp/ficmail
#
# Envoie du Mail avec test si connection OK
# sinon on sauvegarde le mail
#
connect=`ping -c 1 $smtp 2> /dev/null | grep -i "0% packet loss"`
#
if test "$connect" != ""
then
echo "Sending email to "$mail
/opt/bin/esmtp $mail < /tmp/ficmail
#
# envoie mail en retard
#
for bou in `/bin/ls /etc/tps.mail* 2> /dev/null`
do
/opt/bin/esmtp $mail < $bou
echo "Sending delayed email" $bou
rm -f $bou
done
#
# Sauvegarde du mail si pas de connexion
#
else
echo "Sending email to "$mail" impossible / delayed"
mv /tmp/ficmail /etc/tps.mail$jourlog
fi
#
# Nettoyage
#
rm -f /tmp/ficmail /tmp/tps*
#
# Initialisation des donnees temperatures
# et mise a jour de la date courante.
#
if test "$jourlog" != "$jour"
then
echo "Updating Temperature statements"
cat /etc/tps.conf | sed -e 's/tps-jour="..*"/tps-jour="'$jour'"/' > /tmp/tps.conf
mv /tmp/tps.conf /etc
> /etc/tps.log
fi
#
# Ecriture des donnees si le jour courant = base de donnee
#
else
echo "Average="$Tmoy" Delta="$delta" Disks="$temp" "$heure >> /etc/tps.log
fi
else
#
# cas ou le fichier de donnees temperatures
# n'existe pas
#
cat /etc/tps.conf | sed -e 's/tps-jour="..*"/tps-jour="'$jour'"/' > /tmp/tps.conf
mv /tmp/tps.conf /etc
> /etc/tps.log
fi
fi