class Opsviewconfig

Public Class Methods

new(config) click to toggle source
# File lib/opsviewconfig.rb, line 18
def initialize(config)
  # Connect to opsview and return handler
  @connection = OpsviewRest.new("http://" + config['opsviewhost'] + "/", :username => config['opsviewuser'], :password => config['opsviewpassword'])
end

Public Instance Methods

connection() click to toggle source
# File lib/opsviewconfig.rb, line 23
def connection
  return @connecton
end
export(resourcetype,folder) click to toggle source
# File lib/opsviewconfig.rb, line 27
def export(resourcetype,folder)
  res = @connection.list(:type => resourcetype)
  # Need to parse out junk we don't need to export
  res = export_parse(res,resourcetype)
  res.each do |resource|
    filename = resource['name'].dup
    filename.gsub!(/[^0-9A-Za-z.\-]/, '_')
    #puts "Exporting #{resource['name']} to #{filename}"
    Dir.mkdir(folder) unless Dir.exist?(folder)
    File.write("#{folder}/#{filename}.json",JSON.pretty_generate(resource))
  end
  return true
end
export_parse(export,resourcetype) click to toggle source

Function to clean up the exported object

# File lib/opsviewconfig.rb, line 42
def export_parse(export,resourcetype)
  cleanexport = Array.new()
  export.each do |resource|
    # Delete the id's, since these are installation specific
    resource.delete("id")

    # Delete the hosts to which the some of these resources are assigned to, since these might not exist elsewhere
    if resourcetype == "servicecheck" || resourcetype == "hosttemplate" || resourcetype == "hostcheckcommand"
      resource.delete("hosts")
    end

    # Remove hosttemplates from servicechecks
    if resourcetype == "servicecheck"
      resource.delete("hosttemplates")
    end

    if resourcetype == "host"
      # Don't save if this is an EC2 host instance, those should be generated automatically and we don't need to version control them
      unless resource["hosttemplates"].nil?
        next if resource["hosttemplates"].find { |h| h['name'] == 'ec2instance' }
      end
      # Remove id's from the host attributes since they are auto-generated
      unless resource["hostattributes"].nil?
        resource["hostattributes"].each { |attr| attr.delete("id") }
      end
    end

    # Don't save this if this is one of the default timeperiods
    if resourcetype == "timeperiod"
      next if ["workhours","nonworkhours","none","24x7"].include? resource["name"]
    end

    # Save
    cleanexport << resource.sort_by_key(true)
  end
  return cleanexport
end
import(type,filename=nil,folder=nil) click to toggle source
# File lib/opsviewconfig.rb, line 80
def import(type,filename=nil,folder=nil)
  resourceconfig = JSON.parse(File.read(filename))
  resourceconfig[:type] = :"#{type}"
  resourceconfig[:replace] = true
  res = @connection.create(resourceconfig)
  return true
end
reload() click to toggle source
# File lib/opsviewconfig.rb, line 88
def reload()
  @connection.initiate_reload()
  return true
end