Use Bash to Store Disc Info in SQLite.

Use Bash to Store Disc Info in SQLite.

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.

Cecil Westerhof

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'

回复

要查看或添加评论,请登录

Cecil Westerhof的更多文章

  • Print Free and Total Disk of 'Real' Partitions

    Print Free and Total Disk of 'Real' Partitions

    Someone from the group got this code, but did not understand it: df -h | xargs | awk '{ print "Free/total disk: " $11 "…

    2 条评论
  • Bash: Check Parameters

    Bash: Check Parameters

    EDIT 2023-03-08: Removed bug from nrOfFilesAndDirs I wanted to have the possibility to count in a directory the number…

  • Script To Add A Swap File

    Script To Add A Swap File

    A certain operation needed more as 20 GB of memory. Because my system only has 14 GB RAM and 9 GB swap that was a…

    1 条评论
  • Efficiently Calculating Multiplicative Persistence

    Efficiently Calculating Multiplicative Persistence

    Yesterday I participated in 'The SQLite & Tcl Conference 2021'. This brought into my mind an interesting problem I…

  • Exactly One Ace, King, Queen, Jack, Diamond, Hearth, Spade and Club in Rows, Columns and Diagonals

    Exactly One Ace, King, Queen, Jack, Diamond, Hearth, Spade and Club in Rows, Columns and Diagonals

    Today I saw this video: Euler Squares. This made me think: when we have the Aces, Kings, Queens and Jacks of a deck of…

  • Check, Update AND Rollback Pip Packages

    Check, Update AND Rollback Pip Packages

    Every year I learn at least one new language. This year it was Tcl (Tickle).

  • Tcl For System Scripts With Ease

    Tcl For System Scripts With Ease

    Introduction I want to learn new language every year. This year it was Tcl (Tickle).

    4 条评论
  • Simple Test Programs With Bash

    Simple Test Programs With Bash

    The problem There was a challenge to write a Java program that displays the number of connections for an undirected…

  • Maintaining PATH In Bash

    Maintaining PATH In Bash

    Introduction In Bash you ‘often’ have to add a directory to PATH. But sometimes this results in a contaminated PATH:…

  • Updating the Extension Pack of VirtualBox

    Updating the Extension Pack of VirtualBox

    When working with VirtualBox you have sometimes to update the extension pack. It is not difficult, but because it is…

社区洞察

其他会员也浏览了