{"id":156019,"date":"2019-08-29T09:04:21","date_gmt":"2019-08-29T01:04:21","guid":{"rendered":"https:\/\/gulass.cn\/?p=156019"},"modified":"2019-08-19T10:12:05","modified_gmt":"2019-08-19T02:12:05","slug":"monitor-linux-system","status":"publish","type":"post","link":"https:\/\/gulass.cn\/monitor-linux-system.html","title":{"rendered":"\u76d1\u63a7Linux\u7cfb\u7edf\u5185\u5b58\u7684\u4e00\u4e9b\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u6863\u4ecb\u7ecd\u76d1\u63a7Linux\u7cfb\u7edf\u5185\u5b58\u7684\u4e00\u4e9b\u65b9\u6cd5<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u5b89\u88c5Linux\u4e0b\u9762\u7684\u4e00\u4e2a\u90ae\u4ef6\u5ba2\u6237\u7aefMsmtp\u8f6f\u4ef6(\u7c7b\u4f3c\u4e8e\u4e00\u4e2aFoxmail\u7684\u5de5\u5177)<\/strong><\/div>\n
1\u3001\u4e0b\u8f7d\u5b89\u88c5\uff1ahttps:\/\/marlam.de\/msmtp\/download\/<\/strong><\/span><\/div>\n
\r\n# tar jxvf msmtp-1.4.16.tar.bz2\r\n# cd msmtp-1.4.16\r\n# .\/configure --prefix=\/usr\/local\/msmtp\r\n# make\r\n# make install\r\n<\/pre>\n
2\u3001\u521b\u5efamsmtp\u914d\u7f6e\u6587\u4ef6\u548c\u65e5\u5fd7\u6587\u4ef6\uff08host\u4e3a\u90ae\u4ef6\u57df\u540d\uff0c\u90ae\u4ef6\u7528\u6237\u540dtest\uff0c\u5bc6\u7801123456\uff09<\/strong><\/span><\/div>\n
\r\n# vim ~\/.msmtprc\r\n\r\naccount default \r\nhost 126.com \r\nfrom test@126.com \r\nauth login \r\nuser test \r\npassword 123456 \r\nlogfile ~\/.msmtp.log \r\n\r\n# chmod 600 ~\/.msmtprc\r\n# touch ~\/.msmtp.log\r\n<\/pre>\n
3\u3001mutt\u5b89\u88c5\u914d\u7f6e\uff1a\uff08\u4e00\u822clinux\u4e0b\u6709\u9ed8\u8ba4\u5b89\u88c5mutt\uff09<\/strong><\/span><\/div>\n
\r\n# vim ~\/.muttrc\r\n\r\nset sendmail=\"\/usr\/local\/msmtp\/bin\/msmtp\" \r\nset use_from=yes \r\nset realname=\"memory\" \r\nset from=test@126.com \r\nset envelope_from=yes \r\nset rfc2047_parameters=yes \r\nset charset=\"utf-8\" \r\n<\/pre>\n
4\u3001\u90ae\u4ef6\u53d1\u9001\u6d4b\u8bd5\uff08-s\u90ae\u4ef6\u6807\u9898\uff0c-a\u8868\u52a0\u9644\u4ef6\uff09<\/strong><\/span><\/div>\n
\r\n# echo \"\u90ae\u4ef6\u5185\u5bb9123456\" | mutt -s \"\u90ae\u4ef6\u6807\u9898\u6d4b\u8bd5\u90ae\u4ef6\" -a \/scripts\/test.txt test@126.com\r\n<\/pre>\n
\u4e8c\u3001\u4f7f\u7528Free\u547d\u4ee4\u76d1\u63a7\u7cfb\u7edf\u5185\u5b58<\/strong><\/div>\n
1\u3001\u4f7f\u7528free\u547d\u4ee4\u67e5\u770blinux\u7cfb\u7edf\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a\uff08\u4ee5M\u4e3a\u5355\u4f4d\uff09<\/strong><\/span><\/div>\n
\r\n# free -m \r\n\uff08\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u6837\u5f0f\uff09\r\ntotal used free shared buffers cached\r\nMem: 3952 3414 538 0 168 484\r\n-\/+ buffers\/cache: 2760 1191\r\nSwap: 8191 86 8105\r\n<\/pre>\n
2\u3001\u67e5\u770b\u622a\u53d6\u5269\u4f59\u5185\u5b58free\u7684\u6570\u503c\u547d\u4ee4\uff1a<\/strong><\/span><\/div>\n
\r\n(1) \u7269\u7406\u5185\u5b58free\u503c\uff1a# free -m | grep Mem | awk '{print $4}'\r\n(2) \u7f13\u51b2\u533a\u7684free\u503c\uff1a# free -m | grep - | awk '{print $4}'\r\n(3) Swap\u5206\u533afree\u503c\uff1a# free -m | grep Swap | awk '{print $4}'\r\n<\/pre>\n
3\u3001\u7f16\u5199\u5185\u5b58\u76d1\u63a7\u7684\u811a\u672c\u6587\u4ef6\uff1a<\/strong><\/span><\/div>\n
\r\n# vim \/scripts\/free-mem.sh\r\n\r\n#!\/bin\/bash \r\n#\u4f7f\u7528free\u547d\u4ee4\u76d1\u63a7linux\u7cfb\u7edf\u5185\u5b58\u53d8\u5316 \r\n\r\n#\u53d6\u7cfb\u7edf\u5f53\u524d\u65f6\u95f4\uff08\u4ee5\u8ffd\u52a0\u7684\u65b9\u5f0f\u5199\u5165\u6587\u4ef6>>\uff09 \r\ndate >> \/scripts\/date-time.txt \r\n\r\n#\u6293\u53d6\u7269\u7406\u5185\u5b58free\u503c\uff08\u4ee5\u8ffd\u52a0\u7684\u65b9\u5f0f\u5199\u5165\u6587\u4ef6>>\uff09 \r\necho Mem-free: `free -m | grep Mem | awk '{print $4}'`M >> \/scripts\/mem-free.txt \r\n\r\n#\u6293\u53d6\u7f13\u51b2\u533a\u7684free\u503c\uff08\u4ee5\u8ffd\u52a0\u7684\u65b9\u5f0f\u5199\u5165\u6587\u4ef6>>\uff09 \r\necho buffers\/cache-free: `free -m | grep - | awk '{print $4}'`M >> \/scripts\/buffers-free.txt \r\n\r\n#\u6293\u53d6Swap\u5206\u533afree\u503c\uff08\u4ee5\u8ffd\u52a0\u7684\u65b9\u5f0f\u5199\u5165\u6587\u4ef6>>\uff09 \r\necho Swap-free: `free -m | grep Swap | awk '{print $4}'`M >> \/scripts\/swap-free.txt \r\n\r\n#\u9010\u884c\u8fde\u63a5\u4e0a\u9762\u7684\u65f6\u95f4\u548c\u5185\u5b58\u76f8\u5173\u884c\u6570\u636e\uff08\u6bcf\u6b21\u91cd\u65b0\u5199\u5165\u6587\u4ef6>\uff09 \r\npaste \/scripts\/date-time.txt \/scripts\/mem-free.txt \/scripts\/buffers-free.txt \/scripts\/swap-free.txt > \/scripts\/freemem.txt \r\n\r\n# chmod a+x \/scripts\/free-mem.sh\r\n# \/scripts\/free-mem.sh\r\n<\/pre>\n
4\u3001\u67e5\u770b\u5185\u5b58\u76d1\u63a7\u7684\u7ed3\u679c\u6587\u4ef6\uff1a<\/strong><\/span><\/div>\n
\r\n# cat \/scripts\/freemem.txt\r\n<\/pre>\n
5\u3001\u7f16\u5199free\u7ed3\u679c\u6587\u4ef6\u90ae\u4ef6\u53d1\u9001\u811a\u672c\uff1a<\/strong><\/span><\/div>\n
\r\n# vim \/scripts\/sendmail-mem.sh\r\n\r\n#!\/bin\/bash \r\n#\u628a\u751f\u6210\u7684freemem.txt\u6587\u4ef6\u901a\u8fc7\u90ae\u4ef6\u53d1\u9001\u7ed9\u7528\u6237 \r\n\r\n#\u63d0\u53d6\u672c\u670d\u52a1\u5668\u7684IP\u5730\u5740\u4fe1\u606f \r\nIP=`ifconfig eth0 | grep \"inet addr\" | cut -f 2 -d \":\" | cut -f 1 -d \" \"` \r\n\r\n#\u63d0\u53d6\u5f53\u524d\u65e5\u671f\u65f6\u95f4 \r\ntoday=`date -d \"0 day\" +%Y\u5e74%m\u6708%d\u65e5` \r\n\r\n#\u53d1\u9001\u5185\u5b58\u76d1\u63a7\u7ed3\u679c\u90ae\u4ef6 \r\necho \"\u8fd9\u662f$IP\u670d\u52a1\u5668$today\u7684\u5185\u5b58\u76d1\u63a7\u62a5\u544a\uff0c\u8bf7\u4e0b\u8f7d\u9644\u4ef6\u3002\" | mutt -s \"$IP\u670d\u52a1\u5668$today\u5185\u5b58\u76d1\u63a7\u62a5\u544a\" -a \/scripts\/freemem.txt test@126.com\r\n\r\n# chmod a+x \/scripts\/sendmail-mem.sh\r\n<\/pre>\n
\u4e09\u3001\u76d1\u63a7\u7cfb\u7edf\u4ea4\u6362\u5206\u533aSwap\u7684\u60c5\u51b5\uff0c\u5f53\u4f7f\u7528\u8d85\u8fc780%\u7684\u65f6\u5019\u53d1\u544a\u8b66\u90ae\u4ef6<\/strong><\/div>\n
\r\n# vim \/scripts\/swap-warning.sh\r\n\r\n#!\/bin\/bash \r\n\r\n#\u63d0\u53d6\u672c\u670d\u52a1\u5668\u7684IP\u5730\u5740\u4fe1\u606f \r\nIP=`ifconfig eth0 | grep \"inet addr\" | cut -f 2 -d \":\" | cut -f 1 -d \" \"` \r\n\r\n#\u7cfb\u7edf\u5206\u914d\u7684\u4ea4\u6362\u5206\u533a\u603b\u91cf \r\nswap_total=`free -m | grep Swap | awk '{print $2}'` \r\n\r\n#\u5f53\u524d\u5269\u4f59\u7684\u4ea4\u6362\u5206\u533afree\u5927\u5c0f \r\nswap_free=`free -m | grep Swap | awk '{print $4}'` \r\n\r\n#\u5f53\u524d\u5df2\u4f7f\u7528\u7684\u4ea4\u6362\u5206\u533aused\u5927\u5c0f \r\nswap_used=`free -m | grep Swap | awk '{print $3}'` \r\n\r\nif ((swap_used != 0)); then \r\n\r\n#\u5982\u679c\u4ea4\u6362\u5206\u533a\u5df2\u88ab\u4f7f\u7528\uff0c\u5219\u8ba1\u7b97\u5f53\u524d\u5269\u4f59\u4ea4\u6362\u5206\u533afree\u6240\u5360\u603b\u91cf\u7684\u767e\u5206\u6bd4\uff0c\u7528\u5c0f\u6570\u6765\u8868\u793a\uff0c\u8981\u5728\u5c0f\u6570\u70b9\u524d\u9762\u8865\u4e00\u4e2a\u6574\u6570\u4f4d0 \r\nswap_per=0`echo \"scale=2;$swap_free\/$swap_total\" | bc` \r\n\r\n#\u8bbe\u7f6e\u4ea4\u6362\u5206\u533a\u7684\u544a\u8b66\u503c\u4e3a20%(\u5373\u4f7f\u7528\u8d85\u8fc780%\u7684\u65f6\u5019\u544a\u8b66)\u3002 \r\nswap_warn=0.20 \r\n\r\n#\u5f53\u524d\u5269\u4f59\u4ea4\u6362\u5206\u533a\u767e\u5206\u6bd4\u4e0e\u544a\u8b66\u503c\u8fdb\u884c\u6bd4\u8f83\uff08\u5f53\u5927\u4e8e\u544a\u8b66\u503c(\u5373\u5269\u4f5920%\u4ee5\u4e0a)\u65f6\u4f1a\u8fd4\u56de1\uff0c\u5c0f\u4e8e(\u5373\u5269\u4f59\u4e0d\u8db320%)\u65f6\u4f1a\u8fd4\u56de0 \uff09 \r\nswap_now=`expr $swap_per > $swap_warn` \r\n\r\n#\u5982\u679c\u5f53\u524d\u4ea4\u6362\u5206\u533a\u4f7f\u7528\u8d85\u8fc780%\uff08\u5373\u5269\u4f59\u5c0f\u4e8e20%\uff0c\u4e0a\u9762\u7684\u8fd4\u56de\u503c\u7b49\u4e8e0\uff09\uff0c\u7acb\u5373\u53d1\u90ae\u4ef6\u544a\u8b66 \r\nif (($swap_now == 0)); then \r\necho \"$IP\u670d\u52a1\u5668swap\u4ea4\u6362\u5206\u533a\u53ea\u5269\u4e0b $swap_free M \u672a\u4f7f\u7528\uff0c\u5269\u4f59\u4e0d\u8db320%\uff0c\u4f7f\u7528\u7387\u5df2\u7ecf\u8d85\u8fc780%\uff0c\u8bf7\u53ca\u65f6\u5904\u7406\u3002\" | mutt -s \"$IP \u670d\u52a1\u5668\u5185\u5b58\u544a\u8b66\" test@126.com\r\nfi \r\n\r\nfi \r\n\r\n# chmod a+x \/scripts\/swap-warning.sh\r\n<\/pre>\n
\u56db\u3001\u52a0\u5165\u4efb\u52a1\u8ba1\u5212\uff1a\u5185\u5b58\u6bcf\u5341\u5206\u949f\u68c0\u6d4b\u4e00\u6b21\uff0c\u6709\u544a\u8b66\u5219\u7acb\u5373\u53d1\u90ae\u4ef6(\u5341\u5206\u949f\u53d1\u4e00\u6b21)\uff0c\u5185\u5b58\u68c0\u6d4b\u7ed3\u679c\u90ae\u4ef6\u6bcf\u5929\u65e9\u4e0a8\u70b9\u53d1\u4e00\u6b21<\/strong><\/div>\n
\r\n# crontab -e\r\n\r\n*\/10 * * * * \/scripts\/free-mem.sh \r\n*\/10 * * * * \/scripts\/swap-warning.sh \r\n0 8 * * * \/scripts\/sendmail-mem.sh \r\n\r\n# service crond restart\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

# tar jxvf msmtp-1.4.16.tar.bz2 # cd msmtp-1.4.16 # .\/c […]<\/p>\n","protected":false},"author":63,"featured_media":156020,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-156019","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/156019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=156019"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/156019\/revisions"}],"predecessor-version":[{"id":156133,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/156019\/revisions\/156133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/156020"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=156019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=156019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=156019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}