473,506 Members | 17,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6216
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
4465
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
10989
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
2473
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
16046
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
3738
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
2119
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
2051
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
1364
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
3499
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
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.