{"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":"
\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
\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 \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 |