Class: Opsviewconfig

Inherits:
Object
  • Object
show all
Defined in:
lib/opsviewconfig.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Opsviewconfig

Returns a new instance of Opsviewconfig.



18
19
20
21
# 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

Instance Method Details

#connectionObject



23
24
25
# File 'lib/opsviewconfig.rb', line 23

def connection
  return @connecton
end

#export(resourcetype, folder) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

Function to clean up the exported object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



80
81
82
83
84
85
86
# 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

#reloadObject



88
89
90
91
# File 'lib/opsviewconfig.rb', line 88

def reload()
  @connection.initiate_reload()
  return true
end