472,951 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 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 6173
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul | last post by:
Hi: I am totally new in Sun Solaris. I am not sure someone would like to tell me how to rellocate Sun Solaris disk space where oracle installed? I installed Oracle 8i in Sun Solaris and set...
12
by: New World Order Pigs | last post by:
Is there no way in .net to get disk space remaining for a given drive??? I can't believe it and yet is seems to be so. If someone knows of a way to do this in the .net libraries I'd be very much...
0
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. It doesn't happen all the time but it does happen. When...
2
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. I have 35 Gigs of disk space free. It doesn't happen...
6
by: PyPK | last post by:
how can we compute the current system disk space using a python script.? any ideas or have anyone tried this..
1
by: A.M | last post by:
Hi, I am using ActivePython 2.4.on windows XP I created test.py that contains the following simple script:
4
by: coool | last post by:
Hi I'm trying to run a cronjob on a php script everything is working fine, but I'm getting my server disk space fulled up - so what's wrong here ??? how can I fix this problem ?
1
by: empiresolutions | last post by:
The following query is causing a mysql error The Error number is 28, which means low disk space. So I clear some space, retry and get the error again after a few times querying. After playing with...
2
by: asmpic | last post by:
Hi, I stumbled upon a script on the internet that monitors disk space and if it reaches a certain percentage being full it send an email. However, the problem I encountered was that one of the file...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.