我知道这个问题很啰嗦。指导方针说要问一个可以回答的问题,尽管我意识到如果我们不小心的话,这可能会变成讨论。因此,一般的方法和循环定义可能足以回答这个问题……不过如果有代码,我也不会抱怨。
实现zpool状态的输出确实可以很复杂…我知道这在PHP和VB中是可行的,我怎么在python中实现呢?有一个问题,for each循环似乎不能处理从linux命令行返回的字符串。
信息:操作系统:CentOSPython: 2.6.6 . .试图升级到2.7mysql数据库
DB关系:是复杂的,有点专有的,但我可以这样说:4个表用于池,vdev,子vdev和硬盘所有的关系。
当前Python,这不是很好
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import sys
import os
import subprocess
try:
def get_cols(column_num):
col_return = [os.system("zpool list | awk '{print$%d}'" % column_num)]
return col_return
for line in get_cols(0):
pool_name = get_cols(1)
size = get_cols(2)
alloc = get_cols(3)
free = get_cols(4)
print(pool_name + size + alloc + free)
x = subprocess.Popen(['zpool list'])
print(x)
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
当前错误
Traceback(最近一次调用):文件"/var/www/html/pythonscripts/BH_startup.py",在X =子进程。文件"/usr/lib64/python2.6/subprocess.py",第642行,在init文件"/usr/lib64/python2.6/subprocess.py",第1238行,在_execute_child .py中raise child_exception OSError: [Errno 2] No such file or directory
x = subprocess.Popen(['zpool list'])
应为
x = subprocess.Popen(['zpool', 'list'])
您必须解析命令的输出,提取任何必要的信息。在本例中,我们想要查找ZFS池的状态:
ZFS_Pool_name = "ZFS Pool name"
cmd = ['sudo', 'zpool', 'status', ZFS_Pool_name]
ps_proc = subprocess.Popen(command, stdout=subprocess.PIPE,
close_fds=True)
output, err = ps_proc.communicate()
for entry in map(lambda x: x.strip(), result.split('n')):
if "state" in entry:
pool_status = entry.split(" ")[-1]
# Insert this pool_status to wherever you want