码迷,mamicode.com
首页 > 其他好文 > 详细

云平台服务运行情况检测脚本V0.1

时间:2017-08-30 16:32:22      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:https   __name__   stdout   too   strftime   color   wget   int   建议   

1、准备Python3环境

    

yum groupinstall "Development tools" -y
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y
cd /usr/local/software && wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar xf Python-3.6.2.tgz
cd Python-3.6.2
./configure -prefix=/usr/local/python -enable-shared
make && make install
ln -s /usr/local/python/bin/python3 /usr/bin/python3

3、在运行Python之前需要配置库:

echo /usr/local/python/lib >> /etc/ld.so.conf.d/local.conf && ldconfig

4、运行演示:
python3 -V
Python 3.6.2

5、升级一下pip
pip3 install --upgrade pip

 

2、脚本内容 /usr/local/checkall.py

执行方式:

cd /usr/local/

python3 checkall.py

import subprocess as t
import time
import os, sys
import socket
import requests
import redis

# ===============================================================================================================
# pip3 install requests
# pip3 install redis
# ===============================================================================================================
# 地区名称
Area_Name = 199测试服务器

# 是否需要检查Tomcat2
NeedCheckTomcat2 = False

# 数据库所在的IP
DB_IP = 127.0.0.1

# WEB所在的IP
WEB_IP = 127.0.0.1

RedisPort = 6379
NeedCheckPort = {MYSQL服务: 22066, REDIS服务: RedisPort, SSDB服务: 8888, NGINX服务: 80, ICOMET服务: 8000,
                 RABBITMQ服务: 4369}

SphinxList = [/usr/local/sphinx/etc/csft.conf,
              /usr/local/sphinx/etc/csft_member.conf,
              /usr/local/sphinx/etc/csft_paper.conf,
              /usr/local/sphinx/etc/csft_tk.conf,
              /usr/local/sphinx/etc/csft_tk_my.conf,
              /usr/local/sphinx/etc/csft_group.conf,
              /usr/local/sphinx/etc/csft_my.conf,
              /usr/local/sphinx/etc/csft_paper_my.conf,
              /usr/local/sphinx/etc/csft_cloud.conf,
              /usr/local/sphinx/etc/csft_wkds.conf,
              /usr/local/sphinx/etc/csft_publish.conf,
              /usr/local/sphinx/etc/csft_publish_resource.conf,
              /usr/local/sphinx/etc/csft_publish_paper.conf,
              /usr/local/sphinx/etc/csft_publish_wk.conf,
              /usr/local/sphinx/etc/csft_zy.conf,
              /usr/local/sphinx/etc/csft_news.conf,
              /usr/local/sphinx/etc/csft_kyyj.conf,
              /usr/local/sphinx/etc/csft_xzt.conf,
              /usr/local/sphinx/etc/csft_hd.conf,
              /usr/local/sphinx/etc/csft_allpublish.conf,
              /usr/local/sphinx/etc/csft_wktostudent.conf,
              /usr/local/sphinx/etc/csft_bbs_topic.conf,
              /usr/local/sphinx/etc/csft_bbs_post.conf,
              /usr/local/sphinx/etc/csft_notice.conf,
              /usr/local/sphinx/etc/csft_blog_article.conf,
              /usr/local/sphinx/etc/csft_supervise.conf,
              /usr/local/sphinx/etc/csft_seach.conf
              ]


# ===============================================================================================================
# 检查指定IP的服务端口
def check_port(address, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(3)
    try:
        s.connect((address, port))
        s.close()
        return True
    except Exception as e:
        s.close()
        return False


# 检查磁盘
def check_disk():
    # 检查是不是存在指定的MOUNT盘
    var = t.getoutput("df -h | cat -n ")
    success = False
    for line in var.split("\n"):
        if /usr/local/tomcat7/webapps/dsideal_yy/html/down in line:
            success = True
    if not success:
        return False
    # 尝试写入文件,是不是正常
    try:
        testFile = /usr/local/tomcat7/webapps/dsideal_yy/html/down/test.txt
        with open(testFile, w) as data:
            print(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime(time.time())), file=data)  # 输出默认为sys.stdout
            success = True
        os.remove(testFile)
    except IOError as err:
        success = False
    return success


# 检查服务端口
def check_serviceport():
    for (k, v) in NeedCheckPort.items():
        success = check_port(DB_IP, NeedCheckPort[k])
        if not success:
            return k


# 检查 Tomcat
def check_Tomcat():
    var = t.getoutput("ps -ef | grep tomcat")
    success = False
    for line in var.split("\n"):
        if /usr/local/tomcat7/conf/logging.properties in line:
            success = True

    if not success:
        return False

    # 判断是不是 Tomcat僵死掉
    try:
        requests.get("http://" + WEB_IP + "/dsideal_yy/adminlogin/captchaText", timeout=1)
    except requests.exceptions.ConnectTimeout:
        return False
    except requests.exceptions.Timeout:
        return False

    # 判断验证码是不是能正常生成?
    r = requests.get("http://" + WEB_IP + "/dsideal_yy/adminlogin/captchaText")
    if r.status_code != 200:
        return False
    else:
        # 下载图片
        try:
            r = requests.get("http://" + WEB_IP + "/dsideal_yy/adminlogin/captchaImage?text=" + r.json()[text])
            with open(/usr/local/software/picture.jpg, wb) as file:
                file.write(r.content)
            # 用完就删除了吧
            os.remove(/usr/local/software/picture.jpg)
            return True
        except Exception as err:
            print(Tomcat检查发现异常: + str(err))
            return False


