Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Server Control è libero !
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian)
View previous topic :: View next topic  
Author Message
lxnay
Retired Dev
Retired Dev


Joined: 09 Apr 2004
Posts: 661
Location: Italy

PostPosted: Sat Jun 25, 2005 11:50 am    Post subject: Server Control è libero ! Reply with quote

Lo sto usando da un mese e funziona davvero bene. C'è da dire che comunque, il software è stato progettato specificatamente per il mio server ma come potete notare, è molto scalabile e adattabile ;).

I requisiti sono, programma lm_sensors (sensors) correttamente configurato (editate i " | grep" in poll_* in modo adeguato), sendEmail, una riga da aggiungere in /etc/sudoers se si vuole runnare in da utente e bc (un programma per fare dei calcoli matematici da shell). Facendolo partire senza parametri, stampa il rapporto a video, usando check, fa un controllo del sistema e in caso di problemi manda una mail (i livelli di criticità sono giallo e rosso, quest'ultimo porta allo spegnimento del PC), usando report, manda via mail un rapporto.

Code:
#!/bin/sh
# Programma che tiene sott'occhio le temperature dei componenti e
# nel caso di problemi non critici (semaforo giallo) manda una mail,
# nel caso di problemi critici (semaforo rosso) manda una mail e spegne
# il computer. Ogni giorno mi manda un rapporto dello stato del sistema
# catturato alle 8.00, alle 12.00 e alle 18.00.

# Nella mail, oltre al rapporto giornaliero sulle temperature, mi faccio
# inviare anche le ultime n righe di messages, dmesg o qualsiasi file io voglia

# Check_temp variables
# CPU limit temperature
cpu_temp_limit=60
# MB limit temperature
mb_temp_limit=44
# CASE limit temperature
case_temp_limit=35
# HD limit temperature
hd_temp_limit=54
# Limit between low and high risk
limit_between_yellow_and_red_flag=5
# HD to monitor, must be IDE or sata using a working kernel and >=hddtemp-0.3-beta13
hd=/dev/hda
dati_hd=/dev/sda
datibck_hd=/dev/sdb

# Check_voltages variables
upper_limit_33V=3.70
upper_limit_5V=5.70
upper_limit_12V=13.15
lower_limit_33V=3.05
lower_limit_5V=4.40
lower_limit_12V=11.20
# can be between 0.01 and xx.yy
voltage_limit_between_yellow_and_red_flag=0.35

# Master variables
who_am_i=$(whoami)
# Configuration file for sudo
sudoers_file="/etc/sudoers"

if [ ! -e $PWD/controllo_server.sh ]; then

   echo "*** You're not into the executable directory ***"
   exit 1

fi

sensors_executable=$( whereis sensors )
if [ "$sensors_executable" = "sensors:" ]; then

   echo "*** It seems that you don't have lm_sensors installed, sensors executabile cannot be found ***"
   exit 2

fi

# insert sensors check (if it's configured or not)

# checking for bc executable
bc_executable=$( whereis bc )
if [ "$bc_executable" = "bc:" ]; then

   echo "*** It seems that you don't have bc installed, bc executabile cannot be found ***"

   if [ "$who_am_i" != "root" ]; then
   
      echo "*** It seems that you aren't root, please run as root once to install sys-apps/bc ***"
      exit 2
   
   else

      echo "*** Installing sys-apps/bc ***"
      # put what you want below in order to be able to run with your distro (for eg, apt-get install bc).
      emerge sys-apps/bc

   fi

fi

# checking for sendEmail executable
sendemail_executable=$( whereis sendEmail )
if [ "$sendemail_executable" = "sendEmail:" ]; then

   echo "*** It seems that you don't have sendEmail installed, sendEmail executabile cannot be found ***"

   if [ "$who_am_i" != "root" ]; then
   
      echo "*** It seems that you aren't root, please run as root once to install net-mail/sendEmail ***"
      exit 2
   
   else

      echo "*** Installing net-mail/sendEmail ***"
      # put what you want below in order to be able to run with your distro (for eg, apt-get install sendEmail ?).
      emerge net-mail/sendEmail

   fi

fi

# checking for hddtemp executable

# checking for sudo executable
sudo_executable=$( whereis sudo )
if [ "$sudo_executable" = "sudo:" ]; then

   echo "*** It seems that you don't have sudo installed, sudo executabile cannot be found ***"

   if [ "$who_am_i" != "root" ]; then
   
      echo "*** It seems that you aren't root, please run as root once to install app-admin/sudo ***"
      exit 2
   
   else

      echo "*** Installing app-admin/sudo ***"
      # put what you want below in order to be able to run with your distro (for eg, apt-get install sudo ?).
      emerge app-admin/sudo

   fi

# doing extra controls
else


   if [ "$who_am_i" != "root" ];then

      if [ ! -e $sudoers_file ]; then
         echo "*** You aren't root, so you need sudo, but" $sudoers_file " cannot be found ***"   
      fi

      string_to_search="ALL=NOPASSWD:/usr/sbin/hddtemp"
      search_result=$(cat $sudoers_file | grep -i "$string_to_search" )
      echo $searc_result
      if [ -z "$search_result" ]; then
         echo "You need to append >>" $who_am_i "<< to " $sudoers_file "in order to get hddtemp work as user"
         exit 2
      fi
   

   fi

fi

# inserire controlli su livello RAM ??


# DO NOT CHANGE BELOW
poll_cputemp=$( sensors | grep "CPU Temp" | cut -f 4 -d " " | sed "s/+//" | sed "s/°C//" )
poll_mbtemp=$( sensors | grep "M/B Temp" | cut -f 6 -d " " | sed "s/+//" | sed "s/°C//" )
poll_casetemp=$( sensors | grep "temp3" | cut -f 6 -d " " | sed "s/+//" | sed "s/°C//" )
poll_5V=$( sensors | grep "+5V" | cut -f 8 -d " " | sed "s/+//" )
poll_12V=$( sensors | grep "+12V" | cut -f 6 -d " " | sed "s/+//" )
poll_33V=$( sensors | grep "+3.3V" | cut -f 6 -d " " | sed "s/+//"  )
poll_hd=$( sudo /usr/sbin/hddtemp $hd | cut -f 4 -d " " | sed "s/°C//" )
poll_datihd=$( sudo /usr/sbin/hddtemp $dati_hd | cut -f 4 -d " " | sed "s/°C//" )
poll_datibckhd=$( sudo /usr/sbin/hddtemp $datibck_hd | cut -f 4 -d " " | sed "s/°C//" )
data=$(date +%A_%d-%m-%Y_%kh%Mm | sed "s/ /0/")
red_flag="0"
yellow_flag="0"
send_email="/usr/bin/sendEmail"
senderemail="la_mando_io@mestesso.org"
receiveremail="email1@libero.it"
receiveremail2="email2@libero.it"
smtp="smtp.tiscali.it:25"
shutdown_possibility="1"
logfile1="/var/log/messages"
logfile2="/var/log/auth.log"
logfile3=""
logfile1_lines="40"
logfile2_lines="40"
logfile3_lines="40"
dmesg_lines="30"
report_file="reports/$HOSTNAME-$data.txt"


# check_temp is an universal function that is capable to check and compare two input temperatures ($1 and $2)
check_temp() {

first_operand=$1
second_operand=$2

is_float=$( echo $first_operand | grep -i "\." )
if [ -n "$is_float" ]; then
   first_operand=$( echo $first_operand | cut -d "." -f 1 )
fi
unset is_float


if [ "$first_operand" -ge $second_operand ];then
   differenza=$( echo "$first_operand-$second_operand" | bc )
   if [ "$differenza" -ge "$limit_between_yellow_and_red_flag" ]; then
      echo "Very critical (limit: "$second_operand"°C)" >> $report_file
      red_flag="1"
   else
      echo "Quite critical (limit: "$second_operand"°C)" >> $report_file
      yellow_flag="1"
   fi

else
   differenza=$( echo "$second_operand-$first_operand" | bc )
   echo -e "Good, ~"$differenza"°C before limit" >> $report_file

fi

unset first_operand
unset second_operand
unset differenza

}

check_voltages() {

poll_voltage=$1
upper_limit=$2
lower_limit=$3

# upper and lower limit check
upper_offset=$( echo "$upper_limit-$poll_voltage" | bc )
lower_offset=$( echo "$lower_limit-$poll_voltage" | bc )

# if upper_offset is positive, the limit is not exceeded
# if upper_offset is negative, the limit is exceeded !!
# if lower_offset is positive, the limit is exceeded !!
# if lower_offset is negative, the limit is not exceeded

upper_is_exceeded=$(echo $upper_offset | grep -i "-" )
lower_is_not_exceeded=$(echo $lower_offset | grep -i "-" )
voltage_limit_between_yellow_and_red_flag=$( echo $voltage_limit_between_yellow_and_red_flag | sed "s/\.//" )

if [ -n "$upper_is_exceeded" ];then

   upper_offset=$( echo $upper_offset | sed "s/-//" | sed "s/\.//" )

   if [ "$upper_offset" -ge "$voltage_limit_between_yellow_and_red_flag" ];then
      echo "Very critical (upper limit is:" $upper_limit"V)" >> $report_file
      red_flag="1"
   else
      echo "Quite critical (upper limit is:" $upper_limit"V)" >> $report_file
      yellow_flag="1"
   fi

elif [ ! -n "$lower_is_not_exceeded" ]; then

   lower_offset=$( echo $lower_offset | sed "s/-//" | sed "s/\.//" )

   if [ "$lower_offset" -ge "$voltage_limit_between_yellow_and_red_flag" ];then
      echo "Very critical (lower limit is:" $lower_limit"V)" >> $report_file
      red_flag="1"
   else
      echo "Quite critical (lower limit is:" $lower_limit"V)" >> $report_file
      yellow_flag="cd /home/fabio/progetti/controllo-server/; ./controllo_sever.sh report1"
   fi

else

   echo -e "Good, in range between" $lower_limit"V ~ "$upper_limit"V" >> $report_file

fi


unset is_exceded
unset poll_voltage
unset upper_limit
unset lower_limit

}

# DO NOT CHANGE ABOVE


echo -e " :: Starting Server Report :: "
echo

if [ ! -d reports ]; then
   mkdir reports
fi

if [ ! -e $report_file ]; then
   #echo "File reports/"$HOSTNAME"-"$data".txt does not exist, creating..."
   touch $report_file
else
   rm $report_file
   touch $report_file
fi


#
# STEP A: Creating a detailed report
#

# for Cron Jobs
   source /etc/profile

echo -en "Creating report..."
echo "'°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°'" >> $report_file
echo "°                                                                             °" >> $report_file
echo "°                 Server Report, displaying computer status                   °" >> $report_file
echo "°                                                                             °" >> $report_file
echo "'°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°'" >> $report_file
echo "Uptime: $(uptime)">> $report_file
echo >> $report_file
echo "CPU Info:" >> $report_file
cat /proc/cpuinfo >> $report_file
echo >> $report_file
echo "Memory Info:" >> $report_file
cat /proc/meminfo >> $report_file
echo >> $report_file

# Calculating temperatures:

echo -e " × Temperatures ×" >> $report_file
echo >> $report_file

echo -en " ¹ M/B Temp:\t\t" $poll_mbtemp"°C\t\tSTATUS: " >> $report_file
     check_temp "$poll_mbtemp" "$mb_temp_limit"

echo -en " ² CASE Temp:\t\t" $poll_casetemp"°C\t\tSTATUS: " >> $report_file
     check_temp "$poll_casetemp" "$case_temp_limit"

echo -en " ³ CPU Temp:\t\t" $poll_cputemp"°C\t\tSTATUS: " >> $report_file
     check_temp "$poll_cputemp" "$cpu_temp_limit"

echo -en " × HD Temp ("$hd"):\t" $poll_hd"°C\t\tSTATUS: " >> $report_file
     check_temp "$poll_hd" "$hd_temp_limit"
echo

echo -en " × DATI HD Temp ("$dati_hd"):\t" $poll_datihd"°C\t\tSTATUS: " >> $report_file
     check_temp "$poll_datihd" "$hd_temp_limit"

echo -en " × DATIBCK Temp ("$datibck_hd"):\t" $poll_datibckhd"°C\t\tSTATUS: " >> $report_file
     check_temp "$poll_datibckhd" "$hd_temp_limit"


# Calculating voltages:

echo >> $report_file
echo -e " × Voltages ×" >> $report_file
echo >> $report_file

echo -en " ¹ 3.3V Rail:\t\t" $poll_33V"V\t\tSTATUS: " >> $report_file
     check_voltages "$poll_33V" "$upper_limit_33V" "$lower_limit_33V"

echo -en " ² 5V Rail:\t\t" $poll_5V"V\t\tSTATUS: " >> $report_file
     check_voltages "$poll_5V" "$upper_limit_5V" "$lower_limit_5V"

echo -en " ³ 12V Rail:\t\t" $poll_12V"V\t\tSTATUS: " >> $report_file
     check_voltages "$poll_12V" "$upper_limit_12V" "$lower_limit_12V"

echo >> $report_file
echo >> $report_file

if [ -n "$logfile1" ]; then

   if [ -e "$logfile1" ]; then
      echo "===>" $logfile1 ":" >> $report_file
      cat $logfile1 | tail -n $logfile1_lines >> $report_file
      echo "===> end of" $logfile1 >> $report_file
      echo >> $report_file
      echo >> $report_file
   else
      echo $logfile1 "does not exist" >> $report_file
   fi

elif [ -n "$logfile2" ]; then

   if [ -e "$logfile2" ]; then
      echo "===>" $logfile2 ":" >> $report_file
      cat $logfile2 | tail -n $logfile1_lines >> $report_file
      echo "===> end of" $logfile2 >> $report_file
      echo >> $report_file
      echo >> $report_file
   else
      echo $logfile2 "does not exist" >> $report_file
   fi

elif [ -n "$logfile3" ]; then

   if [ -e "$logfile3" ]; then
      echo "===>" $logfile3 ":" >> $report_file
      cat $logfile3 | tail -n $logfile1_lines >> $report_file
      echo "===> end of" $logfile3 >> $report_file
      echo >> $report_file
      echo >> $report_file
   else
      echo $logfile3 "does not exist" >> $report_file
   fi

fi


echo "===> dmesg :" >> $report_file
   dmesg | tail -n $dmesg_lines >> $report_file
echo >> $report_file
echo "===> end of dmesg" >> $report_file
echo >> $report_file
echo >> $report_file
#echo "ENVIRONMENT:" >> $report_file
#echo >> $report_file
#env >> $report_file
#echo >> $report_file
#echo >> $report_file

# End of report creation
echo -e "Done"

# STEP B: checking for yellow or red flags
# - if yellow, send an e-mail and continue to monitor
# - if red, shutdown the computer

# If $1 is null, display report
# If $1 is report, write an e-mail
# If $1 is check, check and if something is wrong, write an e-mail and take the right action

if [ -z "$1" ]; then

   echo
   echo "Displaying report..."
   cat $report_file

elif [ "$1" = "report" ]; then

   echo -en "Sending Report via e-mail..."
   cat $report_file | $send_email -f $senderemail -t $receiveremail $receiveremail2 -u "Status Report - $(date)" -s $smtp -q
   echo -e "Done"

elif [ "$1" = "check" ]; then

   echo -en "Checking System Status..."
   if [ "$red_flag" = "1" ] || [ "$yellow_flag" = "1" ]; then

      if [ "$red_flag" = "1" ];then

      echo -en "There is a very critical problem !! sending Report via e-mail..."
      cat $report_file | $send_email -f $senderemail -t $receiveremail $receiveremail2 -u "RED FLAG Problem - $(date)" -s $smtp -q
      echo -e "Done"
      
         if [ "$shutdown_possibility" = "1" ]; then
            sync
            
            if [ "$who_am_i" = "root" ];then
               # -f force shutdown immediately
               /sbin/halt -p
            else
               echo "...halt disabled as user"
            fi
   
         else
            echo "...halt command ignored"
         fi

      elif [ "$yellow_flag" = "1" ]; then

      if [ ! -e "reports/yellow-mail-sent" ]; then
         touch "reports/yellow-mail-sent"
         echo "45" > "reports/yellow-mail-sent"
         echo -en "There is a quite critical problem !! sending Report via e-mail (if not already done)..."
         cat $report_file | $send_email -f $senderemail -t $receiveremail $receiveremail2 -u "YELLOW FLAG Problem - $(date)" -s $smtp -q
         echo -e "Done"
      else

         count=$(cat "reports/yellow-mail-sent")
         if [ "$count" = "0" ]; then
            unset count
            rm "reports/yellow-mail-sent"
         else
            let "count = $count - 1"
            echo $count > "reports/yellow-mail-sent"
         fi

      fi

      fi

   else
      echo -e "All OK"
      
      if [ -e "reports/yellow-mail-sent" ]; then
         rm "reports/yellow-mail-sent" -rf
      fi
   fi



fi


Download: Server Control 0.2
deve essere eseguito dalla sua stessa cartella, comunque è anti-idiota, quindi non correte problemi. Se non trova qualche applicazione già installata, basta eseguirlo una volta da root che si arrangia a fare tutto.
_________________
http://www.sabayon.org


Last edited by lxnay on Sat Jun 25, 2005 4:37 pm; edited 2 times in total
Back to top
View user's profile Send private message
xchris
Advocate
Advocate


Joined: 10 Jul 2003
Posts: 2824

PostPosted: Sat Jun 25, 2005 11:57 am    Post subject: Reply with quote

perche' non fai un upload da qualche parte?

sai che ci sono un sacco di pigri in giro :) :lol:

