mcelog: Shell Script To Send Email Alert When Hardware Errors Detected

#!/bin/bash
# Linux 64 bit kernel shell script to collect hardware errors via /var/log/mcelog
# and send email alert.
# -------------------------------------------------------------------------
# Copyright (c) 2008 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Tested under RHEL and Debian Linux 64 bit version.
# mcelog must be installed. 
# See: http://www.cyberciti.biz/tips/linux-server-predicting-hardware-failure.html
LOGGER=/usr/bin/logger
FILE=/var/log/mcelog
AEMAIL="vivek@nixcraft.net.in"
ASUB="H/W Error - $(hostname)"
AMESS="Warning - Hardware errors found on $(hostname) @ $(date). See log file for the details /var/log/mcelog."
OK_MESS="$0 - OK: NO Hardware Error Found."
WARN_MESS="$0 - ERROR: Hardware Error Found."
 
die(){
	echo "$@"
	exit 999
}
 
warn(){
	echo $AMESS | email -s "${ASUB}" ${AEMAIL}
	$LOGGER "$WARN_MESS"
}
 
[ ! -f "$FILE" ] && die "Error - No $FILE exists or mcelog is not configured"
[ $(grep -c -i "hardware error" $FILE) -gt 0 ] && warn || $LOGGER $OK_MESS
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • bandar.s Apr 4, 2010 @ 8:25

    Thanks ,this will be useful

  • Mehdi Aug 14, 2009 @ 10:43

    Vivek;

    I tried the script on my ubuntu904 (i386 machine) but I get this error;
    (I apt-get install mcelog ) but still get;
    Error – No /var/log/mcelog exists or mcelog is not configured

    How do I configure the mcelog?

    Thanks!

    • 🐧 Vivek Gite Aug 14, 2009 @ 10:54

      This script only works with 64bit Linux kernel.

      • vamsi Nov 19, 2009 @ 4:34

        can you please tell how to get this work on 32 bit os ?

        Thanks :)

        • 🐧 Vivek Gite Nov 20, 2009 @ 18:08

          No, you can’t this only work in 64bit CPU / computer.

  • Mehdi Aug 14, 2009 @ 10:36

    Thanks, I’ll edit and use it…(heh I dont think you want to get emails for my hardware failure) :-)

    Regards;

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.