#!/usr/bin/env python # -*- python-mode -*- """Script to set NFS debug levels symbolically """ __copyright__ = """ Copyright (C) 2007, Chuck Lever 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. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ """ History: Thu Jun 21 10:41:40 EDT 2007 cel - created """ import sys, os, time, popen2 """ From include/linux/nfs_fs.h """ NfsDebugFlags = { 'vfs' : 0x0001, 'dircache' : 0x0002, 'lookupcache' : 0x0004, 'pagecache' : 0x0008, 'proc' : 0x0010, 'xdr' : 0x0020, 'file' : 0x0040, 'root' : 0x0080, 'callback' : 0x0100, 'client' : 0x0200, 'mount' : 0x0400, } NoNfsFlags = 0 AllNfsFlags = 0x07ff def DoCmd(cmd): p = popen2.Popen3(cmd, True) output = p.fromchild.read() if p.wait(): print '%s failed (%s)' % (str(cmd), p.childerr.read().strip()) return output def CurrentSetting(): string = DoCmd("/sbin/sysctl sunrpc.nfs_debug") words = string.split() return int(words[2]) def SetNfsFlags(flags): cmd = '/usr/bin/sudo /sbin/sysctl -w sunrpc.nfs_debug=%d' % flags DoCmd(cmd) return def ProcessFlag(flag): if flag not in NfsDebugFlags: print 'Unrecognized NFS debugging flag.' return current = CurrentSetting() SetNfsFlags(current | NfsDebugFlags[flag]) return # # Main # if len(sys.argv) == 1: current = CurrentSetting() if current == NoNfsFlags: print 'NFS debugging is currently disabled.' elif current == AllNfsFlags: print 'NFS debugging is currently fully enabled.' else: print 'Current NFS debugging flags:' for i in NfsDebugFlags.items(): if (current & i[1]) == i[1]: print ' %s' % i[0] sys.exit(0) if sys.argv[1] in [ '-h', '--help' ]: print 'Usage: nfsdebug [flag name] [flag name] ...' print ' where flag name is one of: all, none, %s' % \ ', '.join(NfsDebugFlags.keys()) sys.exit(1) for arg in sys.argv: if arg == sys.argv[0]: continue elif arg in [ 'all', 'on', 'true', 'yes' ]: SetNfsFlags(AllNfsFlags) print 'NFS debugging is now enabled.' elif arg in [ 'none', 'off', 'false', 'no', 'clear', 'zero' ]: SetNfsFlags(NoNfsFlags) print 'NFS debugging is now disabled.' else: ProcessFlag(arg) sys.exit(0)