psutilをインストール
1 |
<span class="nv">$ </span>pip <span class="nb">install</span> <span class="nt">-U</span> psutil |
CPU、メモリ、ディスク使用率をゲットするプログラム
resource.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="kn">import</span> <span class="nn">psutil</span> <span class="c1"># mem </span><span class="n">mem</span> <span class="o">=</span> <span class="n">psutil</span><span class="p">.</span><span class="n">virtual_memory</span><span class="p">()</span> <span class="k">print</span><span class="p">(</span><span class="n">mem</span><span class="p">.</span><span class="n">percent</span><span class="p">)</span> <span class="c1"># cpu </span><span class="n">cpu</span> <span class="o">=</span> <span class="n">psutil</span><span class="p">.</span><span class="n">cpu_percent</span><span class="p">(</span><span class="n">interval</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="n">cpu</span><span class="p">)</span> <span class="c1"># disk </span><span class="n">disk</span> <span class="o">=</span> <span class="n">psutil</span><span class="p">.</span><span class="n">disk_usage</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="n">disk</span><span class="p">.</span><span class="n">percent</span><span class="p">)</span> |
実行結果
1 2 3 4 |
<span class="nv">$ </span>python resource.py 23.9 0.0 18.4 |