Module: Zenoss
- Included in:
- Connection, Model, Model::DeviceClass, Model::DeviceHW, Model::OperatingSystem, Model::RRDDataPoint, Model::ServiceOrganizer, Model::System
- Defined in:
- lib/zenoss/jsonapi.rb,
lib/zenoss.rb,
lib/zenoss/model.rb,
lib/zenoss/restapi.rb,
lib/zenoss/connection.rb,
lib/zenoss/exceptions.rb,
lib/zenoss/events/event.rb,
lib/zenoss/events/zevent.rb,
lib/zenoss/model/devices.rb,
lib/zenoss/model/rrd_view.rb,
lib/zenoss/model/event_view.rb,
lib/zenoss/model/devices/device.rb,
lib/zenoss/model/systems/system.rb,
lib/zenoss/jsonapi/device_router.rb,
lib/zenoss/jsonapi/events_router.rb,
lib/zenoss/jsonapi/report_router.rb,
lib/zenoss/model/z_device_loader.rb,
lib/zenoss/model/devices/device_hw.rb,
lib/zenoss/model/rrd/rrd_data_point.rb,
lib/zenoss/model/devices/device_class.rb,
lib/zenoss/model/zen_property_manager.rb,
lib/zenoss/model/zenpack/zenpack_manager.rb,
lib/zenoss/model/devices/operating_system.rb,
lib/zenoss/model/services/service_organizer.rb
Overview
Copyright © 2010 Dan Wanek <[email protected]>
This file is part of zenoss_client.
zenoss_client is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
zenoss_client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with zenoss_client. If not, see <www.gnu.org/licenses/>.
Defined Under Namespace
Modules: Events, JSONAPI, Model, RESTAPI Classes: Connection, ZenossError
Class Method Summary collapse
-
.connect(server, user, pass, opts = {}, &block) ⇒ Object
initialize a connection to a Zenoss server.
Instance Method Summary collapse
-
#parse_array(list, first = true) ⇒ Array?
Some of the REST methods return Strings that are formated like a Python list.
-
#pdatetime_to_datetime(pdt) ⇒ DateTime
Converts a String in Python’s DateTime format to Ruby’s DateTime format If the pdt parameter is nil the return value is also nil.
-
#pdict_to_hash(dict) ⇒ Hash?
Converts a String formatted like a Python Dictionary to a Ruby Hash.
-
#plist_to_array(list) ⇒ Array?
Some of the REST methods return Strings that are formated like a Python list.
-
#ptuples_to_hash(tuple_array) ⇒ Hash
This takes an array of two element Python tuples and turns it into a Ruby hash.
-
#sanitize_str(str) ⇒ String
Do some clean-up on the string returned from REST calls.
Class Method Details
.connect(server, user, pass, opts = {}, &block) ⇒ Object
initialize a connection to a Zenoss server. This is the same as doing
Zenoss::Connection.new(server,user,pass)
35 36 37 |
# File 'lib/zenoss.rb', line 35 def Zenoss.connect(server, user, pass, opts = {}, &block) Connection.new(server, user, pass, opts, &block) end |
Instance Method Details
#parse_array(list, first = true) ⇒ Array?
Some of the REST methods return Strings that are formated like a Python list. This method turns that String into a Ruby Array. If the list parameter is nil the return value is also nil. WARNING: This will soon supersede #plist_to_array
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/zenoss.rb', line 59 def parse_array(list, first = true) return nil if list.nil? open = false narray = [] list = list.chars.to_a unless list.is_a?(Array) while( token = list.shift ) case token when /[\[\(]/ open = true if(first) narray = parse_array(list, false) else narray << parse_array(list, false) end when /[\]\)]/ open = false return narray when /["']/ qtype = token tokenstr = '' while( (token = list.shift) !~ /#{qtype}/ ) tokenstr << token end narray << tokenstr when /\d/ while( list[0] =~ /\d/ ) token << list.shift end narray << token.to_i end end narray end |
#pdatetime_to_datetime(pdt) ⇒ DateTime
Converts a String in Python’s DateTime format to Ruby’s DateTime format If the pdt parameter is nil the return value is also nil.
112 113 114 115 116 117 |
# File 'lib/zenoss.rb', line 112 def pdatetime_to_datetime(pdt) return nil if pdt.nil? pdt = pdt.split(/\s+/) tz = TZInfo::Timezone.get(pdt.last) DateTime.strptime("#{pdt[0]} #{pdt[1]} #{tz.current_period.abbreviation.to_s}", '%Y/%m/%d %H:%M:%S.%N %Z') end |
#pdict_to_hash(dict) ⇒ Hash?
Converts a String formatted like a Python Dictionary to a Ruby Hash.
98 99 100 101 102 103 104 105 |
# File 'lib/zenoss.rb', line 98 def pdict_to_hash(dict) return nil if dict.nil? dict = sanitize_str(dict) dict = dict.sub(/^\{(.*)\}$/,'\1').split(/[,:]/).map do |str| str.strip end Hash[*dict] end |
#plist_to_array(list) ⇒ Array?
Some of the REST methods return Strings that are formated like a Python list. This method turns that String into a Ruby Array. If the list parameter is nil the return value is also nil.
45 46 47 48 49 |
# File 'lib/zenoss.rb', line 45 def plist_to_array(list) return nil if list.nil? list = sanitize_str(list) list.gsub(/[\[\]]/, '').split(/,\s+/) end |
#ptuples_to_hash(tuple_array) ⇒ Hash
This takes an array of two element Python tuples and turns it into a Ruby hash.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/zenoss.rb', line 124 def ptuples_to_hash(tuple_array) return nil if tuple_array.empty? thash = {} tuple_array.each do |tuple| str = sanitize_str(tuple.strip) k, *v = str.slice!(1..-2).split(/\s*,\s*/) if(v.length <= 1) thash[k] = v.first else v[0] = v[0].slice(1..-1) v[-1] = v[-1].slice(0..-2) thash[k] = v end end thash end |
#sanitize_str(str) ⇒ String
Do some clean-up on the string returned from REST calls. Removes some quote characters embedded in the string and other misc.
146 147 148 149 150 |
# File 'lib/zenoss.rb', line 146 def sanitize_str(str) str.gsub!(/['"]/,'') str.chomp! str end |