﻿{"id":633,"date":"2011-08-07T13:57:33","date_gmt":"2011-08-07T05:57:33","guid":{"rendered":"http:\/\/nick.workao.org\/?p=633"},"modified":"2011-08-07T13:57:33","modified_gmt":"2011-08-07T05:57:33","slug":"%e7%94%a8monit%e7%9b%91%e6%8e%a7%e7%b3%bb%e7%bb%9f%e5%85%b3%e9%94%ae%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/linux\/633","title":{"rendered":"\u7528monit\u76d1\u63a7\u7cfb\u7edf\u5173\u952e\u8fdb\u7a0b"},"content":{"rendered":"<p><a href=\"http:\/\/mmonit.com\/monit\/\" target=\"_blank\">monit<\/a>\u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684\u7cfb\u7edf\u72b6\u6001\u3001\u8fdb\u7a0b\u3001\u6587\u4ef6\u3001\u76ee\u5f55\u548c\u8bbe\u5907\u7684\u76d1\u63a7\u8f6f\u4ef6\uff0c\u7528\u4e8e*nix\u5e73\u53f0\uff0c \u5b83\u53ef\u4ee5\u81ea\u52a8\u91cd\u542f\u90a3\u4e9b\u5df2\u7ecf\u6302\u6389\u7684\u7a0b\u5e8f\uff0c\u975e\u5e38\u9002\u5408\u76d1\u63a7\u7cfb\u7edf\u5173\u952e\u7684\u8fdb\u7a0b\u548c\u8d44\u6e90\uff0c\u5982\uff1anginx\u3001apache\u3001mysql\u548ccpu\u5360\u6709\u7387\u7b49\u3002\u800c\u76d1\u63a7\u7ba1\u7406Python\u8fdb\u7a0b\uff0c\u5e38\u7528\u7684\u662f<a href=\"http:\/\/supervisord.org\/\" target=\"_self\">supervisor<\/a>\uff0c\u540e\u7eed\u4f1a\u53e6\u5916\u64b0\u6587\u4ecb\u7ecd\u3002<\/p>\n<p>\u4e0b\u9762\u5206\u522b\u4ecb\u7ecdmonit\u7684\u5b89\u88c5\u3001\u914d\u7f6e\u548c\u542f\u52a8\u3002<\/p>\n<h2>\u5b89\u88c5<\/h2>\n<p>\u5728debian\u6216ubuntu\u4e0a\u5b89\u88c5monit\u975e\u5e38\u65b9\u4fbf\uff0c\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4<\/p>\n<div>\n<div>\n<pre>sudo apt-get install monit<\/pre>\n<\/div>\n<\/div>\n<p>\u5373\u53ef\uff0c\u5176\u5b83*nix\u4e0a\u4e5f\u5f88\u7b80\u5355\uff0c\u4e0b\u8f7d\u6e90\u7801\u8d70\u4e00\u904d\u5b89\u88c5\u4e09\u6b65\u5c31OK\u4e86\u3002<\/p>\n<div>\n<div>\n<pre>.\/configure\nmake\nmake install<\/pre>\n<\/div>\n<\/div>\n<p>\u5b89\u88c5\u540e\uff0c\u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6\u4e3a\/etc\/monit\/monitrc\u3002<\/p>\n<h2>\u914d\u7f6e<\/h2>\n<p>\u6dfb\u52a0\u9700\u8981\u76d1\u63a7\u7684\u8fdb\u7a0b\u7b49\u4fe1\u606f\u81f3monit\u7684\u914d\u7f6e\u6587\u4ef6\uff0cmonit\u7684\u914d\u7f6e\u8be6\u89c1\u4e0b\u9762\u7684\u793a\u4f8b\u6587\u4ef6\uff0c\u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\uff0c\u5173\u952e\u7684\u914d\u7f6e\u6211\u90fd\u7ed9\u51fa\u4e86\u4e2d\u6587\u7684\u6ce8\u91ca\u3002<\/p>\n<pre>##\n## \u98de\u9f99\u65e5\u5fd7 \u793a\u4f8bmonit\u914d\u7f6e\u6587\u4ef6\uff0c\u8bf4\u660e\uff1a\n## 1. \u57df\u540d\u4ee5example.com\u4e3a\u4f8b\u3002\n## 2. \u540e\u9762\u5e26xxx\u7684\u5747\u662f\u4e3e\u4f8b\u7528\u7684\u540d\u5b57\uff0c\u9700\u8981\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u4fee\u6539\u3002\n##\n###############################################################################\n## Monit control file\n###############################################################################\n#\n# \u68c0\u67e5\u5468\u671f\uff0c\u9ed8\u8ba4\u4e3a2\u5206\u949f\uff0c\u5bf9\u4e8e\u7f51\u7ad9\u6765\u8bf4\u6709\u70b9\u957f\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u884c\u8c03\u8282\uff0c\u8fd9\u6539\u621030\u79d2\u3002\nset daemon  30\n\n# \u65e5\u5fd7\u6587\u4ef6\nset logfile \/var\/log\/monit.log\n\n#\n# \u90ae\u4ef6\u901a\u77e5\u670d\u52a1\u5668\n#\n#set mailserver mail.example.com\nset mailserver localhost           \n\n#\n# \u901a\u77e5\u90ae\u4ef6\u7684\u683c\u5f0f\u8bbe\u7f6e\uff0c\u4e0b\u9762\u662f\u9ed8\u8ba4\u683c\u5f0f\u4f9b\u53c2\u8003\n#\n## Monit by default uses the following alert mail format:\n##\n## --8<--\n## From: monit@$HOST                         # sender\n## Subject: monit alert --  $EVENT $SERVICE  # subject\n##\n## $EVENT Service $SERVICE                   #\n##                                           #\n##  Date:        $DATE                   #\n##  Action:      $ACTION                 #\n##  Host:        $HOST                   # body\n##  Description: $DESCRIPTION            #\n##                                           #\n## Your faithful employee,                   #\n## monit                                     #\n## --8<--\n##\n## You can override the alert message format or its parts such as subject\n## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc.\n## are expanded on runtime. For example to override the sender:\n#\n# \u7b80\u5355\u7684\uff0c\u8fd9\u53ea\u6539\u4e86\u4e00\u4e0b\u53d1\u9001\u4eba\uff0c\u6709\u9700\u8981\u53ef\u4ee5\u81ea\u5df1\u4fee\u6539\u5176\u5b83\u5185\u5bb9\u3002\nset mail-format { from: webmaster@example.com }\n\n# \u8bbe\u7f6e\u90ae\u4ef6\u901a\u77e5\u63a5\u6536\u8005\u3002\u5efa\u8bae\u53d1\u5230gmail\uff0c\u65b9\u4fbf\u90ae\u4ef6\u8fc7\u6ee4\u3002\nset alert userxxx@gmail.com\n\nset httpd port 2812 and            # \u8bbe\u7f6ehttp\u76d1\u63a7\u9875\u9762\u7684\u7aef\u53e3\n     use address www.example.com   # http\u76d1\u63a7\u9875\u9762\u7684IP\u6216\u57df\u540d\n     allow localhost               # \u5141\u8bb8\u672c\u5730\u8bbf\u95ee\n     allow 58.68.78.0\/24           # \u5141\u8bb8\u6b64IP\u6bb5\u8bbf\u95ee\n     ##allow 0.0.0.0\/0.0.0.0       # \u5141\u8bb8\u4efb\u4f55IP\u6bb5\uff0c\u4e0d\u5efa\u8bae\u8fd9\u6837\u5e72\n     allow userxxx:passwordxxx     # \u8bbf\u95ee\u7528\u6237\u540d\u5bc6\u7801\n\n###############################################################################\n## Services\n###############################################################################\n#\n# \u7cfb\u7edf\u6574\u4f53\u8fd0\u884c\u72b6\u51b5\u76d1\u63a7\uff0c\u9ed8\u8ba4\u7684\u5c31\u53ef\u4ee5\uff0c\u53ef\u4ee5\u81ea\u5df1\u53bb\u5fae\u8c03\n#\n# \u7cfb\u7edf\u540d\u79f0\uff0c\u53ef\u4ee5\u662fIP\u6216\u57df\u540d\ncheck system www.example.com\n    if loadavg (1min) > 4 then alert\n    if loadavg (5min) > 2 then alert\n    if memory usage > 75% then alert\n    if cpu usage (user) > 70% then alert\n    if cpu usage (system) > 30% then alert\n    if cpu usage (wait) > 20% then alert\n\n#\n# \u76d1\u63a7nginx\n#\n# \u9700\u8981\u63d0\u4f9b\u8fdb\u7a0bpid\u6587\u4ef6\u4fe1\u606f\ncheck process nginx with pidfile \/var\/run\/nginx.pid\n    # \u8fdb\u7a0b\u542f\u52a8\u547d\u4ee4\u884c\uff0c\u6ce8\uff1a\u5fc5\u987b\u662f\u547d\u4ee4\u5168\u8def\u5f84\n    start program = \"\/etc\/init.d\/nginx start\"\n    # \u8fdb\u7a0b\u5173\u95ed\u547d\u4ee4\u884c\n    stop program  = \"\/etc\/init.d\/nginx stop\"\n    # nginx\u8fdb\u7a0b\u72b6\u6001\u6d4b\u8bd5\uff0c\u76d1\u6d4b\u5230nginx\u8fde\u4e0d\u4e0a\u4e86\uff0c\u5219\u81ea\u52a8\u91cd\u542f\n    if failed host www.example.com port 80 protocol http then restart\n    # \u591a\u6b21\u91cd\u542f\u5931\u8d25\u5c06\u4e0d\u518d\u5c1d\u8bd5\u91cd\u542f\uff0c\u8fd9\u79cd\u5c31\u662f\u7cfb\u7edf\u51fa\u73b0\u4e25\u91cd\u9519\u8bef\u7684\u60c5\u51b5\n    if 3 restarts within 5 cycles then timeout\n    # \u53ef\u9009\uff0c\u8bbe\u7f6e\u5206\u7ec4\u4fe1\u606f\n    group server\n\n#   \u53ef\u9009\u7684ssl\u7aef\u53e3\u7684\u76d1\u63a7\uff0c\u5982\u679c\u6709\u7684\u8bdd\n#    if failed port 443 type tcpssl protocol http\n#       with timeout 15 seconds\n#       then restart\n\n#\n# \u76d1\u63a7apache\n#\ncheck process apache with pidfile \/var\/run\/apache2.pid\n    start program = \"\/etc\/init.d\/apache2 start\"\n    stop program  = \"\/etc\/init.d\/apache2 stop\"\n    # apache\u5403cpu\u548c\u5185\u5b58\u6bd4\u8f83\u5389\u5bb3\uff0c\u989d\u5916\u6dfb\u52a0\u4e00\u4e9b\u5173\u4e8e\u8fd9\u65b9\u9762\u7684\u76d1\u63a7\u8bbe\u7f6e\n    if cpu > 50% for 2 cycles then alert\n    if cpu > 70% for 5 cycles then restart\n    if totalmem > 1500 MB for 10 cycles then restart\n    if children > 250 then restart\n    if loadavg(5min) greater than 10 for 20 cycles then stop\n    if failed host www.example.com port 8080 protocol http then restart\n    if 3 restarts within 5 cycles then timeout\n    group server\n    # \u53ef\u9009\uff0c\u4f9d\u8d56\u4e8enginx\n    depends on nginx\n\n#\n# \u76d1\u63a7spawn-fcgi\u8fdb\u7a0b(\u5176\u5b9e\u5c31\u662ffast-cgi\u8fdb\u7a0b)\n#\ncheck process spawn-fcgi with pidfile \/var\/run\/spawn-fcgi.pid\n    # spawn-fcgi\u4e00\u5b9a\u8981\u5e26-P\u53c2\u6570\u624d\u4f1a\u751f\u6210pid\u6587\u4ef6\uff0c\u9ed8\u8ba4\u662f\u6ca1\u6709\u7684\n    start program = \"\/usr\/bin\/spawn-fcgi -a 127.0.0.1 -p 8081 -C 10 -u userxxx -g groupxxx -P \/var\/run\/spawn-fcgi.pid -f \/usr\/bin\/php-cgi\"\n    stop program = \"\/usr\/bin\/killall \/usr\/bin\/php-cgi\"\n    # fast-cgi\u8d70\u7684\u4e0d\u662fhttp\u534f\u8bae\uff0cmonit\u7684protocol\u53c2\u6570\u4e5f\u6ca1\u6709cgi\u5bf9\u5e94\u7684\u8bbe\u7f6e\uff0c\u8fd9\u91cc\u53bb\u6389protocol http\u5373\u53ef\u3002\n    if failed host 127.0.0.1 port 8081 then restart\n    if 3 restarts within 5 cycles then timeout\n    group server\n    depends on nginx<\/pre>\n<p>\u867d\u7136\u5728\u6ce8\u91ca\u91cc\u6709\u8be6\u7ec6\u8bf4\u660e\uff0c\u4f46\u662f\u6211\u8fd8\u662f\u8981\u518d\u5f3a\u8c03\u8bf4\u660e\u51e0\u70b9\uff1a<\/p>\n<ol>\n    <li>start\u548cstop\u7684program\u53c2\u6570\u91cc\u7684\u547d\u4ee4\u5fc5\u987b\u662f\u5168\u8def\u5f84\uff0c\u5426\u5219monit\u4e0d\u80fd\u6b63\u5e38\u542f\u52a8\uff0c\u6bd4\u5982killall\u5e94\u8be5\u662f\/usr\/bin\/killall\u3002<\/li>\n    <li>\u5bf9\u4e8espawn-fcgi\uff0c\u5f88\u591a\u4eba\u4f1a\u7528\u5b83\u6765\u7ba1\u7406PHP\u7684fast-cgi\u8fdb\u7a0b\uff0c\u4f46spawn-fcgi\u672c\u8eab\u4e5f\u662f\u6709\u53ef\u80fd\u6302\u6389\u7684\uff0c\u6240\u4ee5\u8fd8\u662f\u9700\u8981\u7528 monit\u6765\u76d1\u63a7spawn-fcgi\u3002spawn-fcgi\u5fc5\u987b\u5e26-P\u53c2\u6570\u624d\u4f1a\u6709pid\u6587\u4ef6\uff0c\u800c\u4e14fast-cgi\u8d70\u7684\u4e0d\u662fhttp\u534f\u8bae\uff0cmonit\u7684 protocol\u53c2\u6570\u4e5f\u6ca1\u6709cgi\u5bf9\u5e94\u7684\u8bbe\u7f6e\uff0c\u4e00\u5b9a\u8981\u53bb\u6389protocol http\u8fd9\u9879\u8bbe\u7f6e\u624d\u7ba1\u7528\u3002<\/li>\n    <li>\u8fdb\u7a0b\u591a\u6b21\u91cd\u542f\u5931\u8d25monit\u5c06\u4e0d\u518d\u5c1d\u8bd5\u91cd\u542f\uff0c\u6536\u5230\u8fd9\u6837\u7684\u901a\u77e5\u90ae\u4ef6\u8868\u660e\u7cfb\u7edf\u51fa\u73b0\u4e86\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u8981\u5f15\u8d77\u8db3\u591f\u7684\u91cd\u89c6\uff0c\u9700\u8981\u8d76\u7d27\u4eba\u5de5\u5904\u7406\u3002<\/li>\n<\/ol>\n<p>\u5f53\u7136monit\u9664\u4e86\u7ba1\u7406\u8fdb\u7a0b\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u76d1\u63a7\u6587\u4ef6\u3001\u76ee\u5f55\u3001\u8bbe\u5907\u7b49\uff0c\u672c\u6587\u4e0d\u505a\u8ba8\u8bba\uff0c\u5177\u4f53\u914d\u7f6e\u65b9\u5f0f\u53ef\u4ee5\u53bb\u53c2\u8003<a href=\"http:\/\/mmonit.com\/monit\/documentation\/monit.html\" target=\"_blank\">monit\u7684\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n<h2>\u542f\u52a8\u3001\u505c\u6b62\u3001\u91cd\u542f<\/h2>\n<p>\u6807\u51c6\u7684start\u3001stop\u3001restart<\/p>\n<div>\n<div>\n<pre>sudo \/etc\/init.d\/monit start\nsudo \/etc\/init.d\/monit stop\nsudo \/etc\/init.d\/monit restart<\/pre>\n<\/div>\n<\/div>\n<p>\u770b\u5230\u6b63\u786e\u7684\u63d0\u793a\u4fe1\u606f\u5373\u53ef\uff0c\u82e5\u9047\u5230\u95ee\u9898\u53ef\u4ee5\u53bb\u67e5\u770b\u914d\u7f6e\u91cc\u6307\u5b9a\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u5982\/var\/log\/monit.log\u3002<\/p>\n<p>\u4ece\u6211\u7684\u670d\u52a1\u5668\u8fd9\u51e0\u5e74\u7684\u8fd0\u884c\u60c5\u51b5\uff08monit\u53d1\u4e86\u7684\u901a\u77e5\u90ae\u4ef6\uff09\u6765\u770b\uff0cnginx\u6302\u6389\u7684\u4e8b\u51e0\u4e4e\u6ca1\u6709\uff0c\u4f46apache\u6216fast-cgi\u51fa\u95ee\u9898\u7684\u60c5\u51b5\u8fd8\u662f\u6bd4\u8f83\u591a\u89c1\uff0c\u8d76\u5feb\u7528\u4e0amonit\u6765\u7ba1\u7406\u4f60\u7684\u670d\u52a1\u5668\u4ee5\u63d0\u9ad8\u670d\u52a1\u5668\u7a33\u5b9a\u6027\uff0c\u8ddf502 Bad Gateway\u4e4b\u7c7b\u9519\u8bef\u8bf4\u62dc\u62dc\u5427\u3002<\/p>\n<p>\u9644\u4ef6\uff1a<a href=\"http:\/\/feilong.me\/wp-content\/uploads\/2011\/02\/monitrc.txt\" target=\"_blank\">monit\u793a\u4f8b\u914d\u7f6e\u6587\u4ef6<\/a> (\u6ce8\uff1a\u4e0b\u8f7d\u540e\u8bf7\u53bb\u6389.txt\u6587\u4ef6\u540e\u7f00)<\/p>","protected":false},"excerpt":{"rendered":"<p>monit\u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684\u7cfb\u7edf\u72b6\u6001\u3001\u8fdb\u7a0b\u3001\u6587\u4ef6\u3001\u76ee\u5f55\u548c\u8bbe\u5907\u7684\u76d1\u63a7\u8f6f\u4ef6\uff0c\u7528\u4e8e*nix\u5e73\u53f0\uff0c \u5b83\u53ef\u4ee5\u81ea\u52a8\u91cd\u542f\u90a3\u4e9b\u5df2\u7ecf\u6302\u6389\u7684\u7a0b\u5e8f\uff0c\u975e\u5e38\u9002\u5408\u76d1\u63a7\u7cfb\u7edf\u5173\u952e\u7684\u8fdb\u7a0b\u548c\u8d44\u6e90\uff0c\u5982\uff1anginx\u3001apache\u3001mysql\u548c&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[440,314],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-monit"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":2,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":635,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions\/635"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}