简体   繁体   English

用于检查 Linux 绑定状态的 Python 脚本

[英]Python script to check bond status for Linux

I have the Below python code which I'm using to determine the Linux bond/team status.我有下面的 python 代码,我用它来确定 Linux 绑定/团队状态。 This code works just fine .这段代码工作得很好 I am not good at aligning the output formatting thus getting little hiccup.我不擅长对齐输出格式,因此很少打嗝。

I wanted the Printing Format into a Certain format, would appreciate any help on the same.我希望将打印格式转换为某种格式,希望得到任何帮助。

Below is the code exercise:下面是代码练习:

#!/usr/bin/python
# Using below file to process the data
# cat /proc/net/bonding/bond0

import sys
import re

def usage():
        print '''USAGE: %s [options] [bond_interface]

Options:
        --help, -h      This usage document

Arguments:
        bond_interface  The bonding interface to query, eg. 'bond0'. Default is 'bond0'.
''' % (sys.argv[0])
        sys.exit(1)

# Parse arguments
try:
        iface = sys.argv[1]
        if iface in ('--help', '-h'):
                usage()
except IndexError:
        iface = 'bond0'

# Grab the inf0z from /proc
try:
        bond = open('/proc/net/bonding/%s' % iface).read()
except IOError:
        print "ERROR: Invalid interface %s\n" % iface
        usage()

# Parse and output
active = 'NONE'
Link = 'NONE'
slaves = ''
state = 'OK'
links = ''
bond_status = ''
for line in bond.splitlines():
        m = re.match('^Currently Active Slave: (.*)', line)
        if m:
                active = m.groups()[0]

        m = re.match('^Slave Interface: (.*)', line)
        if m:
                s = m.groups()[0]
                slaves += ', %s' % s

        m = re.match('^Link Failure Count: (.*)', line)
        if m:
                l = m.groups()[0]
                links += ', %s' % l

        m = re.match('^MII Status: (.*)', line)
        if m:
                s = m.groups()[0]
                if slaves == '':
                        bond_status = s
                else:
                        slaves += ' %s' % s

                if s != 'up':
                        state = 'FAULT'

print "%s %s (%s) %s %s %s"  % (iface, state, bond_status, active, slaves, links)

Result:结果:

$ ./bondCheck.py
bond0 OK (up) ens3f0 , ens3f0 up, ens3f1 up , 0, 0

Expected:预期的:

bond0: OK (up), Active Slave: ens3f0 , PriSlave: ens3f0(up), SecSlave: ens3f1(up) , LinkFailCountOnPriInt: 0, LinkFailCountOnSecInt: 0

I tried to format in a very basic way as shown below :我尝试以一种非常基本的方式进行格式化,如下所示:

print "%s: %s (%s), Active Slave: %s, PriSlave: %s (%s), SecSlave: %s (%s), LinkFailCountOnPriInt: %s, LinkFailCountOnSecInt: %s"  % (iface, state, bond_status, active, slaves.split(',')[1].split()[0], slaves.split(',')[1].split()[1], slaves.split(',')[2].split()[0], slaves.split(',')[2].split()[1], links.split(',')[1], links.split(',')[2])

RESULT:结果:

bond0: OK (up), Active Slave: ens3f0, PriSlave: ens3f0 (up), SecSlave: ens3f1 (up), LinkFailCountOnPriInt:  1, LinkFailCountOnSecInt:  1

However, I would suggest to get the values into variables prior and then use them in the print statement so as to avoid "out of index" issues during print() , as in rare cases like bond with only one interface will report indexing error while splitting hence good to get the values in variable and suppress the out of index into exception for those cases.但是,我建议先将值放入变量中,然后在 print 语句中使用它们,以避免在 print() 期间出现“索引不足”问题,因为在极少数情况下,例如只有一个接口的债券会报告索引错误,而拆分因此很好地获取变量中的值并将索引外的值抑制为这些情况的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM