用Python實現系統監控
阿新 • • 發佈:2018-11-01
要求:
對(系統、CPU、記憶體、硬碟、使用者)進行實時監控
- 主函式(run.py):
import random import socket import platform import psutil from flask import Flask, render_template from datetime import datetime import getpass fla=Flask(__name__) fla.config['SECRET_KEY']=random._urandom(24) @fla.route('/') def sysinfo(): boot_time=psutil.boot_time() boot_time=datetime.fromtimestamp(boot_time) now_time=datetime.now() delta_time=now_time-boot_time delta_time=str(delta_time).split('.')[0] return render_template('sysinfo.html', master=socket.gethostname(), system=platform.system(), machine=platform.machine(), version=platform.version(), architecture=platform.architecture(), now_time=now_time, boot_time=boot_time, delta_time=delta_time ) @fla.route('/cpu/') def cpu(): C_times=psutil.cpu_times() return render_template('cpu.html', Phy_cpu=psutil.cpu_count(logical=False), Log_cpu=psutil.cpu_count(), user=C_times.user, system=C_times.system, nice=C_times.nice, iowait=C_times.iowait, ) @fla.route('/memory/') def memory(): info=psutil.virtual_memory() return render_template('memory.html', total=str(round(info.total / 1024 / 1024 / 1024)), available=str(round(info.available / 1024 / 1024 / 1024)), percent=str(round(info.percent)), used=str(round(info.used / 1024 / 1024 / 1024)), free=str(round(info.free / 1024 / 1024 / 1024)), buffers=str(round(info.buffers / 1024 / 1024)), cached=str(round(info.cached / 1024 / 1024 / 1024)) ) @fla.route('/disk/') def disk(): disk_info=psutil.disk_partitions() info1=disk_info[0] content1=psutil.disk_usage(info1.mountpoint) info2=disk_info[1] content2= psutil.disk_usage(info2.mountpoint) info3=disk_info[2] content3 = psutil.disk_usage(info3.mountpoint) info4=disk_info[3] content4 = psutil.disk_usage(info4.mountpoint) info5=disk_info[4] content5 = psutil.disk_usage(info5.mountpoint) return render_template('disk.html', device1=info1.device, mountpoint1=info1.mountpoint, total1=str(round(content1.total /1024 /1024 /1024)), used1=str(round(content1.used /1024 /1024 /1024)), percent1=str(round(content1.percent)), free1=str(round(content1.free /1024 /1024 /1024)), fstype1=info1.fstype, opts1=info1.opts, device2=info2.device, mountpoint2=info2.mountpoint, total2=str(round(content2.total / 1024 / 1024 / 1024)), used2=str(round(content2.used / 1024 / 1024 / 1024)), percent2=str(round(content2.percent)), free2=str(round(content2.free / 1024 / 1024 / 1024)), fstype2=info2.fstype, opts2=info2.opts, device3=info3.device, mountpoint3=info3.mountpoint, total3=str(round(content3.total / 1024 / 1024 / 1024)), used3=str(round(content3.used / 1024 / 1024 / 1024)), percent3=str(round(content3.percent)), free3=str(round(content3.free / 1024 / 1024 / 1024)), fstype3=info3.fstype, opts3=info3.opts, device4=info4.device, mountpoint4=info4.mountpoint, total4=str(round(content4.total / 1024 / 1024 / 1024)), used4=str(round(content4.used / 1024 / 1024 / 1024)), percent4=str(round(content4.percent)), free4=str(round(content4.free / 1024 / 1024 / 1024)), fstype4=info4.fstype, opts4=info4.opts, device5=info5.device, mountpoint5=info5.mountpoint, total5=str(round(content5.total / 1024 / 1024 / 1024)), used5=str(round(content5.used / 1024 / 1024 / 1024)), percent5=str(round(content5.percent)), free5=str(round(content5.free / 1024 / 1024 / 1024)), fstype5=info5.fstype, opts5=info5.opts, ) @fla.route('/user/') def user(): return render_template('user.html', user=getpass.getuser(), master=socket.gethostname(), now_time=datetime.now() ) fla.run()
- 模版頁面(base.html):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="../static/css/bootstrap.min.css"> <link rel="stylesheet" href="../static/css/main.css"> <script href="js/bootstrap.min.js"></script> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="index.html"></a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="#">Sys Info<span class="sr-only">(current)</span></a></li> <li><a href="/">系統</a></li> <li><a href="/cpu/">CPU</a></li> <li><a href="/memory/">記憶體</a></li> <li><a href="/disk/">硬碟</a></li> <li><a href="/user/">使用者</a></li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> <div class="col-xs-1"> </div> {% block content %} {% endblock %} <div class="col-xs-1"> </div> </body> </html>
- 系統頁面(sysinfo.html):
{% extends 'base.html' %} {% block content %} <div class="col-xs-9"> <table class="table table-bordered"> <tr > <th width="400">主機名</th> <th>{{master}}</th> </tr> <tr> <th>核心名稱</th> <th>{{system}}</th> </tr> <tr> <th>發行版本號</th> <th>{{machine}}</th> </tr> <tr> <th>核心版本</th> <th>{{version}}</th> </tr> <tr> <th>系統構架</th> <th>{{architecture}}</th> </tr> <tr> <th>現在時間</th> <th>{{now_time}}</th> </tr> <tr> <th>開機時間</th> <th>{{boot_time}}</th> </tr> <tr> <th>執行時間</th> <th>{{delta_time}}</th> </tr> </table> </div> {% endblock %}
- CPU頁面(cpu.html):
{% extends 'base.html' %}
{% block content %}
<div class="col-xs-9">
<table class="table table-bordered">
<tr >
<th width="400">物理CPU核心數</th>
<th>{{Phy_cpu}}</th>
</tr>
<tr>
<th>邏輯CPU核心數</th>
<th>{{Log_cpu}}</th>
</tr>
<tr>
<th>使用者</th>
<th>{{user}}</th>
</tr>
<tr>
<th>系統</th>
<th>{{system}}</th>
</tr>
<tr>
<th>空閒</th>
<th>{{}}</th>
</tr>
<tr>
<th>iowait</th>
<th>{{iowait}}</th>
</tr>
</table>
</div>
{% endblock %}
- 記憶體頁面(memory.html):
{% extends 'base.html' %}
{% block content %}
<div class="col-xs-9">
<table class="table table-bordered">
<tr >
<th width="400">記憶體大小</th>
<th>{{total}}G</th>
</tr>
<tr>
<th>可用記憶體</th>
<th>{{available}}G</th>
</tr>
<tr>
<th>記憶體佔用</th>
<th>{{percent}}%</th>
</tr>
<tr>
<th>以用記憶體</th>
<th>{{used}}G</th>
</tr>
<tr>
<th>空閒記憶體</th>
<th>{{free}}G</th>
</tr>
<tr>
<th>buffers</th>
<th>{{buffers}}M</th>
</tr>
<tr>
<th>cached</th>
<th>{{cached}}G</th>
</tr>
</table>
</div>
{% endblock %}
- 硬碟頁面(disk.html):
{% extends 'base.html' %}
{% block content %}
<div class="col-xs-9">
<table class="table table-bordered">
<tr >
<th width="300">裝置</th>
<th width="300">掛載點</th>
<th width="300">容量</th>
<th width="300">以用</th>
<th width="300">剩餘</th>
<th width="300">型別</th>
<th width="300">選項</th>
</tr>
<tr>
<th>{{device1}}</th>
<th>{{mountpoint1}}</th>
<th>{{total1}}G</th>
<th>{{used1}}G({{percent1}}%)</th>
<th>{{free1}}G</th>
<th>{{fstype1}}</th>
<th>{{opts1}}</th>
</tr>
<tr>
<th>{{device2}}</th>
<th>{{mountpoint2}}</th>
<th>{{total2}}G</th>
<th>{{used2}}G({{percent2}}%)</th>
<th>{{free2}}G</th>
<th>{{fstype2}}</th>
<th>{{opts2}}</th>
</tr>
<tr>
<th>{{device3}}</th>
<th>{{mountpoint3}}</th>
<th>{{total3}}G</th>
<th>{{used3}}G({{percent3}}%)</th>
<th>{{free3}}G</th>
<th>{{fstype3}}</th>
<th>{{opts3}}</th>
</tr>
<tr>
<th>{{device4}}</th>
<th>{{mountpoint4}}</th>
<th>{{total4}}G</th>
<th>{{used4}}G({{percent4}}%)</th>
<th>{{free4}}G</th>
<th>{{fstype4}}</th>
<th>{{opts4}}</th>
</tr>
<tr>
<th>{{device5}}</th>
<th>{{mountpoint5}}</th>
<th>{{total5}}G</th>
<th>{{used5}}G({{percent5}}%)</th>
<th>{{free5}}G</th>
<th>{{fstype5}}</th>
<th>{{opts5}}</th>
</tr>
</table>
</div>
{% endblock %}
- 使用者頁面(user.html):
{% extends 'base.html' %}
{% block content %}
<div class="col-xs-9">
<table class="table table-bordered">
<tr >
<th width="300">使用者名稱</th>
<th width="300">終端</th>
<th width="300">主機名</th>
<th width="300">登入時間</th>
</tr>
<tr>
<th>{{user}}</th>
<th>123</th>
<th>{{master}}</th>
<th>{{now_time}}</th>
</tr>
</table>
</div>
{% endblock %}
執行結果:
系統頁面:
CPU頁面:
記憶體頁面:
硬碟頁面:
使用者頁面:
對系統中的這些資訊主要用的還是(psutil
)函式