Use Bash to Store Disc Info in SQLite.
Cecil Westerhof
Als trainer Persoonlijke Ontwikkeling help ik ECHT dienstbare ondernemers hun stress om te ruilen voor rust en plezier in hun leven.
Introduction.
In a lot of situations you can use SQLite. First of all in place of a ‘normal’ database, but also for log information, or application information. But that is for another time.
In this posting I want to show how you can easily use Bash in combination with SQLite to store information about your folders and partitions.
What was the problem?
Sometimes your partitions get filled up without you noticing it. This can lead to all kind of nasty surprises. I already had a script that mailed me every night the usage of my partitions, but it would be useful to have some historic data to compare it with. I could use a log file for this, but putting it in a SQLite database has several advantages.
What do I want?
I want to get every morning information about my partitions and some folders. It should be easy to change the partitions and folders that are monitored and to retrieve historic information to understand what is happening.
What is the solution?
The solution was to store the partition and directory information in two tables: 'directoryUsage' and 'partitionUsage'. I also defined a table 'variables' to store the directories and partitions to monitor.
Then once a day I run a script (through cron) that gets the needed information and emails that to me and stores it in the database.
You can find the definitions of the tables here.
The code.
For storing the directory information the following code is used:
while read used directory ; do
printf "${DIR_OUTPUT}" "${directory}" "${used}"
printf "${DIR_INSERT}" "${DATE}" "${directory}" "${used}" | \
sqlite3 "${DATABASE}"
done < <(du -hs ${DIRS})
With "< <(du -hs ${DIRS})" we loop over all the lines that du puts out.
First the information is printed.Then it is saved in the database.
Storing the partition information uses the following code:
{
read line
while read partition size used available percentUsed ; do
printf \
"${PART_OUTPUT}" \
"${partition}" \
"${size}" "${used}" "${available}" \
"${percentUsed}"
printf \
"${PART_INSERT}" \
"${DATE}" \
"${partition}" \
"${size}" "${used}" "${available}" \
"${percentUsed}" | sqlite3 "${DATABASE}"
done
} < <(df -h --output=target,size,used,avail,pcent ${PARTITIONS})
Because df generates an header we need to throw it away. That is why we use '{' and '}'. In this way we can use "read line" before the while to throw the header away.
For the rest it is the same.
You can find the complete bash script here.
What do you think about this script?
Do you find it useful?
Do you have any improvements?
What do you miss?
If you like me to treat a certain subject about bash or SQLite: let me now.
Als trainer Persoonlijke Ontwikkeling help ik ECHT dienstbare ondernemers hun stress om te ruilen voor rust en plezier in hun leven.
9 年By the way I use: ????partitions??????'/ /home' ????directories????'/tmp /usr/lib64 /usr/share /var/lib /var/log'