471,338 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Disk Space Script

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.
Nov 24 '07 #1
5 6081
On Nov 24, 11:46 am, "mostro...@gmail.com" <mostro...@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/Fi...ent-in-Python/

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

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

Mike
Nov 24 '07 #2
On Nov 24, 2:11 pm, kyoso...@gmail.com wrote:
On Nov 24, 11:46 am, "mostro...@gmail.com" <mostro...@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/Fi...ent-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...
Nov 24 '07 #3
On Nov 24, 2:20 pm, "mostro...@gmail.com" <mostro...@gmail.comwrote:
On Nov 24, 2:11 pm, kyoso...@gmail.com wrote:
On Nov 24, 11:46 am, "mostro...@gmail.com" <mostro...@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/Fi...ent-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...
If you need some more help, just let us know.

Mike
Nov 24 '07 #4
mo*******@gmail.com wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
Who'd ever think it / Such a squalid little ending
-- The American and Florence, _Chess_
Nov 24 '07 #5
On Nov 24, 11:46 am, "mostro...@gmail.com" <mostro...@gmail.com>
wrote:
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
Nov 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by New World Order Pigs | last post: by
6 posts views Thread by PyPK | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.