我有一个python脚本,它拆分Icinga GUI命令并将特定命令发送到卫星主机。
你知道吗
这个脚本完成了它的目的,但是我遇到了两个问题:
第一:我没有在第16行(“cgi\u cmd=open(cgi\u cmd\u file,'r')”之后得到调试输出
第二:脚本使用了高达100%的CPU,即使每次获取空行时都应该暂停5秒。
你知道吗
我需要一些帮助,谢谢!你知道吗
我的第一个问题与命名管道的工作方式有关。只有先打开它进行写入时,才能打开它进行读取。
第二个问题是我检查回路内部线路的方式,多亏了彼得·伍德。
#!/usr/bin/python
import string, os, sys, re, spur, logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('Imports done.')
# Define files and open
cgi_cmd_file = '/var/spool/icinga/cmd/cgi.cmd'
icinga_cmd_file = '/var/spool/icinga/cmd/icinga.cmd'
icinga_log_file = '/var/log/icinga/iscrdr_collector.log'
sat_log_file = '/var/log/icinga/iscrdr_satelite.log'
logging.debug('Files defined.')
cgi_cmd = open(cgi_cmd_file, 'r')
icinga_cmd = open(icinga_cmd_file, 'w')
logging.debug('Files opened.')
# Compile regex pattern for reschedule service and host checks
pattern = re.compile('SCHEDULE_.*_.*_CHECK.+?\d{10}')
logging.debug('Regex pattern compiled.')
# List of satelite hosts
satelite_hosts = [
['hostname','user','pw'],
['hostname','user','pw'],
['hostname','user','pw'],
]
logging.debug('Satelite hosts defined.')
#
# Code Area
#
def send_to_sat(message):
command = "/bin/echo '" + string.strip(message) + "' > /var/spool/icinga/cmd/icinga.cmd"
for i in satelite_hosts:
shell = spur.SshShell(hostname=i[0], username=i[1], password=i[2])
with shell:
result = shell.run(["sh", "-c", command])
logging.debug('Command send to' + i[0] + '.')
with open(sat_log_file, 'a') as sat_log:
sat_log.write(string.strip(message + str(result.return_code)) + "\n")
def send_to_icinga(message):
with open(icinga_cmd_file, 'w') as icinga_cmd:
icinga_cmd.write(string.strip(message) + "\n")
with open(icinga_log_file, 'a') as icinga_log:
icinga_log.write(string.strip(message) + "\n")
logging.debug('Command send to Icinga.')
# Split reschedule commands from command stream and write them to file
while True:
logging.debug('This is a loop run.')
line = cgi_cmd.readline()
if re.findall(r'SCHEDULE_.*_.*_CHECK.+?\d{10}', line):
send_to_sat(line)
elif line != '':
send_to_icinga(line)
else:
logging.debug('No Command.')
time.sleep(5)
continue
目前没有回答
相关问题 更多 >
编程相关推荐