Disk Space Script

M

mostro713

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.
 
K

kyosohma

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
 
M

mostro713

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

Thanks for the info... I will do some reading...
 
K

kyosohma

On Nov 24, 11:46 am, "(e-mail address removed)" <[email protected]>
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:

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:

or there's this good article:

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

Mike

Thanks for the info... I will do some reading...

If you need some more help, just let us know.

Mike
 
E

Erik Max Francis

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?

Read it in line by line with os.popen, or one of the more involved
popen... modules, and then parse it however you like.
 
M

MonkeeSage

I would like to write a script in Python to email me when disk space
gets below a certain value.

OK, I'll give you the easy way using your example and popen, and then
a more complex example that doesn't rely on df/grep/awk and uses only
system calls:


Easy way:

import os

# open a pipe to "df ...", read from its stdout,
# strip the trailing \n, split it into a list on
# every \n, and put the results in 'data'
pipe = os.popen("df -x cifs -x iso9660 | " +
"grep --color=never -E '^/dev' | " +
"awk '{print $1, $4}'")
data = pipe.read().strip().split('\n')
pipe.close()

# 'data' now looks like:
# ['/dev/hda1 1405056', '/dev/hda3 152000']

quota = 2097152 # 2GB

# iterate the list, splitting each element
# on ' ', and assigning the first and second
# elements to 'device' and 'free'
for node in data:
device, free = node.split(' ')
if int(free) < quota:
# your mail stuff here
print "Device `%s' has less than %.2f GB free space" % \
(device, quota/1024.0/1024)


More complex way (well, less complex actually, just requires more
knowledge of python/fs):

quota = 2097152 # 2 GB

# map of mount point / device name
device_map = {'/' : '/dev/hda1',
'/mnt/shared' : '/dev/hda3'}

for mount, device in device_map.items():

# statvfs returns tuple of device status,
# see also "man statvfs" which this call wraps
vstat = os.statvfs(mount)

# (block size * free blocks) / 1024 = free bytes
# NB: for large drives, there is a margin of error
# in this naive computation, because it doesn't account
# for things like inode packing by the fs. But for a
# large quota like 2 GB, a +/- hundrend megs margin of
# error should not cause any problem.
free = (vstat[0] * vstat[4]) / 1024

if free < quota:
# your mail stuff here
print "Device `%s' has less than %.2f GB free space" % \
(device, quota/1024.0/1024)


References:

statvfs:
http://www.python.org/doc/lib/os-file-dir.html
http://www.python.org/doc/lib/module-statvfs.html

Regards,
Jordan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top