zenoss_client 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -16,19 +16,13 @@ Gems:
16
16
  * tzinfo: For Python DateTime to Ruby DateTime conversion
17
17
 
18
18
 
19
- Zenoss Event Information:
19
+ == UNSUPPORTED REST METHODS:
20
+ Some methods within Zope are unsupported due to type conversion issues.
21
+ I have created a work-around but you must add a custom Python script
22
+ to Zope in order to do this. Please see this blog post for information
23
+ on how add the custom script:
20
24
 
21
- If you want to access Zenoss event information via REST you may need the
22
- following patch. The methods Zenoss::Model::EventView#get_status_img_src and
23
- EventView#get_status_css_class depend on the 'status' parameter being an
24
- integer. When the call is unmarshalled on the Zenoss side it is always a
25
- string unless you apply this patch:
26
-
27
- http://gist.github.com/328414
28
-
29
- This has been reported and is filed as a product defect:
30
-
31
- http://dev.zenoss.com/trac/ticket/6282
25
+ http://distributed-frostbite.blogspot.com/2010/04/using-ruby-with-zenoss-part-1.html
32
26
 
33
27
 
34
28
  == TO USE:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -22,6 +22,7 @@ module Zenoss
22
22
  class Device
23
23
  include Zenoss::Model
24
24
  include Zenoss::Model::EventView
25
+ include Zenoss::Model::RRDView
25
26
  include Zenoss::Model::DeviceResultInt
26
27
 
27
28
  attr_reader :path, :device, :os, :hw
@@ -55,13 +55,23 @@ module Zenoss
55
55
 
56
56
  # Name of the device in Zenoss. This method will return the first
57
57
  # match if the device_name is not fully qualified.
58
+ #
59
+ # @param [String] device_name the hostname of the device
60
+ # @return [Device,nil] Device object of passed hostname or nil if the device doesn't exist
58
61
  def find_device_path(device_name)
59
62
  devpath = rest("findDevicePath?devicename=#{device_name}")
60
- return Device.new(devpath)
63
+ devpath.nil? ? nil : Device.new(devpath)
61
64
  end
62
65
 
66
+ # @return [Array<Device>] an Array of Devices (or some Device subclass)
63
67
  def get_sub_devices
64
- plist_to_array( rest('getSubDevices') )
68
+ (plist_to_array( rest('getSubDevices') )).map do |dstr|
69
+ # I'm using this regex here so some day we can generate
70
+ # Devices and sub Device types with an eval like so:
71
+ # eval "#{$1}.new(#{$2})"
72
+ dev = dstr.sub(/^<([\w]+)\s+at\s+(.*)>$/,'\2')
73
+ Device.new(dev)
74
+ end
65
75
  end
66
76
 
67
77
 
@@ -1,23 +1,22 @@
1
1
  #############################################################################
2
- # Copyright © 2009 Dan Wanek <dwanek@nd.gov>
2
+ # Copyright © 2010 Dan Wanek <dwanek@nd.gov>
3
3
  #
4
4
  #
5
- # This file is part of Zenoss-RubyREST.
5
+ # This file is part of zenoss_client.
6
6
  #
7
- # Zenoss-RubyREST is free software: you can redistribute it and/or
7
+ # zenoss_client is free software: you can redistribute it and/or
8
8
  # modify it under the terms of the GNU General Public License as published
9
9
  # by the Free Software Foundation, either version 3 of the License, or (at
10
10
  # your option) any later version.
11
11
  #
12
- # Zenoss-RubyREST is distributed in the hope that it will be useful,
12
+ # zenoss_client is distributed in the hope that it will be useful,
13
13
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15
15
  # Public License for more details.
16
16
  #
17
17
  # You should have received a copy of the GNU General Public License along
18
- # with Zenoss-RubyREST. If not, see <http://www.gnu.org/licenses/>.
18
+ # with zenoss_client. If not, see <http://www.gnu.org/licenses/>.
19
19
  #############################################################################
20
-
21
20
  module Zenoss
22
21
  module Model
23
22
  module EventView
@@ -40,7 +39,7 @@ module Zenoss
40
39
  # need to apply this patch to Zenoss:
41
40
  # http://gist.github.com/328414
42
41
  def get_status_img_src(status_number)
43
- rest("getStatusImgSrc?status=#{status_number}")
42
+ custom_rest("getStatusImgSrc?status=#{status_number}")
44
43
  end
45
44
 
46
45
  # Fetches the css class for this status number. This is usually the
@@ -48,7 +47,7 @@ module Zenoss
48
47
  # need to apply this patch to Zenoss:
49
48
  # http://gist.github.com/328414
50
49
  def get_status_css_class(status_number)
51
- rest("getStatusCssClass?status=#{status_number}")
50
+ custom_rest("getStatusCssClass?status=#{status_number}")
52
51
  end
53
52
 
54
53
  end # EventView
data/lib/model/model.rb CHANGED
@@ -46,6 +46,7 @@ module Zenoss
46
46
  end # Zenoss
47
47
 
48
48
  require 'model/event_view'
49
+ require 'model/rrd_view'
49
50
 
50
51
  # Device Loader interface. You can use it directly or use the
51
52
  # utility methods in DeviceClass to create devices beneath
@@ -0,0 +1,56 @@
1
+ #############################################################################
2
+ # Copyright © 2010 Dan Wanek <dwanek@nd.gov>
3
+ #
4
+ #
5
+ # This file is part of zenoss_client.
6
+ #
7
+ # zenoss_client is free software: you can redistribute it and/or
8
+ # modify it under the terms of the GNU General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or (at
10
+ # your option) any later version.
11
+ #
12
+ # zenoss_client is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15
+ # Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License along
18
+ # with zenoss_client. If not, see <http://www.gnu.org/licenses/>.
19
+ #############################################################################
20
+ module Zenoss
21
+ module Model
22
+ class RRDDataPoint
23
+ include Zenoss
24
+ include Zenoss::Model
25
+
26
+ def initialize(datapoint_path)
27
+ @path = datapoint_path
28
+
29
+ # Initialize common things from Model
30
+ model_init
31
+ end
32
+
33
+ # ------------------------- Utility Methods ------------------------- #
34
+ # These are methods that do not exist as part of the official Zenoss
35
+ # API, but from an object model they seem to make sense to me.
36
+ # ------------------------------------------------------------------- #
37
+
38
+
39
+
40
+ # --------------------------- REST Methods -------------------------- #
41
+
42
+ # @return [String] Name of the data source
43
+ def name
44
+ @cache_vars[:name] ||= rest('name')
45
+ end
46
+
47
+
48
+ private
49
+
50
+ def rest(method, path = "#{@path}")
51
+ super("#{path}/#{method}")
52
+ end
53
+
54
+ end # DeviceClass
55
+ end # Model
56
+ end # Zenoss
@@ -0,0 +1,46 @@
1
+ #############################################################################
2
+ # Copyright © 2010 Dan Wanek <dwanek@nd.gov>
3
+ #
4
+ #
5
+ # This file is part of zenoss_client.
6
+ #
7
+ # zenoss_client is free software: you can redistribute it and/or
8
+ # modify it under the terms of the GNU General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or (at
10
+ # your option) any later version.
11
+ #
12
+ # zenoss_client is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15
+ # Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License along
18
+ # with zenoss_client. If not, see <http://www.gnu.org/licenses/>.
19
+ #############################################################################
20
+ module Zenoss
21
+ module Model
22
+ module RRDView
23
+ include Zenoss::Model
24
+
25
+ # @return [Array] of datapoints
26
+ def get_rrd_data_points
27
+ (plist_to_array( custom_rest('getRRDDataPoints') )).map do |dstr|
28
+ dp = dstr.sub(/^<([\w]+)\s+at\s+(.*)>$/,'\2')
29
+ RRDDataPoint.new(dp)
30
+ end
31
+ end
32
+
33
+ # Get key/value pairs of RRD Values for the passed data source names.
34
+ #
35
+ # @param [Array <String>] dsnames data source names from RRDDataPoint#name
36
+ # @return [Hash] key/value pairs of data source name and data source values
37
+ def get_rrd_values(dsnames)
38
+ pdict_to_hash(custom_rest("getRRDValues?dsnames=[#{dsnames.join(',')}]"))
39
+ end
40
+
41
+ end # RRDView
42
+ end # Model
43
+ end # Zenoss
44
+
45
+ # Load the RRD related files
46
+ require 'model/rrd/rrd_data_point'
data/lib/zenoss.rb CHANGED
@@ -25,6 +25,9 @@ require 'uri'
25
25
  module Zenoss
26
26
 
27
27
  # Set the Base URI of the Zenoss server
28
+ #
29
+ # @param [URI, String] uri is the URL we use to connect to the Zenoss server
30
+ # @return [URI] the URI that was parsed and used as our base connection
28
31
  def Zenoss.uri(uri)
29
32
  if(uri.kind_of?(URI))
30
33
  uri.path << '/' unless(uri.path.index /\/$/)
@@ -35,23 +38,26 @@ module Zenoss
35
38
  end
36
39
  end
37
40
 
41
+ # @param [String] user
42
+ # @param [String] pass
43
+ # @return [Boolean]
38
44
  def Zenoss.set_auth(user, pass)
39
45
  const_set(:USER, user)
40
46
  const_set(:PASS, pass)
41
47
  true
42
48
  end
43
49
 
44
- # Return the base DeviceClass /zport/dmd/Devices
50
+ # @return [Model::DeviceClass] the base DeviceClass /zport/dmd/Devices
45
51
  def Zenoss.devices
46
52
  Model::DeviceClass.new('/zport/dmd/Devices')
47
53
  end
48
54
 
49
- # Return the base ServiceOrganizer /zport/dmd/Services
55
+ # @return [Model::ServiceOrganizer] the base ServiceOrganizer /zport/dmd/Services
50
56
  def Zenoss.services
51
57
  Model::ServiceOrganizer.new('/zport/dmd/Services')
52
58
  end
53
59
 
