1. #coding=utf8
  2. import sys
  3. import traceback
  4. import win32con
  5. import win32evtlog
  6. import win32evtlogutil
  7. import winerror
  8. try:
  9. from _utils.patrol2 import run_cmd, data_format, report_format
  10. except:
  11. print 'no module _utils'
  12. import platform
  13. import datetime,psutil
  14.  
  15. def getAllEvents(server, logtypes,time_flag):
  16. """
  17. """
  18. if not server:
  19. serverName = "localhost"
  20. else:
  21. serverName = server
  22. for logtype in logtypes:
  23. result=getEventLogs(server, logtype,time_flag)
  24. return result
  25.  
  26. # ----------------------------------------------------------------------
  27. def getEventLogs(server, logtype, time_flag,logPath=None):
  28. """
  29. Get the event logs from the specified machine according to the
  30. logtype (Example: Application) and save it to the appropriately
  31. named log file
  32. """
  33. print "Logging %s events" % logtype
  34. # log = codecs.open(logPath, encoding='utf-8', mode='w')
  35. # line_break = '-' * 80
  36. #
  37. # log.write("\n%s Log of %s Events\n" % (server, logtype))
  38. # log.write("Created: %s\n\n" % time.ctime())
  39. # log.write("\n" + line_break + "\n")
  40. # 读取本机的,system系统日志
  41. hand = win32evtlog.OpenEventLog(server, logtype)
  42. # 获取system日志的总行数
  43. total = win32evtlog.GetNumberOfEventLogRecords(hand)
  44. print "Total events in %s = %s" % (logtype, total)
  45. flags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
  46. events = win32evtlog.ReadEventLog(hand, flags, 0)
  47. # 错误级别类型
  48. evt_dict = {win32con.EVENTLOG_AUDIT_FAILURE: 'EVENTLOG_AUDIT_FAILURE',
  49. win32con.EVENTLOG_AUDIT_SUCCESS: 'EVENTLOG_AUDIT_SUCCESS',
  50. win32con.EVENTLOG_INFORMATION_TYPE: 'EVENTLOG_INFORMATION_TYPE',
  51. win32con.EVENTLOG_WARNING_TYPE: 'EVENTLOG_WARNING_TYPE',
  52. win32con.EVENTLOG_ERROR_TYPE: 'EVENTLOG_ERROR_TYPE'}
  53.  
  54. try:
  55. events = 1
  56. count=0
  57. while events:
  58. events = win32evtlog.ReadEventLog(hand, flags, 0)
  59.  
  60. for ev_obj in events:
  61. the_time = ev_obj.TimeGenerated.Format() # '12/23/99 15:54:09'
  62. the_time=datetime.datetime.strptime(the_time, "%m/%d/%y %H:%M:%S")
  63. if the_time < time_flag:
  64. continue
  65. evt_id = str(winerror.HRESULT_CODE(ev_obj.EventID))
  66. computer = str(ev_obj.ComputerName)
  67. cat = ev_obj.EventCategory
  68. ## seconds=date2sec(the_time)
  69. record = ev_obj.RecordNumber
  70. msg = win32evtlogutil.SafeFormatMessage(ev_obj, logtype)
  71.  
  72. source = str(ev_obj.SourceName)
  73. if not ev_obj.EventType in evt_dict.keys():
  74. evt_type = "unknown"
  75. else:
  76. evt_type = str(evt_dict[ev_obj.EventType])
  77.  
  78. if evt_id=='4625':
  79. count+=1
  80. # log.write("Event Date/Time: %s\n" % the_time)
  81. # log.write("Event ID / Type: %s / %s\n" % (evt_id, evt_type))
  82. # log.write("Record #%s\n" % record)
  83. # log.write("Source: %s\n\n" % source)
  84. # log.write(msg)
  85. # log.write("\n\n")
  86. # log.write(line_break)
  87. # log.write("\n\n")
  88. return count
  89. except:
  90. print traceback.print_exc(sys.exc_info())
  91. sys.exit(1)
  92.  
  93. def get_start_time():
  94. dt = datetime.datetime.fromtimestamp(psutil.boot_time())
  95. return dt
  96.  
  97. if __name__ == "__main__":
  98. time_flag= get_start_time()
  99. print time_flag
  100. server = None # None = local machine
  101. logTypes = ["Security"]#"System", "Application",
  102. result=getAllEvents(server, logTypes,time_flag)
  103. if result==0:
  104. alert=0
  105. else:
  106. alert = 1
  107.  
  108. hostname=platform.node()
  109. report=data_format('登录失败次数',result,alert)
  110. reports=report_format(hostname,report,is_json=True)
  111.  
  112. print reports

  

