Yum Update Notification Script for CentOS/RHEL

Simple script, when added to a cron job, will email you if your server has pending updates that need to be installed. I just threw the script in /etc/cron.daily and let it run.

#!/usr/bin/python
 
SERVER = "localhost"
FROM = "servername@example.com"
TO = ["username@example.com"] # must be a list
 
#**********************************************************************
# Do not edit below this line unless you know what you are doing
#**********************************************************************
 
import yum
import smtplib
import sys
 
# Run yum update and see if anything needs to be updated
yb = yum.YumBase()
yb.doConfigSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
updates = yb.update()
updates.sort()
 
if not updates:
	sys.exit()
 
formatedUpdates = []
count = 0
for pkg in updates:
	formatedUpdates.append("%s.%s.%s : %s" % (pkg.name,pkg.version,pkg.arch,pkg.repoid))
	count += 1
 
SUBJECT = "There are %d updates pending." % count
TEXT = ""
 
# Prepare actual message
message = """
From: %s
To: %s
Subject: %s
 
%s
 
%s
""" % (FROM, ", ".join(TO), SUBJECT,SUBJECT, "n".join(formatedUpdates))
 
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

Leave a Reply

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