霍比特人监视器和Python纸条

2024-09-30 01:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个非常基本的python脚本(没有定义类或函数),运行良好。我要把数据发给霍比特监视器。 在bash中,我会像这样发送它,$BB和{}由hobbit程序在环境路径中设置,变量$MACHINE和{}主机和测试名称,$COLOR由脚本确定并由hobbit显示,MSG包含实际数据:

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

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

^{pr2}$

你知道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()   

Tags: importlogiftimeossysalertago

热门问题