Bonjour,

Je cherche un moyen de récupérer l'espace disque restant sur certaines partitions au moyen d'un script écrit en Python.
J'avais trouvé le moyen de faire en Bash, mais je ne parviens pas à adapter mon code pour qu'il fonctionne dans mon nouveau script.

Voici mon script (il sert à monitorer des serveurs via XMPP/Jabber) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python                              
# by Hubert Chathi, 2007, 2008, 2009
# This file is hereby placed in the public domain.  Feel free to modify
# and redistribute at will.
# (Note, however, that python-jabberbot, which this file depends on, is
# licensed under the GNU GPL v3 or later, and xmppy is licensed under the
# GNU GPL v2 or later.)
# coding: utf-8
# -*- coding: utf-8 -*-
 
import xmpp
import os
from jabberbot import JabberBot, botcmd
from ConfigParser import RawConfigParser
 
class SystemBot(JabberBot):
    @botcmd
    def who(self, mess, args):
        """Display who's currently logged in."""
        who_pipe = os.popen('/usr/bin/who', 'r')
        who = who_pipe.read().strip()
        who_pipe.close()
 
        return who
 
    def idle_proc(self):
        status = []
 
        load = '%s %s %s - ' % os.getloadavg()
        status.append(load)
 
        # espace libre
#       DFVAR=`df -h |grep xvdb | mawk -Wp '{print $4}'`
#       DFROOT=`df -h |grep xvda1 | mawk -Wp '{print $4}'`
#       DFBACKUPS=`df -h |grep xvdd | mawk -Wp '{print $4}'`
 
 
 
        # calculate the uptime
        uptime_file = open('/proc/uptime')
        uptime = uptime_file.readline().split()[0]
        uptime_file.close()
 
        uptime = float(uptime)
        (uptime,secs) = (int(uptime / 60), uptime % 60)
        (uptime,mins) = divmod(uptime,60)
        (days,hours) = divmod(uptime,24)
 
        uptime = 'up %d jour%s, %d:%02d' % (days, days != 1 and 's' or '', hours, mins)
        status.append(uptime)
 
        # calculate memory and swap usage
        meminfo_file = open('/proc/meminfo')
        meminfo = {}
        for x in meminfo_file:
            try:
                (key,value,junk) = x.split(None, 2)
                key = key[:-1] # strip off the trailing ':'
                meminfo[key] = int(value)
            except:
                pass
        meminfo_file.close()
 
        memusage = 'Memory used: %d of %d kB (%d%%) - %d kB free' \
                   % (meminfo['MemTotal']-meminfo['MemFree'],
                      meminfo['MemTotal'],
                      100 - (100*meminfo['MemFree']/meminfo['MemTotal']),
                      meminfo['MemFree'])
        status.append(memusage)
        if meminfo['SwapTotal']:
            swapusage = 'Swap used: %d of %d kB (%d%%) - %d kB free' \
                      % (meminfo['SwapTotal']-meminfo['SwapFree'],
                         meminfo['SwapTotal'],
                         100 - (100*meminfo['SwapFree']/meminfo['SwapTotal']),
                         meminfo['SwapFree'])
            status.append(swapusage)
 
        status = '\n'.join(status)
        # TODO: set "show" based on load? e.g. > 1 means "away"
        if self.status_message != status:
            self.status_message = status
        return
 
config = RawConfigParser()
config.read(['/etc/systembot.cfg','systembot.cfg'])
 
bot = SystemBot(config.get('systembot','username'),
                config.get('systembot','password'))
bot.serve_forever()
Vous l'aurez sans doute remarqué, mon ancien code en Bash est commenté :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
        # espace libre
#       DFVAR=`df -h |grep xvdb | mawk -Wp '{print $4}'`
#       DFROOT=`df -h |grep xvda1 | mawk -Wp '{print $4}'`
#       DFBACKUPS=`df -h |grep xvdd | mawk -Wp '{print $4}'`
Quelle serait l'équivalent en Python pour récupérer ces valeurs dans des variables que je pourrai exploiter par la suite dans le script ci-dessus ?

Merci d'avance.