0

我有一个运行良好的非常基本的 python 脚本(没有定义类或函数)。我需要将数据发送到 hobbit 监视器。在 bash 中,我会这样发送它,$BB$$BBDISP通过 hobbit 程序、变量$MACHINE以及$TEST主机和测试名称在环境路径中设置,$COLOR由脚本确定并由 hobbit 显示,MSG其中包含实际数据:

$BB $BBDISP "status $MACHINE.$TEST $COLOR `date` $MSG"

对于 Python,我有这个,但它不起作用:

[os.environ['BB'], os.environ['BBDISP'], "status arin,fliers,com.store-backup, color, date_now, msg, alert"]

知道如何调整 Python 语法吗?

这是脚本:

#!/usr/bin/python26
# import needed modules, using sys, os, re for os level use, glob for pattern matching, and time for log related timestamping
import sys
import os
import subprocess
import glob
import time
import re
import datetime

#BB = ("/home/hobbit/bin/bb")
date_now = datetime.datetime.now()

#only run if backup log exists:
if os.path.exists("/mnt/backup/full"):
    now = time.time()
    log_mostrc = max(glob.glob('/mnt/backup/full/*.log'), key=os.path.getctime)
    c_time = time.ctime(os.path.getctime(log_mostrc))

    #check if backup was successful
    if "innobackupex: completed OK!" in open(log_mostrc).read():
            msg = "innobackupex: completed OK!"
    else:
            msg = "backup may not have been successful. Please investigate."
            sys.exit()

    #time measures
    file_epoch = os.path.getctime(log_mostrc)
    hours_recent = (now - (26*60*60)) # 26 hours ago
    hours_old = (now - (52*60*60)) # 52 hours ago
    sec_ago = now - file_epoch
    hours_ago = int(sec_ago) / 3660



    #test age of files
    if file_epoch < hours_old:
            alert = "backup created over 52 hours ago! Now %d hours old! Please investigate!" % hours_ago
            color = 'red'
    elif file_epoch < hours_recent:
            alert = "backup created over 26 hours ago! Now %d hours old! Please investigate." % hours_ago
            color = 'yellow'
    else:
            alert = "backup created about %d hours ago. Still fresh." % hours_ago
            color = 'green'

    #       print msg                               
    #       print color, log_mostrc, c_time, alert


    [os.environ['BB'], os.environ['BBDISP'], "status arin,flier,com.store-backup, color, date_now, msg, alert"]


else:
    sys.exit()   
4

1 回答 1

1

代替

[os.environ['BB'], os.environ['BBDISP'], "status arin,flier,com.store-backup, color, date_now, msg, alert"] 

您应该从 python 运行命令,例如:

os.system(
    ' '.join([os.environ['BB'], os.environ['BBDISP'], '"status arin,flier,com.store-backup,', ','.join([color, str(date_now), msg, alert,'"'])])
)
于 2014-11-08T00:14:36.270 回答