God是一個由Ruby編寫的監控架構,它能夠保障你的進程爲運行狀態,以及能夠對一些特殊狀況進行進程的重啓。拓展能夠經過frigga來進行全局god的管理。前端
最好的安裝方式(經過ruby-gems):
python
gem install god
快速啓動ruby
注意:快速啓動須要0.12版本以上的,你能夠使用如下命令查看版本:
bash
god --version
一個簡單的例子:使用god保持一個簡單的進程。架構
下面是一個簡單的腳本,名字爲hello.pyide
#!/usr/bin/env python # import time while True: print "hello" time.sleep(1)
如今寫一個god的配置文件來管理上邊的hello.py的進程,simple.god:
spa
God.watch do |w| w.name = "hello" w.start = "python /root/god/hello.py" w.keepalive end
這是一個簡單的god配置,咱們首先聲明一個God.watch模塊,它能夠監控、控制上邊的進程。每一個watch都必須有一個惟一的名字和啓動這個進程的命令。keepalive告訴god保證這個進程的存活,若是這個進程死亡了,god會經過上邊定義的start來啓動進程。
unix
這個simple的例子中,咱們將god運行到前端,從而來保證咱們能夠進行相關的查看。rest
要運行god,咱們須要指定配置文件(-C),以及要求他在前端運行(-D)日誌
god -C simple.god -D
有跡象代表,god的監控流程有2種。一、最好的方式(基於事件),不是每一個系統都支持它,可是支持的話會自動使用它。當產生相關事件(退出),god會當即知道。二、對於不支持事件的系統,god會使用輪詢機制。PS:我這裏是基於事件的機制,因爲我這裏的限制,沒有基於輪詢的,若是想看:http://godrb.com/
#Events I [2014-08-11 11:10:10] INFO: Loading simple.god I [2014-08-11 11:10:10] INFO: Syslog enabled. I [2014-08-11 11:10:10] INFO: Using pid file directory: /var/run/god I [2014-08-11 11:10:10] INFO: Socket already in use I [2014-08-11 11:10:10] INFO: Socket is stale, reopening I [2014-08-11 11:10:10] INFO: Started on drbunix:///tmp/god.17165.sock I [2014-08-11 11:10:10] INFO: hello move 'unmonitored' to 'init' I [2014-08-11 11:10:10] INFO: hello moved 'unmonitored' to 'init' I [2014-08-11 11:10:10] INFO: hello [trigger] process is not running (Proce***unning) I [2014-08-11 11:10:10] INFO: hello move 'init' to 'start' I [2014-08-11 11:10:10] INFO: hello start: python /root/god/hello.py I [2014-08-11 11:10:10] INFO: hello moved 'init' to 'start' I [2014-08-11 11:10:10] INFO: hello [trigger] process is running (Proce***unning) I [2014-08-11 11:10:10] INFO: hello move 'start' to 'up' I [2014-08-11 11:10:10] INFO: hello registered 'proc_exit' event for pid 25779 I [2014-08-11 11:10:10] INFO: hello moved 'start' to 'up'
從DEBUG信息中,你能夠看出來,hello這個進程起初是沒有啓動的,然後god將它啓動。PS:若是是基於輪詢模式啓動,你注意觀察,他會5秒鐘檢查一次進程。
爲了體現基於事件,我這裏多加了一步操做(在別的終端殺掉hello.py,以驗證基於事件的形式):
[root@master ~]# ps -ef|grep hello.py root 25779 1 0 11:10 ? 00:00:00 python /root/god/hello.py root 25803 25782 0 11:10 pts/1 00:00:00 grep hello.py [root@master ~]# kill -9 25779
#Event 狀態: I [2014-08-11 11:11:02] INFO: hello [trigger] process 25779 exited {:thread_group_id=>25779, :pid=>25779, :exit_code=>9, :exit_signal=>17} (ProcessExits) I [2014-08-11 11:11:02] INFO: hello move 'up' to 'start' I [2014-08-11 11:11:02] INFO: hello deregistered 'proc_exit' event for pid 25779 I [2014-08-11 11:11:02] INFO: hello start: python /root/god/hello.py I [2014-08-11 11:11:02] INFO: hello moved 'up' to 'start' I [2014-08-11 11:11:02] INFO: hello [trigger] process is running (Proce***unning) I [2014-08-11 11:11:02] INFO: hello move 'start' to 'up' I [2014-08-11 11:11:02] INFO: hello registered 'proc_exit' event for pid 25807 I [2014-08-11 11:11:02] INFO: hello moved 'start' to 'up'
PS:若是是輪詢(Polls)模式,它不是即刻啓動,而是等到檢查週期的到來。
到這裏,你已經知道god如何來保證進程,還有一些更加有空的管理方式,如cpu達到多少重啓進程,memory達到多少重啓進程等等,下面是一個配置的例子:
God.watch do |w| w.name = "hello" w.start = "python /root/god/hello.py" w.keepalive(:memory_max => 150.megabytes, :cpu_max => 50.percent) end
詳解::memory_max選項屬於keepalive的子命令,:cpu_max一樣也是。上邊的配置中,若是內存達到了150M,或CPU達到了50%,god就回重啓進程。默認狀況下,這些進程30秒會被檢查1次,而且會在(5超3次)的時候重啓,以免偶爾的超載狀況。
這裏就不在進行模擬內存泄露的狀況了,下面貼一個重啓cpu的日誌,官方文檔:http://godrb.com/ 搜memory leak
I [2014-08-11 13:35:46] INFO: hello [trigger] cpu out of bounds [5.3571428566083%%, *90.3052064640262%%, *94.7069943292977%%, *96.3414634148933%%] (CpuUsage) I [2014-08-11 13:35:46] INFO: hello move 'up' to 'restart' I [2014-08-11 13:35:46] INFO: hello deregistered 'proc_exit' event for pid 26355 I [2014-08-11 13:35:46] INFO: hello stop: default lambda killer I [2014-08-11 13:35:46] INFO: hello sent SIGTERM I [2014-08-11 13:35:47] INFO: hello process stopped I [2014-08-11 13:35:47] INFO: hello start: python /root/god/hello.py
另外,你能夠使用god對一些進程進行操做:
god start hello #hello爲進程名.對應simple.god文件中的w.name god stop hello god restart hello ...
因此,當你使用god的管理進程時候,能夠本身編寫一些特定的配置文件來管理相關的進程。例如:http出錯、磁盤io較大等等問題出現時,能夠幫助你作一些事。