python on Ubuntu server monitoring scripts and SMS notification

Just finished writing a little bit about python deployed to monitor script ubuntu, real-timeserver information can be sent to the phone. A full toss over the weekend, the serverfinally toss come to an end. From the deployment of the system, the ssh security settings,firewall settings, and wordpress to build the apache, and then go to nginx, which relates to php, wordpress theme changes, all kinds of plug-in settings, etc. are the first to getthem a lot of Fortunately, from the Internet to find some solutions. Unfortunately, some stilldoes not solve the including wp supercache in debug prompt the super the cachedisabled, can not be normal application, only the wp the cache function. First only scrape together to utilize, so have the time to resolve.

The following use python on the server statistics script of the relevant parameters, and then through the smtp module Email sent to the mailbox, the mailbox will mail notificationmessage is sent to my phone. (ps: SMS interface had to toss the transfer under a). Just finished writing the main function for statistics server’s memory and disk informationgenerated through the python call system command. A related reduction in order toreduce the amount of data sent to your phone, you can find it just fine. python is more powerful, the code is also very simple, detailed code as follows. Follow-up have tocontinue to expand the interface, CPU, and process monitoring.

#author:H.J
#blogurl http://www.higis.org

#Address of the mail server and send e-mail account and password
MAIL_FROM='****@163.com'
SMTP_SERVER='smtp.163.com'
EMAIL_USER='*****'
EMAIL_PASSWD='********'

#Corresponds to the incoming mail server, more than words to consider an array
MAILS ='****@163.com'

def sendmail(body):
    #sending mail
    server=smtplib.SMTP(SMTP_SERVER)
    server.login(EMAIL_USER,EMAIL_PASSWD)
    server.sendmail(MAIL_FROM, MAILS, body)
    server.quit()

#Being only memory and hard drive statistics, organizational forms in detail, but sent tomobile phones to reduce their own to read to
Monitor_Items={
    "硬盘使用":"df -h | grep '/dev/xvda' | awk 'BEGIN { OFS=\",\" } {print $2,$3,$4,$5}'",
    "内存使用": "free -m | grep 'Mem' | awk 'BEGIN { OFS=\",\" } {print $2,$3,$4,$7}'"
}

def getCmdData(cmd):
    pipe=os.popen(cmd)
    data=pipe.read().strip()
    return data

def sendinfo():
    body=''

    for item in Monitor_Items:
        body+=item+"\n"
        data= getCmdData(Monitor_Items[item])+"\n"
        body+=data+"\n"
    if  body.isspace():
        body= 'get no data'

    senddata='''''From: %s
To: %s
Subject: %s

%s''' % (MAIL_FROM,MAILS,'Server-'+time.strftime('%Y-%m-%d %H:%M:%S'),body)
    print senddata
    sendmail(senddata)

if __name__=="__main__":
    sendinfo()

本文固定链接: http://www.higis.org/en/2012/04/22/python-on-ubuntu-static/ | Hi,GIS


该日志由 H.J 于2012年04月22日发表在 WebServer, Program 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: python on Ubuntu server monitoring scripts and SMS notification | Hi,GIS
关键字: , , ,

python on Ubuntu server monitoring scripts and SMS notification:等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter