设为首页 收藏本站
查看: 830|回复: 0

[经验分享] 检查一台服务器上多个区服的redis配置脚本

[复制链接]

尚未签到

发表于 2018-11-7 07:36:48 | 显示全部楼层 |阅读模式
  一 应用场景描述
  应开发同事需求,需要对各个游戏区服的Redis库中查看bi这个key的长度,如果长度大于100,就发送报警通知。这款游戏采用的是一台服务器上部署多个区服的模式,每个区服有一个配置文件app.conf.php,每个配置文件中关于redis的配置信息如下:
  'normal' => array(
  'host' => '127.0.0.1',
  'port' => 6402,
  'db' => 8
  ),
  现在需要定期去从各个区服配置中去获取redis的信息,然后连接redis,查看各个库中bi key的长度
  如:redis-cli -p 6402 -n 8  llen bi
  最初是打算采用shell来写的,但是无奈一时没有调试出来。就请一哥们帮忙解决怎么解决,他用python写了程序,但是我对python不是很懂,于是就边写边学,最后写成了如下的脚本。
  二 解决思路
  先查找出所有的redis配置信息
  find /data/zhanguo_app/ -name app.conf.php -exec sed -n '/normal/,+4p' {} \;
  显示如下类似信息
  'normal' => array(
  'host' => '127.0.0.1',
  'port' => 6402,
  'db' => 7
  ),
  'normal' => array(
  'host' => '127.0.0.1',
  'port' => 6379,
  'db' => 10
  ),
  'normal' => array(
  'host' => '127.0.0.1',
  'port' => 6402,
  'db' => 2
  ),
  'normal' => array(
  'host' => '127.0.0.1',
  'port' => 6379,
  'db' => 9
  ),
  然后需要对显示的结果进行处理循环去读取host , port ,db的值,然后根据这三个值去连接redis。这里就需要用到python处理字符串的一些知识。在以下的脚本中对字符串的处理过程如下:
  使用python字符串的replace()将以上的字符形式变成如下的形式
  normal=>arrayhost=>127.0.0.1,port=>6379,db=>11normal=>arrayhost=>127.0.0.1,port=>6402,db=>1normal=>arrayhost=>127.0.0.1,port=>6379,db=>14normal=>arrayhost=>127.0.0.1,port=>6379,db=>1normal=>arrayhost=>127.0.0.1,port=>6405,db=>6normal=>arrayhost=>127.0.0.1,port=>6405,db=>
  然后在使用python字符串的split()将字符串变成如下的形式
  ['', 'host=>127.0.0.1,port=>6379,db=>11', 'host=>127.0.0.1,port=>6402,db=>1', 'host=>127.0.0.1,port=>6379,db=>14', 'host=>127.0.0.1,port=>6379,db=>1', 'host=>127.0.0.1,port=>6405,db=>6', 'host=>127.0.0.1,port=>6405,db=>1', 'host=>127.0.0.1,port=>6379,db=>8', 'host=>127.0.0.1,port=>6402,db=>8']
  然后循环读取以上列表中的元素如 host=>127.0.0.1,port=>6379,db=>11 通过split()变成如下的形式
  ['host=>127.0.0.1', 'port=>6379', 'db=>11']
  然后将以上列表变成
  {'host': '127.0.0.1', 'db': '11', 'port': '6379'}
  然后变成
  [{'host': '127.0.0.1', 'db': '11', 'port': '6379'}, {'host': '127.0.0.1', 'db': '1', 'port': '6402'}, {'host': '127.0.0.1', 'db': '14', 'port': '6379'}, {'host': '127.0.0.1', 'db': '1', 'port': '6379'}, {'host': '127.0.0.1', 'db': '6', 'port': '6405'}, {'host': '127.0.0.1', 'db': '1', 'port': '6405'}, {'host': '127.0.0.1', 'db': '8', 'port': '6379'}]
  三 Python脚本
#/usr/bin/python  

  
import os
  
import subprocess
  
import redis
  
import smtplib
  
from email.MIMEMultipart import MIMEMultipart
  
from email.MIMEText import MIMEText
  
import socket
  

  
#get the localhost name
  
hostname=socket.gethostname()
  

  
mail_server='smtp.exmail.qq.com'
  
mail_server_port=25
  
mail_user='xxxx'
  
mail_password='xxxx'
  

  
from_addr='xxxx'
  
to_addr=['xxxx','xxxxx','xxxxx']
  

  
#define send_email function for reuse
  
def send_email(sub,content):
  
    m=MIMEMultipart()
  
    m["To"]=";".join(to_addr)
  
    m["From"]=from_addr
  
    m["Subject"]=sub
  
    m.attach(MIMEText(content))
  

  
    s=smtplib.SMTP(mail_server,mail_server_port)
  
    s.sendmail(from_addr,to_addr,m.as_string())
  
    s.quit()
  

  
#get redis configuration
  
args="find /data/zhanguo_app/ -name app.conf.php  -exec sed -n '/normal/,+4p' {} \;"
  

  
redis_content=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]
  

  
#check if redis_content file exists
  
if os.path.isfile("redis_content.txt"):
  
   print "redis_content.txt file exists"
  
else:
  
   print "redis_content.txt not exists"
  
#write content into file
  
redis_array_file=file('redis_content.txt','w')
  
redis_array_file.write(redis_content)
  
redis_array_file.close
  

  
redis_array_file=file('redis_content.txt','r')
  
content=redis_array_file.read()
  
redis_array_file.close
  

  
#hanle string
  

  
redis_string_list=redis_string.split("normal=>array")
  

  
redis_config_dic_list=[]
  
for i in redis_string_list:
  
    redis_config_list=i.split(",")
  
    if len(redis_config_list)==3:
  
       redis_config_dic={}
  
       for j in redis_config_list:
  
           redis_config=j.split("=>")
  
           if len(redis_config)==2:
  
              redis_config_dic[redis_config[0]]=redis_config[1]
  
       redis_config_dic_list.append(redis_config_dic)
  

  
#connect to redis and check bi key's length
  
for k in redis_config_dic_list:
  
    redis_host=k['host']
  
    redis_port=k['port']
  
    redis_db=k['db']
  
    try:
  
       r=redis.StrictRedis(host=redis_host, port=redis_port, db=redis_db,socket_timeout=1)
  
       bi_length=r.llen('bi')
  
       if bi_length > 100:
  
          warning_subject="BI KEY WARING ON "+hostname
  
          warning_message=redis_host + ':' + redis_port + ' ' + redis_db +  " The bi key's length is greater than 100"
  
          send_email(warning_subject,warning_message)
  
    except redis.exceptions.ConnectionError:
  
          connect_subject="Connect to redis server on "+hostname+" error"
  
          connect_error="Connect to redis server " + redis_host + ":" +  redis_port  +  "  error"
  
          send_email(connect_subject,connect_error)
  以上脚本中使用socket模块获取本机的主机名,使用smtplib模块发送邮件。使用redis模块操作redis数据库,使用try ... except 作异常处理,使用subprocess模块调用shell命令



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631691-1-1.html 上篇帖子: Redis Sentinel 的配置 下篇帖子: 构建高性能数据库缓存之redis(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表