Recently to do their own development with the relevant services of a checklist, wrote this script, used in the springboard to check the server above the relevant services are normal
Use expect to log on to each machine (because of security issues, you cannot use the SSH trust directly), and then read the service name and the number of processes initiated according to the Yaml file's configuration to check that each service is normal PS: The difficulty is not using port forwarding and only normal user rights
checklist.py
Copy Code code as follows:
#coding =utf-8
Import Sys
#因为我这个脚本要让很多人能运行, but can't show them my cipher algorithm, so it's pyc.
#我这个脚本要给很多其他普通用户去用, is to use my SSH login operation, cannot be placed in my home directory, so put in TMP
Sys.path.append ('/tmp/local/lib/python2.6/site-packages/pyyaml-3.10-py2.6-linux-x86_64.egg ') #依赖yaml
Sys.path.append ('/tmp/local/lib/python2.6/site-packages/pexpect-2.4-py2.6.egg ') #依赖pexpect
Import Yaml
Import Pexpect
Datadict = yaml.load (open ('/tmp/config.yaml ')) #将我的yaml配置load进来
def myprint (color,mes): #以前写的一个终端彩色打印的函数
"Use ANSI control code terminal to display Color" "
D = dict (r=31, g=32, gb=36, y=33, b=34, p=35, o=37)
color = "\X1B[%D;%DM"% (1, D[color])
Print "%s%s\x1b[0m"% (color, MES)
def main ():
list = [' G ', ' B ', ' Y ', ' GB ', ' P ']
Light = 0
For K in Datadict:
If K.startswith (' bj-'):
color = list[light%5] #根据服务器对颜色轮循
SERVER = Datadict[k]
#我这是使用了-F is because I don't have root permissions to modify the Hosts file, but I used aliases in Config.yaml,
And this definition is a custom sshconfig, the default is ~/.ssh/config
Child = Pexpect.spawn (' ssh-f/tmp/sshconfig dongwm@{0} '. Format (server[' host '))
#因为有其他用户, perhaps he has not linked to a server, the beginning will let you confirm the server identification, need to point Yes
f = child.expect ([' Password: ', ' Password: ', ' Continue connecting (yes/no)? '])
If f = 2:
#当这个flag为2 means that the user has not logged on to a server
Child.sendline (' yes ')
Child.expect (' Password: ')
Child.sendline (' {0} '. Format (MYPASSWD (server[' host ')) #mypasswd是加密我服务器权限的函数, each server password is different
If f = 1:
Child.sendline (' {0} '. Format (MYPASSWD (server[' host '))
Child.expect (' ~ ')
For service in server[' service ']:
Flag = 0
#我在配置里面会加服务, you typically specify the number of processes in the service to compare the normal
If Isinstance (service, Dict):
Data =service.items () [0]
Service = Data[0]
num = data[1]
Else
#假如我在配置只指定服务, do not specify the number of processes, so just make sure that the running process does not care about the number of processes
num = 0
Flag = 1
Child.expect (' ~ ')
Child.sendline (' ps-ef|grep {0}|grep-v grep|wc-l '. Format (
Service))
Child.readline ()
#进程数
Pro_num = Child.readline (). Split (' \ r \ n ') [0]
if int (pro_num) = = num or flag:
#进程数符合配置标注的数值
Myprint (color, ' [{0}] [{1}] [{2}] [{3}] '. Format (K.center (12),
server[' IP '].center, service.center, ' OK '. Center (4))
Else
Myprint (' R ', ' [{0}] [{1}] [{2}] [{3}] [{4}!={5}] '. Format (K.center (12),
server[' IP '].center, service.center, ' fail ',
Pro_num, num))
Light + 1
Child.sendline (' exit ')
if __name__ = = ' __main__ ':
Main ()
Config.yaml I only intercepted one of these.
Copy Code code as follows:
Bj-2:
host:s233 #这个s233在sshconfig指定
Ip:XXX.XXX. xxx.233 #只是为了显示出ip Good confirmation.
Service: #服务load后是一个列表
#给XX用
-Nginx:5
-Uwsgi:25
-Supervisord:1
#给本机XX提供mysql服务
-Mysql:3 #django
#给本机XX提供XX
-Celery:12
#给本机XX提供XX
-Rabbitmq:9
-Redis:1
-Mongod:2