# 判断 Tomcat2
def check_Tomcat2():
    var = t.getoutput("ps -ef | grep tomcat")
    success = False
    for line in var.split("\n"):
        if /usr/local/tomcat7_2/conf/logging.properties in line:
            success = True
    return success


# 检查是否存在检查挂盘定时任务
def check_disk_crontab():
    file = open(/etc/crontab)
    success = False
    for line in file:
        # 每分钟进行检查
        if * * * * * root  /usr/local/checkdisk.sh > /dev/null 2>&1 in line:
            success = True
    file.close()
    return success


# 检查磁盘的定时脚本是不是与启动脚本中的挂载方式一致
def check_diskShAndRcLocal():
    typeA = ‘‘
    file = open(/usr/local/checkdisk.sh)
    for line in file:
        if sshfs -o nonempty,allow_other,reconnect in line:
            typeA = ssh
        if mount -t cifs -o in line:
            typeA = samba
    file.close()

    typeB = ‘‘
    file = open(/etc/rc.d/rc.local)
    for line in file:
        if sshfs -o nonempty,allow_other,reconnect in line:
            typeB = ssh
        if mount -t cifs -o in line:
            typeB = samba
    file.close()

    if typeA == ssh:
        print("########################################################################################")
        print(########发现现在在使用sshfs方式挂盘,如果可能 ,还是建议使用samba的方式进行挂盘!########)
        print("########################################################################################")
    if typeA == typeB and typeA != ‘‘:
        return True
    else:
        return False


# 检查处理程序的状态
def checkWinFormExist():
    try:
        r = redis.Redis(host=DB_IP, port=RedisPort, db=0)
        # r.set(WinForm_GetSwf,11111)
        ret = r.get(WinForm_GetSwf)
        if ret is None:
            return False
        else:
            return True
    except Exception as err:
        print(Redis访问异常: + str(err))
        return False


# 检查sphinx服务状态
def checkSphinx():
    for c in SphinxList:
        var = t.getoutput(ps -ef | grep  + c +  | grep -v grep)
        n = 0
        for line in var.split("\n"):
            n = n + 1

        if n == 0:
            print(Sphinx检查失败: + c)
            return c


# 检查sphinx 定时增量索引的配置是否正确
def checkSphinxInc():
    SphinxShList = []
    for c in SphinxList:
        a = c.replace(.conf, .sh).replace(/usr/local/sphinx/etc/, /usr/local/sphinx_sh/)
        SphinxShList.append(a)

    for c in SphinxShList:
        Found = False
        with open(/etc/crontab, r) as f:
            for line in f:
                if c in line:
                    Found = True
                    break
                else:
                    Found = False
            if not Found:
                return c


# 检查sphinx 在自启动脚本中是不是存在
def checkSphinxRcLocal():
    for c in SphinxList:
        with open(/etc/rc.d/rc.local, r) as f:
            Found = False
            for line in f:
                if c in line:
                    Found = True
                    break
                else:
                    Found = False
            if not Found:
                return c


if __name__ == "__main__":
    # 检查磁盘
    if not check_disk():
        print(1、磁盘检查失败,请马上处理!)
        sys.exit()
    else:
        print(1、磁盘检查成功!)

    # 检查Tomcat
    if not check_Tomcat():
        print(2、Tomcat检查失败,请马上处理!)
        sys.exit()
    else:
        print(2、Tomcat检查成功!)

    # 检查Tomcat2,各地区不一样,有的需要进行检查
    if NeedCheckTomcat2:
        if not check_Tomcat2():
            print(3、Tomcat_2检查失败,请马上处理!)
            sys.exit()
        else:
            print(3、Tomcat_2检查成功!)

    # 检查端口
    ret = check_serviceport()
    if ret != None:
        print(4、 + ret + 端口检查失败,请马上处理!)
        sys.exit()
    else:
        print(4、端口检查成功!)

    # 检查定时磁盘检查
    if not check_disk_crontab():
        print(5、磁盘定时探测服务检查失败,请马上处理!)
        sys.exit()
    else:
        print(5、磁盘定时探测服务检查成功!)

    # 检查磁盘探测程序是不是与启动脚本中的挂载磁盘方式一致?
    if not check_diskShAndRcLocal():
        print(6、磁盘挂载方式不一致,请马上处理!)
        sys.exit()
    else:
        print(6、磁盘挂载方式一致性检查成功!)

    # 检查处理程序的状态
    if not checkWinFormExist():
        print(7、处理程序没有正确开启,请马上处理!)
        sys.exit()
    else:
        print(7、处理程序检查成功!)

    # 检查Sphinx服务状态
    ret = checkSphinx()
    if ret != None:
        print(8、检查Sphinx失败, + ret + ,请马上处理!)
        sys.exit()
    else:
        print(8、检查Sphinx成功完成!)

    # 检查Sphinx增量脚本的编写
    ret = checkSphinxInc()
    if ret != None:
        print(9、检查Sphinx增量索引失败, + ret + ,请马上处理!)
        sys.exit()
    else:
        print(9、检查Sphinx增量索引完成!)

        # 检查Sphinx自启动脚本的编写
        ret = checkSphinxRcLocal()
        if ret != None:
            print(10、检查Sphinx自启动脚本失败, + ret + ,请马上处理!文件位置:/etc/crontab)
            sys.exit()
        else:
            print(10、检查Sphinx自启动脚本完成!)

    print(恭喜,所有检查成功完成!没有发现异常!)

 

云平台服务运行情况检测脚本V0.1

标签:https   __name__   stdout   too   strftime   color   wget   int   建议   

原文地址:http://www.cnblogs.com/littlehb/p/7453599.html

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