系统基础信息模块和文件对比模块

74 篇文章 0 订阅
26 篇文章 0 订阅

psutil
系统监控及进程管理且跨平台的系统管理库。
项目实战:Linux服务器主机监控脚本

from datetime import datetime
import psutil
try:
    import os
    os_uname = os.uname()
except Exception as e:
    import platform
    os_uname = platform.uname()
now_time = datetime.now()
boot_time = datetime.fromtimestamp(psutil.boot_time())
run_time = now_time - boot_time
hour,minutes,seconds = str(run_time).split(':')
print("""
        Linux 服务器监控

    主机名: %s
    系统名称: %s
    发行版本号: %s
    内核版本: %s
    系统架构:%s
    现在时间: %s
    开机时间:%s
    运行时间:%s 小时 %s 分钟 %s 秒
""" % (os_uname.node, os_uname.system, os_uname.release,
       os_uname.version, os_uname.machine, now_time, boot_time,
       hour, minutes, seconds ))

项目实战:磁盘监控并且输出表格

import psutil
import time
from prettytable import PrettyTable
# 当前登录的用户
user = psutil.users()
print('当前登录的用户: ',user)
# 电脑开机时间
boot_time = psutil.boot_time()
# 将时间戳转换成字符串时间
str_time = time.ctime(boot_time)
print('开机时间: ' ,str_time)
# cpu信息查看
cpu_count = psutil.cpu_count(logical=False)
print('物理cpu的个数:',cpu_count)
cpu_percent = psutil.cpu_percent(percpu=True)
print(cpu_percent)
# 内存信息查看
memory = psutil.swap_memory()
if memory.percent > 90:
    print('邮件报警')
    # send_mail
print(psutil.virtual_memory())
# 磁盘信息查看
# 实例化PrettyTable类
pt = PrettyTable(field_names=['device','mountpoint','fstype','opts'])
# 返回所有的磁盘信息
disks = psutil.disk_partitions()
for disk in disks:
    # 往表格中添加一行信息
    pt.add_row([disk.device,disk.mountpoint,disk.fstype,disk.opts])
# 元组命名
#     print(disk.device,disk.mountpoint,disk.fstype,disk.opts)
print(pt)

结合web网页
1:创建flask项目
2:编写项目文件

from flask import Flask,render_template
from datetime import datetime
import psutil
import os
import platform

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'
@app.route('/disk/')
def disk():
    disks = psutil.disk_partitions()
    return render_template('disk.html',disks = disks)
@app.route('/hostinfo/')
def hostinfo():
    try:
        os_uname = os.uname()
    except Exception as e:
        os_uname = platform.uname()
    now_time = datetime.now()
    boot_time = datetime.fromtimestamp(psutil.boot_time())
    run_time = now_time - boot_time
    return render_template('hostinfo.html',
                           system = os_uname.system,
                           now_time = now_time,
                           boot_time = boot_time,
                           run_time = run_time)
@app.route('/users/')
def users():
    users = psutil.users()
    return render_template('users.html',users = users)
if __name__ == '__main__':
    app.run()

编写html文件(bootstrap工具)
在bootstrap中有很多摸版可以直接使用:https://www.runoob.com/bootstrap/bootstrap-tutorial.html

nav.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<nav class="navbar navbar-inverse" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="/disk/">SysInfo</a>
        </div>
        <div>
            <ul class="nav navbar-nav">
                <li class="active"><a href="/hostinfo/">主机信息</a></li>
                <li><a href="#">进程信息</a></li>
                <li><a href="/disk/">磁盘信息</a></li>
                <li><a href="/users/">用户信息</a></li>
            </ul>
        </div>
    </div>
</nav>

</body>
</html>

disk.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入 Bootstrap  CSS-->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
{% include 'nav.html' %}
<h1 style="color: darkmagenta">磁盘信息监控</h1>
<table class="table table-striped">
    <tr>
        <td>设备名称</td>
        <td>挂载点</td>
        <td>文件系统类型</td>
        <td>挂载参数</td>
    </tr>
    {% for disk in disks %}
        <tr>
            <td>{{ disk.device }}</td>
            <td>{{ disk.mountpoint }}</td>
            <td>{{ disk.fstype }}</td>
            <td>{{ disk.opts }}</td>
        </tr>
    {% endfor %}

</table>
</body>
</html>

hostinfo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入 Bootstrap  CSS-->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
{% include 'nav.html' %}
<h1 style="color: darkmagenta">Linux服务器统计</h1>
<table class="table table-striped">
    <tr>
        <td>系统名称</td>
        <td>{{ system }}</td>
    </tr>
    <tr>
        <td>开机时间</td>
        <td>{{ boot_time }}</td>
    </tr>
    <tr>
        <td>当前时间</td>
        <td>{{ now_time }}</td>
    </tr>
    <tr>
        <td>开机时长</td>
        <td>{{ run_time }}</td>
    </tr>
</table>
</body>
</html>

users.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入 Bootstrap  CSS-->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
{% include 'nav.html' %}
<h1 style="color: darkmagenta">用户信息监控</h1>
<table class="table table-striped">
    <tr>
        <td>用户名</td>
        <td>登录的终端</td>
        <td>登录时间</td>
    </tr>
    {% for user in users %}
        <tr>
            <td>{{ user.name }}</td>
            <td>{{ user.host }}</td>
            <td>{{ user.started }}</td>
        </tr>
    {% endfor %}

</table>
</body>
</html>

运行主配置文件
在这里插入图片描述

配置文件内容差异性对比
difflib
文件差异性对比模块

import difflib
with open('pas/passwd') as f:
    text1 = f.readlines()
with open('pas/passwd.bak') as f:
    text2 = f.readlines()
d = difflib.HtmlDiff()
result = d.make_file(text1,text2)
with open('result.html','w') as f:
    f.write(result)

脚本运行后会生成一个html文件,进入这个html文件web界面能够清楚的看到文件差异
在这里插入图片描述
文件一致性检查
当需要进行代码审计或校验结果时往往需要检查原始与目标文件一致性

# 使用哈希加密文件返回一个字符串,当两个文件字符串返回内容不同,则文件不同
import hashlib
def get_md5(filename):
    with open(filename) as f:
        countent = f.read().encode('utf-8')
        md5 = hashlib.md5(countent)
        return md5.hexdigest()
if __name__ == '__main__':
    result = get_md5('pas/passwd')
    result1 = get_md5('pas/passwd.bak')
    if result == result1:
        print('文件没有被篡改')
    else:
        print('文件已经被篡改')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值