AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 601539
Accepted
Adam
Adam
Asked: 2015-03-27 04:46:08 +0800 CST2015-03-27 04:46:08 +0800 CST 2015-03-27 04:46:08 +0800 CST

记录磁盘使用情况的脚本或程序

  • 772

我想要一个程序,或者更喜欢一种记录磁盘使用情况的方法。

为了解释我的意思,当有人安装 Ubuntu 时,大约使用了 4.5 GB 的磁盘。然后,当您安装/卸载程序时,这种使用会增加或减少。

我想要的是一种在发生更改(安装/保存或卸载/删除某些内容)时自动记录 txt 文件中使用的当前磁盘以及发生此更改的时间和日期的方法。

scripts
  • 2 2 个回答
  • 1817 Views

2 个回答

  • Voted
  1. Best Answer
    Jacob Vlijm
    2015-03-27T14:16:46+08:002015-03-27T14:16:46+08:00

    使用df跟踪磁盘空间的lsblk命令和跟踪已安装驱动器的命令,下面的脚本在后台运行,将记录所有已安装驱动器可用空间的变化。它创建一个日志文件:~/disklog将更改写入 (in k)。

    如果你在终端运行它,它会同时输出结果。

    日志文件的内容如下所示:

    [mountpoint / change / date/time / used]
    
    / . . . . . . . . . . . . . . . . . .            36 k       Fri Mar 27 08:17:30 2015    used 87989352 k
    /media/intern_2 . . . . . . . . . . .         -1792 k       Fri Mar 27 08:17:32 2015    used 562649592 k
    / . . . . . . . . . . . . . . . . . .            -4 k       Fri Mar 27 08:17:39 2015    used 87989356 k
    / . . . . . . . . . . . . . . . . . .           -36 k       Fri Mar 27 08:17:43 2015    used 87989392 k
    / . . . . . . . . . . . . . . . . . .            -4 k       Fri Mar 27 08:17:55 2015    used 87989396 k
    / . . . . . . . . . . . . . . . . . .             4 k       Fri Mar 27 08:18:11 2015    used 87989392 k
    / . . . . . . . . . . . . . . . . . .           -32 k       Fri Mar 27 08:18:13 2015    used 87989424 k
    

    如何使用

    1. 将下面的脚本复制到一个空文件中,将其保存为log_diskusage.py
    2. 在脚本的头部,设置时间间隔、阈值和日志文件中的最大行数:

      #--- set time interval in seconds, threshold in k, and the max number of lines in the logfile
      interval = 20        # the interval between the checks
      threshold = 0        # in K, you'd probably set this higher
      max_lines = 5000     # if you want no limit, comment out the line line_limit() in the script
      #---
      
      • interval按原样运行磁盘空间检查 20秒
      • The treshold:您可能不想记录所有(非常)小的变化,因为磁盘的可用磁盘空间有很多小变化。事实上,它被设置为10k
      • ,max_lines因为日志文件会快速增长,尤其是当您将阈值设置为零时
    3. 使用以下命令测试运行脚本:

      python3 /path/to/log_diskusage.py
      
    4. 如果一切正常,将其添加到您的启动应用程序:Dash > Startup Applications > Add。

    剧本

    #!/usr/bin/env python3
    import subprocess
    import os
    import time
    log = os.environ["HOME"]+"/disklog.txt"
    #--- set time interval in seconds, threshold in k
    interval = 1
    threshold = 0
    max_lines = 5000
    #---
    
    def line_limit():
        lines = open(log).readlines()
        if len(lines) > max_lines:
            with open(log, "wt") as limit:
                for l in lines[-max_lines:]:
                    limit.write(l)
    
    get = lambda cmd: subprocess.check_output([cmd]).decode("utf-8")
    
    def disk_change():
        mounted = [l[l.find("/"):] for l in get("lsblk").splitlines() if "/" in l]
        data = get("df").splitlines()
        matches = [("/", data[1].split()[-4])]
        for l in mounted:
            if l != "/":
                match = [(l, d.replace(l, "").split()[-3]) for d in data if l in d][0]
                matches.append(match)
        return matches
    
    disk_data1 = disk_change()
    while True:
        time.sleep(interval)
        disk_data2 = disk_change()
        for latest in disk_data2:
            try:
                compare = [(latest[0], int(latest[1]), int(item[1])) for item in disk_data1 if latest[0] == item[0]][0]
                if not compare[1] == compare[2]:
                    diff = compare[2]-compare[1]
                    if abs(diff) > threshold:
                        with open(log, "a") as logfile:
                            drive = compare[0]; lt = 18-int((len(drive)/2)); lk = 14-len(str(diff))
                            s = drive+" ."*lt+lk*" "+str(diff)+" k   \t"+str(time.strftime("%c"))+"\t"+"used "+str(compare[1])+" k\n"
                            logfile.write(s)
                        print(s, end = "")
                        # if you don't want to set a limit to the max number of lines, comment out the line below
                        line_limit()
            except IndexError:
                pass
        disk_data1 = disk_data2
    
    • 4
  2. dubis
    2015-03-27T04:52:49+08:002015-03-27T04:52:49+08:00

    您应该使用已安装在 ubuntu 上的 vmstat,或 iostat,但您必须安装它。 用于 Linux 性能监控的 iostat、vmstat 和 mpstat 示例

    • 0

相关问题

  • 如何在 Nautilus 中管理保存的完整网页及其目录(例如 n.html 和 n_files)

  • 如何每 5 秒运行一次脚本?

  • 如何将必须从其自己的目录中运行的程序添加到面板或主菜单?

  • 如何编写 shell 脚本来安装应用程序列表?

  • Mac OS X Automator 的替代品?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve