使用pywin32获取过去3个月内事件日志中所有错误条目的源头、日期时间和消息列表

问题描述 投票:0回答:1

编辑;我原来的帖子对我的问题太模糊了,所以在我的任务工作了一段时间后,我已经调整了大部分的代码。然而,我仍然无法让它工作。现在,当我在cmd提示符下运行脚本时,我得到了这个消息......

Traceback (most recent call last):
  File "/PythonScriptsR2/autoadmin_a1_errors.py", line 45, in <module>
    log_handle_three = win32evtlog.OpenEventLog(computer,log_type_three)
pywintypes.error: (1722, 'OpenEventLogW', 'The RPC server is 
unavailable.')

这是我现在的代码(我在第45行左右写了一些注释,这样更容易找到)。

import win32evtlog
import win32evtlogutil
import win32security
import win32con
import time
import winerror
import re
import string
import sys
import traceback

####################################################################
# All error entries for the last 3 months
########
#set date format
def date2sec(self,evt_date):
    '''
    convert '12/23/99 15:54:09' to seconds
    print '333333',evt_date
    '''
    regexp=re.compile('(.*)\\s(.*)')
    reg_result=regexp.search(evt_date)
    date = reg.result.group(1)
    time = reg_result.group(2)
    (mon,day,yr) = map(lambda x: sring.atoi(x),string.split(date,'/'))
    (hr,min,sec) = map(lambda x: sring.atoi(x),string.split(time,':'))
    tup = [yr,mon,day,hr,min,sec,0,0,0]
    sec = time.mktime(tup)
    return sec
################
#Initialize variables
flags_three=win32evtlog.EVENTLOG_BACKWARDS_READ|\
            win32evtlog.EVENTLOG_SEQUENTIAL_READ

    #Dictionary to convert event type into human readable form
evt_dict={win32con.EVENTLOG_AUDIT_FAILURE:'EVENTLOG_AUDIT_FAILURE',\
          win32con.EVENTLOG_AUDIT_SUCCESS:'EVENTLOG_AUDIT_SUCCESS',\
          win32con.EVENTLOG_INFORMATION_TYPE:'EVENTLOG_INFORMATION_TYPE',\
          win32con.EVENTLOG_WARNING_TYPE:'EVENTLOG_WARNING_TYPE',\
          win32con.EVENTLOG_ERROR_TYPE:'EVENTLOG_ERROR_TYPE'}

computer='bedrock'
log_type_three='System'
begin_sec=time.time()
begin_time=time.strftime('%H:%M:%S ',time.localtime(begin_sec))
############                 \/ Line 45 \/                   #############
log_handle_three=win32evtlog.OpenEventLog(computer,log_type_three) #line45
############                 /\ Line 45 /\                   #############
################
#Open the Event Log
print(log_type_three,' error events found from the last three months 
since:',begin_time)
try:
    ev_ents=1
    while ev_ents:
        ev_ents=win32evtlog.ReadEventLog(log_handle_three,flags_three, 0)
        for ev_obj in ev_ents:
#################
            #check if  the event is recent enough
            #checking data from the last three months
            date = ev_ent.TimeGenerated.Format()
            time = ev_ent.TimeGenerated.Format()
            seconds = date2sec(date)
            if seconds < begin_sec-7862400: break
#################
            #if the data is recent enough/from the correct time frame, 
            #print it  
            source=str(ev_obj.SourceName)
            message=str(win32evtlogutil.SafeFormatMessage(ev_obj,\ 
                                                          logtype))
            record = str(ev_obj.RecordNumber)
            print(string.join((record,source,date,\
                               time,message[0:15]),':'))
        if seconds < begin_sec-7862400: break #You need to get out of 
                                              #the while loop as well
        win32evtlog.CloseEventLog(log_handle_three)
except:
    print(traceback.print_exc(sys.exc_info()))

所以现在我的问题是,这个错误信息是什么意思,为什么我会收到这个错误信息,以及我可以做什么事情来尝试并解决它。

我想他可能希望我们使用windows powershell来调用脚本,然而,当我这样做的时候,我得到的错误信息和我在cmd提示符下得到的一样,所以我不确定这是否有区别。

python error-handling pywin32 event-log
1个回答
0
投票

出于某种原因,我想它可能是想使用网络协议栈来访问 "bedrock",而不是直接从本地机器上抓取它。我会确保 "bedrock "是你在CLI中执行 "hostname "时得到的实际计算机名称。

我遇到过一个类似的问题,使用 "localhost "这个计算机名,在某些环境下无法工作,出现了同样的错误。将该值改成实际的本地计算机名后,我就解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.