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



如果存在现有值,我想保留现有行,否则将行添加到文件中。我正在编写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 + "tSevtt" + str(default) + "n"
    if vmowner:
        txt = txt + "tVmownertt" + str(default_VM) + "n"
    txt = txt + "tSevOwnertYYYYYYYYYYYYn"
    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
}

预先感谢。

这是我要做的:

  • 阅读整个文件
  • 将其分成块
  • 将每个块分为线
  • 添加所有需要的数据
  • 重制整个块
  • 重制整个文件

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))

您不会在每个主机之后将严重性标志重置为true。这意味着第一个主机将标志设置为false,然后再也无法输入语句检查标志的语句检查。您需要添加逻辑以检查新主机文件的开头以像您的严重性标志一样重置逻辑。

相关内容

  • 没有找到相关文章

最新更新