hostinfo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 abrader
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = hostinfo
2
+
3
+ A library with simplified methods for retrieving pertinent system information
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 abrader. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ GEM = "hostinfo"
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = GEM
10
+ gem.summary = %Q{Simplified methods for retrieving system information}
11
+ gem.description = %Q{Provides simple methods for retreiving user, system, and network information}
12
+ gem.email = "andrew@brader.us"
13
+ gem.homepage = "http://github.com/abrader/hostinfo"
14
+ gem.authors = ["abrader"]
15
+ gem.add_dependency "ohai", "<=0.5.6"
16
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
17
+ gem.require_path = 'lib'
18
+ gem.rubyforge_project = "nowarning"
19
+ gem.has_rdoc = true
20
+ gem.platform = Gem::Platform::RUBY
21
+ gem.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{docs,lib,spec}/**/*")
22
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
23
+ end
24
+ Jeweler::GemcutterTasks.new
25
+ rescue LoadError
26
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
27
+ end
data/lib/hostinfo.rb ADDED
@@ -0,0 +1,191 @@
1
+ require 'ohai'
2
+ require 'resolv'
3
+
4
+ class HostInfo
5
+
6
+ @hst = Ohai::System.new
7
+ @hst.all_plugins
8
+ @hst_os = @hst[:platform]
9
+
10
+ # Returns string of shortened OS type
11
+ def self.os()
12
+ return @hst_os
13
+ end
14
+
15
+ # Returns string of verbose OS type
16
+ def self.os_long()
17
+ os_long = case @hst_os
18
+ when "fedora" then self.first_line("/etc/fedora-release")
19
+ when "redhat" then self.first_line("/etc/redhat-release")
20
+ when "centos" then self.first_line("/etc/redhat-release")
21
+ when "debian" then self.debissue()
22
+ when "ubuntu" then self.ubuissue()
23
+ when "mac_os_x" then "Mac OS X Version " + self.version()
24
+ end
25
+ return os_long
26
+ end
27
+
28
+ # Returns string of the numerical version of the OS (unless Debian or Ubuntu)
29
+ def self.version()
30
+ ver = case @hst_os
31
+ when "fedora" then self.os_long().split(" ")[2]
32
+ when "centos" then self.os_long().split(" ")[2]
33
+ when "redhat" then self.os_long().split(" ")[2]
34
+ when "debian" then self.os_long().split(" ")[2]
35
+ when "ubuntu" then self.os_long().split(" ")[1]
36
+ when "mac_os_x" then @hst[:platform_version]
37
+ end
38
+ return ver
39
+ end
40
+
41
+ # Returns string of fully qualified domain name for host
42
+ def self.hostname()
43
+ return @hst[:fqdn]
44
+ end
45
+
46
+ # Returns string containing the domain name of the host
47
+ def self.domain()
48
+ return @hst[:domain]
49
+ end
50
+
51
+ # Returns string containing default network interface
52
+ def self.default_network_interface()
53
+ begin
54
+ return interface = @hst[:network][:default_interface]
55
+ rescue
56
+ return nil
57
+ end
58
+ end
59
+
60
+ # Returns string containing IPv4 address. If interface is not passed as an argument, method assumes primary interface.
61
+ def self.ipv4(interface=nil)
62
+ interface = self.default_network_interface() unless interface != nil
63
+ if interface.nil? then return nil end
64
+
65
+ begin
66
+ ipv4addr_array = Array.new
67
+ output_array = Array.new
68
+ @hst[:network][:interfaces][interface][:addresses].each_key do |key|
69
+ if key[Resolv::IPv4::Regex] != nil
70
+ ipv4addr_array << key
71
+ end
72
+ end
73
+
74
+ if ipv4addr_array.nil? then return nil end
75
+
76
+ ipv4addr_array.each do |ipv4ip|
77
+ if (@hst[:network][:interfaces][interface][:addresses][ipv4ip][:family] == "inet")
78
+ output_array << ipv4ip
79
+ end
80
+ end
81
+ return output_array
82
+ rescue
83
+ return nil
84
+ end
85
+ end
86
+
87
+ # Returns string containing IPv6 address. If interface is not passed as an argument, method assumes primary interface.
88
+ def self.ipv6(interface=nil)
89
+ interface = self.default_network_interface() unless interface != nil
90
+ if interface.nil? then return nil end
91
+
92
+ begin
93
+ ipv6addr_array = Array.new
94
+ output_array = Array.new
95
+ @hst[:network][:interfaces][interface][:addresses].each_key do |key|
96
+ if key[Resolv::IPv6::Regex] != nil
97
+ ipv6addr_array << key
98
+ end
99
+ end
100
+
101
+ if ipv6addr_array.nil? then return nil end
102
+
103
+ ipv6addr_array.each do |ipv6ip|
104
+ if (@hst[:network][:interfaces][interface][:addresses][ipv6ip][:family] == "inet6")
105
+ output_array << ipv6ip
106
+ end
107
+ end
108
+ return output_array
109
+ rescue
110
+ return nil
111
+ end
112
+ end
113
+
114
+ # Returns string containing the netmask for a particular network interface. If interface is not passed as an argument, method assumes primary interface.
115
+ def self.netmask(interface=nil)
116
+ interface = self.default_network_interface() unless interface != nil
117
+ if interface.nil? then return nil end
118
+
119
+ begin
120
+ ipaddr = self.ipv4(interface)
121
+ return @hst[:network][:interfaces][interface][:addresses][ipaddr][:netmask]
122
+ rescue
123
+ return nil
124
+ end
125
+ end
126
+
127
+ # Returns string containing the broadcast address for a particular network interface. If interface is not passed as an argument, method assumes primary interface.
128
+ def self.broadcast(interface=nil)
129
+ interface = self.default_network_interface() unless interface != nil
130
+ if interface.nil? then return nil end
131
+
132
+ begin
133
+ ipaddr = self.ipv4(interface)
134
+ return @hst[:network][:interfaces][interface][:addresses][ipaddr][:broadcast]
135
+ rescue
136
+ return nil
137
+ end
138
+ end
139
+
140
+ # Returns string containing the MAC address for a particular network interface. If interface is not passed as an argument, method assumes primary interface.
141
+ def self.mac_address(interface=nil)
142
+ interface = self.default_network_interface() unless interface != nil
143
+ if interface.nil? then return nil end
144
+
145
+ begin
146
+ ipaddr = self.ipv4(interface)
147
+ macaddr = @hst[:network][:interfaces][interface][:addresses].to_s[/[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}/]
148
+ if (@hst[:network][:interfaces][interface][:addresses][macaddr][:family] == "lladdr")
149
+ return macaddr
150
+ else
151
+ return nil
152
+ end
153
+ rescue
154
+ return nil
155
+ end
156
+ end
157
+
158
+ # Returns string of current users' public SSH key. If type is not passed as an argument, method assumes DSA.
159
+ def self.ssh_pub_key(type=nil)
160
+ if type.nil?
161
+ return @hst[:keys][:ssh][:host_dsa_public]
162
+ else
163
+ begin
164
+ key_type = "host_" + type.downcase + "_public"
165
+ return @hst[:keys][:ssh][key_type]
166
+ rescue
167
+ return nil
168
+ end
169
+ end
170
+ end
171
+
172
+
173
+ private
174
+
175
+
176
+ def self.first_line(filename)
177
+ lines = IO.readlines(filename)
178
+ return lines.first
179
+ end
180
+
181
+ def self.debissue()
182
+ line_split = self.first_line("/etc/issue").split(" ")
183
+ return (line_split[0] + " " + line_split[1] + " " + line_split[2])
184
+ end
185
+
186
+ def self.ubuissue()
187
+ line_split = self.first_line("/etc/issue").split(" ")
188
+ return (line_split[0] + " " + line_split[1])
189
+ end
190
+
191
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'hostinfo'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestHostinfo < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hostinfo
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - abrader
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-07 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ohai
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - <=
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 0
32
+ - 5
33
+ - 6
34
+ version: 0.5.6
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: thoughtbot-shoulda
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Provides simple methods for retreiving user, system, and network information
52
+ email: andrew@brader.us
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - lib/hostinfo.rb
65
+ - test/helper.rb
66
+ - test/test_hostinfo.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/abrader/hostinfo
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project: nowarning
97
+ rubygems_version: 1.3.7
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Simplified methods for retrieving system information
101
+ test_files:
102
+ - test/helper.rb
103
+ - test/test_hostinfo.rb