GNU/Linux >> Linux の 問題 >  >> Ubuntu

ディスク使用量をログに記録するスクリプトまたはプログラム?

プログラム、またはよりできればディスク使用量をログに記録する方法が欲しいのですが。

私が何を意味するかを説明するために、誰かがUbuntuをインストールすると、約4.5GBのディスクが使用されます。次に、プログラムをインストール/アンインストールすると、この使用量は増減します。

私が欲しいのは、変更があったとき(何かがインストール/保存またはアンインストール/削除されたとき)に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

使用方法

  1. 以下のスクリプトを空のファイルにコピーし、log_diskusage.pyとして安全に保管してください
  2. スクリプトの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 、特にしきい値をゼロに設定した場合、ログファイルは急速に大きくなるため
  3. 次のコマンドを使用してスクリプトをテスト実行します:

    python3 /path/to/log_diskusage.py
    
  4. すべて正常に機能する場合は、スタートアップアプリケーションに追加します:ダッシュ>スタートアップアプリケーション>追加。

関連:Aptでパッケージをインストールする方法-Ubuntuでプロキシ経由で接続されたシステムにアクセスしますか?

スクリプト

#!/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
  1. ISPConfigまたはLAMPを使用してログファイルのディスク使用量を削減する方法

  2. CentOSWebPanelでディスク使用量を表示する

  3. Crontab ログ:私の Cron スクリプトの出力をログに記録する方法

  1. cPanel でディスク使用量を表示する方法

  2. cPanel ディスク使用ツールの使用方法

  3. Bash モニターのディスク使用量

  1. ディスク使用量アナライザー

  2. ディスク使用量が90%を超えた場合にアラートメールを送信するシェルスクリプト?

  3. $の意味は?シェルスクリプトでは?