A simple, code-updated script for Web site troubleshooting

Source: Internet
Author: User
Tags mysql host

Fault Debug Script

Recently, the company's servers often have a small problem, based on a variety of reasons to go wrong. Here I wrote a script in Python, the main four call OS module operation. One is to count the MySQL maximum number of connections. Because it seems like Python does not support "show processlist", so I wrote a module mysqlconn.py. Placed in the/python directory. The main function of this module is to perform "sh/python/mysqlconn.sh". This script. The script then generates a file called Mysqlconn.txt that is placed under:/python/mysqlconn.txt. The main statistic is the current number of connections. And the maximum number of connections allowed by default for MySQL.

The last thing to say is that my Code update directory is:/www/zhs/tool (test environment). Update previous to:/python directory, then use for to go to the statistics file. First back up to/tmp directory: The following is part of the content effect (the code is relatively simple, here do not explain the beginning of the login account is: Xiaoluo. Password is: 123456):

650) this.width=650; "style=" width:472px;height:66px; "title=" QQ picture 20141016173559.jpg "alt=" Wkiol1q_kdhaza_ Taabmom6kbrg023.jpg "src=" http://s3.51cto.com/wyfs02/M00/4C/8C/wKioL1Q_kdHAzA_TAABmOM6Kbrg023.jpg "width=" 290 " Height= "/>"

650) this.width=650; "title=" QQ picture 20141016173712.jpg "alt=" wkiol1q_khocmg7zaacxbeilu58349.jpg "src="/http S3.51cto.com/wyfs02/m02/4c/8c/wkiol1q_khocmg7zaacxbeilu58349.jpg "/>

Code Show: (Copy to use)

#!/usr/bin/env python
Import OS
Import Fileinput
Import Mysqlconn
While True:
Name=raw_input ("Please input name:"). Strip ()
If name! = "Xiaoluo":
Print "Try Agine:"
Continue
Password=raw_input ("Please input your password:"). Strip ()
If password! = "123456":
Print "Try Agine:"
Continue
Break

While True:
Print "1.webserver:[1]"
Print "2.mysqlserver:[2]"
Print "3.update code:[3]"

Choose=raw_input ("Your server is:")
If choose== "1":
User=os.getlogin ()
Print user
Userlogin=os.system ("W | grep 192|wc-l ")
Userlogin=raw_input (' Check your free io:[y/n] (60) ')
If userlogin== "Y":
Io=os.system ("Iostat|awk ' nr==4{print $NF} ' |cut-f 1-d. | Grep-v ^0$ ")
# Print IO
Else
Pass
Cpu=raw_input ("Check your free cpu:[y/n]")
If cpu== "Y":
Cpu_idle=os.system ("Top-b-N 1 | grep Cpu | awk ' {print $} ' | Cut-f 1-d. ")
Else
Pass
Swap=raw_input ("Check your free_swap:[y/n]")
If swap== "Y":
Free_swap=os.system ("/usr/bin/free-m|grep Mem|awk ' {print $4} '")
Else
Pass
Web_load=raw_input ("Check your load:[y/n] (18)")
If web_load== "Y":
Web_load=os.system ("Uptime|awk ' {print $NF} ' |cut-f 1-d.")
Else
Pass
User_disk=raw_input ("Check your uses disk:[y/n]")
If user_disk== "Y":
User_load=os.system ("Df-h|sort-k5nr|grep-ve ' filesystem|tmpfs|cdrom ' |awk ' {print $" "$}")

Else
Pass

Tcp_connection=raw_input ("Check your TCP connection:[y/n]")
If tcp_connection== "Y":
Tcp_connection=os.system ("Netstat-nat |grep 80|awk ' {print $6} ' |sort|uniq-c|sort-rn")
Else
Pass
Top_ip=raw_input ("Check yout top IP connection:[y/n]")
If top_ip== "Y":
Top_ip=os.system ("Netstat-anlp|grep 80|grep Tcp|awk ' {print $} ' |awk-f: ' {print '} ' |sort|uniq-c|sort-nr|head-n20 ')
Else
Pass
If choose== "2":
Mysqluser=raw_input ("Please input MySQL User:"). Strip ()
If Len (mysqluser) ==0:
Print "Empty mysqluser,try again"
Continue
Else
Pass
Mysqlpassword=raw_input ("Please input mysql password:"). Strip ()
If Len (Mysqlpassword) ==0:
Print "Empty mysqluser,try again"
Continue
Else
Pass
Host=raw_input ("Please input MySQL host:"). Strip ()
If Len (host) ==0:
Print "Empty mysqluser,try again"
Continue
Else
Pass
User=os.getlogin ()
Print user
Userlogin=os.system ("W | grep 192|wc-l ")
Userlogin=raw_input (' Check your io:[y/n] ')
If userlogin== "Y":
Io=os.system ("Iostat|awk ' nr==4{print $NF} ' |cut-f 1-d. | Grep-v ^0$ ")
# Print IO
Else
Pass
Cpu=raw_input ("Check your free cpu:[y/n]")
If cpu== "Y":
Cpu_idle=os.system ("Top-b-N 1 | grep Cpu | awk ' {print $} ' | Cut-f 1-d. ")
Else
Pass
Swap=raw_input ("Check your free_swap:[y/n]")
If swap== "Y":
Free_swap=os.system ("/usr/bin/free-m|grep Mem|awk ' {print $4} '")
Else
Pass
Web_load=raw_input ("Check your load:[y/n]")
If web_load== "Y":
Web_load=os.system ("Uptime|awk ' {print $NF} ' |cut-f 1-d.")
Else
Pass
User_disk=raw_input ("Check your uses disk:[y/n]")
If user_disk== "Y":
User_load=os.system ("Df-h|sort-k5nr|grep-ve ' filesystem|tmpfs|cdrom ' |awk ' {print $" "$}")

Else
Pass
Mysql_prolist=raw_input ("Check your mysql curren connection:[y/n]")
If mysql_prolist== "Y":
F=open ("/python/mysqlconn.txt")
U=f.readline ()
Print U
Mysql_max_prolist=raw_input ("Check your mysql max_connection:[y/n]")
If mysql_max_prolist== "Y":
F1=open ("/python/mysqlconn.txt")
U1=f.readlines ()
For line in U1:
Print Line
If choose== "3":
Name=raw_input ("Please input your The Well update package: (. tar)"). Strip ()
If Len (name) ==0:
Print "Empty mysqluser,try again"
Continue
Else
Pass
# Os.system ("cd/python/%s"% (name))
W=os.listdir ('/python/%s '% (name))
Print W
# for Line in W:
Backup=os.system ("mkdir/tmp/' date +%y%m%d '")
For line in W:
Os.system ("Cp-r/www/zhs/%s/%s/tmp/%s"% (name,line,backup))
Os.system ("cp-rf/python/%s/*/www/zhs/%s/"% (name,name))

Second, mysqlconn.py module code:

#!/usr/bin/env python
#xiaoluo
Import OS
Os.system ("sh/python/mysqlconn.sh")

Third, mysqlconn.sh scripts are used to gather information:

#!/bin/bash
#xiaoluo
#18878774260
Mysql-uroot-p123456-h 192.168.38.131-e "Show processlist" |grep-v localhost|wc-l >/python/mysqlconn.txt
Mysql-uroot-p123456-h 192.168.38.131-e "Show variables like ' max_connections '" |grep Max | awk ' {print$2} ' >>/python/mysqlconn.txt

This article is from the "Little Luo" blog, please be sure to keep this source http://xiaoluoge.blog.51cto.com/9141967/1564892

A simple, code-updated script for Web site troubleshooting

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.