Disk Space Script

kyosohma at gmail.com kyosohma at gmail.com
Sat Nov 24 14:11:22 EST 2007


On Nov 24, 11:46 am, "mostro... at gmail.com" <mostro... at gmail.com>
wrote:
> Hello all,
>
> I would like to write a script in Python to email me when disk space
> gets below a certain value.
>
> My first question (I'm sure of many) is how do get this output into a
> dictionary or list to index the values?
>
> import os
> os.system("df -x cifs -x iso9660 | grep -E ^/dev | awk '{ print
> $1,$4 }'")
>
> [What the output looks like]
>
> /dev/sda3 15866012
> /dev/sda4 26126712
>
> I would like to write code that compares /dev/sda* to a number (ex.
> 2000000 -> "2GB") and sends an alert if the indexed value is below it.
>
> I'm a noob so keep it simple.
>
> Thanks in advance.

I don't know the Unix command for this, but I would just redirect the
output to a file on your system. See the following for more info:

http://www.faqs.org/docs/diveintopython/kgp_stdio.html

You could send the output to a variable and create a file-like stream
too. Either way, read the file (or stream) and then for each line,
split it on the space.

Then you can do the compare and use the email module to email you the
result should it go over your specified amount.

By the by, if you're using Python 2.4 or above, you should switch to
using the subprocess module rather than using os.system since the
latter is being deprecated.

For more on file objects, see the docs:

http://docs.python.org/lib/bltin-file-objects.html

or there's this good article:

http://www.devshed.com/c/a/Python/File-Management-in-Python/

And the email module is explained quite well in the docs too:

http://docs.python.org/lib/module-email.html

Mike



More information about the Python-list mailing list