gunslingor
gunslingor

Reputation: 1486

How do I use python to reliably parse the outputs of commands zpool status and zpool list and then insert all information into a mysql database?

Long winded question I know. The guidelines said to ask a question that is answerable, though I realize this could devolve into discussion if we aren't careful. So a general approach and loop definition may be sufficient to answer the question... though I will not complain if there is code.

Realize the outputs of zpool status can be complex indeed... I know it's doable in PHP and VB, how do I really go about it in python? One issue, for each loops don't appear to work on strings returned from linux command line.

INFO: OS: CentOS Python: 2.6.6.. trying to upgrade to 2.7 mysql DB

DB relationships: Are complicated and somewhat proprietary, but I can say this: 4 tables for pools, Vdevs, sub Vdevs and harddrives all relational.

Current Python, which isn't great

#!/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)

Current error

Traceback (most recent call last): File "/var/www/html/pythonscripts/BH_startup.py", line 21, in x = subprocess.Popen(['zpool list']) File "/usr/lib64/python2.6/subprocess.py", line 642, in init errread, errwrite) File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory

Upvotes: 1

Views: 1085

Answers (2)

Ayush Lal Shrestha
Ayush Lal Shrestha

Reputation: 183

You will have to parse the output of the command extract any necessary information. In this case, we want to find the status of ZFS Pool:

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

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304195

x = subprocess.Popen(['zpool list'])

should be

x = subprocess.Popen(['zpool', 'list'])

Upvotes: 0

Related Questions