Skip to content

Instantly share code, notes, and snippets.

@ProBackup-nl
Last active February 17, 2021 20:55
Show Gist options
  • Save ProBackup-nl/4f978ac78ff9d88f85318e6683fa4393 to your computer and use it in GitHub Desktop.
Save ProBackup-nl/4f978ac78ff9d88f85318e6683fa4393 to your computer and use it in GitHub Desktop.
Log all disk and partition write statistics on one line in file for Arch Linux
#!/bin/sh
# # tail /var/log/disk-write-stats.log
# 2021-02-15 13:39:30 Written: mmcblk1p1 [512 B] mmcblk1p2 [150 MB] sda [1.61 TB]
# 2021-02-16 04:00:22 Written: mmcblk1p1 [512 B] mmcblk1p2 [163 MB] sda [1.96 TB]
# 2021-02-17 04:00:00 Written: mmcblk1p1 [512 B] mmcblk1p2 [179 MB] sda [2.52 TB]
#
# installation:
# # nano /usr/bin/log-write-stats.sh; chmod +x /usr/bin/log-write-stats.sh
# # nano /etc/systemd/system/log-write-stats.service
# # /etc/systemd/system/log-write-stats.timer files
# # systemctl enable log-write-stats.timer && systemctl start log-write-stats.timer
/bin/awk -v ORS=" " 'BEGIN {print strftime("%Y-%m-%d %H:%M:%S Written:")} /sd|mmcblk1p/{ split( "B KB MB GB TB PB EB" , v ); s=1; t=$10*512; while( t>1024 ){ t/=1024; s++ } printf ("%s [%.3g %s] ", $3, t, v[s] ) } END {print "\n"}' /proc/diskstats >> /var/log/disk-write-stats.log
[Unit]
Description=Log amount of data written for mmcblk1* partitions and sd* devices
[Service]
Type=oneshot
ExecStart=/usr/bin/log-write-stats.sh
[Unit]
Description=Log /proc/diskstat written sectors value for mmcblk1* partitions and sd* devices daily
Requires=log-write-stats.service
[Timer]
OnCalendar=*-*-* 4:00:00
Persistent=true
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment