Class: Cisco::SyslogServer

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/syslog_server.rb

Overview

SyslogServer - node utility class for syslog server configuration management

Constant Summary collapse

LEVEL_TO_NUM =
{ 'emergencies'   => 0,
'alerts'        => 1,
'critical'      => 2,
'error'         => 3,
'warning'       => 4,
'notifications' => 5,
'info'          => 6,
'debugging'     => 7 }.freeze
NUM_TO_LEVEL =
LEVEL_TO_NUM.invert.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeUtil

client, #client, config_get, #config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, #node, node, platform, #platform, supports?, #supports?

Constructor Details

#initialize(opts, instantiate = true) ⇒ SyslogServer

Returns a new instance of SyslogServer.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cisco_node_utils/syslog_server.rb', line 37

def initialize(opts, instantiate=true)
  @name = opts['name']
  @level = opts['level'] || opts['severity_level']
  @port = opts['port']
  @vrf = opts['vrf']
  @severity_level = opts['severity_level'] || opts['level']

  hostname_regex = /^(?=.{1,255}$)[0-9A-Za-z]
  (?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?
  (?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/x

  unless @name =~ Resolv::AddressRegex ||
         @name =~ hostname_regex
    fail ArgumentError,
         "Invalid value '#{@name}' \
    (Must be valid IPv4/IPv6 address or hostname)"
  end

  create if instantiate
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



25
26
27
# File 'lib/cisco_node_utils/syslog_server.rb', line 25

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/cisco_node_utils/syslog_server.rb', line 25

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/cisco_node_utils/syslog_server.rb', line 25

def port
  @port
end

#severity_levelObject (readonly)

Returns the value of attribute severity_level.



25
26
27
# File 'lib/cisco_node_utils/syslog_server.rb', line 25

def severity_level
  @severity_level
end

#vrfObject (readonly)

Returns the value of attribute vrf.



25
26
27
# File 'lib/cisco_node_utils/syslog_server.rb', line 25

def vrf
  @vrf
end

Class Method Details

.syslogserversObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cisco_node_utils/syslog_server.rb', line 58

def self.syslogservers
  keys = %w(name level port vrf severity_level)
  hash = {}
  syslogservers_list = config_get('syslog_server', 'server')
  return hash if syslogservers_list.nil?

  syslogservers_list.each do |id|
    value_hash = Hash[keys.zip(id)]
    value_hash['severity_level'] = value_hash['level']
    value_hash['vrf'] = 'default' if value_hash['vrf'].nil?
    hash[id[0]] = SyslogServer.new(value_hash, false)
  end

  hash
end

Instance Method Details

#==(other) ⇒ Object



74
75
76
# File 'lib/cisco_node_utils/syslog_server.rb', line 74

def ==(other)
  (name == other.name) && (vrf == other.vrf)
end

#createObject



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
# File 'lib/cisco_node_utils/syslog_server.rb', line 78

def create
  if platform == :ios_xr

    # This provider only support a 1-1 mapping between host and VRF.
    # Thus, we must remove the other entries on different VRFs.
    all_vrfs = config_get('syslog_server', 'vrf', name)
    destroy(all_vrfs) if all_vrfs.is_a?(Array) && all_vrfs.count > 1

    config_set('syslog_server',
               'server',
               state: '',
               ip:    @name,
               level: @level ? "severity #{NUM_TO_LEVEL[@level]}" : '',
               vrf:   @vrf ? "vrf #{@vrf}" : '',
              )
  else
    config_set('syslog_server',
               'server',
               state: '',
               ip:    @name,
               level: @level ? "#{@level}" : '',
               port:  @port ? "port #{@port}" : '',
               vrf:   @vrf ? "use-vrf #{@vrf}" : '',
              )
  end
end

#destroy(duplicate_vrfs = []) ⇒ Object



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
# File 'lib/cisco_node_utils/syslog_server.rb', line 105

def destroy(duplicate_vrfs=[])
  if platform == :ios_xr
    if duplicate_vrfs.empty?
      config_set('syslog_server',
                 'server',
                 state: 'no',
                 ip:    @name,
                 level: '',
                 vrf:   @vrf ? "vrf #{@vrf}" : '',
                )
    else
      warn("#{name} is configured multiple times on the device" \
        ' (possibly in different VRFs). This is unsupported by this' \
        ' API and the duplicate entries are being deleted.')
      duplicate_vrfs.each do |dup|
        config_set('syslog_server',
                   'server',
                   state: 'no',
                   ip:    @name,
                   level: '',
                   vrf:   "vrf #{dup}",
                  )
      end
    end
  else
    config_set('syslog_server',
               'server',
               state: 'no',
               ip:    @name,
               level: '',
               port:  '',
               vrf:   '',
              )
  end
end