#!/usr/bin/env python# -*- coding: utf-8 -*-# last night stars # script: Check all partitions of the server, If the partition has used space as a percentage of total space to exceed the threshold, send mail to the alert mailbox. # Note: This script can only be executed under Linux system and is applicable to python 2. import osimport reimport smtplibimport string# get all partitions of the system def Getallpartitionofsystem (): result = [] f = Os.popen (' Mount ') text = f.readlines () f.close () for line in text: if Re.search (R ' \bext\d ', line): Result.append (Line.split () [2]) return result# gets the percentage of space that the partition has used to occupy the total space def Getusedofpartition (path): sv = os.statvfs (path) free = (sv.f_bavail * sv.f_frsize) total =  (sv.f_blocks * sv.f_frsize) used = (sv.f_blocks - Sv.f_bfree) * sv.f_frsize return (float (used) / total) * 100# Send mail Def sendmail (Account, password, host, subject, to, from, text): body = string.join ( ' from: %s ' % FROM, ' to: %s ' % TO, ' subject: %s ' % SUBJECT, ', text ) server = smtplib, ' \ n '). SMTP () server.connect (host, ') server.login (account, password) server.sendmail (from, [to], body) server.quit () if __name__ == ' __main__ ': server = ' xx server ' partitionlist = getallpartitionofsystem () for partition in partitionList: percent = getusedofpartition (partition) if percent > 80: account = ' [email protected] ' PASSWORD = ' xxx ' host = ' smtp.163.com ' subject The%s partition for = '%s ' has more than 80% percent of space used for total space, please log in to the service check! ' % (server, partition)         &NBsp; to = ' [email protected] ' FROM = ' [email protected] ' TEXT = ' RT ' sendmail (Account, password, host, subject, to, from, text)
This article is from the "Last Night Stars" blog, please make sure to keep this source http://yestreenstars.blog.51cto.com/1836303/1749827
Check that the partition has used space as a percentage of total space and send alert messages