Module: WinFFI

Defined in:
lib/fluent/plugin/winffi.rb

Overview

A thin wrapper over Win32 API. You can use this as follows:

Usage:

require_relative 'winffi'
WinFFI.GetMemoryStatus()  # => {:TotalPhys=>4294496256,  ... }

Public API:

* WinFFI.GetMemoryStatus()    ... Wraps GetMemoryStatusEx()
* WinFFI.GetPerformanceInfo() ... Wraps GetPerformanceInfo()
* WinFFI.GetWorkstationInfo() ... Wraps NetWkstaGetInfo()
* WinFFI.GetRegistryInfo()    ... Return bits from registry

Defined Under Namespace

Modules: Kernel32, NetAPI32, Psapi

Class Method Summary collapse

Class Method Details

.GetMemoryStatusObject


API functions




85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fluent/plugin/winffi.rb', line 85

def self.GetMemoryStatus()
  buf = Kernel32::MemoryStatusEx.malloc
  buf.dwLength = Kernel32::MemoryStatusEx.size
  if not Kernel32.GlobalMemoryStatusEx(buf)
    raise "GetMemoryStatusEx() failed (err=#{Fiddle.win32_last_error})"
  end

  return {
    :TotalPhys => buf.ullTotalPhys,
    :AvailPhys => buf.ullAvailPhys,
    :TotalPageFile => buf.ullTotalPageFile,
    :AvailPageFile => buf.ullAvailPageFile,
    :TotalVirtual => buf.ullTotalVirtual,
    :AvailVirtual => buf.ullAvailVirtual,
    :AvailExtendedVirtual => buf.ullAvailExtendedVirtual
  }
end

.GetPerformanceInfoObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/winffi.rb', line 103

def self.GetPerformanceInfo()
  buf = Psapi::PERFORMANCE_INFORMATION.malloc
  size = Psapi::PERFORMANCE_INFORMATION.size
  buf.cb = size
  if not Psapi.GetPerformanceInfo(buf, size)
    raise "GetPerformanceInfo() failed (err=#{Fiddle.win32_last_error})"
  end
  return {
    :CommitTotal => buf.CommitTotal,
    :CommitLimit => buf.CommitLimit,
    :CommitPeak => buf.CommitPeak,
    :PhysicalTotal => buf.PhysicalTotal,
    :PhysicalAvailable => buf.PhysicalAvailable,
    :SystemCache => buf.SystemCache,
    :KernelTotal => buf.KernelTotal,
    :KernelPaged => buf.KernelPaged,
    :KernelNonpaged => buf.KernelNonpaged,
    :PageSize => buf.PageSize,
    :HandleCount => buf.HandleCount,
    :ProcessCount => buf.ProcessCount,
    :ThreadCount => buf.ThreadCount
  }
end

.GetRegistryInfoObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fluent/plugin/winffi.rb', line 149

def self.GetRegistryInfo()
  info = {}
  pagesize = 0
  Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Microsoft\Windows NT\CurrentVersion') do |reg|
    info[:ProductName] = reg["ProductName"]
    info[:CurrentBuildNumber] = reg["CurrentBuildNumber"]
  end
  Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management') do |reg|
    for path in reg['ExistingPageFiles', Win32::Registry::REG_MULTI_SZ] do
      pagesize += File.stat(path.gsub("\\??\\", "")).size
    end
  end
  info[:PagingLimitBytes] = pagesize
  return info
end

.GetWorkstationInfoObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fluent/plugin/winffi.rb', line 127

def self.GetWorkstationInfo
  buf = "\0" * Fiddle::SIZEOF_VOIDP
  ret = NetAPI32.NetWkstaGetInfo(nil, 102, buf)
  if ret != 0
    raise "NetWkstaGetInfo() failed (ret=#{ret})"
  end
  ptr = buf.unpack('j')[0]
  if ptr == 0
    raise "NetWkstaGetInfo() returned a null pointer"
  end

  data = NetAPI32::WKSTA_INFO_102.new(ptr)
  info = {
    :PlatformID => data.wki102_platform_id,
    :VersionMajor => data.wki102_ver_major,
    :VersionMinor => data.wki102_ver_minor,
    :LoggedOnUsers => data.wki102_logged_on_users
  }
  NetAPI32.NetApiBufferFree(ptr)
  return info
end