Class: OptparseNagiosProbe

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

Overview

OptparseNagiosProbe - opennebula-nagios probes ARGV parser class.

Constant Summary collapse

VERSION =
0.99

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/probe/optparse_nagios_probe.rb', line 24

def self.parse(args)
  options = OpenStruct.new

  options.debuglevel = 0

  options.hostname = 'localhost'
  options.port     = 2633
  options.path     = '/'
  options.protocol = :http
  options.username = 'oneadmin'
  options.password = 'onepass'

  options.service = :oned
  options.occi    = :occi

  options.timeout = 60

  options.user_cred = nil
  options.voms = false

  opts_ = OptionParser.new do |opts|
    opts.banner = 'Usage: check_opennebula.rb [options]'

    opts.separator ''
    opts.separator 'Connection options:'

    opts.on('--protocol [http|https]', [:http, :https], "Protocol to use, defaults to 'http'") do |protocol|
      options.protocol = protocol
    end

    opts.on('--hostname [HOSTNAME]', String, "Host to be queried, defaults to 'localhost'") do |hostname|
      options.hostname = hostname
    end

    opts.on('--port [PORT_NUMBER]', Integer, "Port to be queried, defaults to '2633'") do |port|
      options.port = port
    end

    opts.on('--path [PATH]', String, 'Path to the service endpoint'\
    + "(the last part of the URI, should always start with a slash), defaults to '/'") do |path|
      options.path = path
    end

    opts.on('--username [USERNAME]', String, 'Username for authentication purposes, '\
    + "defaults to 'oneadmin'") do |username|
      options.username = username
    end

    opts.on('--password [PASSWORD]', String, 'Password for authentication purposes, '\
    + "defaults to 'onepass'") do |password|
      options.password = password
    end

    opts.separator ''
    opts.separator 'Session options:'

    opts.on('--timeout [SECONDS]', Integer, "Timeout time in seconds, defaults to '60'") do |timeout|
      options.timeout = timeout
    end

    opts.separator ''
    opts.separator 'Service options:'

    opts.on('--service [SERVICE_NAME]', [:oned, :occi, :econe, :rocci], 'Name of the cloud service'\
     + " to check [oned, occi, rocci, econe], defaults to 'oned'") do |service|
      options.service = service
    end

    opts.on("--check-network [ID'S]", Array, 'Comma separated list of network IDs to check') do |network|
      options.network = network
    end

    opts.on("--check-storage [ID'S]", Array, 'Comma separated list of storage IDs to check') do |storage|
      options.storage = storage
    end

    opts.on("--check-compute [ID'S]", Array, 'Comma separated list of VM IDs to check') do |compute|
      options.compute = compute
    end

    opts.on('--createvm [TEMPLATE_UUID]', String, 'rOCCI template uuid') do |tmpl|
      options.template_uuid = tmpl
    end

    opts.on('--name [NAME]', String, 'Name for VM instantiated from template') do |vmname|
      options.vmname = vmname
    end

    opts.separator ''
    opts.separator 'X.509 options:'

    opts.on('--user-cred [PATH]', String, "Path to user's X.509 credentials, defaults to ~/.globus/usercred.pem'")\
      do |ucred|
      options.user_cred = ucred
    end

    opts.on('--ca-file [PATH]', String, 'Path to CA certificates bundle in a file') do |cafile|
      options.ca_file = cafile
    end

    opts.on('--ca-path [PATH]', String, 'Path to CA certificates directory, defaults to "/etc/grid-security/certificates"')\
     do |capath|
      options.ca_path = capath
    end

    opts.on('--voms', '--[no-]voms', 'Enable VOMS credentials; modifies behavior of the X.509 authN module')\
     do |voms|
      options.voms = voms
    end

    opts.separator ''
    opts.separator 'Common options:'

    opts.on('--debuglevel [NUMBER]', Integer, "Run with debugging mode on certain level, defaults to '0'") do |debug|
      if !debug
        options.debug_level = 1
      else
        options.debug_level = debug
      end

      # Param. correction - normalize debug level to max 2, minus sign interpreted as switch, no problem there
      options.debug_level %= 3
    end

    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit!
    end

    opts.on_tail('--version', 'Show version') do
      puts VERSION
      exit!
    end

  end

  opts_.parse!(args)

  # Emphasize required fields
  mandatory = [:protocol, :hostname, :port, :path, :service, :password]
  mandatory << :username unless options.user_cred

  options_hash = options.marshal_dump

  # Bug here, i am not sure the mandatory params working
  missing = mandatory.select { |param| options_hash[param].nil? }
  fail StandardError, "Missing required arguments #{missing.join(', ')}" unless missing.empty?

  options
end