sys-uptime 0.4.5-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/CHANGES +109 -0
  2. data/MANIFEST +17 -0
  3. data/README +20 -0
  4. data/doc/uptime.txt +97 -0
  5. data/lib/sys/uptime.rb +97 -0
  6. data/test/tc_uptime.rb +80 -0
  7. metadata +54 -0
data/CHANGES ADDED
@@ -0,0 +1,109 @@
1
+ == 0.4.5 - 19-Nov-2006
2
+ * Internal layout changes, minor doc updates and gemspec improvements.
3
+ * No code changes.
4
+
5
+ == 0.4.4 - 30-Jun-2006
6
+ * Added inline rdoc documentation to the source files.
7
+ * Added a gemspec.
8
+
9
+ == 0.4.3 - 18-Dec-2005
10
+ * Changed the Linux version to pure Ruby. The current method of determining
11
+ uptime in unix.c does not work in Linux kernel 2.6+. So, from now on it
12
+ reads out of /proc/uptime.
13
+
14
+ == 0.4.2 - 6-May-2005
15
+ * Fixed a potential boot_time bug.
16
+ * Removed the version.h file. It's no longer needed since the Windows
17
+ version is pure Ruby.
18
+ * NetBSD 2.x and FreeBSD 5.x now supported.
19
+ * Removed the INSTALL file. Installation instructions are now in
20
+ the README file.
21
+ * Removed the uptime.rd file. You can generate html documentation by
22
+ running rdoc over the uptime.txt file.
23
+ * Made most documents rdoc friendly.
24
+ * Moved project to RubyForge.
25
+
26
+ == 0.4.1 - 14-Dec-2004
27
+ * Moved freebsd code into unix.c file.
28
+ * Should now work past 249 days (2**31) on systems that have utmpx.h.
29
+ * Fixed a bug with regards to boot_time, where it was possible that it would
30
+ simply be empty.
31
+
32
+ == 0.4.0 - 8-Jul-2004
33
+ * Removed all reference to the CLK_TCK constant, as per documentation from
34
+ Richard Stevens that it is deprecated and that sysconf() should be used
35
+ instead (well, I knew about this, but ignored it until now).
36
+ * Scrapped the C version for Windows in favor of a pure Ruby version that
37
+ uses win32ole + WMI.
38
+ * Added a boot_time method for Unix systems (Windows already had this).
39
+ * Added an UptimeError class on Unix systems (and replaced UptimeException
40
+ on Windows).
41
+ * Modified an internal function to raise an UptimeError if the times()
42
+ function fails. Also, it now returns an unsigned long instead of a plain
43
+ long.
44
+ * Replaced the two different test suites with a single, unified test suite.
45
+ * Cleaned up the extconf.rb script. I now assume that TestUnit is installed.
46
+ * Removed the load_avg method (which was never advertised and which I hope
47
+ you weren't using). You can find a load_avg method in the sys-cpu package.
48
+ * Changed uptime.rd2 to uptime.rd to be consistent with my other packages.
49
+ * Removed the uptime.html file - you may generate that on your own.
50
+ * Added warranty and license info.
51
+
52
+ == 0.3.2 - 30-Dec-2003
53
+ * Cleaned up some warnings that showed up with -Wall on some unix platforms
54
+ (int vs long format, explicit include)
55
+ * Minor test suite and extconf.rb changes
56
+
57
+ == 0.3.1 - 25-Jun-2003
58
+ * Modified test files to handle HP-UX extensions
59
+ * Minor doc updates
60
+ * Added the dhms() method. Actually, this was in the 0.3.0
61
+ release, I just forgot to mention it in this file :)
62
+
63
+ == 0.3.0 - 22-Jun-2003
64
+ * Added OS X support - thanks go to Mike Hall for the patch
65
+ * Fixed incorrect values in FreeBSD (again Mike Hall)
66
+ * Modified tc_unix.rb test suite to handle OS X, along with a bit
67
+ * of minor cleanup
68
+ * Removed VERSION class method. Use the constant instead
69
+ * Separated FreeBSD/OS X source into its own file (freebsd.c).
70
+ The #ifdefs were starting to get too ugly for me
71
+
72
+ == 0.2.1 - 13-May-2003
73
+ * Fixed bug in extconf.rb file, and made some major changes
74
+ * Modified test.rb for those without TestUnit
75
+ * Modified TestUnit tests - some bogus tests were removed
76
+ * Added a README file with some additional info
77
+ * Created a version.h file, so that I can keep the VERSION number
78
+ in one place
79
+ * Docs automatically included in doc directory (i.e. no more interactive
80
+ document creation)
81
+
82
+ == 0.2.0 - 13-Mar-2003
83
+ * Added MS Windows support
84
+ * Added a VERSION constant
85
+ * Added a test suite (for those with TestUnit installed)
86
+ * Internal directory layout change
87
+ * uptime.c is now split into unix.c and windows.c (and linked appropriately)
88
+ * Changelog and Manifest are now CHANGES and MANIFEST, respectively
89
+ * Many changes to extconf.rb
90
+
91
+ == 0.1.3 - 6-Jan-2003
92
+ * Added a VERSION class method
93
+ * Added a copyright notice
94
+ * Fixed up the docs a bit and moved them to the doc directory
95
+ * Changed the tarball name to match the RAA package name
96
+ * Modified test.rb script to make it better
97
+ * Changed install instructions slightly
98
+
99
+ == 0.1.2 - 25-Aug-2002
100
+ * Slight change to preprocessor commands to avoid redefining CLK_TCK on
101
+ those systems that define it in time.h
102
+ * Added an INSTALL file
103
+
104
+ == 0.1.1 - 21-Jun-2002
105
+ * The CLK_TCK constant wasn't necessarily being set correctly, which could
106
+ lead to some odd results. This has been fixed.
107
+
108
+ == 0.1.0 - 17-Jun-2002
109
+ * Initial release
data/MANIFEST ADDED
@@ -0,0 +1,17 @@
1
+ CHANGES
2
+ MANIFEST
3
+ README
4
+ install.rb
5
+ sys-uptime.gemspec
6
+
7
+ doc/uptime.txt
8
+
9
+ examples/test.rb
10
+
11
+ ext/extconf.rb
12
+ ext/uptime.c
13
+
14
+ lib/sys/linux.rb
15
+ lib/sys/windows.rb
16
+
17
+ test/tc_uptime.rb
data/README ADDED
@@ -0,0 +1,20 @@
1
+ = Description
2
+ A Ruby interface for getting system uptime information.
3
+
4
+ = Prerequisites
5
+ Ruby 1.8.0 or later.
6
+ Ruby 1.8.2 or later is recommended on MS Windows.
7
+
8
+ = Installation
9
+ === Unix:
10
+ ruby extconf.rb
11
+ make
12
+ ruby test/tc_uptime.rb (optional)
13
+ make install
14
+
15
+ === Windows and Linux:
16
+ ruby test\tc_uptime.rb (optional)
17
+ ruby install.rb
18
+
19
+ == Notes
20
+ For additional documentation see doc/uptime.txt.
data/doc/uptime.txt ADDED
@@ -0,0 +1,97 @@
1
+ == Synopsis
2
+ require 'sys/uptime'
3
+ include Sys
4
+
5
+ p Uptime.days
6
+ p Uptime.hours
7
+ p Uptime.minutes
8
+ p Uptime.seconds
9
+ p Uptime.dhms.join(', ')
10
+ p Uptime.uptime
11
+ p Uptime.boot_time
12
+
13
+ == Description
14
+ This package provides uptime and boot time information, similar to the
15
+ 'uptime' Unix command.
16
+
17
+ == Constants
18
+ VERSION
19
+ Returns the current version number of this package (as a string).
20
+
21
+ == Class Methods
22
+ Uptime.boot_time
23
+ Returns the boot time as a Time object.
24
+
25
+ Uptime.days
26
+ Returns the total number of days the system has been up.
27
+
28
+ Uptime.hours
29
+ Returns the total number of hours the system has been up.
30
+
31
+ Uptime.minutes
32
+ Returns the total number of minutes the system has been up.
33
+
34
+ Uptime.seconds
35
+ Returns the total number of seconds the system has been up.
36
+
37
+ Uptime.dhms
38
+ Calculates and returns the number of days, hours, minutes and
39
+ seconds the system has been running as a four-element Array.
40
+
41
+ Uptime.uptime
42
+ Calculates and returns the number of days, hours, minutes and
43
+ seconds the system has been running as a colon-separated string.
44
+
45
+ == Exceptions
46
+ UptimeError
47
+ Raised if something goes wrong. On Unix, this would likely mean a
48
+ failure of the times() function. On Windows, it probably means you
49
+ failed to connect to WMI properly.
50
+
51
+ == Notes
52
+ On MS Windows each of the class methods optionally takes a host name as
53
+ a single argument. The default is localhost (or whatever the result of
54
+ Socket.gethostname returns).
55
+
56
+ The current time, users and load average are NOT included in this
57
+ module, even though you may be used to seeing them with the command
58
+ line version of 'uptime'. This is because these things have
59
+ _absolutely nothing_ to do with uptime (at least, not as I
60
+ logically think of uptime).
61
+
62
+ This package was tested successfully on a Solaris system with over 1600
63
+ days of uptime.
64
+
65
+ == Known Bugs
66
+ None that I am aware of. Please log any bugs you find on the project
67
+ website at http://www.rubyforge.org/projects/sysutils.
68
+
69
+ == Questions
70
+ "Doesn't Struct::Tms do this?" - No.
71
+
72
+ == License
73
+ Ruby's
74
+
75
+ == Copyright
76
+ Copyright 2002-2006, Daniel J. Berger
77
+
78
+ All Rights Reserved. This module is free software. It may be used,
79
+ redistributed and/or modified under the same terms as Ruby itself.
80
+
81
+ == Warranty
82
+ This package is provided "as is" and without any express or
83
+ implied warranties, including, without limitation, the implied
84
+ warranties of merchantability and fitness for a particular purpose.
85
+
86
+ == Acknowledgements
87
+ Mike Hall for help with the BSD side of things.
88
+ Ola Eriksson, whose source code I shamelessly plagiarized to get a better
89
+ implementation for systems that have the utmpx.h header file.
90
+
91
+ == Author
92
+ Daniel J. Berger
93
+ djberg96 at gmail dot com
94
+ imperator on IRC (Freenode)
95
+
96
+ == See Also
97
+ uptime
data/lib/sys/uptime.rb ADDED
@@ -0,0 +1,97 @@
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'parsedate'
4
+
5
+ module Sys
6
+ class UptimeError < StandardError; end
7
+ class Uptime
8
+ VERSION = '0.4.5'
9
+
10
+ # Returns the boot time as a Time object.
11
+ #
12
+ def self.boot_time(host=Socket.gethostname)
13
+ cs = "winmgmts://#{host}/root/cimv2"
14
+ begin
15
+ wmi = WIN32OLE.connect(cs)
16
+ rescue WIN32OLERuntimeError => e
17
+ raise UptimeError, e
18
+ else
19
+ query = "select LastBootupTime from Win32_OperatingSystem"
20
+ results = wmi.ExecQuery(query)
21
+ results.each{ |ole|
22
+ time_array = ParseDate.parsedate(ole.LastBootupTime)
23
+ return Time.mktime(*time_array)
24
+ }
25
+ end
26
+ end
27
+
28
+ # Calculates and returns the number of days, hours, minutes and
29
+ # seconds the system has been running as a colon-separated string.
30
+ #
31
+ def self.uptime(host=Socket.gethostname)
32
+ arr = self.get_dhms(host)
33
+ return arr.join(":")
34
+ end
35
+
36
+ # Calculates and returns the number of days, hours, minutes and
37
+ # seconds the system has been running as a four-element Array.
38
+ #
39
+ def self.dhms(host=Socket.gethostname)
40
+ return self.get_dhms(host)
41
+ end
42
+
43
+ # Returns the total number of days the system has been up.
44
+ #
45
+ def self.days(host=Socket.gethostname)
46
+ return self.get_dhms(host).first
47
+ end
48
+
49
+ # Returns the total number of hours the system has been up.
50
+ #
51
+ def self.hours(host=Socket.gethostname)
52
+ return self.get_dhms(host)[1]
53
+ end
54
+
55
+ # Returns the total number of minutes the system has been up.
56
+ #
57
+ def self.minutes(host=Socket.gethostname)
58
+ return self.get_dhms(host)[2]
59
+ end
60
+
61
+ # Returns the total number of seconds the system has been up.
62
+ #
63
+ def self.seconds(host=Socket.gethostname)
64
+ return self.get_dhms(host).last
65
+ end
66
+
67
+ private
68
+
69
+ def self.get_dhms(host)
70
+ cs = "winmgmts://#{host}/root/cimv2"
71
+ begin
72
+ wmi = WIN32OLE.connect(cs)
73
+ rescue WIN32OLERuntimeError => e
74
+ raise UptimeError, e
75
+ else
76
+ query = "select LastBootupTime from Win32_OperatingSystem"
77
+ results = wmi.ExecQuery(query)
78
+ now = Time.now
79
+
80
+ results.each{ |ole|
81
+ time_array = ParseDate.parsedate(ole.LastBootupTime)
82
+ boot_time = Time.mktime(*time_array)
83
+ break
84
+ }
85
+
86
+ seconds = (now - boot_time).to_i
87
+ days = (seconds / 86400).to_i
88
+ seconds -= days * 86400
89
+ hours = seconds / 3600
90
+ seconds -= hours * 3600
91
+ minutes = seconds / 60
92
+ seconds -= minutes * 60
93
+ return [days,hours,minutes,seconds]
94
+ end
95
+ end
96
+ end
97
+ end
data/test/tc_uptime.rb ADDED
@@ -0,0 +1,80 @@
1
+ #################################
2
+ # tc_uptime.rb
3
+ #
4
+ # Test suite for sys-uptime.
5
+ #################################
6
+ base = File.basename(Dir.pwd)
7
+ if base == 'test' || base =~ /sys-uptime.*/
8
+ require 'fileutils'
9
+ Dir.chdir('..') if base == 'test'
10
+ Dir.mkdir('sys') unless File.exists?('sys')
11
+
12
+ if RUBY_PLATFORM.match('mswin')
13
+ FileUtils.cp('lib/sys/windows.rb', 'sys/uptime.rb') rescue nil
14
+ elsif RUBY_PLATFORM.match('linux')
15
+ FileUtils.cp('lib/sys/linux.rb', 'sys/uptime.rb') rescue nil
16
+ else
17
+ require 'rbconfig'
18
+ file = 'ext/uptime.' + Config::CONFIG['DLEXT']
19
+ FileUtils.cp(file, 'sys')
20
+ end
21
+
22
+ $LOAD_PATH.unshift(Dir.pwd)
23
+ $LOAD_PATH.unshift(Dir.pwd + '/lib')
24
+ end
25
+
26
+ require 'sys/uptime'
27
+ require 'test/unit'
28
+ include Sys
29
+
30
+ class TC_Uptime < Test::Unit::TestCase
31
+ def test_version
32
+ assert_equal('0.4.5', Uptime::VERSION)
33
+ end
34
+
35
+ def test_seconds
36
+ assert_respond_to(Uptime, :seconds)
37
+ assert_nothing_raised{ Uptime.seconds }
38
+ assert_kind_of(Fixnum, Uptime.seconds)
39
+ assert_equal(true, Uptime.seconds > 0)
40
+ end
41
+
42
+ def test_minutes
43
+ assert_respond_to(Uptime, :minutes)
44
+ assert_nothing_raised{ Uptime.minutes }
45
+ assert_kind_of(Fixnum, Uptime.minutes)
46
+ end
47
+
48
+ def test_hours
49
+ assert_respond_to(Uptime, :hours)
50
+ assert_nothing_raised{ Uptime.hours }
51
+ assert_kind_of(Fixnum, Uptime.hours)
52
+ end
53
+
54
+ def test_days
55
+ assert_respond_to(Uptime,:days)
56
+ assert_nothing_raised{ Uptime.days }
57
+ assert_kind_of(Fixnum, Uptime.days)
58
+ end
59
+
60
+ def test_uptime
61
+ assert_respond_to(Uptime,:uptime)
62
+ assert_nothing_raised{ Uptime.uptime }
63
+ assert_kind_of(String, Uptime.uptime)
64
+ assert_equal(false, Uptime.uptime.empty?)
65
+ end
66
+
67
+ def test_dhms
68
+ assert_respond_to(Uptime,:dhms)
69
+ assert_nothing_raised{ Uptime.dhms }
70
+ assert_kind_of(Array, Uptime.dhms)
71
+ assert_equal(false, Uptime.dhms.empty?)
72
+ assert_equal(4, Uptime.dhms.length)
73
+ end
74
+
75
+ def test_boot_time
76
+ assert_respond_to(Uptime,:boot_time)
77
+ assert_nothing_raised{ Uptime.boot_time }
78
+ assert_kind_of(Time, Uptime.boot_time)
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: sys-uptime
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.4.5
7
+ date: 2006-11-20 00:00:00 -07:00
8
+ summary: A Ruby interface for getting system uptime information.
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/sysutils
13
+ rubyforge_project: sysutils
14
+ description: A Ruby interface for getting system uptime information.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
25
+ platform: mswin32
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - doc/uptime.txt
33
+ - test/tc_uptime.rb
34
+ - CHANGES
35
+ - README
36
+ - MANIFEST
37
+ - lib/sys/uptime.rb
38
+ test_files:
39
+ - test/tc_uptime.rb
40
+ rdoc_options: []
41
+
42
+ extra_rdoc_files:
43
+ - CHANGES
44
+ - README
45
+ - MANIFEST
46
+ - doc/uptime.txt
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ requirements: []
52
+
53
+ dependencies: []
54
+