Python:如果不在文件中添加新行,请保留现有行?

时间:2016-06-30 18:15:48

标签: python nagios

如果存在现有值,我想保留现有行,或者将该行添加到文件中。我正在编写nagios主机文件的脚本。

主机文件:

define host{
          use             hoststatus
          host_name       linuxhost1
          alias           linuxhost1
          hostgroups      linuxgroup
          Sev             1
}
define host{
          use             hoststatus
          host_name       linuxhost2
          alias           linuxhost2
          hostgroups      linuxgroup
          Sev             2
}
define host{
          use             hoststatus
          host_name       linuxhost3
          alias           linuxhost3
          hostgroups      linuxgroup
}

define host{
          use             hoststatus
          host_name       linuxhost4
          alias           linuxhost4
          hostgroups      linuxgroup
}

我编写了这个脚本,如果该行不存在则添加该行(即最后两个主机中不存在Sev行,所以如果Sev 存在则跳过它)对于前两个主持人我不想添加任何东西。

代码:

import re,sys

with open(sys.argv[1],'r') as f1:
    data = f1.readlines()

txt=''

with open(sys.argv[1],'r') as f3:
    severity=True
    default=4
    vmowner=True
    default_VM = "XXXXXXXXXXXXXX"

    for line in f3:
        if line.strip().startswith('Sev'):
            severity=False

        if line.strip().startswith('Vmowner'):
            vmowner=False

    if severity:
        txt = txt + "\tSev\t\t" + str(default) + "\n"

    if vmowner:
        txt = txt + "\tVmowner\t\t" + str(default_VM) + "\n"
    txt = txt + "\tSevOwner\tYYYYYYYYYYYY\n"
    txt = txt + "}\n"

with open(sys.argv[1],'r+') as f2:
    for line in data:
        if line.strip().startswith('}'):
            line = line.replace('}',txt)
#        f2.write(line)
        print line,

但问题是我没有得到确切的输出。

生成输出:

define host{
          use             hoststatus
          host_name       linuxhost1
          alias           linuxhost1
          hostgroups      linuxgroup
          Sev             1
        Vmowner         XXXXXXXXXXXXXX
        SevOwner        YYYYYYYYYYYY
}

define host{
          use             hoststatus
          host_name       linuxhost2
          alias           linuxhost2
          hostgroups      linuxgroup
          Sev             2
        Vmowner         XXXXXXXXXXXXXX
        SevOwner        YYYYYYYYYYYY
}

define host{
          use             hoststatus
          host_name       linuxhost3
          alias           linuxhost3
          hostgroups      linuxgroup
        Vmowner         XXXXXXXXXXXXXX
        SevOwner        YYYYYYYYYYYY
}


define host{
          use             hoststatus
          host_name       linuxhost4
          alias           linuxhost4
          hostgroups      linuxgroup
        Vmowner         XXXXXXXXXXXXXX
        SevOwner        YYYYYYYYYYYY
}

预期输出:

define host{
          use             hoststatus
          host_name       linuxhost1
          alias           linuxhost1
          hostgroups      linuxgroup
          Sev             1
        Vmowner         XXXXXXXXXXXXXX
        SevOwner        YYYYYYYYYYYY
}

define host{
          use             hoststatus
          host_name       linuxhost2
          alias           linuxhost2
          hostgroups      linuxgroup
          Sev             2
        Vmowner         XXXXXXXXXXXXXX
        SevOwner        YYYYYYYYYYYY
}

define host{
          use             hoststatus
          host_name       linuxhost3
          alias           linuxhost3
          hostgroups      linuxgroup
          Sev             4
          Vmowner         XXXXXXXXXXXXXX
          SevOwner        YYYYYYYYYYYY
}


define host{
          use             hoststatus
          host_name       linuxhost4
          alias           linuxhost4
          hostgroups      linuxgroup
          Sev             4
          Vmowner         XXXXXXXXXXXXXX
          SevOwner        YYYYYYYYYYYY
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

这就是我要做的事情:

  • 阅读整个文件
  • 将其拆分为块
  • 将每个块拆分为行
  • 添加任何所需数据
  • 翻拍整个街区
  • 重制整个文件
from collections import OrderedDict

default_sev = 4
default_VM = "XXXXXXXXXXXXXX"
default_sevowner = "YYYYYYYYYYYY"

def add_missing(data_block):
    data_block = data_block.strip("}\n")
    lines = OrderedDict([line.split() for line in data_block.splitlines()])
    if "Sev" not in lines:
        lines["Sev"] = default_sev
    if "Vmowner" not in lines:
        lines["Vmowner"] = default_VM
    if "SevOwner" not in lines:
        lines["SevOwner"] = default_sevowner
    data = ""
    for key, value in lines.items():
        data += "          {: <16}{}\n".format(key, value)
    return "define host{{\n{}\n}}".format(data)

with open(sys.argv[1]) as f1:
    data = f1.read()

blocks = data.split('define host{') #split into blocks
blocks = filter(None, blocks) #remove empty blocks

with open(sys.argv[1], 'w') as f1:
    for block in blocks:
        f1.write(add_missing(block))

答案 1 :(得分:0)

您并未在每台主机后将严重性标记重置为true。这意味着第一个主机将标志设置为false,然后永远不能输入后面的if语句检查标志。您需要添加逻辑来检查新主机文件的开头,以重置逻辑,如严重性标记。