windows使用python原生组件包获取系统日志信息的更多相关文章

  1. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop

    [源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...

  2. 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化

    [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  3. python 面向对象编程、获取对象信息

    面向对象与面向过程 参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0 ...

  4. 测试成长记录:python调adb无法获取设备信息bug记录

    背景介绍: 一直在负责公司Android自动化的编写工作,采用的是uiautomator2,需要获取设备id来连接设备,就是 adb devices 问题描述: 之前一直用 subprocess.ch ...

  5. python+adb实现自动化获取手机信息

    首先我们先看一下使用adb查看Android手机信息的指令 #获取手机名称NAME = 'adb shell getprop ro.product.model'#获取手机版本VERSION = 'ad ...

  6. 利用wireshark抓包获取cookie信息

    以下是一些过滤规则: 1. 百度的cookie: http.cookie matches "BDUSS" 2. 博客园的cookie: http.cookie matches &q ...

  7. Python连接MySQL数据库获取数据绘制柱状图

    一.Python通过pymysql包获取MySQL数据库中的数据(没有对应包的可以通过pip install pymysql 安装对应的包) import matplotlib.pyplot as p ...

  8. 微信小程序在组件中获取界面上的节点信息wx.createSelectorQuery

    节点信息查询 API 可以用于获取节点属性.样式.在界面上的位置等信息. 最常见的用法是使用这个接口来查询某个节点的当前位置,以及界面的滚动位置. 示例代码: const query = wx.cre ...

  9. 小程序wx.getUserInfo获取用户信息方案介绍

    问题模块 框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本 API和组件 - -   - -     背景 小程序一个比较重要的能力就是获取用户信息,也就是使用 wx.g ...

随机推荐

  1. yml实例

    producer.yml apiVersion: v1kind: Podmetadata:name: producer-consumerspec:containers:- image: busybox ...

  2. Elasticsearch利用cat api快速查看集群状态、内存、磁盘使用情况

    使用场景 当Elasticsearch集群中有节点挂掉,我们可以去查看集群的日志信息查找错误,不过在查找错误日志之前,我们可以通过elasticsearch的cat api简单判断下各个节点的状态,包 ...

  3. android广播(内部类)使用

    1.广播定义在一个单独的文件中 源码: public class MessageReceiver extends BroadcastReceiver{ @Override public void on ...

  4. 洛谷P1247 取火柴游戏

    经典NIM游戏. 取XOR和即可. 注意输出方案时,找到大于异或和sum的,变为a[i] ^ sum即可. #include <cstdio> ; int a[N]; int main() ...

  5. 串口、COM口、TTL、RS-232、RS-485区别详解

    1.串口.COM口是指的物理接口形式(硬件).而TTL.RS-232.RS-485是指的电平标准(电信号).    2.接设备的时候,一般只接GND RX TX.不会接Vcc或者+3.3v的电源线,避 ...

  6. Int2BinaryString.java

    /****************************************************************************** * Compilation: javac ...

  7. Java面向对象----个人参考资料

    Java面向对象 :什么是面向对象.类与对象.封装.构造方法.static关键字.继承.抽象类.接口.多态 一.什么是面向对象 1.面向过程思想 面向过程:(PO,Procedure Oriented ...

  8. CodeForces911D 逆序对

    http://codeforces.com/problemset/problem/911/D 题意 给你一个长度为N的序列,有M次操作.每次翻转[l,r]的区间,每次操作后询问序列逆序对个数的奇偶性 ...

  9. spring-data-jpa初步认识

    什么是spring data jpa? spirng data jpa是spring提供的一套简化JPA开发的框架,按照约定好的[方法命名规则]写dao层接口,就可以在不写接口实现的情况下,实现对数据 ...

  10. javascript 字符串的连接和截取

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...