Supervisor安裝及進程監控配置介紹:

Supervisor基於Python開發,用於對Unix-like系統進行監控、管理。html

1、Supervisor安裝步驟:python

1、確認系統Python版本高於2.4web

[root@localhost ~]# python -V
Python 2.7.5
[root@localhost ~]# yum install python-meld3  #
安裝依賴程序服務器

2supervisor能夠經過Python包管理工具pip進行安裝:python2.7

[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
ssh

3supervisor程序相關程序、配置文件:socket

[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.confide

2、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)

;
用於指定所管理進程的配置文件路徑ui

注意:若是要包含外部進行配置文件,要記得把include前面的「 ; 」去掉!!!!
[include]
;files = relative/directory/*.ini
files = /etc/supervisor/*.ini  ; 爲了便於組織supervisor所管理的進程的配置文件,supervisor支持每一個進程的配置文件存儲在單獨的文件中,服務器端程序運行時會自動識別並加載,該選項用於指定程序存儲配置文件的目錄路徑和文件名

3、配置案例:

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選項,是由於httpdsshd等服務時,默認是以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 <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

supervisor>

 4、systemd監控supervisord&supervisord監控ssh隧道進程:

一、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

二、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

參考文章:

一、http://www.cnblogs.com/shanyou/p/5075744.html

相關文章
相關標籤/搜索