control.sh 3.31 KB
#!/bin/bash
HOST=$(hostname)
PHONE="5493415959169"
NOW=$(date "+%D %T")
NOW_EPOCH=$(date -d "$NOW" +"%s")
LOG_FILE="/dev/shm/info_colmena"
ENDL="
"
BASE="*$HOST*:$ENDL$ENDL"

# Inicializacion
if [ ! -e $LOG_FILE ]
then
  touch $LOG_FILE
fi

### Carga de CPU ###
MSG=$BASE

# Cantidad maxima de carga
RESULT=$(cat /proc/cpuinfo | grep processor | tail -n1 | cut -c13)
MAX=$(echo $(( ($RESULT + 1) * 100 )) )

# Cantidad actual de carga
RESULT=$(cat /proc/loadavg | cut -c6-9 | tr -d .)
RESULT=$(expr $RESULT + 0)

PERC=$(echo $(( $RESULT * 100 / $MAX )) )

# Reinicio automatico
#if [ ! $PERC -gt 40 ]
#then
  #MSG="${MSG}La carga promedio es *$PERC%*. *Reiniciando...*"
  #wget "${WPPURL}${PHONE}${MSG}"
  #reboot
#fi

# Carga maxima superada
LAST_REPORT=$(grep carga $LOG_FILE | tail -n1 | cut -d ">" -f1)
LAST_REPORT_EPOCH=$(date -d "$LAST_REPORT + 1 day" +"%s") # Ventana de un dia

if [ -z "$LAST_REPORT" ] || [ $NOW_EPOCH \> $LAST_REPORT_EPOCH ]
then
  if [ ! $RESULT -lt $MAX ]
  then
    echo "${NOW} > carga" >> $LOG_FILE
    MSG="${MSG}La carga promedio supera la carga maxima de *$MAX*. *Carga: $RESULT*.$ENDL"
  fi
fi

### Temperatura ###
LAST_REPORT=$(grep temperatura $LOG_FILE | tail -n1 | cut -d ">" -f1)
LAST_REPORT_EPOCH=$(date -d "$LAST_REPORT + 1 day" +"%s") # Ventana de un dia

if [ -z "$LAST_REPORT" ] || [ $NOW_EPOCH \> $LAST_REPORT_EPOCH ]
then
  RESULT=$(sensors | grep °C | head -n1 | cut -c16-17)

  if [ ! $RESULT -lt 70 ]
  then
    echo "${NOW} > temperatura" >> /dev/shm/info_colmena
    MSG="${MSG}La temperatura alcanza *$RESULT°C*.$ENDL"
  fi
fi

### Uso del disco ###
LAST_REPORT=$(grep disco $LOG_FILE | tail -n1 | cut -d ">" -f1)
LAST_REPORT_EPOCH=$(date -d "$LAST_REPORT + 1 day" +"%s") # Ventana de un dia

if [ -z "$LAST_REPORT" ] || [ $NOW_EPOCH \> $LAST_REPORT_EPOCH ]
then
  RESULT=$(df / | cut -c52-54 | grep 9.)

  if [ ! -z "$RESULT" ]
  then
    echo "${NOW} > disco" >> /dev/shm/info_colmena
    MSG="${MSG}El disco se encuentra por arriba del *90%* de uso.$ENDL"
  fi

  RESULT=$(df / | cut -c52-54 | grep 100)

  if [ ! -z "$RESULT" ]
  then
    echo "${NOW} > disco" >> /dev/shm/info_colmena
    MSG="${MSG}El disco se encuentra en el *100%* del uso.$ENDL"
  fi
fi

### Disco roto ###
LAST_REPORT=$(grep fallo-critico $LOG_FILE | tail -n1 | cut -d ">" -f1)
LAST_REPORT_EPOCH=$(date -d "$LAST_REPORT + 1 day" +"%s") # Ventana de un dia

if [ -z "$LAST_REPORT" ] || [ $NOW_EPOCH \> $LAST_REPORT_EPOCH ]
then
  RESULT=$(dmesg | grep "exception Emask" | tail -n1)

  if [ ! -z "$RESULT" ]
  then
    echo "${NOW} > fallo-critico" >> /dev/shm/info_colmena
    MSG="${MSG}*HAY UN FALLO CRITICO EN UN DISCO*.$ENDL"
  fi
fi

### Uso de memoria Swap ###
LAST_REPORT=$(grep swap $LOG_FILE | tail -n1 | cut -d ">" -f1)
LAST_REPORT_EPOCH=$(date -d "$LAST_REPORT + 1 day" +"%s") # Ventana de un dia

if [ -z "$LAST_REPORT" ] || [ $NOW_EPOCH \> $LAST_REPORT_EPOCH ]
then
  # Cantidad maxima de Swap
  MAX=$(free | grep Swap | sed 's/\s\+/,/g' | cut -d , -f2)

  # Cantidad usada de Swap
  RESULT=$(free | grep Swap | sed 's/\s\+/,/g' | cut -d , -f3)

  RESULT=$(echo $(($RESULT * 100 / $MAX)) )

  if [ ! $RESULT -lt 50 ]
  then
    echo "${NOW} > swap" >> /dev/shm/info_colmena
    MSG="${MSG}El swap se encuentra en *$RESULT%* de uso.$ENDL"
  fi
fi

# Se pidio mandar algo?
if [[ ! "$MSG" == "$BASE" ]]
then
  ./send.sh wpp1 "$MSG" "$PHONE" "text"
fi