supervisor管理後臺程序之flume
之前後臺程序都是用nohup *.sh 來跑的,不方便維護和監控,
每臺伺服器啟一個flume收集日誌的後臺程序。
可以進行監控,和通過頁面,啟動,關閉服務,檢視日誌
1.2. 介紹
Supervisor是一個程序控制系統. 它是一個C/S系統(注意: 其提供WEB介面給使用者查詢和控制), 它允許使用者去監控和控制在類UNIX系統的程序. 它的目標與launchd, daemontools和runit有些相似, 但是與它們不一樣的是, 它不是作為init(程序號pid是1)執行. 它是被用來控制程序, 並且它在啟動的時候和一般程式並無二致.
那麼通俗點,它的作用是什麼?你的Nginx,Tomcat,memcache,Redis...會崩麼,不會?好吧,那你自己寫的伺服器監測指令碼呢?好吧,不要再糾結了,交給Supervisor吧,它會幫你維護這些,即使它們不小心崩了,Supervisor會幫你看住它們,維護它們。
安裝(Centos):
- # yum install python-setuptools
- # easy_install supervisor
- 如果easy_install不好使就從官方下載:
- 然後通過python安裝:
- # tar zxf supervisor-3.1.3.tar.gz
- # cd supervisor
- # python setup.py install
成功安裝後可以登陸python控制檯輸入import supervisor 檢視是否能成功載入。
生成配置檔案(supervisord.conf):
echo_supervisord_conf > /etc/supervisord.conf
配置檔案:
; Sample supervisor config file. ; ; For more information on the config file, please see: ; http://supervisord.org/configuration.html ; ; Notes: ; - Shell expansion ("~" or "$HOME") is not supported. Environment ; variables can be expanded using this syntax: "%(ENV_HOME)s". ; - Quotes around values are not supported, except in the case of ; the environment= options as shown below. ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment". ; - Command will be truncated if it looks like a config file comment, e.g. ; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ". [unix_http_server] file=/tmp/supervisor.sock ; the path to the socket file ;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) [inet_http_server] ; inet (TCP) server disabled by default port=*:9001 ;username=admin ; default is no username (open server) ;password=admin ; default is no password (open server) [supervisord] logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; log level; default info; others: debug,warn,trace pidfile=/tmp/supervisord.pid ; supervisord pidfile; default 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 ; default is current user, required if 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:///tmp/supervisor.sock ; use a unix:// URL for a unix 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) [program:gdata] command=/home/web_admin/pro/server/gdata/run.sh directory=/home/web_admin/pro/server/gdata [program:x1flume] command=/home/web_admin/opt/v2_flume-apache170/bin/flume-ng agent --conf /home/web_admin/opt/v2_flume-apache170/conf --conf-file /home/web_admin/opt/v2_flume-apache170/conf/x1_dir_to_db_flume.conf --name a1 -Dflume.root.logger=INFO,console directory=/home/web_admin/opt/v2_flume-apache170/starDir autostart = false autorestart = false redirect_stderr=true stdout_logfile=/home/web_admin/opt/v2_flume-apache170/starDir/log/x1_dir.log stdout_logfile_maxbytes=200MB stdout_logfile_backups=10 stderr_logfile=/home/web_admin/opt/v2_flume-apache170/starDir/log/x1_dir_err.log stderr_logfile_maxbytes=100MB [program:x2flume] command=/home/web_admin/opt/v2_flume-apache170/bin/flume-ng agent --conf /home/web_admin/opt/v2_flume-apache170/conf --conf-file /home/web_admin/opt/v2_flume-apache170/conf/x2_dir_to_db_flume.conf --name a1 -Dflume.root.logger=INFO,console directory=/home/web_admin/opt/v2_flume-apache170/starDir autostart = false autorestart = false redirect_stderr=true stdout_logfile=/home/web_admin/opt/v2_flume-apache170/starDir/log/x2_dir.log stdout_logfile_maxbytes=100MB stdout_logfile_backups=10 [program:x3flume] command=/home/web_admin/opt/v2_flume-apache170/bin/flume-ng agent --conf /home/web_admin/opt/v2_flume-apache170/conf --conf-file /home/web_admin/opt/v2_flume-apache170/conf/x3_dir_to_db_flume.conf --name a1 -Dflume.root.logger=INFO,console directory=/home/web_admin/opt/v2_flume-apache170/starDir autostart = false autorestart = false redirect_stderr=true stdout_logfile=/home/web_admin/opt/v2_flume-apache170/starDir/log/x3_dir.log stdout_logfile_maxbytes=100MB stdout_logfile_backups=10 [program:x4flume] command=/home/web_admin/opt/v2_flume-apache170/bin/flume-ng agent --conf /home/web_admin/opt/v2_flume-apache170/conf --conf-file /home/web_admin/opt/v2_flume-apache170/conf/x4_dir_to_db_flume.conf --name a1 -Dflume.root.logger=INFO,console directory=/home/web_admin/opt/v2_flume-apache170/starDir autostart = false autorestart = false redirect_stderr=true stdout_logfile=/home/web_admin/opt/v2_flume-apache170/starDir/log/x4_dir.log stdout_logfile_maxbytes=100MB stdout_logfile_backups=10 [program:x8flume] command=/home/web_admin/opt/v2_flume-apache170/bin/flume-ng agent --conf /home/web_admin/opt/v2_flume-apache170/conf --conf-file /home/web_admin/opt/v2_flume-apache170/conf/x8_dir_to_db_flume.conf --name a1 -Dflume.root.logger=INFO,console directory=/home/web_admin/opt/v2_flume-apache170/starDir autostart = false autorestart = false redirect_stderr=true stdout_logfile=/home/web_admin/opt/v2_flume-apache170/starDir/log/x8_dir.log stdout_logfile_maxbytes=100MB stdout_logfile_backups=10 [program:x9flume] command=/home/web_admin/opt/v2_flume-apache170/bin/flume-ng agent --conf /home/web_admin/opt/v2_flume-apache170/conf --conf-file /home/web_admin/opt/v2_flume-apache170/conf/x9_dir_to_db_flume.conf --name a1 -Dflume.root.logger=INFO,console directory=/home/web_admin/opt/v2_flume-apache170/starDir autostart = false autorestart = false redirect_stderr=true stdout_logfile=/home/web_admin/opt/v2_flume-apache170/starDir/log/x9_dir.log stdout_logfile_maxbytes=100MB stdout_logfile_backups=10 ; The [include] section can just contain the "files" setting. This ; setting can list multiple files (separated by whitespace or ; newlines). It can also contain wildcards. The filenames are ; interpreted as relative to this file. Included files *cannot* ; include files themselves. ;[include] ;files = relative/directory/*.ini
主要引數說明:
該配置塊包含一個或者多個program段,program來表明supervisord要控制哪些程式。該配置塊的頭部是有固定格式的,一個關鍵字program,後面跟著一個冒號,接下來才是程式名。例如:[program:foo],foo就是程式名,在使用supervisorctl來操作程式的時候,就是以foo來標明的。
command:啟動程式使用的命令,可以是絕對路徑或者相對路徑
process_name:一個python字串表示式,用來表示supervisor程序啟動的這個的名稱,預設值是%(program_name)s
numprocs:Supervisor啟動這個程式的多個例項,如果numprocs>1,則process_name的表示式必須包含%(process_num)s,預設是1
numprocs_start:一個int偏移值,當啟動例項的時候用來計算numprocs的值
priority:權重,可以控制程式啟動和關閉時的順序,權重越低:越早啟動,越晚關閉。預設值是999
autostart:如果設定為true,當supervisord啟動的時候,程序會自動重啟。
autorestart:值可以是false、true、unexpected。false:程序不會自動重啟,unexpected:當程式退出時的退出碼不是exitcodes中定義的時,程序會重啟,true:程序會無條件重啟當退出的時候。
startsecs:程式啟動後等待多長時間後才認為程式啟動成功
startretries:supervisord嘗試啟動一個程式時嘗試的次數。預設是3
exitcodes:一個預期的退出返回碼,預設是0,2。
stopsignal:當收到stop請求的時候,傳送訊號給程式,預設是TERM訊號,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2。
stopwaitsecs:在作業系統給supervisord傳送SIGCHILD訊號時等待的時間
stopasgroup:如果設定為true,則會使supervisor傳送停止訊號到整個程序組
killasgroup:如果設定為true,則在給程式傳送SIGKILL訊號的時候,會發送到整個程序組,它的子程序也會受到影響。
user:如果supervisord以root執行,則會使用這個設定使用者啟動子程式
redirect_stderr:如果設定為true,程序則會把標準錯誤輸出到supervisord後臺的標準輸出檔案描述符。
stdout_logfile:把程序的標準輸出寫入檔案中,如果stdout_logfile沒有設定或者設定為AUTO,則supervisor會自動選擇一個檔案位置。
stdout_logfile_maxbytes:標準輸出log檔案達到多少後自動進行輪轉,單位是KB、MB、GB。如果設定為0則表示不限制日誌檔案大小
stdout_logfile_backups:標準輸出日誌輪轉備份的數量,預設是10,如果設定為0,則不備份
stdout_capture_maxbytes:當程序處於stderr capture mode模式的時候,寫入FIFO佇列的最大bytes值,單位可以是KB、MB、GB
stdout_events_enabled:如果設定為true,當程序在寫它的stderr到檔案描述符的時候,PROCESS_LOG_STDERR事件會被觸發
stderr_logfile:把程序的錯誤日誌輸出一個檔案中,除非redirect_stderr引數被設定為true
stderr_logfile_maxbytes:錯誤log檔案達到多少後自動進行輪轉,單位是KB、MB、GB。如果設定為0則表示不限制日誌檔案大小
stderr_logfile_backups:錯誤日誌輪轉備份的數量,預設是10,如果設定為0,則不備份
stderr_capture_maxbytes:當程序處於stderr capture mode模式的時候,寫入FIFO佇列的最大bytes值,單位可以是KB、MB、GB
stderr_events_enabled:如果設定為true,當程序在寫它的stderr到檔案描述符的時候,PROCESS_LOG_STDERR事件會被觸發
environment:一個k/v對的list列表
directory:supervisord在生成子程序的時候會切換到該目錄
umask:設定程序的umask
serverurl:是否允許子程序和內部的HTTP服務通訊,如果設定為AUTO,supervisor會自動的構造一個url
eg:
啟動
supervisord -c /home/web_admin/opt/supervisor/supervisord.conf //啟動supervisor
開啟控制檯
supervisorctl
status stop start reload update shutdown 命令進行相關操作