Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions pymunin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import os.path
import sys
import re
import cPickle as pickle
try:
import cPickle as pickle
except ImportError:
import pickle

__author__ = "Ali Onur Uyar"
__copyright__ = "Copyright 2011, Ali Onur Uyar"
Expand Down Expand Up @@ -162,11 +165,11 @@ def _parseEnv(self, env=None):
"""
if not env:
env = self._env
if env.has_key('MUNIN_STATEFILE'):
if 'MUNIN_STATEFILE' in env:
self._stateFile = env.get('MUNIN_STATEFILE')
else:
self._stateFile = '/tmp/munin-state-%s' % self.plugin_name
if env.has_key('MUNIN_CAP_DIRTY_CONFIG'):
if 'MUNIN_CAP_DIRTY_CONFIG' in env:
self._dirtyConfig = True

def _getGraph(self, graph_name, fail_noexist=False):
Expand Down Expand Up @@ -196,7 +199,7 @@ def _getSubGraph(self, parent_name, graph_name, fail_noexist=False):
"""
if not self.isMultigraph:
raise AttributeError("Simple Munin Plugins cannot have subgraphs.")
if self._graphDict.has_key(parent_name) is not None:
if (parent_name in self._graphDict) is not None:
subgraphs = self._subgraphDict.get(parent_name)
if subgraphs is not None:
subgraph = subgraphs.get(graph_name)
Expand Down Expand Up @@ -313,7 +316,7 @@ def envHasKey(self, name):
@return: True if environment variable is defined.

"""
return self._env.has_key(name)
return name in self._env

def envGet(self, name, default=None, conv=None):
"""Return value for environment variable or None.
Expand All @@ -324,7 +327,7 @@ def envGet(self, name, default=None, conv=None):
@return: Value of environment variable.

"""
if self._env.has_key(name):
if name in self._env:
if conv is not None:
return conv(self._env.get(name))
else:
Expand All @@ -349,7 +352,7 @@ def envGetList(self, name, attr_regex = '^\w+$', conv=None):
"""
key = "list_%s" % name
item_list = []
if self._env.has_key(key):
if key in self._env:
if attr_regex:
recomp = re.compile(attr_regex)
else:
Expand Down Expand Up @@ -413,7 +416,7 @@ def envCheckFlag(self, name, default = False):
@return: Return True if the flag is enabled.

"""
if self._flags.has_key(name):
if name in self._flags:
return self._flags[name]
else:
val = self._env.get(name)
Expand Down Expand Up @@ -509,8 +512,8 @@ def appendSubgraph(self, parent_name, graph_name, graph):
"""
if not self.isMultigraph:
raise AttributeError("Simple Munin Plugins cannot have subgraphs.")
if self._graphDict.has_key(parent_name):
if not self._subgraphDict.has_key(parent_name):
if parent_name in self._graphDict:
if parent_name not in self._subgraphDict:
self._subgraphDict[parent_name] = {}
self._subgraphNames[parent_name] = []
self._subgraphDict[parent_name][graph_name] = graph
Expand Down Expand Up @@ -563,7 +566,7 @@ def hasGraph(self, graph_name):
@return: Boolean

"""
return self._graphDict.has_key(graph_name)
return graph_name in self._graphDict

def hasSubgraph(self, parent_name, graph_name):
"""Return true if Root Graph with name parent_name has a subgraph with
Expand Down Expand Up @@ -601,7 +604,7 @@ def getSubgraphList(self, parent_name):
"""
if not self.isMultigraph:
raise AttributeError("Simple Munin Plugins cannot have subgraphs.")
if self._graphDict.has_key(parent_name):
if parent_name in self._graphDict:
return self._subgraphNames[parent_name] or []
else:
raise AttributeError("Invalid parent graph name %s."
Expand Down Expand Up @@ -717,22 +720,22 @@ def config(self):

"""
if self._host_name is not None:
print "host_name %s" % self._host_name
print("host_name %s" % self._host_name)
for parent_name in self._graphNames:
graph = self._graphDict[parent_name]
if self.isMultigraph:
print "multigraph %s" % self._getMultigraphID(parent_name)
print self._formatConfig(graph.getConfig())
print
print("multigraph %s" % self._getMultigraphID(parent_name))
print(self._formatConfig(graph.getConfig()))
print()
if (self.isMultigraph and self._nestedGraphs
and self._subgraphDict and self._subgraphNames):
for (parent_name, subgraph_names) in self._subgraphNames.iteritems():
for (parent_name, subgraph_names) in self._subgraphNames.items():
for graph_name in subgraph_names:
graph = self._subgraphDict[parent_name][graph_name]
print "multigraph %s" % self.getMultigraphID(parent_name,
graph_name)
print self._formatConfig(graph.getConfig())
print
print("multigraph %s" % self.getMultigraphID(parent_name,
graph_name))
print(self._formatConfig(graph.getConfig()))
print()
return True

