Python login cacti crawl service infographic auto send weekly

Source: Internet
Author: User
Tags timedelta

#/usr/bin/env python# -*- coding: utf-8 -*- import time,datetime,socket, urllib,urllib2,cookielib,reimport smtplib,stringfrom email.mime.multipart import  mimemultipartfrom email.mime.text import mimetextfrom email.mime.image import  Mimeimagelog_url= ' HTTP://IP address/cacti/' host = "smtp.139.com"   #定义smtp主机SUBJECT  =  "Locik Weekly"   #定义邮件主题TO  =  "[email protected],[email protected]"   #定义邮件收件人TO_list  =  To.split (",") S_to = string.splitfields (To, ",") from =  "[email protected]"  # Define message Sender def time_shifting ():    threedayago =  (Datetime.datetime.now ()   - datetime.timedelta (days = 11))      otherStyleTime =  Threedayago.strftime ("%y-%m-%d %h:%m:%s")      format_otherStyleTime =  "%s 00:00:00"  % otherstyleTime.split () [0]    global start    start=time.mktime ( Time.strptime (format_otherstyletime, '%y-%m-%d %h:%m:%s '))     threeDayAgo =  (Datetime.datetime.now ()  - datetime.timedelta (days = 5))      Otherstyletime = threedayago.strftime ("%y-%m-%d %h:%m:%s")     result =   "%s 00:00:00"  % otherstyletime.split () [0]    global end     end=time.mktime (Time.strptime (result, '%y-%m-%d %h:%m:%s '))      Return start,enddef login ():     cj = cookielib. Cookiejar ()     global opener    opener = urllib2.build_ Opener (Urllib2. Httpcookieprocessor (CJ)     login_data = urllib.urlencode ( {' action ': ' Login ', ' login_username ': ' username ', ' login_password ': ' Password ' &NBSP;})     page = opener.open (log_url,data = login_data )      html = page.read ()     if re.findall ("graphs_new.php", HTML):         print  "OK"     else:         print  "Eerr"     page.close () def list_url_186 ():     global list_186    list_186 = ["HTTP://IP address/cacti/ graph_image.php?local_graph_id=78&rra_id=0&view_type=tree&graph_start=%s&graph_end=%s "% (int ( Start), int (end), "HTTP://IP address/cacti/graph_image.php?local_graph_id=30&rra_id=0&view_type=tree& graph_start=%s&graph_end=%s " % (int (start), int (end))," HTTP://IP address/cacti/graph_image.php?local_graph_id =31&rra_id=0&view_type=tree&graph_start=%s&graph_end=%s " % (int (start), int (end)),"/HTTP/ IP address/cacti/graph_image. php?local_graph_id=29&rra_id=0&view_type=tree&graph_start=%s&graph_end=%s " % (int (start), int (end)),]def get_image ():     count=0    for url in  list_186:        print url         count+=1        img=opener.open (URL)          print count        if  count == 1:            file_object  = open (' img/186-1week-disk.png ', ' WB ')              file_object.write (Img.read ())         elif count ==  2:            file_object = open (' Img/186-1week-cpuload.png ', 'WB ')             file_object.write (Img.read ())         elif count == 3:             file_object = open (' img/186-1week-network.png ', ' WB ')              file_object.write (Img.read ())          elif count == 4:             file_object = open (' img/186-1week-memory.png ', ' WB ')              file_object.write (Img.read ()) def addimg (Src,imgid):   #添加图片函数, parameter 1: Picture path, Parameter 2: Picture Id    fp = open (SRC, ' RB ')   #打开文件      msgimage = mimeimage (Fp.read ())   #创建MIMEImage对象, read the picture content and as a parameter      Fp.close ()   #关闭文件 &nbsP;   msgimage.add_header (' Content-id ', imgid)   #指定图片文件的Content-id, tag src used to      return msgImage  #返回msgImage对象def  sendmail ():    msg  = mimemultipart (' related ')   #创建MIMEMultipart对象, using related to define the embedded resources of the message body     msgtext  = mimetext ("" "    <table width="  border= "0"  cellspacing= " 0 " cellspacing=" 4 ">    <tr bgcolor=" #CECFAD " height="  style= " Font-size:14px ">    <td colspan=2> Below is the IP address system status graph </td>     </tr>    <tr bgcolor= "#EFEBDE"  height= " style=" font-size : 13px ">    <td>    </td ><td>    </td>    </ tr>  &Nbsp; <tr bgcolor= "#EFEBDE"  height= " style=" "font-size:13px" >     <td>    </td><td>     </td>    </tr>     </table> "" "," html "," Utf-8 ")  # the src attribute of the tag is referenced by Content-id     msg.attach (msgtext)   #MIMEMultipart对象附加MIMEText的内容     msg.attach (addimg ("img/ 186-1week-network.png "," Network "))   #使用MIMEMultipart对象附加MIMEImage的内容     msg.attach ( Addimg ("Img/186-1week-cpuload.png", "Load"))     msg.attach (Addimg ("Img/186-1week-memory.png "," mem "))     msg.attach (addimg (" Img/186-1week-disk.png "," Disk "))      msg[' Subject '] = subject    msg[' from ']=from    msg[' to ']  = to    try:        server = smtplib. SMTP ()         server.connect (HOST, "")          server.starttls ()         server.login (' [email  Protected] ', ' Password ')         server.sendmail (from,to_list,msg.as_string ())         server.quit ()          print  "Mail sent successfully!"     except Exception,e:        print  "Failure : "+ str (e)     print type (to) Def main ():     time_ Shifting ()     login ()     list_url_186 ()         get_image ()     sendmail () if __name__ ==  ' __main__ ':     main ()


This article is from the "Server" blog, so be sure to keep this source http://zhangfang2012.blog.51cto.com/6380212/1586624

Python login cacti crawl service infographic auto send weekly

Related Article

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.