Recently to do their own development with related services of a checklist, wrote this script, used in the springboard to check the server above the relevant services are normal
Log on to each machine using expect (because of security issues, you cannot use SSH trust directly), and then read the service name and the number of processes initiated according to the configuration of the Yaml file to check if each service is normal PS: the difficulty is that there is no port forwarding and only normal user permissions
checklist.py
The code is as follows:
#coding =utf-8
Import Sys
#因为我这个脚本要让很多人能运行, but they can't see my cipher algorithm, so it's pyc.
#我这个脚本要给很多其他普通用户去用, is using 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 do not have root permissions to modify the Hosts file, but I used aliases in Config.yaml.
And this definition is custom sshconfig, the default is ~/.ssh/config
Child = Pexpect.spawn (' ssh-f/tmp/sshconfig dongwm@{0} '. Format (server[' host '))
#因为有其他用户, maybe he doesn't have a link to a server, and at the very beginning you'll be asked to confirm the server identity and need a yes.
f = child.expect ([' Password: ', ' Password: ', ' Continue connecting (yes/no)? ')
if f = = 2:
#当这个flag为2 indicates 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 has a different password
if f = = 1:
Child.sendline (' {0} '. Format (MYPASSWD (server[' host ')))
Child.expect (' ~ ')
For service in server[' service ']:
Flag = 0
#我在配置里面会加服务, it is common to specify the number of processes for the service to compare to 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 running the 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've only intercepted one of them here.
The code is 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