python:sys.argv输入未显示在新创建的文件中

时间:2015-01-05 21:46:13

标签: python

我是python的新手,我正在尝试读取各种配置文件,检查主机组,服务或服务组。此脚本将根据用户输入的内容检查主机组是否存在。如果它不存在,则使用主机组,服务和服务组创建新的配置文件。现在,无论何时创建新的配置文件(主机组,服务,服务组),我都无法通过我的sys.argv为用户提供输入。文件和主机组名称与用户输入相同。这是脚本:

#!/usr/bin/python
import sys
from sys import argv
script, solution_id = argv

from pynag import Model

## This is for the custom nagios module
sys.path.insert(1, '../')
from pynag.Parsers import config

target_host = sys.argv[1]

## Create the plugin option
nc = config('/etc/nagios/nagios.cfg')
nc.parse()


hostgroup = nc.get_hostgroup(target_host)

def addObject():


    # Create hostgroup object
    hg = Model.Hostgroup()

    hg.set_filename('/etc/nagios/objects/solution1/target_host.cfg')

    # Set some attributes
    hg.notification_options = 'n'
    hg.alias = 'target_host'
    hg.hostgroup_name = 'target_host'

    # Save
    hg.save()

    print "hostgroup added"

    # Create service object
    s = Model.Service()

    s.set_filename('/etc/nagios/objects/solution1/target_host.cfg')

    # Set some attributes
    s.notification_options = 'n'
    s.use = 'generic-service'
    s.service_description = 'target_host'
    s.hostgroup_name = 'target_host'

    # Save
    s.save()

    print "service added"

    # Create servicegroup object
    sg = Model.Servicegroup()

    sg.set_filename('/etc/nagios/objects/solution1/target_host.cfg')

    # Set some attributes
    sg.servicegroup_name = 'target_host'
    sg.alias = 'target_host'
    sg.action_url = '/etc/nagios/objects/solution1/target_host.cfg'

    # Save        
    sg.save()

    print "service group added"





#if len(sys.argv) != 2:
    #sys.stderr.write("Usage:  %s 'solution_id'\n" % (sys.argv[0]))
    #sys.exit(2)





if not hostgroup:
    sys.stderr.write("Object not found: %s\n" % sys.argv[1])
    addObject()
    sys.exit(2)

print nc.print_conf(hostgroup)

以下是系统调用以创建新文件时创建的配置文件。

# Configuration file /etc/nagios/objects/solution1/target_host.cfg
# Edited by PyNag on Mon Jan  5 21:42:12 2015

define hostgroup {
     alias                          target_host                   
     hostgroup_name                 target_host                   
}

# Configuration file /etc/nagios/objects/solution1/target_host.cfg
# Edited by PyNag on Mon Jan  5 21:42:12 2015

define service {
     service_description            target_host                   
     use                            generic-service               
     notification_options           n                             
     hostgroup_name                 target_host                   
}

# Configuration file /etc/nagios/objects/solution1/target_host.cfg
# Edited by PyNag on Mon Jan  5 21:42:12 2015

define servicegroup {
     alias                          target_host                   
     action_url                     /etc/nagios/objects/solution1/target_host.cfg
     servicegroup_name              target_host                   
}

1 个答案:

答案 0 :(得分:1)

string literals中的字符序列不会被碰巧具有相同名称的任何变量的内容神奇地替换 - 只要想象会产生的混乱!如果你希望Python使用变量,你需要实际使用变量。如果你对这类事情有疑问,我强烈建议你使用a tutorial

您的脚本中的任何地方都在使用字符串 'target_host'(请注意单引号),您应该将其更改为变量名称 target_host,用于引用您在第12行的用户输入中设置的变量。例如:

hg.notification_options = 'n'
hg.alias = target_host
hg.hostgroup_name = target_host

如果您还需要将用户输入插入配置文件名,我建议您使用string formatting将变量插入基本字符串。在下面的示例中,格式字符串中的字符{0}将替换为target_host变量的内容。

hg.set_filename('/etc/nagios/objects/solution1/{0}.cfg'.format(target_host))