Class: Oxidized::Model
- Inherits:
-
Object
show all
- Extended by:
- DSLCommands, DSLSetup, Macros
- Includes:
- Config::Vars, SemanticLogger::Loggable
- Defined in:
- lib/oxidized/model/model.rb,
lib/oxidized/model/macros.rb,
lib/oxidized/model/outputs.rb,
lib/oxidized/model/dslsetup.rb,
lib/oxidized/model/dslcommands.rb
Direct Known Subclasses
ACMEPACKET, ACOS, ACSW, ADVA, AEN, ALTEONOS, AOS, AOS7, AOSW, ARBOS, ASA, AWPlus, AddPack, Adtran, Aireos, Airfiber, Airos, Alvarion, Aoscx, ApcAos, Apc_aos, AricentISS, ArubaInstant, AsterNOS, AsyncOS, AudioCodes, AudioCodesMP, AxOS, BDCOM, BR6910, Boss, C4CMTS, CNOS, Cambium, CambiumePMP, Casa, Catos, CiscoCE, CiscoNGA, CiscoSMA, CiscoSMB, CiscoVPN3k, ComnetMS, Comtrol, Comware, Coriant8600, CoriantGroove, CoriantTmos, Cumulus, DCNOS, DNOS, DataCom, Defacto, DellX, Dlink, DlinkNextGen, ECIapollo, EFOS, EOS, EatonNetwork, EdgeCOS, EdgeSwitch, Edgeos, Eltex, Enterasys, Enterasys800, Enterprise_SONiC, Exalink, F5OS, FSOS, FTOS, FabricOS, FastIron, FiberDriver, FireLinuxOS, Firebrick, FirewareOS, FortiGate, FortiOS, FortiWLC, FujitsuPY, GaiaOS, Garderos, GcomBNPS, GrandStream, GrandstreamHT8xx, H3C, HPEBladeSystem, HPMSM, Hatteras, Hios, Hirschmann, HpeMsa, IBOS, IOS, IOSXR, IPOS, ISAM, Icotera, Ingate, IronWare, Ivanti, JunOS, KornfeldOS, LANCOM, LenovoNOS, LinksysSRW, LinuxGeneric, ML66, MLNXOS, MasterOS, Mimosab11, Mtrlrfs, NDMS, NOS, NSXConfig, NSXDfw, NSXFirewall, NXOS, NecIX, NetScaler, Netgear, Netonix, Nodegrid, OS10, OS6, OcNOS, OneFinity, OneOS, OpenGear, OpenWrt, Openbsd, OpnSense, PanOS, PanOS_API, Perle, PfSense, Planet, PowerConnect, Procurve, PurityOS, QTECH, QuantaOS, RAISECOM, RGOS, Riverbed, RouterOS, SAOS, SAOS10, SGOS, SLXOS, SROS, SROSMD, ScreenOS, Siklu, SikluMHTG, SixWind, SmartAX, SmartByte, SmartCS, SonicOS, SpeedTouch, StoneOS, SwOS, TDRE, TELCO, TMOS, TNSR, TPLink, Trango, TrueNAS, UCS, UPLINKOLT, Unifiap, VOLTAIRE, VRP, Viptela, Voss, Vyatta, Vyos, WEOS, XOS, Yamaha, ZTEOLT, ZhoneOLT, Zy1308, ZyNOS, ZyNOSADSL, ZyNOSCLI, ZyNOSGS, ZyNOSMGS
Defined Under Namespace
Modules: DSLCommands, DSLSetup, Macros
Classes: Outputs
Constant Summary
collapse
- METADATA_DEFAULT =
rubocop:disable Style/FormatStringToken
"%{comment}Fetched by Oxidized with model %{model} " \
"from host %{name} [%{ip}]\n".freeze
Constants included
from Macros
Macros::VERBS
Instance Attribute Summary collapse
Attributes included from DSLCommands
#procs
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from DSLSetup
cfgs, input_sequence, inputs
cmds, post, pre
#vars
Instance Attribute Details
Returns the value of attribute input.
48
49
50
|
# File 'lib/oxidized/model/model.rb', line 48
def input
@input
end
|
#node ⇒ Object
Returns the value of attribute node.
48
49
50
|
# File 'lib/oxidized/model/model.rb', line 48
def node
@node
end
|
Class Method Details
.inherited(klass) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/oxidized/model/model.rb', line 26
def inherited(klass)
super
if klass.superclass == Oxidized::Model
klass.instance_variable_set('@cmd', Hash.new { |h, k| h[k] = [] })
klass.instance_variable_set('@cfg', Hash.new { |h, k| h[k] = [] })
klass.instance_variable_set('@procs', Hash.new { |h, k| h[k] = [] })
klass.instance_variable_set '@expect', []
klass.instance_variable_set '@comment', nil
klass.instance_variable_set '@prompt', nil
klass.instance_variable_set '@metadata', {}
klass.instance_variable_set '@inputs', nil
else instance_variables.each do |var|
iv = instance_variable_get(var)
klass.instance_variable_set var, iv.dup
@cmd[:cmd] = iv[:cmd].dup if var.to_s == "@cmd"
end
end
end
|
Instance Method Details
#cfg ⇒ Object
125
126
127
|
# File 'lib/oxidized/model/model.rb', line 125
def cfg
self.class.cfgs
end
|
#cmd(string, input: nil, &block) ⇒ Object
input specifies to run this command only with this input type if input is not specified, always run the command
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/oxidized/model/model.rb', line 52
def cmd(string, input: nil, &block)
logger.debug "Executing #{string}"
out = if input.nil? || input.include?(@input.to_sym)
out = @input.cmd(string)
else
return ''
end
return false unless out
out = out.b unless Oxidized.config.input.utf8_encoded?
self.class.cmds[:all].each do |all_block|
out = instance_exec out, string, &all_block
end
if vars :remove_secret
self.class.cmds[:secret].each do |all_block|
out = instance_exec out, string, &all_block
end
end
out = instance_exec out, &block if block
process_cmd_output out, string
end
|
174
175
176
177
178
179
180
|
# File 'lib/oxidized/model/model.rb', line 174
def (str)
data = String.new('')
str.each_line do |line|
data << self.class. << line
end
data
end
|
#expect ⇒ Object
121
122
123
|
# File 'lib/oxidized/model/model.rb', line 121
def expect(...)
self.class.expect(...)
end
|
#expects(data) ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/oxidized/model/model.rb', line 133
def expects(data)
self.class.expects.each do |re, cb|
if data.match re
data = cb.arity == 2 ? instance_exec([data, re], &cb) : instance_exec(data, &cb)
end
end
data
end
|
#get ⇒ Object
Get the commands from the model
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
|
# File 'lib/oxidized/model/model.rb', line 143
def get
logger.debug 'Collecting commands\' outputs'
outputs = Outputs.new
self.class.cmds[:cmd].each do |data|
command = data[:cmd]
args = data[:args]
block = data[:block]
next if args.include?(:if) && !instance_exec(&args[:if])
out = cmd command, input: args[:input], &block
return false unless out
outputs << out
end
procs = self.class.procs
procs[:pre].each do |pre_proc|
outputs.unshift process_cmd_output(instance_eval(&pre_proc), '')
end
procs[:post].each do |post_proc|
outputs << process_cmd_output(instance_eval(&post_proc), '')
end
if vars("metadata") == true
metadata_top = metadata(:top)
metadata_bottom = metadata(:bottom)
outputs.unshift metadata_top if metadata_top
outputs << metadata_bottom if metadata_bottom
end
outputs
end
|
#interpolate_string(template) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/oxidized/model/model.rb', line 93
def interpolate_string(template)
return nil unless template
time = Time.now
template_variables = {
model: self.class.name,
name: node.name,
ip: node.ip,
group: node.group,
comment: self.class.,
year: time.year,
month: "%02d" % time.month,
day: "%02d" % time.day,
hour: "%02d" % time.hour,
minute: "%02d" % time.min,
second: "%02d" % time.sec
}
template % template_variables
end
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/oxidized/model/model.rb', line 75
def metadata(position)
return unless %i[top bottom].include? position
model_metadata = self.class.instance_variable_get(:@metadata)
var_position = { top: "metadata_top", bottom: "metadata_bottom" }
if model_metadata[:top] || model_metadata[:bottom]
value = model_metadata[position]
value.is_a?(Proc) ? instance_eval(&value) : interpolate_string(value)
elsif vars("metadata_top") || vars("metadata_bottom")
interpolate_string(vars(var_position[position]))
elsif position == :top
interpolate_string(METADATA_DEFAULT)
end
end
|
#output ⇒ Object
113
114
115
|
# File 'lib/oxidized/model/model.rb', line 113
def output
@input.output
end
|
#prompt ⇒ Object
129
130
131
|
# File 'lib/oxidized/model/model.rb', line 129
def prompt
self.class.prompt
end
|
#screenscrape ⇒ Object
200
201
202
|
# File 'lib/oxidized/model/model.rb', line 200
def screenscrape
@input.class.to_s.match(/Telnet/) || vars(:ssh_no_exec)
end
|
#send(data) ⇒ Object
117
118
119
|
# File 'lib/oxidized/model/model.rb', line 117
def send(data)
@input.send data
end
|
#significant_changes(config) ⇒ Object
204
205
206
207
208
209
|
# File 'lib/oxidized/model/model.rb', line 204
def significant_changes(config)
self.class.cmds[:significant_changes].each do |block|
config = instance_exec config, &block
end
config
end
|
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/oxidized/model/model.rb', line 182
def (str)
data = String.new('')
str.each_line do |_line|
data << '<!-- ' << str.gsub(/-(?=-)/, '- ').chomp << " -->\n"
end
data
end
|