{"id":32601,"date":"2024-11-25T11:54:07","date_gmt":"2024-11-25T03:54:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32601\/"},"modified":"2024-11-25T11:54:07","modified_gmt":"2024-11-25T03:54:07","slug":"%e5%b0%86transforming-a-wordpress-server-dashboard-into-a-widget","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32601\/","title":{"rendered":"\u5c06Transforming a WordPress Server Dashboard into a Widget"},"content":{"rendered":"<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679387331.png\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe\" \/><\/p>\n<p>  \u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u63d2\u4ef6\u7684\u57fa\u672c\u7ed3\u6784\u3002\u73b0\u5728\u662f\u65f6\u5019\u4e3a\u6bcf\u4e2a\u5c0f\u90e8\u4ef6\u5b9e\u73b0\u6e32\u67d3\u7c7b\u4e86\u3002 <\/p>\n<p>\u56de\u60f3\u4e00\u4e0b\uff0c\u6240\u6709\u5c0f\u90e8\u4ef6\u63d0\u4f9b\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5b9e\u73b0 Provider \u63a5\u53e3\u3002\u5b83\u4eec\u8fd8\u5fc5\u987b\u4f4d\u4e8e\u540d\u4e3a widget \u7684\u6587\u4ef6\u5939\u5185\uff0c\u5e76\u4f4d\u4e8e\u540d\u79f0\u7a7a\u95f4 AXStatBoardWidget \u4e0b\u3002\u5982\u679c\u6211\u4eec\u60f3\u6dfb\u52a0\u4e00\u79cd\u65b0\u7684\u6307\u6807\uff0c\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u76f8\u5e94\u7684\u7c7b\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5e76\u4f7f\u7528 add_provider \u65b9\u6cd5\u5c06\u5176\u6dfb\u52a0\u5230 Widget \u7c7b\u4e2d\u3002<\/p>\n<\/p>\n<h2>RAM \u4f7f\u7528\u60c5\u51b5\u5c0f\u90e8\u4ef6<\/h2>\n<p>\u6211\u4eec\u60f3\u8981\u663e\u793a\u7684\u7b2c\u4e00\u6761\u4fe1\u606f\u662f\u5f53\u524d\u6b63\u5728\u4f7f\u7528\u7684 RAM \u91cf\u4ee5\u53ca\u5f53\u524d\u7a7a\u95f2\u7684 RAM \u91cf\u3002<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0cfree -m \u662f\u6211\u4eec\u7684\u670b\u53cb &#8211; \u5b83\u544a\u8bc9\u6211\u4eec RAM \u4f7f\u7528\u60c5\u51b5\u3002 -m \u5f00\u5173\u662f\u4ee5\u5146\u5b57\u8282\u4e3a\u5355\u4f4d\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ free -m\n             total       used       free     shared    buffers     cached\nMem:           589        366        223          0          9         57\n-\/+ buffers\/cache:        299        290\nSwap:            0          0          0\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6211\u4eec\u5c06\u7c7b\u547d\u540d\u4e3a Ram\u3002\u76f8\u5e94\u7684\u6587\u4ef6\u5c06\u4e3a widget\/ram.php\u3002\u6211\u4eec\u5728\u8fd9\u91cc\u53ea\u662f\u7f16\u5199\u57fa\u7840\u5e76\u5b9e\u73b0 get_title \u3002<\/p>\n<\/p>\n<pre>&lt;?php namespace AXStatBoardWidget;\n\nclass Ram implements Provider {\n  function __construct() {\n  }\n\n  public function get_title() {\n    return \"Ram Usage\";\n  }\n\n?&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0 get_metric \u6765\u5b9e\u9645\u8fd0\u884c\u5fc5\u8981\u7684 shell \u547d\u4ee4\u5e76\u63d0\u53d6\u4fe1\u606f\u3002\u7a0d\u540e\u6211\u5c06\u89e3\u91ca get_metric \u7684\u66f4\u591a\u7ec6\u8282\u3002  <\/p>\n<pre>&lt;?php function get_metric() {\n    $df = `free -m | grep -E \"(Mem|Swap)\" | awk '{print $1, $2, $3, $4}'`;\n    $df = explode(\"\n\", $df);\n    if ( is_array( $df ) &amp;&amp; 2 &lt;= count( $df ) ) {\n      $df = array_map( function ( $line ) {\n        if ( empty( $line ) ) {\n          return;\n        }\n        $segment = preg_split( '\/s+\/', $line );\n\n        return array(\n          'type' =&gt; trim( $segment[0],\" :\" ),\n          'total' =&amp;gt; (int)$segment[1],\n          'used' =&amp;gt;  (int)$segment[2],\n          'free' =&amp;gt;  (int)$segment[3],\n        );\n      }, $df );\n      return $df;\n    }\n    return false;\n  }\n?&amp;gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u4eec\u6267\u884c\u547d\u4ee4 free -m | grep -E &#8220;\u5185\u5b58|\u4ea4\u6362&#8221; | awk &#8216;{print $1, $2, $3, $4}&#8217; .\u5b83\u7684\u8f93\u51fa\u770b\u8d77\u6765\u4e0e\u6b64\u7c7b\u4f3c\u3002 <\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ free -m | grep -E \"Mem|Swap\" | awk '{print $1, $2, $3, $4}'\nMem: 589 541 47\nSwap: 255 0 255\n[vagrant@vagrant-centos64 ~]$\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u4eec\u901a\u8fc7\u5c06\u884c\u62c6\u5206\u4e3a\u6570\u7ec4\u6765\u4f7f\u7528 PHP \u89e3\u6790\u6bcf\u4e00\u4f4d\u4fe1\u606f\u3002\u6211\u4eec\u4f7f\u7528 array_map \u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u884c\uff0c\u6211\u4eec\u7528\u7a7a\u683c\u5206\u9694\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684<br \/>\n<em>\u5173\u8054<\/em>\u6570\u7ec4\uff1a <\/p>\n<ul>\n<li> type\uff1a\u7b2c\u4e00\u4e2a\u5b57\u6bb5\n<\/p>\n<\/li>\n<li> total\uff1a\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\n<\/p>\n<\/li>\n<li> used\uff1a\u7b2c\u4e09\u4e2a\u5b57\u6bb5<\/li>\n<li> free\uff1a\u7b2c\u56db\u4e2a\u5b57\u6bb5\n<\/p>\n<\/li>\n<\/ul>\n<p>\u73b0\u5728\uff0c\u662f get_content \u7684\u65f6\u5019\u4e86\u3002  <\/p>\n<pre> public function get_content() {\n    $metric = $this-&amp;gt;get_metric();\n    $data = array(\n      array('Type', 'Used(MB)', 'Free(MB)')\n    );\n\n    foreach ($metric as $item) {\n      if (empty($item)) {\n        continue;\n      }\n      if ($item['type'] !== 'Mem' &amp;amp;&amp;amp; $item['type'] !== 'Swap') {\n        continue;\n      }\n      if ( 0 == ($item['free'] + $item['used'])) {\n        continue;\n      }\n\n      $data[] = array(\n        $item['type'],$item['used'], $item['free']\n        );\n    }\n    $data   =  json_encode($data);\n    echo <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236     EOD; } \u6211\u4eec\u4f7f\u7528\u5806\u79ef\u6761\u5f62\u56fe\u6765\u663e\u793a RAM \u4f7f\u7528\u60c5\u51b5\u3002  <\/p>\n<p>   \u9996\u5148\uff0c\u6211\u4eec\u8c03\u7528 get_metric() \u6765\u83b7\u53d6\u5fc5\u8981\u7684\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ea\u9700\u5faa\u73af\u5b83\u5e76\u683c\u5f0f\u5316\u5b83\u4ee5\u5339\u914d Google \u56fe\u8868\u6570\u636e\u8981\u6c42\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 json_encode \u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff08\u6216 JSON\uff09\u3002\u7136\u540e\uff0c\u6211\u4eec\u8f93\u51fa\u4e00\u4e2a HTML \u4ee3\u7801\uff0c\u5176\u4e2d\u5305\u542b div \u5143\u7d20\u6765\u4fdd\u5b58\u56fe\u8868\u5bf9\u8c61\u3002  <\/p>\n<p>   \u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u76f8\u5e94\u7684 Google Chart API \u5c06\u56fe\u8868\u6e32\u67d3\u5230 div \u5143\u7d20\u4e2d\u3002   <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679390461.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe1\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe1\" \/><\/p>\n<h2>\u5b89\u88c5\u7684\u8f6f\u4ef6<\/h2>\n<p> \u6211\u4eec\u5c06\u4ecb\u7ecd\u7684\u7b2c\u4e8c\u4e2a\u5c0f\u90e8\u4ef6\u662f\u663e\u793a\u5df2\u5b89\u88c5\u8f6f\u4ef6\u7684\u5c0f\u90e8\u4ef6\u3002\u5b83\u662f\u4e00\u4e2a\u5c0f\u90e8\u4ef6\uff0c\u65e8\u5728\u663e\u793a\u6211\u4eec\u5728\u670d\u52a1\u5668\u4e0a\u6709\u54ea\u4e9b\u5e38\u89c1\u8f6f\u4ef6\u5305\u4ee5\u53ca\u54ea\u4e2a\u7248\u672c\u3002 <\/p>\n<\/p>\n<p>   \u4f8b\u5982\uff0c\u6211\u4eec\u662f\u5426\u5b89\u88c5\u4e86 NodeJS\uff0c\u662f\u5426\u5b89\u88c5\u4e86 Ruby\uff1f\u6211\u4eec\u4f7f\u7528\u7684\u662f\u54ea\u4e2a\u7248\u672c\u7684 PHP\uff1f\u7b49\u7b49\u3002   <\/p>\n<p>   \u8ba9\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u521d\u59cb\u5185\u5bb9\u521b\u5efa widget\/software.php\uff1a  <\/p>\n<pre>&lt;?php namespace AXStatBoardWidget;\n\nclass Software implements Provider {\n  function __construct() {\n  }\n\n  public function get_title() {\n    return \"Installed Software\";\n  }\n  function get_metric() {\n    $cmds = array();\n\n    $package = array(\n      'php'   =&gt; '-v', \n      'node'  =&amp;gt; '-v',\n      'mysql' =&amp;gt; '-V', \n      'vim'   =&amp;gt; '--version',\n      'python' =&amp;gt; '-V', \n      'ruby'  =&amp;gt; '-v', \n      'java'  =&amp;gt; '-version',\n      'curl'  =&amp;gt; '-V');\n  \n    foreach ( $package as $cmd=&amp;gt;$version_query ) {\n      if ( NULL == $cmds[$cmd] = shell_exec( \"which $cmd\" ) ) {\n        $cmds[ $cmd ] = 'Not installed';\n        continue;\n      }\n      $version = shell_exec( \"$cmd $version_query\" );\n      $version = explode( \"\n\", $version );\n      if ( is_array( $version ) ) {\n        $version = array_shift( $version );\n      }\n      $cmds[ $cmd ] .= '&lt;br&gt;' . $version;\n    }\n    return $cmds;\n  }\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u56e0\u6b64\uff0c\u4e0e\u5f80\u5e38\u4e00\u6837\uff0c\u6211\u4eec\u6709 get_title \uff0c\u5b83\u53ea\u8fd4\u56de\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e get_metric()\uff0c\u6211\u4eec\u60f3\u77e5\u9053\u662f\u5426\u5b89\u88c5\u4e86\u7279\u5b9a\u7684\u8f6f\u4ef6\u3002\u5982\u679c\u6709\uff0c\u5219\u83b7\u53d6\u5176\u7248\u672c\u4fe1\u606f\u3002   <\/p>\n<p>   \u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u663e\u793a\u8f6f\u4ef6\u7248\u672c\u7684\u5f00\u5173\u521b\u5efa\u4e00\u4e2a\u547d\u4ee4\u6570\u7ec4\u3002\u4ee5PHP\u4e3a\u4f8b\uff0cphp -v \u663e\u793a\u7248\u672c\u4fe1\u606f\uff0cmysql &#8211;version \u663e\u793aMySQL\u4fe1\u606f\u3002   <\/p>\n<p>   \u5982\u679c\u547d\u4ee4\u8fd4\u56de\u4e14\u9519\u8bef\u6216\u672a\u627e\u5230\u547d\u4ee4\uff0c\u5219 shell_exec \u5c06\u8fd4\u56de false\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u8be5\u8f6f\u4ef6\u6ca1\u6709\u5b89\u88c5\uff1b\u5426\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u89e3\u6790\u7ed3\u679c\u4ee5\u663e\u793a\u7248\u672c\u4fe1\u606f\u3002\u7136\u540e\uff0c\u6211\u4eec\u9010\u884c\u5206\u5272\u7ed3\u679c\uff0c\u5e76\u68c0\u7d22\u7b2c\u4e00\u884c\u4f5c\u4e3a\u7248\u672c\u4fe1\u606f\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u7684\u4fe1\u606f\u53ea\u80fd\u5728\u7b2c\u4e00\u884c\u627e\u5230\u3002   <\/p>\n<p>   \u5bf9\u4e8e\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\uff0c\u67d0\u4e9b\u547d\u4ee4\u8fc7\u4e8e\u5197\u957f\uff0c\u5305\u542b\u8bb8\u591a\u4fe1\u606f\u3002\u4e00\u65e6\u6211\u4eec\u6709\u4e86\u6570\u636e\uff0c\u5c31\u53ef\u4ee5\u521b\u5efa get_content \u65b9\u6cd5\u4e86\u3002 <\/p>\n<\/p>\n<pre>  public function get_content() {\n  \n    $cmds = $this-&amp;gt;get_metric();\n    $content = '';\n    \n    foreach ( $cmds as $cmd =&amp;gt; $info ) {\n      $content .= \"&lt;p&gt;&lt;strong&gt;$cmd&lt;\/strong&gt;&nbsp; $info&lt;\/p&gt;\";\n    }\n    echo $content;\n    \n  }\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u4eec\u53ea\u662f\u5c55\u793a\u6b64\u7c7b\u6570\u636e\u7684\u57fa\u672c\u8868\u683c\u3002\u8fd9\u662f\u663e\u793a\u65f6\u7684\u4eea\u8868\u677f\uff1a <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679360995.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe2\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe2\" \/><\/p>\n<h3>\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5<\/h3>\n<p>\u73b0\u5728\u6211\u4eec\u5c06\u89e3\u51b3\u78c1\u76d8\u4f7f\u7528\u95ee\u9898\u3002\u6211\u4eec\u5c06\u5904\u7406\u6b64\u4efb\u52a1\u7684\u7c7b\u547d\u540d\u4e3a Disk\u3002\u8ba9\u6211\u4eec\u5148\u5236\u4f5c\u57fa\u672c\u9aa8\u67b6\u3002<\/p>\n<\/p>\n<pre>&lt;?php namespace AXStatBoardWidget;\n\nclass Disk implements Provider {\n  function __construct() {\n  }\n  public function get_title() {\n    return \"Disk Usage\";\n  }\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u4e0e\u5f80\u5e38\u4e00\u6837\uff0c\u6211\u4eec\u5fc5\u987b\u5b9e\u73b0 Provider \u63a5\u53e3\u3002\u6211\u4eec\u5728\u8fd9\u91cc\u4e3a\u6211\u4eec\u7684\u5c0f\u90e8\u4ef6\u8bbe\u7f6e\u4e00\u4e2a\u6807\u9898\u3002\u63a5\u4e0b\u6765\u662f\u6211\u4eec\u8bfe\u7a0b\u7684\u6838\u5fc3\uff1a\u83b7\u53d6\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u7684\u65b9\u6cd5\u3002 <\/p>\n<\/p>\n<pre>&lt;?php function get_metric() {\n    $df = `df -h`;\n    $df = explode(\"\n\", $df);\n    if (is_array($df) &amp;&amp; count($df)&gt;=2) {\n      array_shift($df); \/\/Get rid the first line\n      $df = array_map(function ($line) {\n        if (empty($line)) {\n          return NULL;\n        }\n        $segment=preg_split('\/s+\/', $line);\n\n        return array(\n          'filesystem' =&amp;gt; $segment[0],\n          'size' =&amp;gt; $segment[1],\n          'used' =&amp;gt; $segment[2],\n          'available' =&amp;gt; $segment[3],\n          'use_percent' =&amp;gt; $segment[4],\n        );\n      }, $df);\n      return $df;\n    }\n    return false;\n  }\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u5728\u672c\u7cfb\u5217\u7684\u7b2c\u4e00\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5bf9 df \u547d\u4ee4\u6709\u4e86\u4e00\u4e9b\u4e86\u89e3\uff0c\u56e0\u6b64\u7406\u89e3\u4ee5\u4e0b\u547d\u4ee4\u5e94\u8be5\u5f88\u5bb9\u6613\uff1a   <\/p>\n<p>   \u56de\u60f3\u4e00\u4e0b df \u8f93\u51fa\uff1a <\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ df -h\nFilesystem            Size  Used Avail Use% Mounted on\n\/dev\/sda1             7.3G  1.4G  5.6G  20% \/\ntmpfs                 295M     0  295M   0% \/dev\/shm\n\/vagrant               60G   55G  4.9G  92% \/vagrant\n\/data\/GeoIP            60G   55G  4.9G  92% \/data\/GeoIP\n\/var\/webapps           60G   55G  4.9G  92% \/var\/webapps\n\/var\/www\/html          60G   55G  4.9G  92% \/var\/www\/html\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u4eec\u5c06\u5176\u9010\u884c\u62c6\u5206\uff0c\u5c06\u5176\u53d8\u6210\u6570\u7ec4\u3002\u6211\u4eec\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u7528\u7a7a\u683c\u5206\u5272\u6574\u884c\uff0c\u518d\u6b21\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u503c\u6620\u5c04\u4e3a\u66f4\u53cb\u597d\u3001\u4eba\u7c7b\u53ef\u8bfb\u7684\u5173\u8054\u6570\u7ec4\u3002\u5f53\u6211\u4eec\u6709\u4e86\u8fd9\u4e9b\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u653e\u5165 get_content.   <\/p>\n<pre>  public function get_content() {\n    $metric = $this-&amp;gt;get_metric();\n    $data = array(\n      array( 'Disk', 'Space' )\n    );\n\n    $disk_container = array();\n    $data_partition = array(\n      array('Filesystem', 'Free(GB)', 'Used(GB)')\n    );    \n    foreach ( $metric as $disk ) {\n      $size = intval( $disk['size'] );\n      if ( 'M' == substr( $disk['size'], -1 ) ) {\n        $size = round( $size \/ 1024, 2 );\n      }\n      $used = intval( $disk['used'] );\n      if ('M' == substr( $disk['used'], -1 ) ) {\n        $used = round( $used \/ 1024, 2 );\n      }\n\n      if ( empty( $size ) ) {\n        continue;\n      }\n      $data[] = array( $disk['filesystem'], $size );\n      $data_partition[] = array($disk['filesystem'], $size - $used, $used);\n    }\n  }\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u4eec\u904d\u5386\u5ea6\u91cf\u6570\u7ec4\u5e76\u5c1d\u8bd5\u5c06 MB \u7a7a\u95f4\u8f6c\u6362\u4e3a GB \u7a7a\u95f4\u3002\u6211\u4eec\u6784\u5efa\u4e00\u4e2a\u6570\u7ec4\u6765\u5339\u914d\u56fe\u8868\u6570\u636e\u683c\u5f0f\u8981\u6c42\u3002\u6570\u636e\u6570\u7ec4\u5e94\u5982\u4e0b\u6240\u793a\uff1a <\/p>\n<\/p>\n<pre>[  ['File System', 'Free', 'Used',\n  ['\/dev\/sda1', 10, 24],\n  ['\/dev\/sda2', 28, 19]]\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u4e00\u65e6\u6211\u4eec\u6709\u4e86\u6570\u636e\uff0c\u6211\u4eec\u5c31\u5f00\u59cb\u6e32\u67d3\u56fe\u8868\u3002\u6211\u4eec\u5c06\u5236\u4f5c\u4e24\u4e2a\u56fe\u8868\uff1a   <\/p>\n<ol>\n<li>\u7b2c\u4e00\u4e2a\u56fe\u8868\u663e\u793a\u4e86\u6bcf\u4e2a\u5df2\u5b89\u88c5\u6587\u4ef6\u7cfb\u7edf\u7684\u603b\u7a7a\u95f4\u3002\u5bf9\u4e8e\u6b64\u6570\u636e\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u997c\u56fe\u3002\n<\/p>\n<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u56fe\u8868\u7528\u4e8e\u663e\u793a\u6bcf\u4e2a\u5df2\u5b89\u88c5\u6587\u4ef6\u7cfb\u7edf\u7684\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u6761\u5f62\u56fe\u3002\n<\/p>\n<\/li>\n<\/ol>\n<p>\u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u65b9\u6cd5\u4fee\u6539\u4e3a\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<pre>  public function get_content() {\n    $metric = $this-&amp;gt;get_metric();\n    $data = array(\n      array('Disk', 'Space')\n    );\n\n    $disk_container = array();\n    $data_partition = array(\n      array('Filesystem', 'Free(GB)', 'Used(GB)')\n    );    \n    foreach ($metric as $disk) {\n      $size = intval($disk['size']);\n      if ('M' == substr($disk['size'], -1)) {\n        $size = round($size \/ 1024, 2);\n      }\n      $used = intval($disk['used']);\n      if ('M' == substr($disk['used'], -1)) {\n        $used = round($used \/ 1024, 2);\n      }\n\n      if (empty($size)) {\n        continue;\n      }\n      $data[] = array($disk['filesystem'], $size);\n      $data_partition[] = array($disk['filesystem'], $size - $used, $used);\n    }\n    $data = json_encode($data);\n    $data_partition = json_encode($data_partition);\n\n    echo <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236      EOD; } \u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a div \u5143\u7d20\u6765\u5305\u542b\u4fe1\u606f  <\/p>\n<pre>      &lt;div id=\"widget_disk_usage\"&gt;&lt;\/div&gt;\n      &lt;div id=\"widget_disk_partion\"&gt;&lt;\/div&gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u7136\u540e\uff0c\u4f7f\u7528\u56fe\u8868 API \u7684\u7ed8\u5236\u65b9\u6cd5\u5c06\u56fe\u8868\u6e32\u67d3\u5728\u8fd9\u4e9b\u5143\u7d20\u5185\u3002\u8fd9\u91cc\u6700\u4ee4\u4eba\u56f0\u60d1\u7684\u4e8b\u60c5\u53ef\u80fd\u662f\u6211\u4eec\u56fe\u8868\u7684\u6570\u636e\u683c\u5f0f\u3002   <\/p>\n<p>   \u7ed3\u679c\u5982\u4e0b\uff1a <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679376628.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe3\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe3\" \/><\/p>\n<h2>\u670d\u52a1\u5668\u4fe1\u606f<\/h2>\n<p>\u8fd9\u4e2a\u5c0f\u90e8\u4ef6\u5411\u6211\u4eec\u663e\u793a\u4fe1\u606f\uff1aLinux \u5185\u6838\u3001CPU \u67b6\u6784\u3001\u6b63\u5e38\u8fd0\u884c\u65f6\u95f4\u3001IP \u5730\u5740\u3002\u6211\u4eec\u8fd9\u91cc\u4e0d\u9700\u8981\u56fe\u8868\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u8868\u5c31\u53ef\u4ee5\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\u3002\u8c03\u7528\u8be5\u7c7b\u662fServer\u3002\u8fd9\u662f widget\/server.php<\/p>\n<p> \u7684\u7b2c\u4e00\u4e2a\u5185\u5bb9  <\/p>\n<pre>&lt;?php namespace AXStatBoardWidget;\nuse DateTime;\n\nclass Server implements Provider {\n  function __construct() {\n  }\n\n  public function get_title() {\n    return \"Server Info\";\n  }\n  \n  \/**\n   * Return server info: OS, Kernel, Uptime, and hostname\n   * @return array with 3 metric:\n   *          * hostname\n   *          * os\n   *          * uptime\n   *\/\n  function get_metric() {\n    $server = array();\n    $server['hostname'] = `hostname`;\n    $server['os']       = `uname -sr`;\n    $server['core']     = `grep -c ^processor \/proc\/cpuinfo`;\n    $total_uptime_sec = time() - `cut -d. -f1 \/proc\/uptime`;\n    \n    $now = new DateTime(\"now\");\n    $server['uptime'] = $now-&gt;diff(new DateTime(\"@$total_uptime_sec\"))-&amp;gt;format('%a days, %h hours, %i minutes and %s seconds');\n\n    \/\/ Get the external ip with ifconfig.me, a website that show you ip address in plaintext\n    \/\/ when sending request with curl header\n    $server['ip'] = `curl ifconfig.me`;\n    $server['ram'] = `free -m | grep Mem | awk '{print $2}'`;\n    $server['cpu'] =`cat \/proc\/cpuinfo | grep \"model name\" | awk '{print $4,$5,$6,$7}'`;\n\n    return $server;\n  }\n\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u81f3\u6b64\uff0c\u60a8\u5e94\u8be5\u719f\u6089 get_title()\u3002\u6211\u4eec\u53ea\u662f\u8fd4\u56de\u8fd9\u4e2a\u5c0f\u90e8\u4ef6\u7684\u6807\u9898\u3002 <\/p>\n<\/p>\n<p>   \u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528\u8bed\u53e5 use DateTime \u56e0\u4e3a\u6211\u4eec\u4f4d\u4e8e\u540d\u79f0\u7a7a\u95f4 AXStatBoardWidget \u4e2d\uff0c\u5e76\u4e14 DateTime \u7c7b\u6765\u81ea\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002\u6bcf\u6b21\u6211\u4eec\u60f3\u8981\u4f7f\u7528 DateTime \u65f6\uff0c\u6211\u4eec\u90fd\u5fc5\u987b\u8f93\u5165 DateTime\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u5bfc\u5165\u6765\u4f7f DateTime \u540d\u79f0\u5728\u6211\u4eec\u5f53\u524d\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u53ef\u7528\u3002   <\/p>\n<p>   \u5c06\u5176\u89c6\u4e3a\u7b26\u53f7\u94fe\u63a5\u3002\u5728 get_metric \u4e2d\uff0c\u6211\u4eec\u8fd0\u884c shell \u547d\u4ee4\u6765\u83b7\u53d6\u7ed3\u679c\u5e76\u5c06\u5176\u5206\u914d\u56de\u6765\u3002   <\/p>\n<h3><b>\u4e3b\u673a\u540d<\/b><\/h3>\n<p>\u663e\u793a\u60a8\u7684\u670d\u52a1\u5668\u4e3b\u673a\u540d\u3002<\/p>\n<h3><b>\u540d\u79f0-sr<\/b><\/h3>\n<p>\u663e\u793aLinux\u5185\u6838\u4fe1\u606f\uff1a<\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ uname -sr\nLinux 2.6.32-358.23.2.el6.x86_64\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3><b>grep -c ^\u5904\u7406\u5668\/proc\/cpuinfo<\/b><\/h3>\n<p> -c \u5f00\u5173\u6253\u5370\u8f93\u5165\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\u884c\u7684\u8ba1\u6570\u3002 \/proc\/cpuinfo \u5305\u542b\u5904\u7406\u5668\u4fe1\u606f\u3002\u6211\u4eec grep \u5b83\u5e76\u8ba1\u7b97\u6587\u5b57\u5904\u7406\u5668\u7684\u51fa\u73b0\u6b21\u6570\u3002\u8fd9\u662f\u6211\u7684 32 \u6838\u7ed3\u679c\u3002<\/p>\n<\/p>\n<pre>$ grep -c ^processor \/proc\/cpuinfo\n32\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3> <b>\u526a\u5207-d\u3002 -f1 \/proc\/uptime<\/b> <\/h3>\n<\/p>\n<p>\u6b64\u547d\u4ee4\u663e\u793a\u670d\u52a1\u5668\u5df2\u542f\u52a8\u5e76\u8fd0\u884c\u7684\u79d2\u6570\u3002\u6211\u4eec\u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u201cx \u5929 y \u5c0f\u65f6 z \u5206\u949f\u201d\u7684\u683c\u5f0f\uff0c\u4ee5\u4f7f\u5176\u66f4\u52a0\u7528\u6237\u53cb\u597d\u3002 <\/p>\n<p>\u4f7f\u7528 DateTime::diff \u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u5f53\u524d\u65f6\u95f4\u6233\u7684 DateTime \u5bf9\u8c61\uff0c\u53e6\u4e00\u4e2a\u5e26\u6709\u65f6\u95f4\u6233\u7684\u5bf9\u8c61\u662f\u5f53\u524d\u65f6\u95f4\u6233\u51cf\u53bb\u6b63\u5e38\u8fd0\u884c\u65f6\u95f4\u7684\u79d2\u6570\u3002\u7136\u540e\u4f7f\u7528 format \u65b9\u6cd5\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u4eba\u7c7b\u53cb\u597d\u7684\u5b57\u7b26\u4e32\u3002 <\/p>\n<p>\u8fd9\u662f\u6211\u7684\u7ed3\u679c\uff0c\u6b63\u5e38\u8fd0\u884c\u65f6\u95f4\u4e3a 26194091 \u79d2\u3002<\/p>\n<\/p>\n<pre>$ cut -d. -f1 \/proc\/uptime\n26194091\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3> <b>\u5377\u66f2 ifconfig.me<\/b> <\/h3>\n<\/p>\n<p>ifconfig.me \u662f\u4e00\u9879\u5728\u6d4f\u89c8\u5668\u5185\u76f4\u63a5\u8bbf\u95ee\u65f6\u663e\u793a\u60a8\u7684 IP \u5730\u5740\u7684\u670d\u52a1\u3002\u5982\u679c\u60a8\u4f7f\u7528 curl \u5411\u5176\u53d1\u9001\u8bf7\u6c42\uff0c\u5b83\u5c06\u4ee5\u5355\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u8fd4\u56de\u60a8\u7684 IP \u5730\u5740\u3002<\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ curl ifconfig.me\n76.102.253.237\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>CPU\u578b\u53f7<\/h3>\n<p>\u5982\u4e0a\u6240\u8ff0\uff0c\/proc\/cpuinfo\u5b58\u50a8\u4e86CPU\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4ece\u4e2d\u63d0\u53d6CPU\u578b\u53f7\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ cat \/proc\/cpuinfo | grep \"model name\" | awk '{print $4,$5,$6,$7}'\nIntel(R) Core(TM) i5-4250U CPU\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u4e00\u65e6\u6211\u4eec\u5728\u6570\u7ec4\u4e2d\u83b7\u5f97\u4e86\u6240\u6709\u53ef\u7528\u6570\u636e\uff0c\u6211\u4eec\u5c31\u4f1a\u8fd4\u56de\u5b83\u5e76\u5411 get_content \u65b9\u6cd5\u63d0\u4f9b\u8fd9\u4e9b\u6570\u636e\u3002\u8fd9\u662f get_content\uff0c\u4ec5\u663e\u793a\u6570\u636e\uff1a <\/p>\n<\/p>\n<pre>  public function get_content() {\n    $server = $this-&amp;gt;get_metric();\n    echo Ip Address&nbsp;{$server['ip']}&lt;br&gt;&lt;strong&gt;CPU&lt;\/strong&gt;&nbsp; {$server['cpu']}&lt;br&gt;&lt;strong&gt;Number of Core&lt;\/strong&gt;&nbsp; {$server['core']}&lt;br&gt;&lt;strong&gt;Ram&lt;\/strong&gt;&nbsp; {$server['ram']}&lt;br&gt;&lt;strong&gt;Hostname&lt;\/strong&gt;&nbsp;{$server['hostname']}&lt;br&gt;&lt;strong&gt;OS&lt;\/strong&gt; {$server['os']}&lt;br&gt;&lt;strong&gt;Uptime&lt;\/strong&gt; {$server['uptime']}&lt;br&gt;\nEOD;\n  }\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u8fd9\u662f\u4eea\u8868\u677f\u4e0a\u7684\u5c0f\u90e8\u4ef6\u3002 <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679481709.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe4\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe4\" \/><\/p>\n<h2>\u5904\u7406\u5668<\/h2>\n<p>\u76d1\u63a7\u6211\u4eec\u7684\u5904\u7406\u5668\u662f\u5176\u4e2d\u4e4b\u4e00\u6211\u4eec\u53ef\u4ee5\u5c55\u793a\u7684\u6700\u91cd\u8981\u7684\u4e1c\u897f\u3002\u6211\u4eec\u60f3\u77e5\u9053\u67d0\u4e2a\u7279\u5b9a\u8fdb\u7a0b\u6b63\u5728\u4f7f\u7528\u591a\u5c11 CPU \u548c\/\u6216\u6d88\u8017\u4e86\u591a\u5c11\u5185\u5b58\u3002\u6211\u4eec\u5c06\u6211\u4eec\u7684\u7c7b\u79f0\u4e3a Process\uff0c\u9996\u5148\u4ece get_title \u548c get_metric \u5f00\u59cb\u3002\u6211\u5c06\u5728\u4ee3\u7801\u540e\u9762\u89e3\u91ca get_metric \u7684\u66f4\u591a\u7ec6\u8282\uff1a <\/p>\n<\/p>\n<pre>&lt;?php namespace AXStatBoardWidget;\n\nclass Process implements Provider {\n\n\n  public function get_title() {\n    return \"Processes\";\n  }\n\n  \/**\n   * Return server info: OS, Kernel, Uptime, and hostname\n   * @return array with 3 metric:\n   *          * hostname\n   *          * os\n   *          * uptime\n   *\/\n  function get_metric() {\n    $processes = array();\n    $output = `ps -eo pcpu,pmem,pid,user,args,time,start | grep -v '[' | sort -k 1 -r | head -30 | awk '{print $4,$3,$1,$2,$7,$6,$5}'`;\n    $output = explode(\"\n\", $output);\n    if (!is_array($output) || count($output)&lt;2) {\n      return false;      \n    }\n    array_shift($output);\n    foreach ($output as $line) {\n      \/\/$line = preg_split('\/s+\/', $line);\n      $line = explode(' ', $line);\n      if (count($line)&lt;6) {\n        continue;\n      }\n      \/\/var_dump($line);\n      \/\/echo count($line);\n      if (empty($processes[$line[6]])) {\n        $processes[$line[6]] = array_combine(array('user', 'pid', '%cpu', '%mem','start','time', 'command'), $line);\n      } else {\n        $processes[$line[6]]['%cpu'] += $line[2];\n        $processes[$line[6]]['%mem'] += $line[3];\n      }\n    }\n\n    return $processes;\n  }\n\n}\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u663e\u793a\u8fdb\u7a0b\u6b63\u5728\u8fd0\u884c\u7684\u547d\u4ee4\u662f ps\u3002\u5b83\u901a\u8fc7\u5f00\u5173 -e \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u4fe1\u606f\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u67e5\u770b\u6bcf\u4e2a\u8fdb\u7a0b\u3002\u5bf9\u4e8e\u6211\u4eec\u7684\u5c0f\u90e8\u4ef6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u63d0\u53d6 COU\u3001\u5185\u5b58\u3001PID\u3001\u7528\u6237\u3001\u53c2\u6570\u3001\u65f6\u95f4\u548c\u542f\u52a8\u3002   <\/p>\n<p>   \u6211\u4eec\u53ef\u4ee5\u7ed3\u5408 -o \u8868\u793a\u7528\u6237\u5b9a\u4e49\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a ps -eo pcpu,pmem,pid,user,args,time,start\u3002\u5982\u679c\u60a8\u5c1d\u8bd5\u8be5\u547d\u4ee4\uff0c\u60a8\u5c06\u5f97\u5230\u4e00\u4e9b\u5947\u602a\u7684\u8fc7\u7a0b\uff0c\u4f8b\u5982\uff1a <\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ ps -eo pcpu,pmem,pid,user,args,time,start\n%CPU %MEM   PID USER     COMMAND                         TIME  STARTED\n 0.0  0.2     1 root     \/sbin\/init                  00:00:00 06:50:39\n 0.0  0.0     2 root     [kthreadd]                  00:00:00 06:50:39\n 0.0  0.0     3 root     [migration\/0]               00:00:00 06:50:39\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6ce8\u610f [kthread]\u3001[migration\/0]\u3002\u57fa\u672c\u4e0a\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u547d\u4ee4\u65e0\u6cd5\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u627e\u5230\u3002\u5b83\u53ef\u80fd\u662f\u4e00\u4e9b\u5185\u90e8\u7cfb\u7edf\u8fdb\u7a0b\u6216\u5185\u6838\u7ebf\u7a0b\uff0c\u6211\u4eec\u53ef\u80fd\u6c38\u8fdc\u4e0d\u60f3\u5173\u5fc3\u5b83\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 grep \u6765\u6d88\u9664\u8fd9\u4e9b\u8fdb\u7a0b\u3002 grep \u6709 -v \u5f00\u5173\u4f7f\u6211\u4eec\u80fd\u591f\u53cd\u8f6c\u5339\u914d\u3002\u5b83\u8fd4\u56de\u7684\u7ed3\u679c\u4e0d\u5305\u542b\u6211\u4eec\u4f20\u9012\u7ed9\u5b83\u7684\u5b57\u7b26\u4e32\u3002 <\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ ps -eo pcpu,pmem,pid,user,args,time,start | grep -v '['\n%CPU %MEM   PID USER     COMMAND                         TIME  STARTED\n 0.0  0.2     1 root     \/sbin\/init                  00:00:00 06:50:39\n 0.0  0.1   292 root     \/sbin\/udevd -d              00:00:00 06:50:41\n 0.0  0.1   811 root     \/sbin\/dhclient -H vagrant-c 00:00:00 06:50:48\n 0.0  0.2   948 root     \/sbin\/rsyslogd -i \/var\/run\/ 00:00:00 06:50:50\n 0.0  0.1   966 rpc      rpcbind                     00:00:00 06:50:50\n 0.0  0.2   984 rpcuser  rpc.statd                   00:00:00 06:50:50\n 0.0  0.0  1011 root     rpc.idmapd                  00:00:00 06:50:51\n 0.0  0.2  1073 root     \/usr\/sbin\/VBoxService       00:00:00 06:50:51\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u4e3a\u4e86\u4f7f\u6570\u636e\u770b\u8d77\u6765\u4e0d\u9519\uff0c\u6211\u4eec\u5e94\u8be5\u6309\u5185\u5b58\u6216CPU\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u6211\u4eec\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u6309 %MEM \u6392\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Linux\u7684sort\u547d\u4ee4\u6765\u505a\u5230\u8fd9\u4e00\u70b9\u3002 %MEM \u662f\u7b2c\u4e8c\u5217\u3002   <\/p>\n<p>   \u5c31\u50cf\u7d22\u5f15\u4e3a\u96f6\u7684\u6570\u7ec4\u4e00\u6837\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u901a\u8fc7\u7d22\u5f15\u952e 1 \u8bbf\u95ee\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 sort -k 1\u3002\u5b83\u6309\u4ece\u6700\u4f4e\u5230\u6700\u9ad8\u7684\u987a\u5e8f\u6392\u5e8f\u3002\u6211\u4eec\u5b9e\u9645\u4e0a\u5173\u5fc3\u7684\u662f\u9996\u5148\u6d88\u8017\u5927\u91cf\u5185\u5b58\u7684\u8fdb\u7a0b\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 sort -k 1 -r \u53cd\u8f6c\u987a\u5e8f\u3002\u4e00\u65e6\u6211\u4eec\u5f97\u5230\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u80fd\u53ea\u9700\u8981\u524d30\u4e2a\u8fc7\u7a0b\u3002\u5f53\u7136\uff0c\u8fd9\u53d6\u51b3\u4e8e\u60a8\uff0c\u56e0\u4e3a\u60a8\u53ef\u4ee5\u9009\u62e9\u5305\u542b\u6240\u6709\u5185\u5bb9\uff0c\u4f46\u6211\u60f3\u4fdd\u6301\u7b80\u77ed\u3002 30 \u542c\u8d77\u6765\u662f\u4e00\u4e2a\u5408\u7406\u7684\u6570\u5b57\u3002   <\/p>\n<p>   \u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 awk \u6765\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u8fd9\u662f\u6211\u4eec\u7684\u547d\u4ee4\u548c\u793a\u4f8b\u8f93\u51fa\uff1a <\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ ps -eo pcpu,pmem,pid,user,args,time,start | grep -v '[' | sort -k 1 |  head -30 | awk '{print $4,$3,$1,$2,$7,$6,$5}'\nroot 1151 0.0 0.0 00:00:00 -d \/sbin\/udevd\nroot 1152 0.0 0.0 00:00:00 -d \/sbin\/udevd\nroot 292 0.0 0.0 00:00:00 -d \/sbin\/udevd\nroot 811 0.0 0.0 vagrant-c -H \/sbin\/dhclient\nroot 1 0.0 0.1 06:50:39 00:00:00 \/sbin\/init\nroot 2153 0.0 0.1 -q -1 \/sbin\/dhclient\nroot 3642 0.0 0.1 00:00:00 -s \/usr\/sbin\/anacron\nvagrant 3808 0.0 0.1 pcpu,pmem,pid,user,a -eo ps\nvagrant 3810 0.0 0.1 1 -k sort\nvagrant 3811 0.0 0.1 00:00:00 -30 head\nvagrant 3812 0.0 0.1 $4,$3,$1,$2,$7,$ {print awk\nroot 948 0.0 0.1 \/var\/run\/ -i \/sbin\/rsyslogd\nrpc 966 0.0 0.1 06:50:50 00:00:00 rpcbind\nroot 1073 0.0 0.2 06:50:51 00:00:00 \/usr\/sbin\/VBoxService\nroot 1105 0.0 0.2 06:50:51 00:00:00 \/usr\/sbin\/sshd\nroot 1121 0.0 0.2 06:50:52 00:00:00 crond\nrpcuser 984 0.0 0.2 06:50:50 00:00:00 rpc.statd\n496 1088 0.0 0.3 -p -d memcached\nvagrant 3544 0.0 0.3 00:00:00 vagrant@pts\/0 sshd:\nvagrant 3545 0.0 0.3 06:59:27 00:00:00 -bash\nroot 1113 0.0 1.7 06:50:52 00:00:00 \/usr\/sbin\/httpd\napache 1157 0.0 4.2 06:50:53 00:00:01 \/usr\/sbin\/httpd\napache 3438 0.0 4.2 06:55:39 00:00:01 \/usr\/sbin\/httpd\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u4e00\u65e6\u6211\u4eec\u5f97\u5230\u7ed3\u679c\uff0c\u6211\u4eec\u5c06\u5b83\u5206\u6210\u4e00\u4e2a\u6570\u7ec4\u5e76\u4f7f\u7528 foreach \u5faa\u73af\u5b83\u3002\u6211\u4eec\u5c06\u540c\u540d\u8fdb\u7a0b\u5206\u7ec4\u5230\u4e00\u4e2a\u5143\u7d20\u4e2d\uff0c\u5e76\u5c06 CPU \u767e\u5206\u6bd4\u548c\u5185\u5b58\u6dfb\u52a0\u5230\u5176\u4e2d\u3002  <\/p>\n<pre>&lt;?php \/\/...\n\/\/ inside get_content\n\n    foreach ( $output as $line ) {\n      \/\/$line = preg_split( '\/s+\/', $line );\n      $line = explode( ' ', $line );\n      if ( 6 &gt; count( $line ) ) {\n        continue;\n      }\n      if ( empty( $processes[ $line[6] ] ) ) {\n        $processes[ $line[6]] = array_combine( array( 'user', 'pid', '%cpu', '%mem','start','time', 'command' ), $line );\n      } else {\n        $processes[ $line[6] ]['%cpu'] += $line[2];\n        $processes[ $line[6] ]['%mem'] += $line[3];\n      }\n    }\n\/\/...    \n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u4eec\u4f7f\u7528 array_combine \u4ece\u4e24\u4e2a\u6570\u7ec4\u521b\u5efa\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\uff1a\u4e00\u4e2a\u7528\u4e8e\u952e\uff0c\u4e00\u4e2a\u7528\u4e8e\u503c\u3002   <\/p>\n<p>   \u5269\u4e0b\u7684\u5c31\u662f\u5b9e\u73b0 get_content \u65b9\u6cd5\u3002\u53ea\u662f\u63d0\u9192\u6211\u4eec\u5fc5\u987b\u5b9e\u73b0\u4e09\u4e2a\u65b9\u6cd5\uff1a get_title,<br \/>\n<b> <\/b>get_metric<br \/>\n<b> <\/b> \u548c get_content.\u5bf9\u4e8e\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ea\u60f3\u5c55\u793a\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u683c\u3002   <\/p>\n<p>   \u6211\u4eec\u7684 get_content \u65b9\u6cd5\u5f88\u7b80\u5355\u3002 <\/p>\n<pre>  public function get_content() {\n    $processes = $this-&amp;gt;get_metric();\n    $html = '<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<table>\n<thead>\n<tr>\n<th>User<\/th>\n<th>Pid<\/th>\n<th>%CPU<\/th>\n<th>%Mem<\/th>\n<th>Command<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n   &#8216;; foreach ($processes as $process) { $html .= &#8221; <\/p>\n<tr>\n<td>{$process[&#8216;user&#8217;]}<\/td>\n<td>{$process[&#8216;pid&#8217;]}<\/td>\n<td>{$process[&#8216;%cpu&#8217;]}<\/td>\n<td>{$process[&#8216;%mem&#8217;]}<\/td>\n<td>{$process[&#8216;command&#8217;]}<\/td>\n<\/tr>\n<p>&#8220;; } $html .= &#8216;<br \/>\n <\/tbody>\n<\/table>\n<p>&#8216;; echo $html; } \u8fd9\u662f\u6211\u4eec\u5f97\u5230\u7684\u7ed3\u679c\uff1a <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679471097.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe5\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe5\" \/><\/p>\n<h2>\u5e73\u5747\u8d1f\u8f7d<\/h2>\n<p> Linux \u6709\u4e00\u4e2a\u547d\u4ee4\u53ef\u4ee5\u663e\u793a\u8fc7\u53bb\u4e00\u5206\u949f\u3001\u4e94\u5206\u949f\u548c 15 \u5206\u949f\u5185 CPU \u548c IO \u7684\u5e73\u5747\u8d1f\u8f7d\u3002\u8ba9\u6211\u4eec\u628a\u5b83\u538b\u7f29\u6210\u4e00\u4e2a\u5c0f\u90e8\u4ef6\u3002\u79f0\u4e4b\u4e3a Cpuload\uff0c\u5e76\u521b\u5efa\u6211\u4eec\u7684 widget\/cpuload.php<\/p>\n<\/p>\n<pre>&lt;?php namespace AXStatBoardWidget;\n\nclass Cpuload implements Provider {\n  function __construct() {\n  }\n\n  public function get_title() {\n    return \"CPU Load\";\n  }\n  function get_metric() {    $number_of_core = intval(`\/bin\/grep -c processor \/proc\/cpuinfo`);    $loadAvg = `cat \/proc\/loadavg | \/usr\/bin\/awk '{print $1,$2,$3}'`;    $loadAvg = explode(' ', $loadAvg);    if ($loadAvg &lt;3) {      return false;    }    $loadTimes = array('1 min', '5 mins', '15 mins');    return array_map(      function ($loadtime, $value, $number_of_core) {        return array($loadtime, round($value * 100 \/ $number_of_core, 2), $value);      },        $loadTimes,        $loadAvg,        array_fill(0, 3, $number_of_core)      );  }\n\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u9996\u5148\uff0c\u6211\u4eec\u901a\u8fc7\u8bfb\u53d6 \/proc\/cpuinfo \u6765\u7edf\u8ba1 CPU \u6838\u5fc3\u6570\uff0c\u5e76\u7edf\u8ba1\u5305\u542b\u201cprocessor\u201d\u4e00\u8bcd\u7684\u884c\u6570\u3002\u6211\u4eec\u5728<br \/>\n<i>\u670d\u52a1\u5668\u4fe1\u606f<\/i> <\/p>\n<p>\u90e8\u5206\u5bf9\u6b64\u8fdb\u884c\u4e86\u4ecb\u7ecd\u3002 <\/p>\n<p>   \u5728Linux\u4e2d\uff0c\/proc\/loadavg\u4fdd\u5b58\u5e73\u5747\u8d1f\u8f7d\u4fe1\u606f\u3002\u524d\u4e09\u5217\u5206\u522b\u662f1\u5206\u949f\u30015\u5206\u949f\u548c15\u5206\u949f\u7684\u8d1f\u8f7d\u3002 awk \u5728\u8fd9\u91cc\u7528\u4e8e\u8fc7\u6ee4\u6389\u6211\u4eec\u9700\u8981\u7684\u5b57\u6bb5\u3002  <\/p>\n<pre>  ~  cat \/proc\/loadavg\n0.01 0.04 0.05 1\/217 16089\n  ~  cat \/proc\/loadavg | awk '{print $1, $2, $3}'\n0.01 0.04 0.05\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u5c06\u4e0a\u9762\u7684\u7ed3\u679c\u7528\u7a7a\u683c\u5206\u5272\uff0c\u6784\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u3002\u8ba1\u7b97\u6240\u6709\u6838\u5fc3\u7684\u5e73\u5747\u8d1f\u8f7d\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u83b7\u5f97\u7ed3\u679c\uff0c\u6211\u4eec\u4f7f\u7528 array_map \u5faa\u73af\u904d\u5386 $loadAvg \u6570\u7ec4\uff0c\u5e76\u9664\u4ee5\u6211\u4eec\u62e5\u6709\u7684\u6838\u5fc3\u6570\u3002\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u521b\u5efa\u4e86 2 \u4e2a\u4e0e $loadAvg \u957f\u5ea6\u76f8\u540c\u7684\u989d\u5916\u6570\u7ec4\uff0c\u4e00\u4e2a\u7528\u4e8e\u952e\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u4fdd\u5b58\u6838\u5fc3\u6570\uff0c\u4ee5\u4fbf\u5c06\u6240\u6709\u8fd9\u4e9b\u4e00\u4e00\u4f20\u9012\u7ed9 array_map\u3002   <\/p>\n<p>   get_content \u7684\u65f6\u95f4\uff1a <\/p>\n<\/p>\n<pre>  public function get_content() {\n    $metrics = $this-&amp;gt;get_metric();\n    if ( ! $metrics ) {\n      return false;\n    }\n    \/\/ see https:\/\/google-developers.appspot.com\/chart\/interactive\/docs\/gallery\/barchart#Data_Format for more detai of format\n    $data = array( array( 'Duration', '% Load' ) );\n    foreach ( $metrics as $key=&amp;gt;$metric ) {\n      array_push( $data, array( $metric[0], $metric[1] ) );\n    }\n    $data = json_encode( $data );\n    echo <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236     EOD; } \u6211\u4eec\u4f7f\u7528\u6761\u5f62\u56fe\u5e76\u4ece\u6570\u7ec4\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 json_encode \u5c06\u5176\u8f6c\u6362\u4e3a\u4e0e\u6761\u5f62\u56fe\u6570\u636e\u683c\u5f0f\u5339\u914d\u7684 JavaScript \u8868\u793a\u6cd5\u6570\u7ec4\u3002  <\/p>\n<p>   \u4f8b\u5982\uff1a <\/p>\n<\/p>\n<pre>[ [\"Duration\",\"% Load\"], [\"1 min\",20], [\"5 mins\",11], [\"15 mins\",3]]\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u8fd9\u662f\u6e32\u67d3\u56fe\u8868\u65f6\u7684\u7ed3\u679c\uff1a <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679455833.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe6\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe6\" \/><\/p>\n<h2>\u4ee5\u592a\u7f51\u63a5\u53e3<\/h2>\n<p>\u6211\u4eec\u8981\u5904\u7406\u7684\u4e0b\u4e00\u4e2a\u5c0f\u90e8\u4ef6\u662f\u4ee5\u592a\u7f51\u63a5\u53e3\u3002\u67d0\u4e9b\u670d\u52a1\u5668\u53ef\u4ee5\u6709\u591a\u4e2a\u4ee5\u592a\u7f51\u63a5\u53e3\uff0c\u5e76\u4e3a\u5176\u5206\u914d\u4e0d\u540c\u7684 IP \u5730\u5740\u3002 <\/p>\n<p>\u770b\u5230\u8fd9\u4e9b\u4fe1\u606f\u975e\u5e38\u6709\u7528\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u7c7b\u79f0\u4e3a Ethernet\uff0c\u4ece widget\/ethernet.php \u7684\u57fa\u672c\u5185\u5bb9\u5f00\u59cb\u3002<\/p>\n<pre>&lt;?php \/**\n * Adopt from https:\/\/github.com\/afaqurk\/linux-dash\/blob\/master\/sh\/ip.php\n *\n *\/\nnamespace AXStatBoardWidget;\n\nclass Ethernet implements Provider {\n  function __construct() {\n  }\n\n  public function get_title() {\n    return \"Ethernet\";\n  }\n\n  function get_metric() {\n    $ethernet = array();\n    $output = shell_exec(\"ip -oneline link show | awk '{print $2}' | sed 's\/:\/\/'\");\n    if (!$output) { \/\/ It didn't work with \"ip\" , so we do it with ifconfig\n      $output = shell_exec(\n        'ifconfig | \/bin\/grep -B1 \"inet addr\" | \/usr\/bin\/awk '' .\n        '{ if ( $1 == \"inet\" ) { print $2 }' .\n        'else if ( $2 == \"Link\" ) { printf \"%s:\",$1 } }' | \/usr\/bin\/awk' .\n        ' -F: '{ print $1\",\"$3 }''\n      );\n      $output = trim($output, \" \n\");\n      $output = `ifconfig | grep \"Link encap\" | awk '{ print $1 }'`;\n      $interfaces = explode(\"\n\", $output);\n      $output = `ifconfiga | grep \"inet addr\" | awk '{ print $2 }' | sed 's\/addr:\/\/'`;\n      $addreses = explode(\"\n\", $output);\n      $output = trim($output, \" \n\");\n      return array_combine($interfaces, $addreses);\n    }\n\n    $output = trim($output, \" \n\");\n    $interfaces = explode(\"\n\", $output);\n    $addreses = array();\n    foreach ($interfaces as $interface) {\n      $output = shell_exec(\"ip -oneline -family inet addr show $interface | awk '{print $4}' | cut -d'\/' -f1\");\n      $addreses[] = $output;\n    }\n    return array_combine($interfaces, $addreses);\n  }\n\n}\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6240\u4ee5\u5c0f\u90e8\u4ef6\u7684\u6807\u9898\u5c06\u662f<br \/>\n<i>\u4ee5\u592a\u7f51<\/i>\u3002\u5bf9\u4e8e get_metric\uff0c\u6211\u4eec\u5c06\u5c1d\u8bd5\u83b7\u53d6\u6240\u6709\u4ee5\u592a\u7f51\u63a5\u53e3\u540d\u79f0\uff0c\u7136\u540e\u83b7\u53d6\u6bcf\u4e2a\u63a5\u53e3\u7684 IP \u5730\u5740\uff0c\u5e76\u5c06\u8bbe\u5907\u540d\u79f0\u548c IP \u5730\u5740\u7ed3\u5408\u8d77\u6765\u8fd4\u56de\u3002   <\/p>\n<p>   \u6211\u4eec\u9700\u8981\u5904\u7406\u4e24\u79cd\u60c5\u51b5\uff1a\u5982\u679c\u670d\u52a1\u5668\u4f7f\u7528 ifconfig \u6216\u670d\u52a1\u5668\u4f7f\u7528 ip \u5b9e\u7528\u7a0b\u5e8f\u3002\u8f83\u65b0\u7684\u670d\u52a1\u5668\u5f88\u53ef\u80fd\u6709 ip \u800c\u4e0d\u662f ifconfig;&nbsp;\u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u9996\u5148\u8fd0\u884c ip \u6765\u83b7\u53d6\u4ee5\u592a\u7f51\u8bbe\u5907\u3002    <\/p>\n<pre>$output = shell_exec(\"ip -oneline link show | awk '{print $2}' | sed 's\/:\/\/'\");\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u4f7f\u7528 IP \u5b9e\u7528\u7a0b\u5e8f<\/h3>\n<p> \u5e26\u6709 ip \u547d\u4ee4\u548c -oneline \u5c06\u4ec5\u5728\u4e00\u884c\u4e2d\u663e\u793a\u8f93\u51fa\uff0c\u5176\u4e2d link \u548c show \u5c06\u5217\u51fa\u6240\u6709\u8bbe\u5907\u3002\u6211\u4eec\u4f7f\u7528 awk \u83b7\u53d6\u7b2c\u4e8c\u5217\uff0c\u5176\u4e2d\u5305\u542b\u8bbe\u5907\u540d\u79f0\uff1b\u4f46\u662f\u5b83\u5305\u542b : \u5b57\u7b26\u3002\u6211\u4eec\u4f7f\u7528 sed \u5c06 : \u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002 <\/p>\n<\/p>\n<p>   \u8fd9\u662f\u6211\u4eec\u5728\u547d\u4ee4\u884c\u4e0a\u8fd0\u884c\u5b83\u4eec\u65f6\u7684\u7ed3\u679c\uff1a <\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 sbin]$ ip -oneline link show\n1: lo: &lt;loopback&gt; mtu 16436 qdisc noqueue state UNKNOWN     link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n2: eth0: &lt;broadcast&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000    link\/ether 08:00:27:08:c2:e4 brd ff:ff:ff:ff:ff:ff\n3: eth1: &lt;broadcast&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000    link\/ether 08:00:27:eb:11:e4 brd ff:ff:ff:ff:ff:ff\n[vagrant@vagrant-centos64 sbin]$ ip -oneline link show | awk '{print $2}'\nlo:\neth0:\neth1:\n[vagrant@vagrant-centos64 sbin]$ ip -oneline link show | awk '{print $2}' | sed 's\/:\/\/'\nlo\neth0\neth1\n[vagrant@vagrant-centos64 sbin]$\n&lt;\/broadcast&gt;&lt;\/broadcast&gt;&lt;\/loopback&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u5982\u679c shell_exec \u6210\u529f\u8fd0\u884c\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528 IP \u5b9e\u7528\u7a0b\u5e8f\u3002\u4e0a\u9762\u7684\u8f93\u51fa\u88ab\u9010\u884c\u5206\u5272\u6210\u4e00\u4e2a\u6570\u7ec4\u3002  <\/p>\n<pre>$output = trim($output, \" \n\");\n$interfaces = explode(\"\n\", $output);\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u7136\u540e\u6211\u4eec\u5faa\u73af\u904d\u5386\u8be5\u6570\u7ec4\uff0c\u5e76\u518d\u6b21\u4f7f\u7528 ip \u547d\u4ee4 ip -oneline -family inet addr show device_name \u6765\u83b7\u53d6\u5206\u914d\u7ed9\u8bbe\u5907\u7684 IP \u5730\u5740\u3002  <\/p>\n<pre>    $addreses = array();\n    foreach ($interfaces as $interface) {\n      $output = shell_exec(\"ip -oneline -family inet addr show $interface | awk '{print $4}' | cut -d'\/' -f1\");\n      $addreses[] = $output;\n    }\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  IP \u5730\u5740\u51fa\u73b0\u5728\u7b2c\u56db\u5217\u4e2d\uff0c\u56e0\u6b64 awk \u7528\u4e8e\u6253\u5370\u8be5\u503c\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528 cut \u5c06\u547d\u4ee4\u6309 \/ \u5206\u5272\uff0c\u5e76\u4f7f\u7528 swich -f1 \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002   <\/p>\n<p>   \u67e5\u770b\u8f93\u51fa\uff0c\u770b\u770b\u5f53\u6211\u4eec\u5728\u547d\u4ee4\u884c\u4e0a\u8fd0\u884c\u5b83\u4eec\u65f6\uff0c\u5b83\u4eec\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1a <\/p>\n<pre>[vagrant@vagrant-centos64 sbin]$ ip -oneline -family inet addr show eth1\n3: eth1    inet 192.168.1.111\/24 brd 192.168.1.255 scope global eth1\n[vagrant@vagrant-centos64 sbin]$ ip -oneline -family inet addr show eth1 | awk '{print $4}'\n192.168.1.111\/24\n[vagrant@vagrant-centos64 sbin]$ ip -oneline -family inet addr show eth1 | awk '{print $4}' | cut -d'\/' -f1\n192.168.1.111\n[vagrant@vagrant-centos64 sbin]$\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u5f53\u6211\u4eec\u5728\u63a5\u53e3\u6570\u7ec4\u4e2d\u83b7\u53d6\u8bbe\u5907\u540d\u79f0\u5e76\u5728\u5730\u5740\u6570\u7ec4\u4e2d\u83b7\u53d6 IP \u5730\u5740\u65f6\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\uff0c\u5176\u4e2d\u63a5\u53e3\u540d\u79f0\u4f5c\u4e3a\u952e\uff0cIP \u5730\u5740\u4f5c\u4e3a\u503c\u3002  <\/p>\n<pre>return array_combine($interfaces, $addreses);\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u4f7f\u7528 ifconfig \u7684\u670d\u52a1\u5668<\/h3>\n<p>\u5728 ifconfig \u7684\u60c5\u51b5\u4e0b\uff0cip \u7684 shell_exec \u5c06\u8fd4\u56de false\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6539\u4e3a\u8fd0\u884c ifconfig\u3002   <\/p>\n<p>   \u6211\u4eec\u4e0a\u9762\u4ecb\u7ecd\u7684\u903b\u8f91\u5b8c\u5168\u76f8\u540c &#8211; \u5b83\u53ea\u662f\u83b7\u53d6\u4fe1\u606f\u7684\u4e0d\u540c\u5b9e\u7528\u7a0b\u5e8f\u3002  <\/p>\n<pre>    if (!$output) { \/\/ It didn't work with \"ip\" , so we do it with ifconfig\n      $output = `ifconfig | grep \"Link encap\" | awk '{ print $1 }'`;\n      $interfaces = explode(\"\n\", $output);\n      $output = `ifconfig | grep \"inet addr\" | awk '{ print $2 }' | sed 's\/addr:\/\/'`;\n      $addreses = explode(\"\n\", $output);\n      $output = trim($output, \" \n\");\n      return array_combine($interfaces, $addreses);\n    }\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u8ba9\u6211\u4eec\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4e0a\u9762\u7684\u547d\u4ee4\uff0c\u4ee5\u4fbf\u6211\u4eec\u4e86\u89e3\u53d1\u751f\u4e86\u4ec0\u4e48\u3002   <\/p>\n<p>   \u8bf7\u6ce8\u610f\uff0cifconfig \u76f4\u63a5\u5728\u8f93\u51fa\u4e2d\u663e\u793a IP \u5730\u5740\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5b83\u4eec\uff0c\u800c\u4e0d\u662f\u5728\u8bbe\u5907\u6570\u7ec4\u4e0a\u8fd0\u884c\u5faa\u73af\u5e76\u83b7\u53d6\u6bcf\u4e2a\u8bbe\u5907\u7684 IP \u5730\u5740\u3002 <\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 sbin]$ ifconfig\neth0      Link encap:Ethernet  HWaddr 08:00:27:08:C2:E4\n          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0\n          inet6 addr: fe80::a00:27ff:fe08:c2e4\/64 Scope:Link\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:4230 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:2575 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:444488 (434.0 KiB)  TX bytes:2288676 (2.1 MiB)\n\neth1      Link encap:Ethernet  HWaddr 08:00:27:EB:11:E4\n          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0\n          inet6 addr: fe80::a00:27ff:feeb:11e4\/64 Scope:Link\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:4470 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:2449 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:1689803 (1.6 MiB)  TX bytes:271675 (265.3 KiB)\n\nlo        Link encap:Local Loopback\n          inet addr:127.0.0.1  Mask:255.0.0.0\n          inet6 addr: ::1\/128 Scope:Host\n          UP LOOPBACK RUNNING  MTU:16436  Metric:1\n          RX packets:264 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:264 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:15840 (15.4 KiB)  TX bytes:15840 (15.4 KiB)\n\n[vagrant@vagrant-centos64 sbin]$ ifconfig | grep \"Link encap\"\neth0      Link encap:Ethernet  HWaddr 08:00:27:08:C2:E4\neth1      Link encap:Ethernet  HWaddr 08:00:27:EB:11:E4\nlo        Link encap:Local Loopback\n[vagrant@vagrant-centos64 sbin]$ ifconfig | grep \"Link encap\" | awk '{ print $1 }'\neth0\neth1\nlo\n\n[vagrant@vagrant-centos64 sbin]$ ifconfig | grep \"inet addr\"\n          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0\n          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0\n          inet addr:127.0.0.1  Mask:255.0.0.0\n[vagrant@vagrant-centos64 sbin]$ ifconfig | grep \"inet addr\" | awk '{ print $2 }'\naddr:10.0.2.15\naddr:192.168.1.111\naddr:127.0.0.1\n[vagrant@vagrant-centos64 sbin]$ ifconfig | grep \"inet addr\" | awk '{ print $2 }' | sed 's\/addr:\/\/'\n10.0.2.15\n192.168.1.111\n127.0.0.1\n[vagrant@vagrant-centos64 sbin]$\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u4e00\u65e6\u6211\u4eec\u6709\u4e86\u6570\u636e\uff0c\u5c06\u5b83\u4eec\u653e\u5165 get_content \u4e2d\u5c31\u5f88\u5bb9\u6613\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u8fd9\u91cc\u53ea\u663e\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u683c\u3002 <\/p>\n<\/p>\n<pre>  public function get_content() {\n    $interfaces = $this-&amp;gt;get_metric();\n    $html = '<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<table>\n<thead>\n<tr>\n<th>Interface<\/th>\n<th>IP<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n   &#8216;; foreach ( $interfaces as $interface =&gt; $ip ) { $html .= &#8221; <\/p>\n<tr>\n<td>{$interface}<\/td>\n<td>{$ip}<\/td>\n<\/tr>\n<p>&#8220;; } $html .= &#8216;<br \/>\n <\/tbody>\n<\/table>\n<p>&#8216;; echo $html; } \u4ee5\u4e0b\u662f\u5b83\u5411\u7ba1\u7406\u5458\u663e\u793a\u7684\u65b9\u5f0f\uff1a <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679417398.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe7\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe7\" \/><\/p>\n<h2>\u7f51\u7edc\u6d41\u91cf<\/h2>\n<p>\u7f51\u7edc\u6d41\u91cf\u6216\u7f51\u7edc IO \u663e\u793a\u901a\u8fc7\u8ba1\u7b97\u673a\u7f51\u7edc\u4f20\u8f93\u5305\u7684\u72b6\u6001\u3002\u8be5\u4fe1\u606f\u662f\u4ece netstat \u4e2d\u63d0\u53d6\u7684\u3002<\/p>\n<pre>[vagrant@vagrant-centos64 sbin]$ netstat -i\nKernel Interface table\nIface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg\neth0       1500   0     4828      0      0      0     2933      0      0      0 BMRU\neth1       1500   0     4806      0      0      0     2679      0      0      0 BMRU\nlo        16436   0      276      0      0      0      276      0      0      0 LRU\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u8ba9\u6211\u4eec\u5728\u6587\u4ef6 widget\/networkio.php \u4e2d\u83b7\u53d6\u6211\u4eec\u7684\u57fa\u672c\u7c7b Networkio <\/p>\n<\/p>\n<pre>&lt;?php \/**\n * Adopt from https:\/\/github.com\/afaqurk\/linux-dash\/blob\/master\/sh\/ip.php\n *\n *\/\nnamespace AXStatBoardWidget;\n\nclass Networkio implements Provider {\n  function __construct() {\n  }\n\n  public function get_title() {\n    return \"Network IO\";\n  }\n  function get_metric() {    $ethernet = array();       $output = `netstat -i | grep -v -E '(Iface|Interface)' | awk '{print $1\",\"$4\",\"$8}'`;    $lines = explode(\"\n\", $output);    foreach ($lines as $line) {      $line = explode(',', $line);      if (count($line)&lt;3) {        continue;      }      $ethernet[] = array($line[0], intval($line[1]), intval($line[2]));    }    return $ethernet;  }}\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u5c06\u5728\u672c\u6587\u540e\u9762\u89e3\u91ca\u5b83\u4eec\u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5c1d\u8bd5\u8bc4\u4f30\u6211\u4eec\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u547d\u4ee4\u3002   <\/p>\n<p>   \u6211\u5c06\u5c1d\u8bd5\u8fd0\u884c\u591a\u4e2a\u547d\u4ee4\uff0c\u4ee5\u4fbf\u5728\u6211\u4eec\u5c06\u7ed3\u679c\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u7ba1\u9053\u540e\u60a8\u53ef\u4ee5\u7406\u89e3\u5176\u4e2d\u7684\u5dee\u5f02\u3002 <\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 sbin]$ netstat -i\nKernel Interface table\nIface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg\neth0       1500   0     5727      0      0      0     3400      0      0      0 BMRU\neth1       1500   0     5004      0      0      0     2797      0      0      0 BMRU\nlo        16436   0      292      0      0      0      292      0      0      0 LRU\n\n[vagrant@vagrant-centos64 sbin]$ netstat -i | grep -v -E '(Iface|Interface)'\neth0       1500   0     5736      0      0      0     3405      0      0      0 BMRU\neth1       1500   0     5004      0      0      0     2797      0      0      0 BMRU\nlo        16436   0      292      0      0      0      292      0      0      0 LRU\n\n[vagrant@vagrant-centos64 sbin]$ netstat -i | grep -v -E '(Iface|Interface)' | awk '{print $1\",\"$4\",\"$8}'\neth0,5760,3420\neth1,5004,2797\nlo,292,292\n[vagrant@vagrant-centos64 sbin]$\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   netstat \u8fd4\u56de\u5f88\u591a\u4e1c\u897f\uff0c\u6211\u4eec\u4f7f\u7528 grep \u6765\u6392\u9664\u5305\u542b\u5355\u8bcd<br \/>\n<b>Iface<\/b> \u6216<br \/>\n<b>Kernel<\/b> \u7684\u884c\uff08\u524d\u4e24\u884c\uff09\uff0c\u6211\u4eec\u53ea\u5173\u5fc3\u6700\u540e\u4e09\u884c\u662f\u6211\u4eec\u7684\u4ee5\u592a\u7f51\u8bbe\u5907\u53ca\u5176\u5305\u4f20\u8f93\u3002 awk \u7528\u4e8e\u6253\u5370\u51fa\u7b2c\u4e00\u3001\u56db\u3001\u516b\u5217\u7684\u6570\u636e\uff0c\u5206\u522b\u8868\u793a\u63a5\u53e3\u540d\u79f0\u3001RX-OK\u3001TX-OK\u3002 <\/p>\n<p>\u5728\u6211\u4eec\u7684get_metric\u4e2d\uff0c\u6211\u4eec\u5c06\u7ed3\u679c\u9010\u884c\u62c6\u5206\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u56e0\u4e3a\u6bcf\u4e00\u884c\u90fd\u5305\u542b\u7528\u9017\u53f7\u5206\u9694\u7684\u6570\u636e\uff0c\u6240\u4ee5\u5b83\u4eec\u88ab\u518d\u6b21\u5206\u5272\u6210\u4e00\u4e2a\u6570\u7ec4\u3002 <\/p>\n<\/p>\n<p>   \u6211\u4eec\u786e\u4fdd\u53ea\u63a5\u53d7\u5177\u6709\u4e09\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u3002\u6211\u4eec\u8fd8\u5c1d\u8bd5\u5c06\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u503c\u3002\u6570\u636e\u5c06\u4f9d\u6b21\u8f93\u5165 get_content\u3002 <\/p>\n<\/p>\n<pre>  public function get_content() {\n    $interfaces = $this-&amp;gt;get_metric();\n    $data = array_merge(array(array('Interface', 'Receive(package)', 'Transfer(package)')), $interfaces);\n    $data = json_encode($data); \n    echo <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236     EOD; } \u6211\u4eec\u4e4b\u524d\u4f7f\u7528\u4e86\u6761\u5f62\u56fe\uff0c\u5e76\u5c1d\u8bd5\u5c06\u6307\u6807\u6570\u636e\u6570\u7ec4\u683c\u5f0f\u5316\u4e3a\u6761\u5f62\u56fe\u6570\u636e\u683c\u5f0f\uff0c\u7136\u540e\u8fdb\u884c\u6e32\u67d3\u3002  <\/p>\n<p>   \u6570\u636e\u6570\u7ec4\u7684\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u7ec4\u6761\u5f62\u56fe\uff0c\u5176\u4e2d\u5305\u542b\u6761\u5f62\u56fe\u540d\u79f0\u53ca\u5176\u76f8\u5e94\u7684\u503c\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e00\u884c\u90fd\u662f\u63a5\u53e3\u540d\u79f0\u4ee5\u53ca RX \u680f\u548c TX \u680f\u3002   <\/p>\n<p>   \u8fd9\u662f\u6211\u4eec\u5f97\u5230\u7684\uff1a <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679461194.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe8\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe8\" \/><\/p>\n<h3>\u8f93\u5165\/\u8f93\u51fa\u7edf\u8ba1<\/h3>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5904\u7406 io stat\u3002 IO \u8868\u793a\u8f93\u5165\/\u8f93\u51fa\u3002\u6211\u4eec\u5c06\u4e86\u89e3\u6bcf\u79d2\u6267\u884c\u591a\u5c11\u6b21\u8bfb\/\u5199\u64cd\u4f5c\u3002\u6211\u4eec\u8fd8\u5904\u7406 io_wait\u3002 IO\u7b49\u5f85\u662fCPU\u7a7a\u95f2\u7b49\u5f85\u4ece\u786c\u76d8\u8bfb\u53d6\u7ed3\u679c\u7684\u65f6\u95f4\u3002 <\/p>\n<p>\u6bd4\u5982\u4f60\u6b63\u5728\u8bfb\u53d6MySQL\u6570\u636e\uff0cCPU\u4f1a\u7a7a\u95f2\u4e0b\u6765\u7b49\u5f85\u7ed3\u679c\u3002 io wait \u63091\u79d2\u62161000\u6beb\u79d2\u8ba1\u7b97\u3002\u5982\u679c\u60a8\u7684\u4ee3\u7801\u9700\u8981 100 \u6beb\u79d2\u4ece\u786c\u76d8\u8bfb\u53d6\u6570\u636e\uff0c\u5219 io_wait \u4e3a 100\/1000 = 10%\u3002 IO \u7b49\u5f85\u8d8a\u5c11\uff0c\u7cfb\u7edf\u6027\u80fd\u8d8a\u597d\u3002 <\/p>\n<\/p>\n<p>\u4e3a\u4e86\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u8bf7\u786e\u4fdd\u7cfb\u7edf\u4e0a\u6709 <b>sysstat<\/b> \u8f6f\u4ef6\u5305\u3002 <\/p>\n<ol>\n<li>\u5bf9\u4e8e Arch Linux\uff0c\u4f7f\u7528 pacman -S sysstat \u5b89\u88c5\n<\/p>\n<\/li>\n<li>\u5bf9\u4e8e Debian\/Ubuntu\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 apt-get install sysstat \u83b7\u53d6\u5b83\u4eec\n<\/p>\n<\/li>\n<li>\u5bf9\u4e8e Fedora\/Centos\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 yum install sysstat <\/li>\n<li>\u5bf9\u4e8e\u5176\u4ed6\u53d1\u884c\u7248\uff0c\uff1a\u8bf7\u4f7f\u7528\u60a8\u7684\u53d1\u884c\u7248\u5305\u7ba1\u7406\u5668\u8fdb\u884c\u5b89\u88c5<\/li>\n<\/ol>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8ba9\u6211\u4eec\u8bc4\u4f30\u4e00\u4e0b\u6211\u4eec\u5c06\u4f7f\u7528\u7684\u4e00\u4e9b\u547d\u4ee4\u3002\u9996\u5148\u662f\u7b2c\u4e00\u4ef6\u4e8b\uff1a<\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 sbin]$ iostat\nLinux 2.6.32-358.23.2.el6.x86_64 (vagrant-centos64.vagrantup.com)     04\/27\/2014 \t_x86_64_\t(1 CPU)\n\navg-cpu:  %user   %nice %system %iowait  %steal   %idle\n           0.05    0.00    0.25    0.04    0.00   99.66\n\nDevice:            tps   Blk_read\/s   Blk_wrtn\/s   Blk_read   Blk_wrtn\nsda               0.18         7.62         1.04     157826      21584\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u7b2c\u56db\u884c\u5305\u542bIO\u72b6\u6001\u6570\u636e\u3002\u6211\u4eec\u5bf9\u7b2c\u56db\u5217\u4e2d\u7684 iowait \u503c\u611f\u5174\u8da3\u3002\u4ece\u7b2c\u4e03\u884c\u5f00\u59cb\u7684\u6570\u636e\u5305\u542b\u786c\u76d8\u9a71\u52a8\u5668\u6bcf\u79d2\u7684\u8bfb\/\u5199\u5757\u3002   <\/p>\n<p>   \u5982\u679c\u60a8\u6709\u8bb8\u591a\u786c\u76d8\u8fde\u63a5\u5230\u670d\u52a1\u5668\uff0c\u60a8\u5c06\u62e5\u6709\u591a\u4e2a\u8bbe\u5907\uff1asdb\u3001sdc \u7b49\u3002   <\/p>\n<p>   \u6570\u636e\u662f\u5757\u7684\u6570\u91cf\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u5927\u5c0f\uff08\u4ee5\u5146\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002\u6211\u4eec\u5fc5\u987b\u627e\u5230\u5757\u5927\u5c0f\u6765\u8ba1\u7b97\u603b\u5927\u5c0f\u3002   <\/p>\n<p>   \u5757\u5927\u5c0f\u5b58\u50a8\u5728\/sys\/block\/sda\/queue\/physical_block_size<br \/>\n<b>\u4e2d\u3002 <\/b> <\/p>\n<\/p>\n<pre>[vagrant@vagrant-centos64 ~]$ cat \/sys\/block\/sda\/queue\/physical_block_size\n512\n[vagrant@vagrant-centos64 ~]$\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6240\u4ee5\u6211\u7684sda\u7684\u5757\u5927\u5c0f\u662f512\u3002\u6211\u4eec\u5c06\u6bcf\u79d2\u8bfb\u53d6\u7684\u5757\u6570\u4e0e\u5757\u5927\u5c0f\u76f8\u4e58\uff0c\u5f97\u5230\u8bfb\/\u5199\u6570\u636e\u7684\u5b9e\u9645\u5927\u5c0f\u3002 <\/p>\n<p>\u6709\u4e86\u4e0a\u9762\u7684\u57fa\u672c\u77e5\u8bc6\uff0c\u8ba9\u6211\u4eec\u5728 widget\/iostat.php \u4e2d\u521b\u5efa\u6211\u4eec\u7684\u7c7b Iostat \u3002 <\/p>\n<\/p>\n<pre>&lt;?php namespace AXStatBoardWidget;\n\nclass Iostat implements Provider {\n  function __construct() {\n  }\n\n  public function get_title() {\n    return \"Disk IO\";\n  }\n\n\n  \/**\n   * Make sure we install package sysstat\n   * yum install sysstat\n   * or apt-get install sysstat\n   *\n   * Return IO Stat information. CPU waiting time, disk read\/write\n   *\n   *\/\n  function get_metric() {\n    $metric = array();\n\n    $output = `iostat`;\n    $number_of_core = intval(`\/bin\/grep -c processor \/proc\/cpuinfo`);\n\n    $lines = explode(\"\n\", $output);\n    \/\/We should have more than  4 lines\n    if (!is_array($lines) || sizeof($lines)&lt;4) {\n      return false;\n    }\n    $avg_cpu = preg_split(\"\/s+\/\", $lines[3]);\n    $metric['cpu'] = array(\n      'user'    =&gt; floatval($avg_cpu[0]) * $number_of_core,\n      'system'  =&amp;gt; floatval($avg_cpu[2]) * $number_of_core,\n      'io_wait' =&amp;gt; floatval($avg_cpu[3]) * $number_of_core,\n      'other'   =&amp;gt; 100 - ($avg_cpu[0] + $avg_cpu[2] + $avg_cpu[3])\n    );\n    \n    if (sizeof($lines) &amp;gt;=7) {\n      for ($i=6,$l = sizeof($lines);$i floatval($line[2]) * $block_size \/ 1024,\n          'write' =&amp;gt; floatval($line[3]) * $block_size \/ 1024,\n        );\n\n      }  \n    }\n\n    return $metric;\n  }\n\n}\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  \u6211\u4eec\u53ea\u662f\u5c1d\u8bd5\u5c06\u6211\u4eec\u7684\u7406\u8bba\u5e94\u7528\u5230 PHP \u4ee3\u7801\u4e2d\u3002\u83b7\u53d6 iostat \u7684\u8f93\u51fa\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6bcf\u4e00\u884c\u90fd\u662f\u4e00\u4e2a\u5143\u7d20\u3002   <\/p>\n<p>   \u7b2c\u56db\u884c\u88ab\u7a7a\u683c\u5206\u5272\u5e76\u6620\u5c04\u5230\u5173\u8054\u6570\u7ec4\u4e2d\u3002\u7b2c\u4e03\u884c\u4e4b\u540e\u7684\u6240\u6709\u884c\u90fd\u653e\u5165\u53e6\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\u4e2d\uff0c\u5176\u4e2d\u952e\u662f\u8bbe\u5907\u540d\u79f0\uff08sda\uff0csdb\uff0csdc\uff0c..\uff09\uff0c\u5982\u679c\u4ee5\u5146\u5b57\u8282\u4e3a\u5355\u4f4d\u8bfb\u53d6\u548c\u5199\u5165\uff0c\u5219\u503c\u662f\u4e00\u4e2a\u6570\u7ec4\u3002   <\/p>\n<p>   \u4e00\u65e6\u6211\u4eec\u83b7\u5f97\u4e86\u6307\u6807\uff0c\u5c06\u5176\u8f93\u5165 get_content \u5e76\u6e32\u67d3\u6211\u4eec\u7684\u56fe\u8868\u3002 <\/p>\n<\/p>\n<pre>  public function get_content() {\n    $metric = $this-&amp;gt;get_metric();\n    $disk_io = array(\n      array('Disk', 'Read(MB)', 'Write(MB)'),\n    );\n    foreach ($metric['disk'] as $disk=&amp;gt;$stat) {\n      $disk_io[] = array($disk, $stat['read'], $stat['write']);\n    }\n    $disk_io = json_encode($disk_io);   \n  \n    $cpu_io = json_encode(array(\n      array('CPU Time', 'Percent'),\n      array('IO Wait', $metric['cpu']['io_wait']),\n    ));\n    \n    echo <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236      EOD; } \u5bf9\u4e8e\u78c1\u76d8 IO \u8bfb\u5199\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6761\u5f62\u56fe\u3002\u5bf9\u4e8e IO \u7b49\u5f85\uff0c\u6211\u4eec\u4f7f\u7528\u4eea\u8868\u56fe\u6765\u4f7f\u5176\u770b\u8d77\u6765\u66f4\u6f02\u4eae\u3002\u6211\u4eec\u8ba4\u4e3a 80-100 \u7684 IOwait \u662f\u4e00\u4e2a\u4e25\u91cd\u4e8b\u4ef6\uff0c\u5e76\u7528\u7ea2\u8272\u7a81\u51fa\u663e\u793a\u3002  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169374679428100.jpg\" class=\"aligncenter\" title=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe9\" alt=\"\u5c06Transforming a WordPress Server Dashboard into a Widget\u63d2\u56fe9\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c06Transforming a WordPress Server Dashboard into a Widget\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u63d2\u4ef6\u7684\u57fa\u672c\u7ed3\u6784\u3002\u73b0\u5728\u662f\u65f6\u5019\u4e3a\u6bcf\u4e2a\u5c0f\u90e8\u4ef6\u5b9e\u73b0\u6e32\u67d3\u7c7b\u4e86\u3002 \u56de\u60f3\u4e00\u4e0b\uff0c\u6240\u6709\u5c0f\u90e8\u4ef6\u63d0\u4f9b\u7a0b\u5e8f\u90fd\u5fc5\u987b\u5b9e\u73b0 Provider \u63a5\u53e3\u3002\u5b83\u4eec\u8fd8\u5fc5\u987b\u4f4d\u4e8e\u540d\u4e3a widget \u7684\u6587\u4ef6\u5939\u5185\uff0c\u5e76\u4f4d\u4e8e\u540d\u79f0\u7a7a\u95f4 AXStatBoardWidget \u4e0b\u3002\u5982\u679c\u6211\u4eec\u60f3\u6dfb\u52a0\u4e00\u79cd\u65b0\u7684\u6307\u6807\uff0c\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u76f8\u5e94\u7684\u7c7b\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5e76\u4f7f\u7528 add_provider \u65b9\u6cd5\u5c06\u5176\u6dfb\u52a0\u5230 Widget \u7c7b\u4e2d\u3002 RAM \u4f7f\u7528\u60c5\u51b5\u5c0f\u90e8\u4ef6 \u6211\u4eec\u60f3\u8981\u663e\u793a\u7684\u7b2c\u4e00\u6761\u4fe1\u606f\u662f\u5f53\u524d\u6b63\u5728\u4f7f\u7528\u7684 RAM \u91cf\u4ee5\u53ca\u5f53\u524d\u7a7a\u95f2\u7684 RAM \u91cf\u3002 \u5728\u672c\u4f8b\u4e2d\uff0cfree -m \u662f\u6211\u4eec\u7684\u670b\u53cb &#8211; \u5b83\u544a\u8bc9\u6211\u4eec RAM \u4f7f\u7528\u60c5\u51b5\u3002 -m \u5f00\u5173\u662f\u4ee5\u5146\u5b57\u8282\u4e3a\u5355\u4f4d\u8f93\u51fa\u7ed3\u679c\u3002 [vagrant@vagrant-centos64 ~]$ free -m total used free shared buffers cached Mem: 589 366 223 0 9 57 -\/+ buffers\/cache: 299 290 Swap: 0 0 0 \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u5c06\u7c7b\u547d\u540d\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32601","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32601"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32601\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}