Скрипт автоматического уведомления по электронной почте о пороге памяти

проверить права пользователя на файл cron для запуска cron в /var/spool/cron/crontabs.

-rw------- 1 root root 338 Dec  8 12:49 root
0
27.12.2019, 12:38
2 ответа

Сделайте эти линии:

MEM_LOAD=`free -t | awk 'FNR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'`
if [[ $MEM_LOAD > $LOAD ]];

быть

MEM_LOAD=`free -t | awk 'FNR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'`
MEM_L=`free -t | awk 'FNR == 2 {print int($3/$2*100)}'`
if [ $MEM_L -gt $LOAD ];

Вы сравниваете строку с числом. Или вы можете пропустить один `awk:

MEM_L=`free -t | awk 'FNR == 2 {print int($3/$2*100)}'`
MEM_LOAD=`echo "Current Memory Utilization is: "${MEM_L} "%"`
if [ $MEM_L -gt $LOAD ];

И используйте целое число для переменной LOAD

LOAD=80
2
28.01.2020, 02:29

Вот рабочая версия вашего скрипта с некоторыми улучшениями:

#!/bin/bash
# Shell script to monitor or watch the high Mem-load
# It will send an email to $ADMIN, if the (memroy load is in %) percentage
# of Mem-load is >= 80%

# you don't need this, $HOSTNAME is a system variable and
## already set.
#HOSTNAME=`hostname`

#Use lowercase variable names to avoid name collision with system variables. 
load=80
mailer=/bin/mail
mailto="skrishna1@xxx.com"

## You can't use decimals in a shell arithmetic comparison, but you can use awk
## to do the test for you instead.
if free -t | awk -vm="$load" 'NR == 2 { if($3/$2*100 > m){exit 0}else{exit 1}}'; then
  ## You weren't setting your "$CPU_LOAD" anywhere. It looks like you want the % use,
  ## so I am setting it here. Also note how I'm using $() instead of backticks.
  ## There's nothing wrong with backticks, but the $() is cleaner, easier to nest
  ## and generally preferred.
  cores=$(grep -c processor /proc/cpuinfo)
  cpuPerc=$(ps -eo pcpu= | awk -vcores=$cores '{k+=$1}END{printf "%.2f", k/cores}')
  ## Get the more actual value for reporting
  memPerc=$(free -t | awk 'FNR == 2 {printf("%.2f%"), $3/$2*100}')

  ## Avoid using a temp file
  message=$(cat <<EoF
Please check your processess on $HOSTNAME the value of cpu load is $cpuPerc% & Current Memory Utilization is: %$memPerc.
$(ps axo %mem,pid,euser,cmd | sort -nr | head -n 10)
EoF
         )
  printf '%s\n' "$message" | 
    "$mailer" -s "Memory Utilization is High > $load%, $memPerc % on $HOSTNAME" \
     "$mailto"
fi
0
28.01.2020, 02:29

Теги

Похожие вопросы