码迷,mamicode.com
首页 > Web开发 > 详细

一个网站故障排查的、代码更新的简便脚本

时间:2014-10-16 21:32:44      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:服务器   网站   统计   python   

故障排错脚本

由于近来公司的服务器经常会出点小问题,基于各种原因要去排错。这里我用python写了一个脚本,主要四调用os模块操作。有一个是要统计mysql最大连接数的。因为好像python不支持"show processlist",所以自己写了一个模块mysqlconn.py。放在/python目录下。这个模块的作用主要是执行“sh /python/mysqlconn.sh”.这个脚本。然后脚本会生成一个叫mysqlconn.txt文件放到:/python/mysqlconn.txt下面。主要是统计当前连接数。和mysql默认允许的最大连接数。

最后一个要说的就是,我的代码更新目录是:/www/zhs/tool(测试环境)。更新之前上次到:/python目录,然后用for去统计文件。先备份到/tmp的目录下:下面是部分内容效果(代码比较简单,这里不做说明开始部分的登录账号是:xiaoluo.密码是:123456):

bubuko.com,布布扣

bubuko.com,布布扣

代码展示:(复制即可使用)

#!/usr/bin/env python
import os
import fileinput
import mysqlconn
while True:
        name=raw_input("please input name:").strip()
        if name !="xiaoluo":
                        print "please try agine:"
                        continue
        password=raw_input("please input your password:").strip()
        if password !="123456":
                        print "please 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 $5}‘ | 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 $5 " " $1}‘")

                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 $5}‘|awk -F: ‘{print $1}‘|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 $5}‘ | 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 $5 " " $1}‘")

                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 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))

第二、mysqlconn.py模块代码:

#!/usr/bin/env python
#xiaoluo
import os
os.system("sh /python/mysqlconn.sh")

第三、mysqlconn.sh脚本用来收集信息:

#!/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

本文出自 “小罗” 博客,请务必保留此出处http://xiaoluoge.blog.51cto.com/9141967/1564892

一个网站故障排查的、代码更新的简便脚本

标签:服务器   网站   统计   python   

原文地址:http://xiaoluoge.blog.51cto.com/9141967/1564892

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!