{"id":396,"date":"2013-05-08T15:58:29","date_gmt":"2013-05-08T07:58:29","guid":{"rendered":"http:\/\/www.sulabs.net\/?p=396"},"modified":"2013-05-08T16:04:20","modified_gmt":"2013-05-08T08:04:20","slug":"python%e6%8f%92%e4%bb%b6%e5%81%9anagios%e5%8f%91%e6%8a%a5%e8%ad%a6%e9%82%ae%e4%bb%b6%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/www.sulabs.net\/?p=396","title":{"rendered":"python\u63d2\u4ef6\u505anagios\u53d1\u62a5\u8b66\u90ae\u4ef6[\u8f6c]"},"content":{"rendered":"<p>\u6587\u7ae0\u8f6c\u81ea <a href=\"http:\/\/deidara.blog.51cto.com\/400447\/461008\">http:\/\/deidara.blog.51cto.com\/400447\/461008<\/a>\u00a0\u5411\u539f\u4f5c\u8005\u8868\u793a\u611f\u8c22<\/p>\n<p>\u5b89\u88c5\u597dnagios\u540e\uff0c\u4e00\u76f4\u5229\u7528<strong>nagios<\/strong>\u670d\u52a1\u5668\u4e0a\u7684sendmail\u670d\u52a1\u5668\u53bb\u53d1\u90ae\u4ef6\uff0c\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u4f2a\u9020\u522b\u7684\u90ae\u4ef6\u5730\u5740\u53d1\u90ae\u4ef6\uff0c\u4f46\u662f\u4e00\u76f4\u4f1a\u88ab\u90ae\u5c40\u89c6\u4e3a\u5783\u573e\u90ae\u4ef6\uff0c\u6211\u628aemail\u5730\u5740\u52a0\u5728\u4e86QQ\u4e0a\uff0c\u597d\u5904\u662f\u4e00\u6765\u90ae\u4ef6QQ\u53f3\u4e0b\u89d2\u4f1a\u5f39\u63d0\u793a\u3002\u5de5\u4f5c\u65f6\u95f4\u4f1a\u7b2c\u4e00\u65f6\u95f4\u77e5\u9053\u670d\u52a1\u5668\u51fa\u72b6\u51b5\uff08\u624b\u673a\u77ed\u4fe1\u6682\u65f6\u4e0d\u63d0\uff09\uff0c\u4f46\u662f\u5982\u679c\u662f\u5783\u573e\u90ae\u4ef6\uff0cQQ\u4e0d\u4f1a\u63d0\u9192\u7684\uff0c\u5f53\u7136\u53ef\u4ee5\u5728QQ\u90ae\u7bb1\u628a\u5176\u6dfb\u52a0\u4e3a\u4fe1\u4efb\u90ae\u5c40\uff0c\u53ef\u4ee5\u907f\u514d\u3002\u6211\u5c31\u4e0d\u90a3\u4e48\u6d6a\u8d39\u65f6\u95f4\u4e86\u3002\u81ea\u5df1\u5199\u4e00\u4e2a\u53d1\u90ae\u4ef6\u7684\u63d2\u4ef6\u6765\u66ff\u6362\u6389\u672c\u673a\u7684sendmail\u670d\u52a1\uff0c\u53ef\u4ee5\u4e3a\u670d\u52a1\u5668\u8282\u7701\u8d44\u6e90\uff0c\u5927\u5bb6\u4e5f\u77e5\u9053\u542f\u52a8sendmail\u670d\u52a1\u7a0d\u7a0d\u8d39\u70b9\u65f6\u95f4\uff0c\u5982\u679c\u4e3b\u673a\u540d\u6ca1\u6709\u8bbe\u7f6e\u597d\u7684\u8bddsendmail\u670d\u52a1\u4f1a\u542f\u52a8\u5f88\u4e45\u624d\u80fd\u8d77\u6765\u7684\u54e6\u3002\u5e9f\u8bdd\u4e0d\u591a\u8bf4\u4e86\uff0c\u53d1\u6e90\u4ee3\u7801\u3002<br \/>\n<!--more--><\/p>\n<p>vim \/usr\/local\/nagios\/libexec\/sendmail<\/p>\n<pre class=\"brush: perl; gutter: true\">#!\/usr\/bin\/python\r\nimport smtplib\r\nimport string\r\nimport sys\r\nimport getopt\r\n\r\ndef usage():\r\n   print &quot;&quot;&quot;sendmail is a send mail Plugins\r\n   Usage:\r\n\r\n   sendmail [-h|--help][-t|--to][-s|--subject][-m|--message]\r\n\r\n   Options:\r\n          --help|-h)\r\n                 print sendmail help.\r\n          --to|-t)\r\n                 Sets sendmail to email.\r\n          --subject|-s)\r\n                  Sets the mail subject.\r\n          --message|-m)\r\n                  Sets the mail body\r\n    Example:\r\n           only one to email  user\r\n          .\/sendmail -t &#039;eric@nginxs.com&#039; -s &#039;hello eric&#039; -m &#039;hello eric,this is sendmail test!\r\n           many to email  user\r\n          .\/sendmail -t &#039;eric@nginxs.com,yangzi@nginxs.com,zhangsan@nginxs.com&#039; -s &#039;hello eric&#039; -m &#039;hello eric,this is sendmail test!&quot;&quot;&quot;\r\n   sys.exit(3)\r\n\r\ntry:\r\n   options,args = getopt.getopt(sys.argv[1:],&quot;ht:s:m:&quot;,[&quot;help&quot;,&quot;to=&quot;,&quot;subject=&quot;,&quot;message=&quot;])\r\nexcept getopt.GetoptError:\r\n   usage()\r\nfor name,value in options:\r\n    if name in (&quot;-h&quot;,&quot;--help&quot;):\r\n       usage()\r\n    if name in (&quot;-t&quot;,&quot;--to&quot;):\r\n# accept message user\r\n       TO = value\r\n       TO = TO.split(&quot;,&quot;)\r\n    if name in (&quot;-s&quot;,&quot;--title&quot;):\r\n       SUBJECT = value\r\n    if name in (&quot;-m&quot;,&quot;--message&quot;):\r\n       MESSAGE = value\r\n       MESSAGE = MESSAGE.split(&#039;\\\\n&#039;)\r\n       MESSAGE = &#039;\\n&#039;.join(MESSAGE)\r\n\r\n#smtp HOST\r\nHOST = &quot;smtp.126.com&quot;               #\u6539\u4e3a\u4f60\u7684\u90ae\u5c40SMTP \u4e3b\u673a\u5730\u5740\r\n#smtp port\r\nPORT = &quot;25&quot;                               #\u6539\u4e3a\u4f60\u7684\u90ae\u5c40\u7684SMTP \u7aef\u53e3\r\n#FROM mail user\r\nUSER = &#039;eric&#039;                              # \u6539\u4e3a\u4f60\u7684\u90ae\u7bb1\u7528\u6237\u540d\r\n#FROM mail password\r\nPASSWD = &#039;123456&#039;                    # \u6539\u4e3a\u4f60\u7684\u90ae\u7bb1\u5bc6\u7801\r\n#FROM EMAIL\r\nFROM = &quot;yangzi2008@126.com&quot;    # \u6539\u4e3a\u4f60\u7684\u90ae\u7bb1 email\r\n\r\ntry:\r\n   BODY = string.join((\r\n      &quot;From: %s&quot; % FROM,\r\n      &quot;To: %s&quot; % TO,\r\n      &quot;Subject: %s&quot; % SUBJECT,\r\n      &quot;&quot;,\r\n      MESSAGE),&quot;\\r\\n&quot;)\r\n\r\n   smtp = smtplib.SMTP()\r\n   smtp.connect(HOST,PORT)\r\n   smtp.login(USER,PASSWD)\r\n   smtp.sendmail(FROM,TO,BODY)\r\n   smtp.quit()\r\nexcept:\r\n   print &quot;UNKNOWN ERROR&quot;\r\n   print &quot;please look help&quot;\r\n   print &quot;.\/sendmail -h&quot;<\/pre>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a \u53ea\u7ed9\u4e00\u4e2a\u7528\u6237\u53d1\uff1a<\/p>\n<pre>nagios $&gt; .\/sendmail -t &#039;eric@nginxs.com&#039; -s &#039;hello eric&#039; -m &#039;hello eric,this is sendmail test!<\/pre>\n<p>\u7ed9\u591a\u4e2a\u7528\u6237\u53d1\uff1a<\/p>\n<pre>.\/sendmail -t &#039;eric@nginxs.com,yangzi@nginxs.com,zhangsan@nginxs.com&#039; -s &#039;hello eric\r\n&#039; -m &#039;hello eric,this is sendmail test!<\/pre>\n<p>vim \/usr\/local\/nagios\/etc\/objects\/commands.cfg<\/p>\n<pre class=\"brush: bash; gutter: true\">define command{\r\n        command_name    notify-host-by-email\r\n        command_line    $USER1$\/sendmail -t $CONTACTEMAIL$ -s &quot;** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **&quot;  -m  &quot;***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\nHost: $HOSTNAME$\\nState: $HOSTSTATE$\\nAddress: $HOSTADDRESS$\\nInfo: $HOSTOUTPUT$\\n\\nDate\/Time: $LONGDATETIME$\\n&quot;\r\n        }\r\n\r\ndefine command{\r\n        command_name    notify-service-by-email\r\n        command_line    $USER1$\/sendmail -t  $CONTACTEMAIL$ -s &quot;** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$\/$SERVICEDESC$ is $SERVICESTATE$ **&quot;  -m  &quot;***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\n\\nService: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\n\\nDate\/Time: $LONGDATETIME$\\n\\nAdditional Info:\\n\\n$SERVICEOUTPUT$&quot;\r\n        }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u8f6c\u81ea http:\/\/deidara.blog.51cto.com\/40044 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/www.sulabs.net\/?p=396\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[90,91,55],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-linux","tag-nagios","tag-sendmail","tag-smtp"],"_links":{"self":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=396"}],"version-history":[{"count":3,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/396\/revisions"}],"predecessor-version":[{"id":401,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=\/wp\/v2\/posts\/396\/revisions\/401"}],"wp:attachment":[{"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sulabs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}