Skip to content

Instantly share code, notes, and snippets.

@kachick
Created May 22, 2012 05:04
Show Gist options
  • Select an option

  • Save kachick/2766732 to your computer and use it in GitHub Desktop.

Select an option

Save kachick/2766732 to your computer and use it in GitHub Desktop.
昔でっちあげたもの - WMIのらっぱと、それを使ったサマリ作成
# Copyright (C) 2010 Kenichi Kamiya
require 'time'
require 'win32ole'
module WMI
VERSION = '0.0.3'
Version = VERSION.freeze
class Host
attr_reader :name_space, :name, :user_id, :password
def initialize(name_space='root/cimv2', name=nil, user_id=nil, password=nil)
@name_space = name_space
@name, @user_id, @password = *(
case name
when nil, false, /\Alocal/
['.', nil, nil]
else
[name, user_id, password]
end
)
end
def eventlog
@eventlog ||= (
permit_security
EventLog.new connection.ExecQuery('Select * From Win32_NTLogEvent')
)
end
def os
@os ||= OSInformation.new(connection.ExecQuery("Select * from Win32_OperatingSystem"))
end
def service
@service ||= Service.new(connection.ExecQuery("Select * from Win32_Service"))
end
private
def connection
@connection ||= WIN32OLE.new('WbemScripting.SWbemLocator').ConnectServer(@name, @name_space, @user_id, @password)
end
def permit_security
# What's "7" ?, that from MSDN/WbemPrivilegeEnum and others
connection.security_.privileges.add 7
end
end
class << self
def EventLog()
end
end
class OSInformation
ELEMENTS = %w[
BuildNumber
BuildType
Caption
CodeSet
CountryCode
CreationClassName
CSCreationClassName
CSDVersion
CSName
Distributed
Locale
Name
OSLanguage
OSProductSuite
OSType
OtherTypeDescription
ServicePackMajorVersion
ServicePackMinorVersion
Version
].map(&:to_sym).freeze
Elements = Struct.new(*ELEMENTS)
def initialize(source)
@source = source
end
def elements
@elements ||= Elements.new.tap do |result|
@source.each do |win_obj|
Elements.members.each do |m|
result[m] = win_obj.__send__ m
end
end
end
end
def each
@source.each do |win_obj|
Elements.members.each do |m|
yield win_obj.__send__(m)
end
end
end
end
class EventLog
ELEMENTS = [:category, :number, :host_name, :id, :type, :generated, :message].freeze
Entry = Struct.new(*ELEMENTS)
NUMBER_TO_NAME_DEFINES = {
1 => :Error,
2 => :Warning,
3 => :Information,
4 => :SecuritySuccess,
5 => :SecurityFail
}.freeze
def initialize(source)
@source = source
end
def each
@source.each do |evt|
yield Entry.new(
evt.Logfile,
evt.RecordNumber,
evt.SourceName,
evt.EventCode,
type_number_to_name(evt.EventType),
Time.parse(evt.TimeGenerated),
evt.Message
)
end
end
def logs
@logs ||= [].tap{|result|
each do |log|
result << log
end
result.reverse!
}
end
def categorized
logs.group_by(&:category)
end
def type_number_to_name(number)
NUMBER_TO_NAME_DEFINES[number] || :"Unknown/#{number}"
end
end
class Service
ELEMENTS = %w[
AcceptPause
AcceptStop
Caption
CheckPoint
CreationClassName
Description
DesktopInteract
DisplayName
ErrorControl
ExitCode
InstallDate
Name
PathName
ProcessId
ServiceSpecificExitCode
ServiceType
Started
StartMode
StartName
State
Status
SystemCreationClassName
SystemName
TagId
WaitHint
].map(&:to_sym).freeze
Entry = Struct.new(*ELEMENTS)
def initialize(source)
@source = source
end
def each
@source.each do |win_obj|
yield Entry.new(*Entry.members.map{|m|win_obj.__send__ m})
end
end
end
end
#!/usr/bin/ruby -w -EWindows-31J
# coding: Windows-31J
# Copyright (C) 2010 Kenichi Kamiya
require 'csv'
require_relative 'wmi'
class String
def pathable
gsub(%r![/:*?"<>|\\]!, '!')
end
end
def usage
puts 'ARGV[0]="setting file path"', 'ex) this.rb ./host_infos.rb'
end
unless ARGV[0] && load(CONFIG_PATH = ARGV[0])
usage
exit 1
end
OS_SUMMARY = %w[Caption CSDVersion Version ServicePackMajorVersion ServicePackMinorVersion CodeSet].map(&:to_sym).freeze
include WMI
HOST_INFOS.each do |host_info|
name_space, hostname, id, password = host_info[:name_space], host_info[:hostname], host_info[:id], host_info[:password]
puts "Now searching for #{hostname}."
host = Host.new name_space, hostname, id, password
CSV.open CONFIG_PATH + ".#{hostname}.os.detail.csv".pathable, 'w', headers: OSInformation::ELEMENTS, write_headers: true do |csv|
csv << host.os.elements
end
CSV.open CONFIG_PATH + ".#{hostname}.os.summary.csv", 'w', headers: OS_SUMMARY, write_headers: true do |csv|
csv << OS_SUMMARY.map{|name|host.os.elements[name]}
end
CSV.open CONFIG_PATH + ".#{hostname}.service.detail.csv".pathable, 'w', headers: Service::ELEMENTS, write_headers: true do |csv|
host.service.each do |service|
csv << service
end
end
categorized_eventlog = host.eventlog.categorized
categorized_eventlog.each_pair do |category, logs|
CSV.open CONFIG_PATH + ".#{hostname}.eventlog.#{category}.detail.csv".pathable, 'w', headers: EventLog::ELEMENTS, write_headers: true do |csv|
logs.each do |log|
csv << log
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment