我編寫了這個簡單的Munin插件來繪製平均風扇速度,並且我想將它重做OOP - 嚴格地作爲學習練習。不知道從哪裏開始。任何人都想提供一些指導,甚至是完成這個腳本應該看起來像什麼的例子。我會用它來重做一些其他的腳本到OOP風格中;再次用於學習目的。Python:將腳本從程序轉換爲OOP風格
import sys
import subprocess
CMD = "/usr/sbin/omreport chassis fans".split()
# Munin populates sys.argv[1] with "" (an empty argument), lets remove it.
sys.argv = [x for x in sys.argv if x]
if len(sys.argv) > 1:
if sys.argv[1].lower() == "autoconfig":
print "autoconfig"
elif sys.argv[1].lower() == "config":
print "graph_title Average Fan Speed"
print "graph_args --base 1000 -l 0"
print "graph_vlabel speed (RPM)"
print "graph_category Chassis"
print "graph_info This graph shows the average speed of all fans"
print "graph_period second"
print "speed.label speed"
print "speed.info Average fan speed for the five minutes."
else:
try:
data = subprocess.Popen(CMD,stdout=subprocess.PIPE).stdout.readlines()
except OSError, e:
print >> sys.stderr, "Error running '%s', %s" % (" ".join(cmd), e)
sys.exit(1)
count = total = 0
for item in data:
if "Reading" in item:
# Extract variable length fan speed, without regex.
total += int(item.split(":")[1].split()[0])
count += 1
# Sometimes omreport returns zero output if omsa services aren't started.
if not count or not total:
print >> sys.stderr, 'Error: "omreport chassis fans" returned 0 output.'
print >> sys.stderr, 'OMSA running? Try: "srvadmin-services.sh status".'
sys.exit(1)
avg = (total/count)
print "speed.value %s" % avg
我將兩個插件組合成一個基於Lennart Regebro的答案。真的很感激它的一些建議。 http://stackoverflow.com/questions/2191639/python-feedback-corrections-on-first-oop-style-script – CarpeNoctem 2010-02-03 11:57:42