1

I have a similar question to the one asked here: Find size and free space of the filesystem containing a given file, but that question assumes you already know something about the system.

I have a task: for an undetermined number of machines, including new ones that are deployed regularly, I have/need a python script that can report to me if any of the partitions are too full. (Yes, it's deployed by icinga2).

What I don't do is hand craft and personalize the arguments to the script for every machine to inform it of the partitions I want checked; I run the script and it just reports to me on all of the extant partitions on the system. I let the system itself be the authority on itself rather than externally define what partitions to check. This works okay in linux, and as the answer linked above shows, in linux we can parse a file in /proc to get an authoritative list.

But what I'm lacking is a way to get a reliable list of partitions in Mac OS X from python.

Mac OS X doesn't have /proc, so parsing that won't work. I'd rather not call on an external binary, as my goal is to build my python script to run on both linux and mac clients. Any ideas?

Community
  • 1
  • 1
Mercury00
  • 80
  • 2
  • 10
  • Possible duplicate of: [Find size and free space of the filesystem containing a given file](http://stackoverflow.com/questions/4260116/find-size-and-free-space-of-the-filesystem-containing-a-given-file) – Alexander O'Mara Feb 17 '16 at 23:28
  • Except, as I've clearly stated with a link to that question, it's not. There is nowhere in that question an assumption that we're using Mac OS X. As I've explicitly stated, I already have a linux solution, which that link explores. – Mercury00 Feb 17 '16 at 23:45
  • In that case, possible duplicates: [Find free disk space in python on OS/X](http://stackoverflow.com/questions/787776/find-free-disk-space-in-python-on-os-x) and [Cross-platform space remaining on volume using python](http://stackoverflow.com/questions/51658/cross-platform-space-remaining-on-volume-using-python) – Alexander O'Mara Feb 17 '16 at 23:49
  • Both of these assume that I already know what the filesystems are. I have to pass the partition in as an arugment. What I need is something that outputs the partitions themselves so I can use that information in a program like one of these. I can't discover the partitions by asking a program that first needs me to tell it the partitions to work. – Mercury00 Feb 17 '16 at 23:55

2 Answers2

4

Since you want a cross platform (Mac and Linux) option, you can use the df command which is available on both platforms. You can access it through subprocess.

I've tested this for both OS X 10.11 and Ubuntu Linux 15

import subprocess

process = subprocess.Popen(['df -h | awk \'{print $(NF-1),$NF}\''], stdout=subprocess.PIPE, shell=True)
out, err = process.communicate()
out = out.splitlines()[1:] # grab all the lines except the header line
results = {}
for i in out:
    tmp = i.split(' ')
    results[tmp[1]] = tmp[0]

for key, value in results.items():
    print key + " is " + str(value) +" full"

Output on Mac

/dev is 100% full
/net is 100% full
/ is 82% full
/home is 100% full

Output on Linux

/dev is 1% full
/run/lock is 0% full
/run is 1% full
/ is 48% full

Here is how you can do it without awk

import subprocess

process = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE)
out, err = process.communicate()
out = out.splitlines()[1:] # grab all the lines except the header line

for i in out:
    tmp = i.split(' ')
    tmp2 = []
    for x in tmp:
        if x != '':
            tmp2.append(x)
    print tmp2[-1] + " is " + tmp2[-2] + " full" 
Cripto
  • 3,581
  • 7
  • 41
  • 65
  • Thanks, I'll try this; I'll probably prefer to parse in python rather than awk though. – Mercury00 Feb 17 '16 at 23:54
  • 1
    @AThomas: You're right. It's better to parse it manually; mostly because `shell=True ` is unsafe and a security risk. I've updated my answer – Cripto Feb 18 '16 at 00:27
  • 1
    I'll mark this as the solution, because it's what I've decided to use for my problem, but the other answer technically answers the initial question, which will be useful for people looking for that solution too. – Mercury00 Feb 23 '16 at 23:23
2

I don't believe there is a uniform, cross-platform way to do this but you can use the subprocess module and call the command $ diskutil list like this for OS X

import subprocess
p = subprocess.Popen(['diskutil', 'list'], stdout=subprocess.PIPE)
o = p.stdout.read()

o will contain the output of the diskutil command.

wpercy
  • 9,636
  • 4
  • 33
  • 45
  • This looks like the most authoritative method, as long as I have a smart enough parser for diskutil - I've been bitten a few times when the program output format is changed upstream. – Mercury00 Feb 17 '16 at 23:58
  • This is interesting, how do you get disk usage with diskutil? – Cripto Feb 18 '16 at 00:51
  • @Cripto I wasn't trying to give him disk usage - just answering the question "In Python, how do I get a list of all partitions in Mac OS X?" I gave you an upvote for your answer - it's definitely the better option for this task. – wpercy Feb 18 '16 at 01:24
  • @wilbur: I got excited and read through the man pages. I wish diskutil had a means of doing this. I needed that in another project because diskutil knows when a disk is encrypted. – Cripto Feb 18 '16 at 01:26
  • I've marked the other answer as the solution to my particular problem, but this answer is technically the correct answer, which will hopefully be useful for other people looking for the answer to this particular question as well. Thanks! – Mercury00 Feb 23 '16 at 23:23
  • @AThomas Yeah, the other answer was far better for what you were trying to do - namely being able to run on both OS X and Linux – wpercy Feb 24 '16 at 00:53