Supervisor基于Python开发,用于对Unix-like系统进行监控、管理。
一、Supervisor安装步骤:
1、确认系统Python版本高于2.4:
[root@localhost ~]# python -V
Python 2.7.5
[root@localhost ~]# yum install python-meld3 #安装依赖程序
2、supervisor可以通过Python包管理工具pip进行安装:
[root@localhost ~]# pip install supervisor
Requirement already satisfied: supervisor in /usr/lib/python2.7/site-packages
Requirement already satisfied: meld3>=0.6.5 in /usr/lib64/python2.7/site-packages (from supervisor)
[root@localhost ~]# supervisord -v
3.3.3
3、supervisor程序相关程序、配置文件:
[root@localhost ~]# ll /usr/bin/supervisor*
-rwxr-xr-x. 1 root root 323 9月 6 09:34 /usr/bin/supervisorctl #客户端程序用于启动、停止、重启supervisor监控进程
-rwxr-xr-x. 1 root root 319 9月 6 09:34 /usr/bin/supervisord #服务端进行,用于监控配置文件中定义对需要监控对进程
supervisor安装成功后,默认不会生成配置文件,需要执行如下命令才会在指定路径下创建配置文件:
[root@localhost ~]# echo_supervisord_conf > /etc/supervisord.conf
[root@localhost ~]# ll /etc/supervisord.conf
-rw-r--r--. 1 root root 9205 9月 6 11:19 /etc/supervisord.conf
二、Supervisor配置文件配置项介绍:
[unix_http_server]
file=/var/run/supervisor.sock ; 进程sock文件路径,默认位于/tmp/下,建议修改到:/var/run/supervisor.sock
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
; supervisor网页版进程监控页面,可以查看进程启动状态以及对应日志
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; ip_address:port specifier, *:port for all iface
username=user ; default is no username (open server)
password=123 ; default is no password (open server)
[supervisord]
logfile=/var/log/supervisord.log ; 服务端程序supervisord程序日志文件,建议修改路径为/var/log/supervisord.log
logfile_maxbytes=50MB ; 日志文件大小,超过限制大小会开始轮替
logfile_backups=10 ; 保留日志文件的个数
loglevel=debug ; 日志级别,程序启动异常调试时可修改为debug
pidfile=/var/run/supervisord.pid ; 服务器端程序pid文件,建议修改路径:/var/run/supervisord.pid
nodaemon=false ; start in foreground if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
;user=chrism ; 运行进程的用户,注释时默认为root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false
; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work. Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; 指定客户端程序与服务器端程序通信的socket文件,服务器端修改后,客户端要对应修改路径
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample group section below shows all possible group values. Create one
; or more 'real' group: sections to create "heterogeneous" process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; 用于指定所管理进程的配置文件路径
注意:如果要包含外部进行配置文件,要记得把include前面的“ ; ”去掉!!!!
[include]
;files = relative/directory/*.ini
files = /etc/supervisor/*.ini ; 为了便于组织supervisor所管理的进程的配置文件,supervisor支持每个进程的配置文件存储在单独的文件中,服务器端程序运行时会自动识别并加载,该选项用于指定程序存储配置文件的目录路径和文件名
三、配置案例:
1、配置监控sshd服务,检测到进程down之后,自动启动:
[root@localhost supervisor]# pwd
/etc/supervisor
[root@localhost supervisor]# more sshd2288.ini
[program:sshd2288] ; 指定进程名,用于客户端程序supervisorctl根据进程名对程序进行管理,并不是ps命令执行后显示的的进程名
directory = /etc/ssh ; command命令执行之前需要切换到的路径
command = /usr/sbin/sshd -f sshd_config_2288 -D ; 指定程序启动对应的命令,注意,这里启动sshd服务时加了-D选项,是因为httpd,sshd等服务时,默认是以daemon后台程序运行,因为supervisor只能管理前台程序,故需要针对每个特定程序加对应的参数让其在前台运行
numprocs = 1 ; 指定运行的进程数量
autostart = true ; 是否自动启动
startsecs = 10 ; 指定尝试启动进程的时间间隔
autorestart = true ; 进程异常时,是否自动重启
startretries = 3 ; 重启进程的最大尝试次数
user = root ; 运行进行的用户
redirect_stderr = true ; 将标准错误输出重定向到标准输出,可以通过stdout_logfile指定标准输出的日志文件
stdout_logfile_maxbytes = 10MB ; 每个日志文件的大小
stdout_logfile_backups = 5 ; 程序保留的日志数量
stdout_logfile = /var/log/sshd2288_stdout.log ; 日志文件的保存位置
注意:sshd进程与httpd进程对应的非daemon模式运行参数:
-D When this option is specified, sshd will notdetach and does not become a daemon. This allows easy monitor‐ing of sshd.
2、配置监控open***服务,进程异常时自动重启:
[root@localhost supervisor]# more open***1194.ini [program:open***1194] directory = /etc/open*** command = /usr/sbin/open*** --config msgserver.conf --syslog open***0905 numprocs = 1 autostart = true startsecs = 10 autorestart = true startretries = 3 user = root redirect_stderr = true stdout_logfile_maxbytes = 10MB stdout_logfile_backups = 5 stdout_logfile =/var/log/supervisor/open***0905_stdout.log
下图,可以看到supervisord进程与所监控的进程之间的关系:
[root@localhost supervisor]# ps -ef | grep 31319 root 31319 1 0 13:16 ? 00:00:00 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisord.conf nobody 31678 31319 0 13:22 ? 00:00:00 /usr/sbin/open*** --config msgserver.conf --syslog open***0905 root 31679 31319 0 13:22 ? 00:00:00 /usr/sbin/sshd -f sshd_config_2288 -D
下图,可以看到客户端程序管理相关的命令:
[root@localhost supervisor]# supervisorctl -c /etc/supervisord.conf open***1194 RUNNING pid 31678, uptime 0:11:16 sshd2288 RUNNING pid 31679, uptime 0:11:16 supervisor> ? default commands (type help): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version supervisor>
四、systemd监控supervisord&supervisord监控ssh隧道进程:
1、systemd对应supervisord.service进程配置文件:
路径:/usr/lib/systemd/system/*.service [Unit] Description=Supervisor daemon [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown ExecReload=/usr/bin/supervisorctl $OPTIONS reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
2、supervisor对应监控sshd隧道的配置文件:
ssh隧道转发命令: command = /usr/bin/ssh -CNf -p 22 -L *:11111:ip:22 user@serverip 注意:使用supervisor监控时,不能加-f选项,否则会报如下错误: bind: Address already in use channel_setup_fwd_listener: cannot listen to port: 42580 Could not request local forwarding. [program:jksport] directory = /etc/ssh command = /usr/bin/ssh -CNf -p 22 -L *:11111:ip:22 user@serverip numprocs = 1 autostart = true startsecs = 1 autorestart = true startretries = 3 user = root redirect_stderr = true stdout_logfile_maxbytes = 10MB stdout_logfile_backups = 5 stdout_logfile = /dev/null
参考文章:
1、http://www.cnblogs.com/shanyou/p/5075744.html