{"id":228577,"date":"2021-10-24T09:30:45","date_gmt":"2021-10-24T01:30:45","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=228577"},"modified":"2021-10-20T16:31:06","modified_gmt":"2021-10-20T08:31:06","slug":"auto-monitor-disk","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/auto-monitor-disk.html","title":{"rendered":"\u7528\u4e8e\u81ea\u52a8\u76d1\u63a7\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u7684 Shell \u811a\u672c"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5982\u679c\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u5173\u952e\u4efb\u52a1\uff0c\u90a3\u4e48\u76d1\u63a7\u548c\u901a\u77e5\u7ba1\u7406\u5458\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u5f88\u91cd\u8981\u3002\u672c\u6587\u4ecb\u7ecd\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u81ea\u52a8\u76d1\u63a7\u5e76\u5728\u8fbe\u5230\u9608\u503c\u65f6\u5c06\u62a5\u544a\u53d1\u9001\u5230\u81ea\u5df1\u7684\u90ae\u7bb1\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5199\u4e00\u4e2a shell \u811a\u672c\uff0c\u5b83\u5728 crontab \u4e2d\u6307\u5b9a\u56fa\u5b9a\u65f6\u95f4\u95f4\u9694\u76d1\u63a7\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u5728\u62a5\u544a\u8fbe\u5230\u6307\u5b9a\u7684\u9608\u503c\u65f6\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u62a5\u544a\u3002<\/p>\n

\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u5b89\u88c5\u4e86mail<\/code>\u547d\u4ee4\u548cpostfix<\/code>\u53d1\u9001\u90ae\u4ef6\u670d\u52a1\u3002\u4e0b\u9762\u662f\u5728Centos8\u4e2d\u5b89\u88c5\u7684\u547d\u4ee4\uff1a<\/p>\n

\r\n[root@localhost ~]# yum -y install mailx postfix\r\n[root@localhost ~]# systemctl enable --now postfix\r\n<\/pre>\n

\u4e0b\u9762\u4f7f\u7528mail\u547d\u4ee4\u53d1\u9001\u90ae\u4ef6\u6d4b\u8bd5\uff1a<\/p>\n

\r\n[root@localhost ~]# echo \"This message will go into the body of the mail.\" | mail -s \"Testing from the server.\" root@localhost\r\n<\/pre>\n

\"\"
\n\u672c\u6587\u4e0d\u8bb2\u89e3\u5982\u4f55\u914d\u7f6esmtp\u670d\u52a1\u3002<\/p>\n

\u6d4b\u8bd5\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u7684\u811a\u672c<\/strong><\/div>\n

\u4ee5\u4e0b\u662f\u6d4b\u8bd5\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u5e76\u5c06\u62a5\u544a\u53d1\u9001\u5230\u6307\u5b9a\u90ae\u7bb1\u7684\u811a\u672c\u3002\u811a\u672c\u7684\u89e3\u91ca\u5728\u4ee3\u7801\u7247\u6bb5\u4e4b\u540e\u7ed9\u51fa\u3002<\/p>\n

\r\n[root@localhost ~]# vim  disk-monitor.sh\r\n#!\/bin\/bash\r\n\r\nMAILID=\"admin@lrxjmw.cn\"\r\n\r\nVALUE=80\r\n\r\nSERVERNAME=$(hostname)\r\n\r\nMAIL=\/bin\/mail\r\n\r\nfor line in $(df -hP | egrep '^\/dev\/' | awk '{ print $1 \"_:_\" $5 }')\r\n  do\r\n    FILESYSTEM=$(echo \"$line\" | awk -F\"_:_\" '{ print $1 }')\r\n    DISK_USAGE=$(echo \"$line\" | awk -F\"_:_\" '{ print $2 }' | cut -d'%' -f1 )\r\n\r\n    if [ $DISK_USAGE -ge $VALUE ];\r\n    then\r\n      EMAIL=\"$SERVERNAME - $(date): $FILESYSTEM Exceeded the threshold VALUE\\n\"\r\n      EMAIL=\"$EMAIL\\n Usage Details\\n Current Usage:($DISK_USAGE%) Threshold value: ($VALUE%)\"\r\n      echo -e \"$EMAIL\" | $MAIL -s \"\"$SERVERNAME\" Disk Usage Alert: Needs Attention!\" \"$MAILID\"\r\n    elif [ $DISK_USAGE -lt $VALUE ];\r\n    then\r\n      EMAIL=\"$EMAIL\\n$FILESYSTEM ($DISK_USAGE%) is lessthan the threshold ($VALUE%)\"\r\n      EMAIL=\"$EMAIL\\n\"\r\n      echo -e \"$EMAIL\" | $MAIL -s \"\"$SERVERNAME\" Disk Usage Alert: Threshold Not Reached\" \"$MAILID\"\r\n  fi\r\ndone\r\n<\/pre>\n
\u76d1\u63a7\u811a\u672c\u8bf4\u660e<\/strong><\/div>\n

\u4e0a\u8ff0\u811a\u672c\u4f7f\u7528df -hP<\/code>\u547d\u4ee4\u83b7\u53d6\u5185\u5b58\u4fe1\u606f\u3002<\/p>\n

\u73af\u5883\u53d8\u91cfMAILID<\/code>\u4fdd\u5b58\u5fc5\u987b\u5728\u5176\u4e2d\u53d1\u9001\u62a5\u544a\u7684\u90ae\u7bb1\u5730\u5740\u3002<\/p>\n

VALUE<\/code> \u4fdd\u5b58\u8981\u8bbe\u7f6e\u7684\u9608\u503c\u3002<\/p>\n

SERVERNAME<\/code> \u4fdd\u5b58\u670d\u52a1\u5668\u7684\u4e3b\u673a\u540d\uff0c\u7528\u4e8e\u7535\u5b50\u90ae\u4ef6\u901a\u77e5\u4e2d\u7684\u670d\u52a1\u5668\u6807\u8bc6\u3002<\/p>\n

\u4f7f\u7528 for \u5faa\u73af\uff0c\u5b83\u5c06\u904d\u5386\u6240\u6709\u5177\u6709\u201c\/dev\u201d\u6302\u8f7d\u70b9\u7684\u6587\u4ef6\u7cfb\u7edf\u3002\u7136\u540e\u5b83\u5c06\u6587\u4ef6\u7cfb\u7edf\u540d\u79f0\u548c\u78c1\u76d8\u4f7f\u7528\u767e\u5206\u6bd4\u4fdd\u5b58\u5230\u4e24\u4e2a\u53d8\u91cfFILESYSTEM<\/code> \u548c DISK_USAGE<\/code>\u3002<\/p>\n

\u4f7f\u7528\u4e0a\u8ff0\u53d8\u91cf\uff0c\u78c1\u76d8\u4f7f\u7528\u91cf\u7b49\u4e8e\u9608\u503c VALUE<\/code> \u53d8\u91cf\u3002\u6839\u636e\u7ed3\u679c\uff0c\u5c06\u5411\u6307\u5b9a\u7684\u90ae\u7bb1\u53d1\u9001\u4e00\u5c01\u7535\u5b50\u90ae\u4ef6\u3002<\/p>\n

\u76d1\u63a7\u811a\u672c\u6267\u884c<\/strong><\/div>\n

\u4e0b\u9762\u9700\u8981\u4fee\u6539\u4e00\u4e0bdisk-monitor.sh<\/code>\u811a\u672c\u7684\u6743\u9650\uff0c\u4e3a\u5b83\u6dfb\u52a0\u53ef\u6267\u884c\u6743\u9650\uff1a<\/p>\n

\r\n[root@localhost ~]# chmod +x disk-monitor.sh\r\n<\/pre>\n

\u4f7f\u7528\u4e0b\u9762\u65b9\u5f0f\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n

\r\n[root@localhost ~]# .\/disk-monitor.sh\r\n<\/pre>\n
\u81ea\u52a8\u5316\u76d1\u63a7\u8fc7\u7a0b<\/strong><\/div>\n

\u624b\u52a8\u8fd0\u884c\u811a\u672c\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u81ea\u52a8\u8fd0\u884c\u8be5\u8fc7\u7a0b\u3002\u81ea\u52a8\u8fd0\u884c\u8be5\u8fc7\u7a0b\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u5c06\u811a\u672c\u6dfb\u52a0\u5230 crontab<\/code>\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002<\/p>\n

\u4f7f\u7528\u4e0b\u9762\u65b9\u5f0f\u6253\u5f00crontab\uff1a<\/p>\n

\r\n[root@localhost ~]# crontab -e\r\n<\/pre>\n

\u5c06\u4e0b\u9762\u5185\u5bb9\u6dfb\u52a0\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d\uff1a\n\n*\/5 * * * * sh \/home\/ec2-user\/disk-monitor.sh<\/p>\n

\"\"<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u5728\u6587\u7ae0\u4e2d\u6211\u4eec\u5199\u4e86\u4e00\u4e2a shell \u811a\u672c\uff0c\u5b83\u5728 crontab \u4e2d\u6307\u5b9a\u56fa\u5b9a\u65f6\u95f4\u95f4\u9694\u76d1\u63a7\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u5728\u62a5\u544a\u8fbe\u5230\u6307\u5b9a\u7684\u9608\u503c\u65f6\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u62a5\u544a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5199\u4e00\u4e2a shell \u811a\u672c\uff0c\u5b83\u5728 crontab \u4e2d\u6307\u5b9a\u56fa\u5b9a\u65f6\u95f4\u95f4\u9694\u76d1\u63a7\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u5728\u62a5\u544a\u8fbe\u5230 […]<\/p>\n","protected":false},"author":309,"featured_media":228578,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-228577","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/228577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=228577"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/228577\/revisions"}],"predecessor-version":[{"id":228582,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/228577\/revisions\/228582"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/228578"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=228577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=228577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=228577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}