1
#!/bin/bash

SUBJECT="WARNING CPU USAGE HIGH"
TO=darshankkdarshu@gmail.com
MESSAGE=/tmp/messages
echo "#######################" > $MESSAGE
echo "CPU statistics as follows.." >> $MESSAGE
mpstat >> $MESSAGE
echo "#######################" >> $MESSAGE
CPU_USAGE=$(top -b -n1 | awk '/^%Cpu/ {print $2}' | cut -d. -f1)

[ "$CPU_USAGE" -gt 1 ] && mail -s "$SUBJECT" "$TO" < $MESSAGE

The above script sends a mail once CPU usage is more than the limit. Although i have prescribed a vaild gmail id in the code, it is still sending the mail to /var/mail/root. How to fix this? I have done the necessary change following the link below. But the /var/mail/root is showing

Return-Path: <root@server2-SandyBridge-Platform>
Received: by server2-SandyBridge-Platform (Postfix, from userid 0)
        id A4977138269C; Fri, 14 Oct 2016 10:53:11 +0530 (IST)
Subject: WARNING CPU USAGE HIGH
To: <darshankkdarshu@gmail.com>
X-Mailer: mail (GNU Mailutils 2.99.98)
Message-Id: <20161014052311.A4977138269C@server2-SandyBridge-Platform>
Date: Fri, 14 Oct 2016 10:53:11 +0530 (IST)
From: root@server2-SandyBridge-Platform (root)

#######################
CPU statistics as follows..
Linux 3.13.0-98-generic (server2-SandyBridge-Platform)  Friday 14 October 2016  _x86_64_        (4 CPU)

10:53:11  IST  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
10:53:11  IST  all    3.07    0.15    1.13    1.48    0.00    0.01    0.00    0.00    0.00   94.17
#######################`
There is no mail in my mail id though.@Terrance
  • Add output of top -b -n1 | awk '/^%Cpu/ {print $2}' to your question. With my old Ubuntu 11.04 there is not output and with 16.04 output is 0.8. – Cyrus Oct 14 '16 at 05:42
  • Sure, will do. But i will be satisfied if the above code is able to send the mail. – darshan krishnaiah Oct 14 '16 at 05:44
  • I tested your code and it works fine. Did you set up your gmail for command line emailing? See here: http://askubuntu.com/a/522434/231142 – Terrance Oct 14 '16 at 05:51
  • I have done the required by following the prescribed link. Upon executing the script the mail in /var/mail/root shows like this – darshan krishnaiah Oct 14 '16 at 06:04
  • Why are you running those commands as root? Those commands work fine as a normal user. If you set up the command line email as a normal user, then run your script, it should email fine. I tested it on my home system to my @gmail account, and I received the CPU High email just fine. You need to get some sort of command line email setup for any outbound emails to work. – Terrance Oct 14 '16 at 06:27
  • Are you getting those mails in inbox? @Terrance – darshan krishnaiah Oct 14 '16 at 06:36
  • Yes, because I have setup my gmail account to send from my command line. If you do not set it up, root@yourhostname will not send outside, only to your root or user email. You need to set up the command line emailer. Follow through that link I posted the entire thing to set up your gmail account to send from command line. – Terrance Oct 14 '16 at 06:38

0 Answers0