Background and ideas:
51 small vacation before, the company asked me to do a server patrol.
1, wrote a simple script to obtain the various basic information of the server: CPU, memory, swap partition usage, disk, network card information various, concrete see script, append this information to a file, and then do a summary on the monitor machine, summary method does not elaborate, I use for the loop SSH append
2. Then use Python's Xlsxwriter module to generate Excel
3. Finally, use Python to send an Excel message to the specified mailbox
Get the Server Information Section script:
#取所需要的内网IPInt_ip = ' ifconfig|awk '/inet addr/ {gsub (/:/, " ");p rint $3} ' |egrep ' ^ 192.168.18 "|head -1 ' #取除该内网ip以及127. All ipout_ip_list= ' ifconfig|awk '/inet addr/ {gsub except 0.0.1 (/:/, " ");p rint $3} ' |grep -v $Int _ip|grep -v 127.0.0.1 ' #取内存总值, Unit g, rounded memory= ' free -m|awk '/mem/ {printf ("%.f\n", $2/1024)} ' #取内存free值, from a system point of view, take the first line of Freememory_free= ' free -m|awk '/mem/ {printf "%.f\n", $4/1024} ' #取CPU核数Cpu_num = ' grep processor / Proc/cpuinfo|wc -l ' #取服务器运行时间Uptime = ' uptime|awk ' {print $3} ' #取最近15分的负载Load = ' uptime|awk ' { print $12} ' #取磁盘大于80% of the disk directory disk= ' df -h|awk ' {a=+$ (NF-1), if (a>=80) print $NF} ' #swap分区总值, Unit is G, rounded swap= ' free -m|awk '/swap/ {printf ("%.f\n", $2/1024)} ' #swap分区Swap_free = ' free -m|awk '/swap/ {printf '%.f\n ', $4/1024} ' generate excelexcel.py:#!/usr/bin/python# -*- coding: utf-8 -*-from xlsxwriter import workbookimport configparserimport timeimport Sendmaildef write_excel (file): ' 1, settings Excel styles     2, write data to Excel ' # build Excel file work = workbook. Workbook (file) # create sheet, table name default worksheet = work.add_ Worksheet () # set font bold, font size format_title = work.add _format ({' Bold ': true, ' font_size ': 16}) # Set horizontal alignment, vertical alignment format_title.set_align (' center ') format_title.set_align (' vcenter ') format_body = work.add_format ({' Font_size ': 14}) # Set style, row height, column width worksheet.set_row (0, 25) worksheet.set_column (0, 7, 30) worksheet.set_column ( 8,9,50) worksheet.set_column (9,10,100) # define header title = ( ' Server IP ', ' memory size  GB ', ' memory remaining  GB ', ' swap size  GB ', ' Swap remaining  GB ', ' run time days ', ' System load ', ' cpu number of cores ', ' disk exceeds 80% ', ' rest IP ', ) row = 0 col = 0 #写入表头 for item in title: item = unicode (item, "Utf-8") worksheet.write (Row, col, item, format_title) col+=1 # Write Data cf = configparser.configparser () cf.read ('/data/ Scripts/excel/config_total.txt ') for ip in cf.sections (): row+=1 col = 0 for opt in cf.options (IP): key=cf.get (ip,opt) worksheet.write (row,col,key,format_body) &nBsp; col+=1 work.close () if __name__ == ' __main__ ': Excel_name = "Server_%s.xls" % (Time.strftime ("%y-%m-%d", time.localtime ())) write_excel (excel_name) sendmail.send_mail (' ********@139.com ', ' Server Patrol table ', ' fuliao server message ', excel_name)
Sendmail.py:
#!/usr/bin/pythonimport smtplibfrom email.header import headerfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.application import mimeapplicationimport sysmail_host = ' smtp.163.com ' mail _user = ' 163 email account ' mail_pass = ' 163 email password ' mail_postfix = ' 163.com ' Def send_mail (to_list,subject,content,name): me = mail_user+ "<" +mail_user+ "@" +mail_ postfix+ ">" msg = mimemultipart () msg[' Subject '] = subject msg[' from '] = me msg[' to '] = to_list msg.attach (Mimetext (content)) part = Mimeapplication (open (name, ' RB '). Read ()) part.add_header (' Content-disposition ', ' Attachment ', filename=name) msg.attach (part) try: s = smtplib. SMTP () s.connect (mail_host) s.login (Mail_user,mail_pass) s.sendmail (Me,to_list, Msg.as_string ()) s.close () return True except Exception,e: print str (e) return false
The last generated Excel graph:
This article from the "9400142" blog, reproduced please contact the author!
Server patrol shell scripts, Python generates Excel documents and sends messages