def suggest(self):
Expand All @@ -754,18 +757,18 @@ def fetch(self):
for parent_name in self._graphNames:
graph = self._graphDict[parent_name]
if self.isMultigraph:
print "multigraph %s" % self._getMultigraphID(parent_name)
print self._formatVals(graph.getVals())
print
print("multigraph %s" % self._getMultigraphID(parent_name))
print(self._formatVals(graph.getVals()))
print()
if (self.isMultigraph and self._nestedGraphs
and self._subgraphDict and self._subgraphNames):
for (parent_name, subgraph_names) in self._subgraphNames.iteritems():
for (parent_name, subgraph_names) in self._subgraphNames.items():
for graph_name in subgraph_names:
graph = self._subgraphDict[parent_name][graph_name]
print "multigraph %s" % self.getMultigraphID(parent_name,
graph_name)
print self._formatVals(graph.getVals())
print
print("multigraph %s" % self.getMultigraphID(parent_name,
graph_name))
print(self._formatVals(graph.getVals()))
print()
return True

def run(self):
Expand All @@ -783,9 +786,9 @@ def run(self):
elif oper == 'autoconf':
ret = self.autoconf()
if ret:
print "yes"
print("yes")
else:
print "no"
print("no")
ret = True
elif oper == 'suggest':
ret = self.suggest()
Expand Down Expand Up @@ -828,7 +831,7 @@ def __init__(self, title, category = None, vlabel=None, info=None,
by replacing them with '_'.

"""
self._graphAttrDict = dict((k,v) for (k,v) in locals().iteritems()
self._graphAttrDict = dict((k,v) for (k,v) in locals().items()
if (v is not None
and k not in ('self', 'autoFixNames')))
self._fieldNameList = []
Expand Down Expand Up @@ -866,7 +869,7 @@ def addField(self, name, label, type=None, draw=None, info=None, #@ReservedAssi
name = self._fixName(name)
if negative is not None:
negative = self._fixName(negative)
self._fieldAttrDict[name] = dict(((k,v) for (k,v) in locals().iteritems()
self._fieldAttrDict[name] = dict(((k,v) for (k,v) in locals().items()
if (v is not None
and k not in ('self',))))
self._fieldNameList.append(name)
Expand All @@ -880,7 +883,7 @@ def hasField(self, name):
"""
if self._autoFixNames:
name = self._fixName(name)
return self._fieldAttrDict.has_key(name)
return name in self._fieldAttrDict

def getFieldList(self):
"""Returns list of field names registered to Munin Graph.
Expand Down Expand Up @@ -951,7 +954,7 @@ def muninMain(pluginClass, argv=None, env=None, debug=False):
argv = sys.argv
if env is None:
env = os.environ
debug = debug or env.has_key('MUNIN_DEBUG')
debug = debug or 'MUNIN_DEBUG' in env
if len(argv) > 1 and argv[1] == 'autoconf':
autoconf = True
else:
Expand All @@ -964,9 +967,9 @@ def muninMain(pluginClass, argv=None, env=None, debug=False):
else:
return 1
except Exception:
print >> sys.stderr, "ERROR: %s" % repr(sys.exc_info()[1])
print("ERROR: %s" % repr(sys.exc_info()[1]), sys.stderr)
if autoconf:
print "no"
print("no")
if debug:
raise
else:
Expand Down Expand Up @@ -1005,6 +1008,3 @@ def fixLabel(label, maxlen, delim=None, repl='', truncend=True):
return label[:maxlen] + repl
else:
return repl + label[-maxlen:]



26 changes: 13 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class install(_install):
"Extend base install class to provide a post-install step."

def run(self):
if os.environ.has_key('MUNIN_PLUGIN_DIR'):
if 'MUNIN_PLUGIN_DIR' in os.environ:
munin_plugin_dir = os.environ.get('MUNIN_PLUGIN_DIR')
elif self.root is None:
munin_plugin_dir = os.path.normpath(
Expand All @@ -62,7 +62,7 @@ def run(self):
_install.run(self)
# Installing the plugins requires write permission to plugins directory
# (/usr/share/munin/plugins) which is default owned by root.
print "Munin Plugin Directory: %s" % munin_plugin_dir
print("Munin Plugin Directory: %s" % munin_plugin_dir)
if os.path.exists(munin_plugin_dir):
try:
for name in plugin_names:
Expand All @@ -71,18 +71,18 @@ def run(self):
u'%s-%s' % (PYMUNIN_SCRIPT_FILENAME_PREFIX, name)
)
destination = os.path.join(munin_plugin_dir, name)
print "Installing %s to %s." % (name, munin_plugin_dir)
print("Installing %s to %s." % (name, munin_plugin_dir))
shutil.copy(source, destination)
except IOError, e:
except IOError as e:
if e.errno in (errno.EACCES, errno.ENOENT):
# Access denied or file/directory not found.
print "*" * 78
print("*" * 78)
if e.errno == errno.EACCES:
print ("You do not have permission to install the plugins to %s."
% munin_plugin_dir)
print("You do not have permission to install the plugins to %s."
% munin_plugin_dir)
if e.errno == errno.ENOENT:
print ("Failed installing the plugins to %s. "
"File or directory not found." % munin_plugin_dir)
print("Failed installing the plugins to %s. "
"File or directory not found." % munin_plugin_dir)
script = os.path.join(self.install_scripts, 'pymunin-install')
f = open(script, 'w')
try:
Expand All @@ -96,11 +96,11 @@ def run(self):
f.write('cp %s %s\n' % (source, destination))
finally:
f.close()
os.chmod(script, 0755)
print ("You will need to copy manually using the script: %s\n"
"Example: sudo %s"
os.chmod(script, 0o755)
print("You will need to copy manually using the script: %s\n"
"Example: sudo %s"
% (script, script))
print "*" * 78
print("*" * 78)
else:
# Raise original exception
raise
Expand Down