OT:complimenti per i vari Live..
_________________
while True:Gentoo()
Back to top
View user's profile Send private message
lxnay
Retired Dev
Retired Dev


Joined: 09 Apr 2004
Posts: 661
Location: Italy

PostPosted: Sat Jun 25, 2005 4:40 pm    Post subject: Reply with quote

Fatto.... pigrone ;) !
Spero che sia utile a qualcuno.... l'ho messo in piedi in 4 orette prima di andare al mare... quindi vedi tu ;)
_________________
http://www.sabayon.org
Back to top
View user's profile Send private message
lxnay
Retired Dev
Retired Dev


Joined: 09 Apr 2004
Posts: 661
Location: Italy

PostPosted: Sun Jun 26, 2005 9:46 am    Post subject: Reply with quote

...dai 110 visite e nessuno che scrive nemmeno "prot" ?
_________________
http://www.sabayon.org
Back to top
View user's profile Send private message
.:deadhead:.
Advocate
Advocate


Joined: 25 Nov 2003
Posts: 2963
Location: Milano, Italy

PostPosted: Sun Jun 26, 2005 9:59 am    Post subject: Reply with quote

prot :-D
sei finito nella mia todo list, appena lo provo ti fò sapere
_________________
Proudly member of the Gentoo Documentation Project: the Italian Conspiracy ! ;)
Back to top
View user's profile Send private message
rota
l33t
l33t


Joined: 13 Aug 2003
Posts: 960

PostPosted: Sun Jun 26, 2005 1:44 pm    Post subject: Reply with quote

Quote:
prot
8O 8O
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum