Python: View scripts for multiple server processes

Source: Internet
Author: User

Recently I developed a checklist for related services and wrote this script to check whether the related services on each server are normal on the stepping stone.

Log on to each machine using reverse CT (because of security issues, you cannot directly use ssh Trust), and then read the service name and number of started processes according to the yaml file configuration to check whether each service is normal PS: the difficulty is that port forwarding is not used and only common user permissions are required.

Checklist. py
Copy codeThe Code is as follows:
# Coding = UTF-8
Import sys
# Because this script is intended to run by many people, but cannot be seen by them, it is pyc.
# This script should be used by many other common users. It is used for ssh Login and cannot be placed in my home directory, so it is placed in tmp
Sys. path. append ('/tmp/local/lib/python2.6/site-packages/PyYAML-3.10-py2.6-linux-x86_64.egg') # dependent yaml
Sys. path. append ('/tmp/local/lib/python2.6/site-packages/pexpect-2.4-py2.6.egg') # dependent on pexpect
Import yaml
Import pexpect
DataDict = yaml. load (open ('/tmp/config. yaml') # load my yaml Configuration

Def myprint (color, mes): # A function written previously for terminal color printing
'''Use the ANSI control code Terminal to display the 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 \ x1B [0 m" % (color, mes)

Def main ():
List = ['G', 'B', 'y', 'gb', 'P']
Light = 0
For k in dataDict:
If k. startswith ('bj -'):
Color = list [light % 5] # color rotation based on the server
SERVER = dataDict [k]
# I used-F because I do not have the root permission to modify the hosts file, but I used an alias in config. yaml,
The sshconfig is defined. The default value is ~ /. Ssh/config
Child = pexpect. spawn ('ssh-F/tmp/sshconfig dongwm @ {0} '. format (SERVER ['host'])
# Because there are other users, maybe they haven't linked to a server. At first, you will be asked to confirm the server ID. You need to click yes
F = child. expect (['password: ', 'password:', 'continue connecting (yes/no )? '])
If f = 2:
# When the flag is 2, it indicates that the user has not logged on to a server.
Child. sendline ('yes ')
Child. Verify CT ('password :')
Child. sendline ('{0}'. format (mypasswd (SERVER ['host']) # mypasswd is a function used to encrypt my SERVER permissions. Each SERVER has a different password.
If f = 1:
Child. sendline ('{0}'. format (mypasswd (SERVER ['host'])
Child. Secondary CT ('~ ')
For service in SERVER ['service']:
Flag = 0
# I add a service in the configuration. Generally, the number of processes of the service is specified to compare whether the service is normal.
If isinstance (service, dict ):
Data = service. items () [0]
Service = data [0]
Num = data [1]
Else:
# If I only specify the service in the configuration, but do not specify the number of processes, I only need to determine that the number of processes that run the process does not matter.
Num = 0
Flag = 1
Child. Secondary CT ('~ ')
Child. sendline ('ps-ef | grep {0} | grep-v grep | wc-l'. format (
Service ))
Child. readline ()
# Process count
Pro_num = child. readline (). split ('\ r \ n') [0]
If int (pro_num) = num or flag:
# The number of processes conforms to the value specified in the configuration.
Myprint (color, '[{0}] [{1}] [{2}] [{3}]'. format (k. center (12 ),
SERVER ['IP']. center (14), service. center (20), 'OK'. center (4 )))
Else:
Myprint ('R', '[{0}] [{1}] [{2}] [{3}] [{4 }! = {5}] '. format (k. center (12 ),
SERVER ['IP']. center (14), service. center (20), 'failed ',
Pro_num, num ))
Light + = 1
Child. sendline ('exit ')

If _ name _ = '_ main __':
Main ()

Config. yaml
Copy codeThe Code is as follows:
Bj-2:
Host: s233 # This s233 is specified in sshconfig
Ip: XXX. XXX. XXX.233 # Just to show the ip address and confirm it
Service: # after the service load is a list
# For XX
-Nginx: 5
-Uwsgi: 25
-Supervisord: 1
# Provide mysql services to the local machine XX
-Mysql: 3 # django
# Provide XX to the local machine XX
-Celery: 12
# Provide XX to the local machine XX
-Rabbitmq: 9
-Redis: 1
-Mongod: 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.