如何在不重新启动守护程序的情况下读取和截断snmptrapd日志文件

2024-09-30 05:30:46 发布

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

python让naga脚本执行这个检查。这个脚本的功能非常简单,它只解析一个日志并匹配一些用于构造nagios检查输出的信息。日志是一个snmptrapd日志,它记录来自其他服务器的陷阱,然后将它们记录在/var/log/snmptrapd中,之后我用脚本解析它们。为了获得最新的陷阱,每次读完日志后,我都会从python中删除日志。为了保存信息,我做了一个cron作业,将日志的内容以比nagios检查间隔稍小的时间间隔复制到另一个日志中。我不明白的是,为什么日志会增长这么多(我的意思是消息日志的信息量增加了1000倍,我猜是更小的)。根据我在日志中看到的,有很多特殊的字符,比如^@,我认为这是通过我从pyton操作文件的方式完成的,但是看到我有三周的经验,我似乎无法解决问题。在

脚本代码如下:

import sys, os, re

validstring = "OK"
filename = "/var/log/snmptrapd.log"

if os.stat(filename)[6] == 0:
        print validstring
        sys.exit()

else:
        f = open(filename,"r")
        sharestring = ""
        line1 = []
        patte0 = re.compile("[0-9]+-[0-9]+-[0-9]+")
        patte2 = re.compile("NG: [a-zA-Z\s=0-9]+.*")
        for line in f:
                line1 = line.split(" ")
                if re.search(patte0,line1[0]):
                        sharestring = sharestring + line1[1] + " "
                        continue
                result2 = re.search(patte2,line)
                if result2:
                        result22 = result2.group()
                        result22 = result22.replace("NG:","")
                        sharestring = sharestring + result22 + " "
        f.close()
        f1 = open(filename,"w")
        f1.close()
        print sharestring
        sys.exit(2)

~

日志看起来像:

^{pr2}$

我很确定这和我清除文件的方式有关,但我搞不懂。如果你有什么想法,我会很感兴趣的。谢谢您。在

作为一个关于大小的信息,我有93行(如Vim所说),日志占用161K,这是不正常的,因为这些行非常短。在

好吧,这和我读和删除文件的方式无关。是snmptrapd守护进程中的某个东西,当我删除它的日志文件时,它正在执行此操作。我已经修改了我的代码,现在我在打开文件之前将SIGSTOP发送给snmptrapd reight,我对文件进行了修改,然后在我完成后发送SIGCONT,但似乎我也经历了同样的行为。新代码看起来像(不同部分):

else:
    command = "pidof snmptrapd"
    p=subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE)
    pidstring = p.stdout.readline()
    patte1 = re.compile("[0-9]+")
    pidnr = re.search(patte1,pidstring)
    pid = pidnr.group()
    os.kill(int(pid), SIGSTOP)
    time.sleep(0.5)
    f = open(filename,"r+")
    sharestring = ""

以及

                  sharestring = sharestring + result22 + " "
    f.truncate(0)
    f.close()
    time.sleep(0.5)
    os.kill(int(pid), SIGCONT)
    print sharestring

我在考虑停止守护进程删除文件,然后用适当的权限重新创建它并启动守护程序。在


Tags: 文件代码re脚本logifossys
1条回答
网友
1楼 · 发布于 2024-09-30 05:30:46

我想你做不到,但这里有一些事情可以试试

截断文件

f1 = open(filename, 'w')
f1.close()

是一种删除文件内容的骇人听闻的副作用方法,如果其他应用程序打开了该文件,则根据底层操作系统的不同,可能会产生不希望看到的副作用。在

使用文件对象方法truncate()

^{}

Truncate the file's size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note that if a specified size exceeds the file's current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants.

可能唯一确定的方法是

在脚本开始时停止snmptrapd进程,使用正确的os module函数remove,然后重新创建文件并在脚本末尾重新启动snmptrapd守护进程。在

^{pr2}$

Remove (delete) the file path. If path is a directory, OSError is raised; see rmdir() below to remove a directory. This is identical to the unlink() function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

共享资源关注

如果两个进程试图在没有某种锁定机制的情况下写入单个文件,并且文件发生了不确定的情况,那么您可能仍然会遇到问题。我打赌你可以发送一个SIGINT或类似于你的守护进程的东西,让它重新读取文件或其他东西,检查你的文档。在

操作共享资源,尤其是不使用独占锁定的文件资源将会很麻烦,尤其是对于文件系统缓存和应用程序数据缓存。在

相关问题 更多 >

    热门问题