Detecting web site hangs Ma Cheng (Python)

Source: Internet
Author: User

The system administrator usually retrieves the code from the Svn/git, after deploying the site usually first generates the MD5 value of all the files of the site, if the Site page content is tampered with (such as hanging horse), etc., can be compared to the previous generation MD5 value quickly find to go to those files are changed, in order to enable the system administrator to find the first time, Can be combined with tools such as crontab or Nagios.

The program tests are as follows:

    # python check_change.py        Usage:python check_change.py update/home/wwwroot               /home/wwwroot    #  python check_change.py update/data/www #生成站点的md5值    #  echo ' >/data/www/sitemap.html #测试清空文件 
        #  rm-rf/data/www/sitemap.xml #测试删除文件    #  python check_change.py Check/data/www  #查找那些文件被篡改    /data/www/sitemap.xml    /data/www/sitemap.html

The code is as follows (check_change.py):

    #!/usr/bin/env python          Importos,sys,subprocessdefUpdate (PATH): F= Open (file,'W')           forRoot,dirs,filesinchOs.walk (path): forNameinchFiles:line=Os.path.join (Root, name) (Stdin,stderr)= subprocess. Popen (['md5sum', line],stdout=subprocess. PIPE). Communicate () F.write (stdin) f.close ()defCheck (path): F= Open (file,'R')           forLineinchF:CHECK_OK="""echo '%s ' | md5sum-c >/dev/null 2>&1"""% Line#Print Check_ok            if  notSubprocess.call (CHECK_OK, Shell = True) = =0:abnormal=Line.split ()PrintAbnormal[1] F.close ()defUsage ():Print " "Usage:python%s update/home/wwwroot python%s check/home/wwwroot" "%(Sys.argv[0],sys.argv[0]) sys.exit ()ifLen (SYS.ARGV)! = 3: Usage () file='File.key'Model= Sys.argv[1] Path= Sys.argv[2]            ifos.path.exists (path) = =False:Print "\033[;31mthe directory or file does not exist\033[0m"sys.exit ()elifModel = ='Update': Update (PATH)elifModel = ='Check': Check (path)Else: Usage ()

Source: https://blog.linuxeye.com/376.html

Detecting web site hangs Ma Cheng (Python)

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.