###########################################################################
#
# This program is part of Zenoss Core, an open source monitoring platform.
# Copyright (C) 2009, Zenoss Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# For complete information please visit: http://www.zenoss.com/oss/
#
###########################################################################

__doc__ = """df
Determine the filesystems to monitor
"""

import re

from Products.DataCollector.plugins.CollectorPlugin import CommandPlugin

class df(CommandPlugin):
    """
    Run df -k to model filesystem information. Should work on most *nix.
    """
    maptype = "FilesystemMap" 
    command = '/bin/df -Pk'
    compname = "os"
    relname = "filesystems"
    modname = "Products.ZenModel.FileSystem"
    deviceProperties = \
                CommandPlugin.deviceProperties + ('zFileSystemMapIgnoreNames',)

    oses = ['Linux', 'Darwin', 'SunOS', 'AIX']

    def condition(self, device, log):
        return device.os.uname == '' or device.os.uname in self.oses


    def process(self, device, results, log):
        log.info('Collecting filesystems for device %s' % device.id)
        skipfsnames = getattr(device, 'zFileSystemMapIgnoreNames', None)
        rm = self.relMap()
        rlines = results.split("\n")
        bline = ""
        for line in rlines:
            if line.startswith("Filesystem"): continue
            om = self.objectMap()
            spline = line.split()
            if len(spline) == 1:
                bline = spline[0]
                continue
            if bline: 
                spline.insert(0,bline)
                bline = None
            if len(spline) != 6: continue
            (om.storageDevice, tblocks, u, a, p, om.mount) = spline
            if skipfsnames and re.search(skipfsnames,om.mount): continue
            
            if tblocks == "-": om.totalBlocks = 0
            else: om.totalBlocks = long(tblocks)
            
            om.blockSize = 1024
            om.id = self.prepId(om.mount)
            rm.append(om)
        return rm