54
- # Return the base System /zport/dmd/Systems
60
+ # @return [Model::System] the base System /zport/dmd/Systems
55
61
  def Zenoss.systems
56
62
  Model::System.new('/zport/dmd/Systems')
57
63
  end
@@ -61,25 +67,75 @@ module Zenoss
61
67
  private
62
68
 
63
69
  # Prepend the appropriate path and call the REST method on the URL set with Zenoss#uri
70
+ #
71
+ # @param [String] req_path the request path of the REST method
72
+ # @return [String] the response body of the REST call
64
73
  def rest(req_path)
65
74
  Net::HTTP.start(Zenoss::BASE_URI.host,Zenoss::BASE_URI.port) {|http|
66
75
  req = Net::HTTP::Get.new("#{BASE_URI.path}#{req_path}")
67
76
  req.basic_auth USER, PASS if USER
68
77
  response = http.request(req)
78
+ response.body.chomp! unless response.body.nil?
69
79
  return(response.body)
70
80
  }
71
81
  end
72
82
 
83
+ # Call a custom Zope method to work around some issues of unsupported or bad behaving
84
+ # REST methods.
85
+ # @see http://gist.github.com/343627 for more info.
86
+ #
87
+ # @param [String] req_path the request path of the REST method ( as if it wasn't misbehaving )
88
+ # @example req_path
89
+ # getRRDValues?dsnames=['ProcessorTotalUserTime_ProcessorTotalUserTime','MemoryPagesOutputSec_MemoryPagesOutputSec']
90
+ # @return [String] the response body of the REST call
91
+ def custom_rest(req_path)
92
+ meth,args = req_path.split('?')
93
+ meth = "callZenossMethod?methodName=#{meth}"
94
+ unless args.nil?
95
+ meth << '&args=['
96
+ # Remove the named parameters because we can't dynamically call named parameters in Python.
97
+ # This method uses positional parameters via the passed Array (Python List).
98
+ args.split('&').inject(nil) do |delim,arg|
99
+ meth << "#{delim}#{arg.split('=').last}"
100
+ delim = ',' if delim.nil?
101
+ end
102
+ meth << ']'
103
+ end
104
+ rest(meth)
105
+ end
106
+
73
107
  # Some of the REST methods return Strings that are formated like a Python list.
74
108
  # This method turns that String into a Ruby Array.
75
109
  # If the list parameter is nil the return value is also nil.
110
+ #
111
+ # @param [String] list a Python formatted list
112
+ # @return [Array,nil] a bonafide Ruby Array
76
113
  def plist_to_array(list)
77
114
  return nil if list.nil?
115
+ list = sanitize_str(list)
78
116
  (list.gsub /[\[\]]/,'').split /,\s+/
79
117
  end
80
118
 
119
+ # Converts a String formatted like a Python Dictionary to a Ruby Hash.
120
+ #
121
+ # @param [String] dict a Python dictionary
122
+ # @return [Hash,nil] a Ruby Hash
123
+ def pdict_to_hash(dict)
124
+ return nil if dict.nil?
125
+ puts "Dict: #{dict}"
126
+ dict = sanitize_str(dict)
127
+ puts "New Dict: #{dict}"
128
+ dict = dict.sub(/^\{(.*)\}$/,'\1').split(/[,:]/).map do |str|
129
+ str.strip
130
+ end
131
+ Hash[*dict]
132
+ end
133
+
81
134
  # Converts a String in Python's DateTime format to Ruby's DateTime format
82
135
  # If the pdt parameter is nil the return value is also nil.
136
+ #
137
+ # @param [String] pdt a String formatted in Python's DateTime format
138
+ # @return [DateTime] a bonafide Ruby DateTime object
83
139
  def pdatetime_to_datetime(pdt)
84
140
  return nil if pdt.nil?
85
141
  pdt = pdt.split(/\s+/)
@@ -87,6 +143,14 @@ module Zenoss
87
143
  DateTime.strptime("#{pdt[0]} #{pdt[1]} #{tz.current_period.abbreviation.to_s}", '%Y/%m/%d %H:%M:%S.%N %Z')
88
144
  end
89
145
 
146
+ # Do some clean-up on the string returned from REST calls. Removes some
147
+ # quote characters embedded in the string and other misc.
148
+ #
149
+ # @param [String] str string returned from REST call
150
+ # @return [String] sanitized string
151
+ def sanitize_str(str)
152
+ str.gsub(/['"]/,'')
153
+ end
90
154
 
91
155
  end # Zenoss
92
156
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dan Wanek
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-29 00:00:00 -05:00
17
+ date: 2010-04-07 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -54,6 +54,8 @@ files:
54
54
  - lib/model/manufacturers/manufacturers.rb
55
55
  - lib/model/model.rb
56
56
  - lib/model/processes/os_process_organizer.rb
57
+ - lib/model/rrd/rrd_data_point.rb
58
+ - lib/model/rrd_view.rb
57
59
  - lib/model/services.rb
58
60
  - lib/model/services/ip_service.rb
59
61
  - lib/model/services/service.rb