• Python Memcached Script


    介绍

        利用 python 书写了 memcached 的启动等一类操作
        尽量的实现脚本的复用性,以及脚本的可扩展性,已达到一劳永逸的效果,
        并且添加了 memcached 监控搭建

    memcached 操作代码

    #!/usr/bin/env python
    # _*_coding:utf-8_*_
    # Author: "Edward.Liu"
    # Author-Email: lonnyliu@126.com
    
    
    import psutil
    import argparse
    import sys
    import subprocess
    
    
    # 获取 Memcached 程序 pid~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def process_id():
        pid = {}  # 存放获取到的进程 ID 服务端口
        for proc in psutil.process_iter():  # 迭代正在运行的运行的进程,进行排序
            try:
                pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline'])  # 获取返回字符串的指定字段
            except psutil.NoSuchProcess:
                pass
            else:
                if pinfo.get('name') == 'memcached':  # 通过字典获取指定的名称的进程
                    pid[pinfo.get('cmdline')[-1]] = pinfo.get('pid')  # 将端口号与相应的进程 ID 关联存放到 pid{}
        return pid
    
    
    # 选择停止单个或者全部端口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def select_stop(parameter):
        if bool(process_id()):  # 判断 pid 字典是否为空
            try:  # 判断输入的是否为数字
                int(parameter.port)
                if parameter.port in process_id().keys():
                    print "33[32m选择的端口为:%s" % parameter.port + "
    " + "33[32m该端口的 PID:%s33[0m" % process_id()[
                        parameter.port]
                    print "33[31mStoping Memcached %s33[0m" % parameter.port
                    process = psutil.Process(process_id()[parameter.port])  # 实例化该 PID 进程
                    process.terminate()  # 停止该 pid 所指进程
                    try:  # 判断停止是否成功
                        process_id()[parameter.port]
                    except KeyError:
                        print "33[32mStop Memcached %s Sucessful33[0m" % parameter.port + "!" * 20
                else:
                    print "33[31m输入的端口不存在33[0m" + "!" * 20
            except ValueError, err:
                if parameter.port == 'all':  # 当不输入数字是否于此相等
                    print "33[32mStop All Memcached Process33[0m" + "!" * 20
                    for key in process_id().keys():
                        process = psutil.Process(process_id()[key])  # 实例化该 PID 进程
                        process.terminate()
                        print "33[32mStop Memcached %s Sucessful33[0m" % key + "!" * 20  # 选择启动单个端口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                else:
                    print "33[Please Input Number Port33[0m" + "!" * 20
        else:
            print "33[31mNo Process Started33[0m"
    
    
    # 选择启动单个或者多个端口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def select_start(parameter):
        """
        启动方法简介:
         首先判断 -P 参数的类型
            try 是数字
                输入的参数存不存在已经启动的进程中
                    if 不在已启动中,进行启动
            except 不是数字
                是否 等于 'all'    #全部启动11211-11215
                    判断进程时候存在 如果存在 不进行启动 如果不存在 进行启动
        """
        # 获取参数
        port = parameter.port
        user = parameter.user
        memcached_home = "/software/memcached"  # 定义 Memcached_home目录
        try:
            int(port)
            if port in process_id().keys():
                print "33[32mThis Port Is Started33[0m"
            else:
                start_memcached = "%smemcached -c 512 -d -u %s -m 512 -p %s" % (memcached_home, user, port)
                code = subprocess.Popen(start_memcached, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                out, sherr = code.communicate()
                if code.returncode != 0:
                    print "33[31mStart Memcached Ok33[0m" + "!" * 20
                    print sherr
                else:
                    print "33[32mStart Memcached Ok Port:%s33[0m" % port + "!" * 20
        except ValueError, err:
            if port == 'all':
                if bool(process_id()):
                    print "33[32mAll Port Is Started33[0m"
                else:
                    print "33[32mStart Memcached All Port [11211-11215]33[0m"
                    port_list = ['11211', '11212', '11213', '11214', '11215']
                    for i in port_list:
                        start_memcached = "%smemcached -c 512 -d -u %s -m 512 -p %s" % (memcached_home, user, i)
                        code = subprocess.Popen(start_memcached, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                        out, err = code.communicate()
                        if code.returncode != 0:
                            print "33[31mStart Memcached Failed33[0m" + "!" * 20
                            print err
                        else:
                            print "33[32mStart Memcached %s Ok33[0m" % i + "!" * 20
            else:
                print "33[31mPlease Input Number EG: -P 12345633[0m" + "!" * 20
    
    
    # 获取单个进程或者多个进程状态~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def select_status(parameter):
        if str(parameter.port) in process_id().keys():
            print "33[32mMemcached的端口为:%s33[0m" % str(
                    parameter.port) + "
    " + "*" * 20 + "
    " + "33[32mMemcached端口的 PID:%s33[0m" % 
                                                               process_id()[str(parameter.port)]
        elif str(parameter.port) == 'all':
            for s in process_id().keys():
                print "-" * 40
                print "33[32mMemcached的端口为:%s33[0m" % s + "	" + "33[32mMemcached端口的 PID:%s33[0m" % process_id()[s]
        else:
            print "33[32mMemcached端口为%s 不存在33[0m" % str(parameter.port)
    
    
    def check_arg(self, args=None):
        parser = argparse.ArgumentParser(description='Script to learn basic argparse')
        parser.add_argument('-P', '--port', help="Memcached Port Or 'all' start[11211-11215]", required='True')
        parser.add_argument('-u', '--user', help='User Name', default='root')
        parser.add_argument('-t', '--action', help='Action start|stop|status', default='status')
    
        if len(sys.argv) <= 2:
            parser.print_help()
            sys.exit(1)
        return parser.parse_args(args)
    
    
    if __name__ == '__main__':
        args = check_arg(sys.argv[1:])  # 获取到去除脚本本身的所有参数
        if args.action == 'status':
            select_status(args)
        elif args.action == 'start':
            select_start(args)
        elif args.action == 'stop':
            select_stop(args)
    View Code

    memcached 监控

    1.安装 Apache PHP 环境
    2.解压phpMemcachedAdmin-1.2.2-r262.tar.gz 到 /var/www/html/memcached #安装包下载地址https://code.google.com/p/phpmemcacheadmin/
    3.更改Apache 配置文件
    /etc/httpd/conf.d/memcached.conf
    <VirtualHost *:80>
    ServerName 10.90.10.246 #服务器名称 或者 IP 地址
    UseCanonicalName Off
    ServerAdmin "10.90.10.246"
    DocumentRoot "/var/www/html/memcached" #Memcached 监控项目存放的位置 此处需要注意下权限
    CustomLog /var/log//httpd/10.90.10.246-access_log common
    ErrorLog /var/log/httpd/10.90.10.246-error_log
    </VirtualHost>
    4.重启 Apache 访问:http://10.90.10.246
    效果为:

    
    
  • 相关阅读:
    GSM Arena 魅族mx四核评测个人翻译
    Oracle Exists用法|转|
    NC公有协同的实现原理|同13的QQ||更新总部往来协同|
    NC客商bd_custbank不可修改账号、名称但可修改默认银行并更新分子公司trigger
    试玩了plsql中test窗口declare声明变量|lpad函数||plsql sql command test window区别|
    使用windows live writer测试
    用友写insert on bd_custbank 触发器和自动更新单位名称2in1
    oracle触发器select into和cursor用法的区别
    |转|oracle中prior的用法,connect by prior,树形目录
    客商增加自动增加银行账户|搞定!||更新使用游标course写法|
  • 原文地址:https://www.cnblogs.com/edwardlogs/p/5141426.html
Copyright © 2020-2023  润新知