Last active
February 5, 2018 22:04
-
-
Save mmzsource/49357b8c4b00b8023834e643cb818a56 to your computer and use it in GitHub Desktop.
Disk usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This is a script to log (and eventually monitor) diskusage on a filesystem. | |
# It will write the disk usage to a logfile. | |
# The logfile will never grow larger than MAX_LINES number of lines. | |
# | |
# The script is 'gnu awk' specific. GNU AWK is available on mac (brew) | |
# and ubuntu (apt-get) as the package 'gawk'. | |
# | |
# To be used together with a cron job and optionally a GNUPLOT. | |
# | |
## These vars hold the names of the used files | |
BASE_NAME="${HOME}/example" | |
LOG_FILE="${BASE_NAME}.log" | |
ERR_FILE="${BASE_NAME}.err" | |
TMP_FILE="${BASE_NAME}.tmp" | |
# Extract CAPACITY in percentage for the given filesystem | |
# and append DATETIME and CAPACITY to file | |
df --output="pcent" ${HOME} | \ | |
awk 'BEGIN {datetime = strftime("%Y-%m-%dT%H:%M:%S%z", systime());} NR > 1 {print datetime, $1}' \ | |
>> ${LOG_FILE} 2>>${ERR_FILE} | |
# If diskusage file does not exist, something went wrong. Better exit. | |
if [ ! -f "$LOG_FILE" ]; then | |
exit 1 | |
fi | |
# | |
# Make sure this file doesn't grow larger than MAX_LINES lines. | |
# Delete oldest entries. | |
# | |
# Maximum number of lines the logfile may hold | |
MAX_LINES=1000 | |
# Number of lines the logfile currently holds | |
CURRENT_LINES=$(cat ${LOG_FILE} | wc -l) | |
if [ "$CURRENT_LINES" -gt "$MAX_LINES" ]; then | |
# Tail MAX_LINES to tempfile and (force) copy back to original logfile | |
tail -n${MAX_LINES} ${LOG_FILE} > ${TMP_FILE} | |
cp -f ${TMP_FILE} ${LOG_FILE} | |
fi | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2018-01-01T18:00:00+0100 20% | |
2018-02-01T18:00:00+0100 25% | |
2018-03-01T18:00:00+0100 25% | |
2018-04-01T18:00:00+0100 30% | |
2018-05-01T18:00:00+0100 50% | |
2018-06-01T18:00:00+0100 55% | |
2018-07-01T18:00:00+0100 60% | |
2018-08-01T18:00:00+0100 60% | |
2018-09-01T18:00:00+0100 80% | |
2018-10-01T18:00:00+0100 85% | |
2018-11-01T18:00:00+0100 90% | |
2018-12-01T18:00:00+0100 95% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/gnuplot | |
# | |
# This script plots a linegraph. | |
# | |
# It expects an input as created by the diskusage script. | |
# | |
# More specifically, it expects the first column of the logfile to contain | |
# a date according to the ISO8601 format and the second column to contain | |
# percentages in the range of 0% - 100%. | |
# | |
# Furthermore, it expects gnuplot to be installed at the specified location. | |
# | |
# You can run the script by making it executable and running it while | |
# redirecting the output to a png, e.g.: | |
# | |
# ./plot.diskusage > plot.example.log.png | |
# | |
# General setup | |
reset | |
set terminal png | |
# Configure general linegraph properties | |
set title "Disk usage over time" | |
set key reverse Left outside | |
set grid | |
set style data linespoints | |
# Configure x axis | |
set xdata time | |
set timefmt "%Y-%m-%dT%H:%M:%S" | |
set format x "%Y-%m-%d" | |
set xlabel "Date" | |
set xtics rotate by -45 | |
# Configure y axis | |
set ylabel "Diskusage" | |
set yrange [0:100] | |
# Plot capacity (col 2) over time (col 1) | |
plot "example.log" using 1:2 title "Capacity" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment