{"id":158810,"date":"2019-09-30T10:00:11","date_gmt":"2019-09-30T02:00:11","guid":{"rendered":"https:\/\/gulass.cn\/?p=158810"},"modified":"2019-09-23T09:59:56","modified_gmt":"2019-09-23T01:59:56","slug":"bash-sar-cpu-memory","status":"publish","type":"post","link":"https:\/\/gulass.cn\/bash-sar-cpu-memory.html","title":{"rendered":"\u5229\u7528Bash\u811a\u672c\u4eceSAR \u62a5\u544a\u4e2d\u83b7\u53d6 CPU \u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n
\n\u5927\u591a\u6570 Linux \u7ba1\u7406\u5458\u4f7f\u7528 SAR \u62a5\u544a\u76d1\u63a7\u7cfb\u7edf\u6027\u80fd\uff0c\u56e0\u4e3a\u5b83\u4f1a\u6536\u96c6\u4e00\u5468\u7684\u6027\u80fd\u6570\u636e\u3002\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539 [neiqian]\/etc\/sysconfig\/sysstat[\/neiqian] \u6587\u4ef6\u8f7b\u677e\u5730\u5c06\u5176\u5ef6\u957f\u5230\u56db\u5468\u3002\u540c\u6837\uff0c\u8fd9\u6bb5\u65f6\u95f4\u53ef\u4ee5\u5ef6\u957f\u4e00\u4e2a\u6708\u4ee5\u4e0a\u3002\u5982\u679c\u8d85\u8fc7 28\uff0c\u90a3\u4e48\u65e5\u5fd7\u6587\u4ef6\u5c06\u653e\u5728\u591a\u4e2a\u76ee\u5f55\u4e2d\uff0c\u6bcf\u6708\u4e00\u4e2a\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u8981\u5c06\u8986\u76d6\u671f\u5ef6\u957f\u81f3 28 \u5929\uff0c\u8bf7\u5bf9\/etc\/sysconfig\/sysstat \u6587\u4ef6\u505a\u4ee5\u4e0b\u66f4\u6539\u3002
\n\u7f16\u8f91 sysstat \u6587\u4ef6\u5e76\u5c06 HISTORY=7 \u66f4\u6539\u4e3a HISTORY=28\u3002
\n\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e09\u4e2a bash \u811a\u672c\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e00\u4e2a\u5730\u65b9\u8f7b\u677e\u67e5\u770b\u6bcf\u4e2a\u6570\u636e\u6587\u4ef6\u7684\u5e73\u5747\u503c\u3002
\n\u6211\u4eec\u8fc7\u53bb\u52a0\u8fc7\u8bb8\u591a\u6709\u7528\u7684 shell \u811a\u672c\u3002\u5982\u679c\u4f60\u60f3\u67e5\u770b\u5b83\u4eec\uff0c\u8bf7\u8fdb\u5165\u4e0b\u9762\u7684\u94fe\u63a5\u3002<\/p>\n

\u5982\u4f55\u4f7f\u7528 shell \u811a\u672c\u81ea\u52a8\u5316\u65e5\u5e38\u64cd\u4f5c<\/strong><\/div>\n

\u8fd9\u4e9b\u811a\u672c\u7b80\u5355\u660e\u4e86\u3002\u51fa\u4e8e\u6d4b\u8bd5\u76ee\u7684\uff0c\u6211\u4eec\u4ec5\u5305\u62ec\u4e24\u4e2a\u6027\u80fd\u6307\u6807\uff0c\u5373 CPU \u548c\u5185\u5b58\u3002\u4f60\u53ef\u4ee5\u4fee\u6539\u811a\u672c\u4e2d\u7684\u5176\u4ed6\u6027\u80fd\u6307\u6807\u4ee5\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u3002<\/p>\n

\u811a\u672c 1\uff1a\u4ece SAR \u62a5\u544a\u4e2d\u83b7\u53d6\u5e73\u5747 CPU \u5229\u7528\u7387\u7684 Bash \u811a\u672c<\/strong><\/span><\/div>\n

\u8be5 bash \u811a\u672c\u4ece\u6bcf\u4e2a\u6570\u636e\u6587\u4ef6\u4e2d\u6536\u96c6 CPU \u5e73\u5747\u503c\u5e76\u5c06\u5176\u663e\u793a\u5728\u4e00\u4e2a\u9875\u9762\u4e0a\u3002
\n\u7531\u4e8e\u662f\u6708\u672b\uff0c\u5b83\u663e\u793a\u4e86 2019 \u5e74 8 \u6708\u7684 28 \u5929\u6570\u636e\u3002<\/p>\n

\r\n# vi \/opt\/scripts\/sar-cpu-avg.sh\r\n#!\/bin\/sh\r\necho \"+----------------------------------------------------------------------------------+\"\r\necho \"|Average:         CPU     %user     %nice   %system   %iowait    %steal     %idle  |\"\r\necho \"+----------------------------------------------------------------------------------+\"\r\nfor file in `ls -tr \/var\/log\/sa\/sa* | grep -v sar`\r\ndo\r\n    dat=`sar -f $file | head -n 1 | awk '{print $4}'`\r\n    echo -n $dat\r\n    sar -f $file  | grep -i Average | sed \"s\/Average:\/\/\"\r\ndone\r\necho \"+----------------------------------------------------------------------------------+\"\r\n<\/pre>\n

\u8fd0\u884c\u811a\u672c\u540e\uff0c\u4f60\u5c06\u770b\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n

\r\n# sh \/opt\/scripts\/sar-cpu-avg.sh\r\n+----------------------------------------------------------------------------------+\r\n|Average:         CPU     %user     %nice   %system   %iowait    %steal     %idle  |\r\n+----------------------------------------------------------------------------------+\r\n08\/01\/2019        all      0.70      0.00      1.19      0.00      0.00     98.10\r\n08\/02\/2019        all      1.73      0.00      3.16      0.01      0.00     95.10\r\n08\/03\/2019        all      1.73      0.00      3.16      0.01      0.00     95.11\r\n08\/04\/2019        all      1.02      0.00      1.80      0.00      0.00     97.18\r\n08\/05\/2019        all      0.68      0.00      1.08      0.01      0.00     98.24\r\n08\/06\/2019        all      0.71      0.00      1.17      0.00      0.00     98.12\r\n08\/07\/2019        all      1.79      0.00      3.17      0.01      0.00     95.03\r\n08\/08\/2019        all      1.78      0.00      3.14      0.01      0.00     95.08\r\n08\/09\/2019        all      1.07      0.00      1.82      0.00      0.00     97.10\r\n08\/10\/2019        all      0.38      0.00      0.50      0.00      0.00     99.12\r\n.\r\n.\r\n.\r\n08\/29\/2019        all      1.50      0.00      2.33      0.00      0.00     96.17\r\n08\/30\/2019        all      2.32      0.00      3.47      0.01      0.00     94.20\r\n+----------------------------------------------------------------------------------+\r\n<\/pre>\n
\u811a\u672c 2\uff1a\u4ece SAR \u62a5\u544a\u4e2d\u83b7\u53d6\u5e73\u5747\u5185\u5b58\u5229\u7528\u7387\u7684 Bash \u811a\u672c<\/strong><\/span><\/div>\n

\u8be5 bash \u811a\u672c\u4ece\u6bcf\u4e2a\u6570\u636e\u6587\u4ef6\u4e2d\u6536\u96c6\u5185\u5b58\u5e73\u5747\u503c\u5e76\u5c06\u5176\u663e\u793a\u5728\u4e00\u4e2a\u9875\u9762\u4e0a\u3002
\n\u7531\u4e8e\u662f\u6708\u672b\uff0c\u5b83\u663e\u793a\u4e86 2019 \u5e74 8 \u6708\u7684 28 \u5929\u6570\u636e\u3002<\/p>\n

# vi \/opt\/scripts\/sar-memory-avg.sh\r\n#!\/bin\/sh\r\necho \"+-------------------------------------------------------------------------------------------------------------------+\"\r\necho \"|Average:       kbmemfree kbmemused  %memused kbbuffers kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty  |\"\r\necho \"+-------------------------------------------------------------------------------------------------------------------+\"\r\nfor file in `ls -tr \/var\/log\/sa\/sa* | grep -v sar`\r\ndo\r\n    dat=`sar -f $file | head -n 1 | awk '{print $4}'`\r\n    echo -n $dat\r\n    sar -r -f $file  | grep -i Average | sed \"s\/Average:\/\/\"\r\ndone\r\necho \"+-------------------------------------------------------------------------------------------------------------------+\"<\/pre>\n

\u8fd0\u884c\u811a\u672c\u540e\uff0c\u4f60\u5c06\u770b\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n

\r\n# sh \/opt\/scripts\/sar-memory-avg.sh\r\n+--------------------------------------------------------------------------------------------------------------------+\r\n|Average:        kbmemfree kbmemused  %memused kbbuffers kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty  |\r\n+--------------------------------------------------------------------------------------------------------------------+\r\n08\/01\/2019      1492331   2388461     61.55     29888   1152142   1560615     12.72   1693031    380472         6\r\n08\/02\/2019      1493126   2387666     61.53     29888   1147811   1569624     12.79   1696387    373346         3\r\n08\/03\/2019      1489582   2391210     61.62     29888   1147076   1581711     12.89   1701480    370325         3\r\n08\/04\/2019      1490403   2390389     61.60     29888   1148206   1569671     12.79   1697654    373484         4\r\n08\/05\/2019      1484506   2396286     61.75     29888   1152409   1563804     12.75   1702424    374628         4\r\n08\/06\/2019      1473593   2407199     62.03     29888   1151137   1577491     12.86   1715426    371000         8\r\n08\/07\/2019      1467150   2413642     62.19     29888   1155639   1596653     13.01   1716900    372574        13\r\n08\/08\/2019      1451366   2429426     62.60     29888   1162253   1604672     13.08   1725931    376998         5\r\n08\/09\/2019      1451191   2429601     62.61     29888   1158696   1582192     12.90   1728819    371025         4\r\n08\/10\/2019      1450050   2430742     62.64     29888   1160916   1579888     12.88   1729975    370844         5\r\n.\r\n.\r\n.\r\n08\/29\/2019      1365699   2515093     64.81     29888   1198832   1593567     12.99   1781733    376157        15\r\n08\/30\/2019      1361920   2518872     64.91     29888   1200785   1595105     13.00   1784556    375641         8\r\n+-------------------------------------------------------------------------------------------------------------------+\r\n<\/pre>\n
\u811a\u672c 3\uff1a\u4ece SAR \u62a5\u544a\u4e2d\u83b7\u53d6 CPU \u548c\u5185\u5b58\u5e73\u5747\u5229\u7528\u7387\u7684 Bash \u811a\u672c<\/strong><\/span><\/div>\n

\u8be5 bash \u811a\u672c\u4ece\u6bcf\u4e2a\u6570\u636e\u6587\u4ef6\u4e2d\u6536\u96c6 CPU \u548c\u5185\u5b58\u5e73\u5747\u503c\u5e76\u5c06\u5176\u663e\u793a\u5728\u4e00\u4e2a\u9875\u9762\u4e0a\u3002
\n\u8be5\u811a\u672c\u4e0e\u4e0a\u9762\u76f8\u6bd4\u7a0d\u5fae\u4e0d\u540c\u3002\u5b83\u5728\u540c\u4e00\u4f4d\u7f6e\u540c\u65f6\u663e\u793a\u4e24\u8005\uff08CPU \u548c\u5185\u5b58\uff09\u5e73\u5747\u503c\uff0c\u800c\u4e0d\u662f\u5176\u4ed6\u6570\u636e\u3002<\/p>\n

# vi \/opt\/scripts\/sar-cpu-mem-avg.sh\r\n#!\/bin\/bash\r\nfor file in `ls -tr \/var\/log\/sa\/sa* | grep -v sar`\r\ndo\r\n    sar -f $file | head -n 1 | awk '{print $4}'\r\n    echo \"-----------\"\r\n    sar -u -f $file | awk '\/Average:\/{printf(\"CPU Average: %.2f%\\n\"), 100 - $8}'\r\n    sar -r -f $file | awk '\/Average:\/{printf(\"Memory Average: %.2f%\\n\"),(($3-$5-$6)\/($2+$3)) * 100 }'\r\n    printf \"\\n\"\r\ndone<\/pre>\n

\u8fd0\u884c\u811a\u672c\u540e\uff0c\u4f60\u5c06\u770b\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n

\r\n# sh \/opt\/scripts\/sar-cpu-mem-avg.sh\r\n08\/01\/2019\r\n-----------\r\nCPU Average: 1.90%\r\nMemory Average: 31.09%\r\n08\/02\/2019\r\n-----------\r\nCPU Average: 4.90%\r\nMemory Average: 31.18%\r\n08\/03\/2019\r\n-----------\r\nCPU Average: 4.89%\r\nMemory Average: 31.29%\r\n08\/04\/2019\r\n-----------\r\nCPU Average: 2.82%\r\nMemory Average: 31.24%\r\n08\/05\/2019\r\n-----------\r\nCPU Average: 1.76%\r\nMemory Average: 31.28%\r\n.\r\n.\r\n.\r\n08\/29\/2019\r\n-----------\r\nCPU Average: 3.83%\r\nMemory Average: 33.15%\r\n08\/30\/2019\r\n-----------\r\nCPU Average: 5.80%\r\nMemory Average: 33.19%\r\n<\/pre>\n

via: https:\/\/www.2daygeek.com\/linux-get-average-cpu-memory-utilization-from-sar-data-report\/<\/p>\n

\u4f5c\u8005\uff1aMagesh Maruthamuthu \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1ageekpi \u6821\u5bf9\uff1awxy<\/p>\n

\u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8981\u5c06\u8986\u76d6\u671f\u5ef6\u957f\u81f3 28 \u5929\uff0c\u8bf7\u5bf9\/etc\/sysconfig\/sysstat \u6587\u4ef6\u505a\u4ee5\u4e0b\u66f4\u6539\u3002 \u7f16\u8f91 syss […]<\/p>\n","protected":false},"author":1920,"featured_media":158813,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-158810","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\/158810","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\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=158810"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/158810\/revisions"}],"predecessor-version":[{"id":159087,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/158810\/revisions\/159087"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/158813"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=158810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=158810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=158810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}