用于记录磁盘使用情况的脚本或程序
我想要一个程序或更好的方式来记录磁盘使用情况。
为了解释我的意思,当有人安装Ubuntu时,大约使用了4.5 GB的磁盘。 然后,当您安装/卸载程序时,此用法会增加或减少。
我想要的是一种方法,当发生此更改的时间和日期发生更改(安装/保存或卸载/删除某些内容)时,自动记录txt文件中使用的当前磁盘。
使用df
命令跟踪磁盘空间,使用lsblk
命令跟踪已安装的驱动器,下面的脚本在后台运行,将记录所有已安装驱动器的可用空间中的更改。 它创建一个日志文件: ~/disklog
,它将更改写入(以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
如何使用
- 将下面的脚本复制到一个空文件中,将其安全为
log_diskusage.py
-
在脚本的head部分中,将时间间隔设置为日志文件中的阈值和最大行数:
#--- 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秒 -
treshold
:您可能不希望记录所有(非常)小的更改,因为磁盘在可用磁盘空间中有很多小的变化。 实际上,它设置为10k
-
max_lines
,因为日志文件将快速增长,特别是如果您将阈值设置为零
- 运行磁盘空间检查的
-
使用以下命令测试脚本:
python3 /path/to/log_diskusage.py
-
如果一切正常,请将其添加到启动应用程序: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
您应该使用已安装在ubuntu或iostat上的vmstat,但您必须安装它。 iostat,vmstat和mpstat Linux性能监视示例