python写简单的nagios主机配置文件

__author__ = ‘Administrator‘
import sys,os
dir_cfg="./"
#file=sys.argv[1]
#hostname=sys.argv[2]
#app=sys.argv[3]
#ip=sys.argv[4]
#port=sys.argv[5]

def test(file,hostname,app,ip,port):
    filepath=dir_cfg+file
    if os.path.exists(filepath):
        try:
            wf=open(filepath,‘a‘)
            wf.write(data_02%(hostname,app,port))
            wf.close()
        except:
            print(‘Write file fail2!‘)
            wf.close()
    else:
        try:
            wf=open(filepath,‘w‘)
            wf.write(data_01%(hostname,hostname,ip,hostname,app,port))
            wf.close()
        except:
            print(‘Write file fail!‘)
            wf.close()

data_01=‘‘‘
define host{
        use                     generic-host
        host_name               %s
        alias                   %s
        address                 %s
        }

define service {
        use                             generic-service
        host_name                       %s
        service_description             %s
        check_command                   check_tcp!%s
}
‘‘‘
data_02=‘‘‘
define service {
        use                             generic-service
        host_name                       %s
        service_description             %s
        check_command                   check_tcp!%s
}
‘‘‘
#print("%-40s %-10s"%(‘7899999999999999999999‘,‘hhh‘))

if __name__ == "__main__":
    test(‘kkk.cfg‘,‘kkk‘,‘SSH‘,‘127.0.0.1‘,‘80‘)


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。