sys-uname 0.8.6 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,465 @@
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'time'
4
+
5
+ # The Sys module provides a namespace only.
6
+ module Sys
7
+
8
+ # The Uname class encapsulates uname (platform) information.
9
+ class Uname
10
+
11
+ # This is the error raised if any of the Sys::Uname methods should fail.
12
+ class Error < StandardError; end
13
+
14
+ VERSION = '0.9.0'
15
+
16
+ fields = %w[
17
+ boot_device
18
+ build_number
19
+ build_type
20
+ caption
21
+ code_set
22
+ country_code
23
+ creation_class_name
24
+ cscreation_class_name
25
+ csd_version
26
+ cs_name
27
+ current_time_zone
28
+ debug
29
+ description
30
+ distributed
31
+ foreground_application_boost
32
+ free_physical_memory
33
+ free_space_in_paging_files
34
+ free_virtual_memory
35
+ install_date
36
+ last_bootup_time
37
+ local_date_time
38
+ locale
39
+ manufacturer
40
+ max_number_of_processes
41
+ max_process_memory_size
42
+ name
43
+ number_of_licensed_users
44
+ number_of_processes
45
+ number_of_users
46
+ organization
47
+ os_language
48
+ os_product_suite
49
+ os_type
50
+ other_type_description
51
+ plus_product_id
52
+ plus_version_number
53
+ primary
54
+ quantum_length
55
+ quantum_type
56
+ registered_user
57
+ serial_number
58
+ service_pack_major_version
59
+ service_pack_minor_version
60
+ size_stored_in_paging_files
61
+ status
62
+ system_device
63
+ system_directory
64
+ total_swap_space_size
65
+ total_virtual_memory_size
66
+ total_visible_memory_size
67
+ version
68
+ windows_directory
69
+ ]
70
+
71
+ # The UnameStruct is used to store platform information for some methods.
72
+ UnameStruct = Struct.new("UnameStruct", *fields)
73
+
74
+ # Returns the version plus patch information of the operating system,
75
+ # separated by a hyphen, e.g. "2915-Service Pack 2".
76
+ #--
77
+ # The instance name is unpredictable, so we have to resort to using
78
+ # the 'InstancesOf' method to get the data we need, rather than
79
+ # including it as part of the connection.
80
+ #
81
+ def self.version(host=Socket.gethostname)
82
+ cs = "winmgmts://#{host}/root/cimv2"
83
+ begin
84
+ wmi = WIN32OLE.connect(cs)
85
+ rescue WIN32OLERuntimeError => e
86
+ raise Error, e
87
+ else
88
+ query = "select * from Win32_OperatingSystem"
89
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
90
+ str = "#{ole.Version} #{ole.BuildNumber}-"
91
+ str << "#{ole.ServicePackMajorVersion}"
92
+ return str
93
+ }
94
+ end
95
+ end
96
+
97
+ # Returns the operating system name, e.g. "Microsoft Windows XP Home"
98
+ #
99
+ def self.sysname(host=Socket.gethostname)
100
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
101
+ cs << "//#{host}/root/cimv2"
102
+ begin
103
+ wmi = WIN32OLE.connect(cs)
104
+ rescue WIN32OLERuntimeError => e
105
+ raise Error, e
106
+ else
107
+ query = "select * from Win32_OperatingSystem"
108
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
109
+ return ole.Caption
110
+ }
111
+ end
112
+ end
113
+
114
+ # Returns the nodename. This is usually, but not necessarily, the
115
+ # same as the system's hostname.
116
+ #
117
+ def self.nodename(host=Socket.gethostname)
118
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
119
+ cs << "//#{host}/root/cimv2"
120
+ begin
121
+ wmi = WIN32OLE.connect(cs)
122
+ rescue WIN32OLERuntimeError => e
123
+ raise Error, e
124
+ else
125
+ query = "select * from Win32_OperatingSystem"
126
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
127
+ return ole.CSName
128
+ }
129
+ end
130
+ end
131
+
132
+ # Returns the machine hardware type. e.g. "i686".
133
+ #--
134
+ # This may or may not return the expected value because some CPU types
135
+ # were unknown to the OS when the OS was originally released. It
136
+ # appears that MS doesn't necessarily patch this, either.
137
+ #
138
+ def self.machine(cpu_num=0, host=Socket.gethostname)
139
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
140
+ cs << "//#{host}/root/cimv2:Win32_Processor='cpu#{cpu_num}'"
141
+ begin
142
+ wmi = WIN32OLE.connect(cs)
143
+ rescue WIN32OLERuntimeError => e
144
+ raise Error, e
145
+ else
146
+ # Convert a family number into the equivalent string
147
+ case wmi.Family
148
+ when 1
149
+ return "Other"
150
+ when 2
151
+ return "Unknown"
152
+ when 3
153
+ return "8086"
154
+ when 4
155
+ return "80286"
156
+ when 5
157
+ return "80386"
158
+ when 6
159
+ return "80486"
160
+ when 7
161
+ return "8087"
162
+ when 8
163
+ return "80287"
164
+ when 9
165
+ return "80387"
166
+ when 10
167
+ return "80487"
168
+ when 11
169
+ return "Pentium brand"
170
+ when 12
171
+ return "Pentium Pro"
172
+ when 13
173
+ return "Pentium II"
174
+ when 14
175
+ return "Pentium processor with MMX technology"
176
+ when 15
177
+ return "Celeron"
178
+ when 16
179
+ return "Pentium II Xeon"
180
+ when 17
181
+ return "Pentium III"
182
+ when 18
183
+ return "M1 Family"
184
+ when 19
185
+ return "M2 Family"
186
+ when 24
187
+ return "K5 Family"
188
+ when 25
189
+ return "K6 Family"
190
+ when 26
191
+ return "K6-2"
192
+ when 27
193
+ return "K6-3"
194
+ when 28
195
+ return "AMD Athlon Processor Family"
196
+ when 29
197
+ return "AMD Duron Processor"
198
+ when 30
199
+ return "AMD2900 Family"
200
+ when 31
201
+ return "K6-2+"
202
+ when 32
203
+ return "Power PC Family"
204
+ when 33
205
+ return "Power PC 601"
206
+ when 34
207
+ return "Power PC 603"
208
+ when 35
209
+ return "Power PC 603+"
210
+ when 36
211
+ return "Power PC 604"
212
+ when 37
213
+ return "Power PC 620"
214
+ when 38
215
+ return "Power PC X704"
216
+ when 39
217
+ return "Power PC 750"
218
+ when 48
219
+ return "Alpha Family"
220
+ when 49
221
+ return "Alpha 21064"
222
+ when 50
223
+ return "Alpha 21066"
224
+ when 51
225
+ return "Alpha 21164"
226
+ when 52
227
+ return "Alpha 21164PC"
228
+ when 53
229
+ return "Alpha 21164a"
230
+ when 54
231
+ return "Alpha 21264"
232
+ when 55
233
+ return "Alpha 21364"
234
+ when 64
235
+ return "MIPS Family"
236
+ when 65
237
+ return "MIPS R4000"
238
+ when 66
239
+ return "MIPS R4200"
240
+ when 67
241
+ return "MIPS R4400"
242
+ when 68
243
+ return "MIPS R4600"
244
+ when 69
245
+ return "MIPS R10000"
246
+ when 80
247
+ return "SPARC Family"
248
+ when 81
249
+ return "SuperSPARC"
250
+ when 82
251
+ return "microSPARC II"
252
+ when 83
253
+ return "microSPARC IIep"
254
+ when 84
255
+ return "UltraSPARC"
256
+ when 85
257
+ return "UltraSPARC II"
258
+ when 86
259
+ return "UltraSPARC IIi"
260
+ when 87
261
+ return "UltraSPARC III"
262
+ when 88
263
+ return "UltraSPARC IIIi"
264
+ when 96
265
+ return "68040"
266
+ when 97
267
+ return "68xxx Family"
268
+ when 98
269
+ return "68000"
270
+ when 99
271
+ return "68010"
272
+ when 100
273
+ return "68020"
274
+ when 101
275
+ return "68030"
276
+ when 112
277
+ return "Hobbit Family"
278
+ when 120
279
+ return "Crusoe TM5000 Family"
280
+ when 121
281
+ return "Crusoe TM3000 Family"
282
+ when 128
283
+ return "Weitek"
284
+ when 130
285
+ return "Itanium Processor"
286
+ when 144
287
+ return "PA-RISC Family"
288
+ when 145
289
+ return "PA-RISC 8500"
290
+ when 146
291
+ return "PA-RISC 8000"
292
+ when 147
293
+ return "PA-RISC 7300LC"
294
+ when 148
295
+ return "PA-RISC 7200"
296
+ when 149
297
+ return "PA-RISC 7100LC"
298
+ when 150
299
+ return "PA-RISC 7100"
300
+ when 160
301
+ return "V30 Family"
302
+ when 176
303
+ return "Pentium III Xeon"
304
+ when 177
305
+ return "Pentium III Processor with Intel SpeedStep Technology"
306
+ when 178
307
+ return "Pentium 4"
308
+ when 179
309
+ return "Intel Xeon"
310
+ when 180
311
+ return "AS400 Family"
312
+ when 181
313
+ return "Intel Xeon processor MP"
314
+ when 182
315
+ return "AMD AthlonXP Family"
316
+ when 183
317
+ return "AMD AthlonMP Family"
318
+ when 184
319
+ return "Intel Itanium 2"
320
+ when 185
321
+ return "AMD Opteron Family"
322
+ when 190
323
+ return "K7"
324
+ when 200
325
+ return "IBM390 Family"
326
+ when 201
327
+ return "G4"
328
+ when 202
329
+ return "G5"
330
+ when 250
331
+ return "i860"
332
+ when 251
333
+ return "i960"
334
+ when 260
335
+ return "SH-3"
336
+ when 261
337
+ return "SH-4"
338
+ when 280
339
+ return "ARM"
340
+ when 281
341
+ return "StrongARM"
342
+ when 300
343
+ return "6x86"
344
+ when 301
345
+ return "MediaGX"
346
+ when 302
347
+ return "MII"
348
+ when 320
349
+ return "WinChip"
350
+ when 350
351
+ return "DSP"
352
+ when 500
353
+ return "Video Processor"
354
+ else
355
+ return "Unknown"
356
+ end
357
+ end
358
+ end
359
+
360
+ # Returns the release number, e.g. 5.1.2600.
361
+ #
362
+ def self.release(host=Socket.gethostname)
363
+ cs = "winmgmts://#{host}/root/cimv2"
364
+ begin
365
+ wmi = WIN32OLE.connect(cs)
366
+ rescue WIN32OLERuntimeError => e
367
+ raise Error, e
368
+ else
369
+ query = "select * from Win32_OperatingSystem"
370
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
371
+ return ole.Version
372
+ }
373
+ end
374
+ end
375
+
376
+ # Returns a struct of type UnameStruct that contains sysname, nodename,
377
+ # machine, version, and release, as well as a plethora of other fields.
378
+ # Please see the MSDN documentation for what each of these fields mean.
379
+ #
380
+ def self.uname(host=Socket.gethostname)
381
+ cs = "winmgmts://#{host}/root/cimv2"
382
+ begin
383
+ wmi = WIN32OLE.connect(cs)
384
+ rescue WIN32OLERuntimeError => e
385
+ raise Error, e
386
+ else
387
+ query = "select * from Win32_OperatingSystem"
388
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |os|
389
+ return UnameStruct.new(
390
+ os.BootDevice,
391
+ os.BuildNumber,
392
+ os.BuildType,
393
+ os.Caption,
394
+ os.CodeSet,
395
+ os.CountryCode,
396
+ os.CreationClassName,
397
+ os.CSCreationClassName,
398
+ os.CSDVersion,
399
+ os.CSName,
400
+ os.CurrentTimeZone,
401
+ os.Debug,
402
+ os.Description,
403
+ os.Distributed,
404
+ os.ForegroundApplicationBoost,
405
+ self.convert(os.FreePhysicalMemory),
406
+ self.convert(os.FreeSpaceInPagingFiles),
407
+ self.convert(os.FreeVirtualMemory),
408
+ self.parse_ms_date(os.InstallDate),
409
+ self.parse_ms_date(os.LastBootUpTime),
410
+ self.parse_ms_date(os.LocalDateTime),
411
+ os.Locale,
412
+ os.Manufacturer,
413
+ os.MaxNumberOfProcesses,
414
+ self.convert(os.MaxProcessMemorySize),
415
+ os.Name,
416
+ os.NumberOfLicensedUsers,
417
+ os.NumberOfProcesses,
418
+ os.NumberOfUsers,
419
+ os.Organization,
420
+ os.OSLanguage,
421
+ os.OSProductSuite,
422
+ os.OSType,
423
+ os.OtherTypeDescription,
424
+ os.PlusProductID,
425
+ os.PlusVersionNumber,
426
+ os.Primary,
427
+ os.QuantumLength,
428
+ os.QuantumType,
429
+ os.RegisteredUser,
430
+ os.SerialNumber,
431
+ os.ServicePackMajorVersion,
432
+ os.ServicePackMinorVersion,
433
+ self.convert(os.SizeStoredInPagingFiles),
434
+ os.Status,
435
+ os.SystemDevice,
436
+ os.SystemDirectory,
437
+ self.convert(os.TotalSwapSpaceSize),
438
+ self.convert(os.TotalVirtualMemorySize),
439
+ self.convert(os.TotalVisibleMemorySize),
440
+ os.Version,
441
+ os.WindowsDirectory
442
+ )
443
+ }
444
+ end
445
+ end
446
+
447
+ private
448
+
449
+ # Converts a string in the format '20040703074625.015625-360' into a
450
+ # Ruby Time object.
451
+ #
452
+ def self.parse_ms_date(str)
453
+ return if str.nil?
454
+ return Time.parse(str.split('.')[0])
455
+ end
456
+
457
+ # There is a bug in win32ole where uint64 types are returned as a
458
+ # String rather than a Fixnum/Bignum. This deals with that for now.
459
+ #
460
+ def self.convert(str)
461
+ return nil if str.nil? # Don't turn nil into 0
462
+ return str.to_i
463
+ end
464
+ end
465
+ end