Check that the partition has used space as a percentage of total space and send alert messages

Source: Internet
Author: User

#!/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 =&nbsp (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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.