Class: Cisco::SyslogServer

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

Overview

NtpServer - node utility class for NTP 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(name, level = nil, vrf = nil, instantiate = true) ⇒ SyslogServer

Returns a new instance of SyslogServer.



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

def initialize(name,
               level=nil,
               vrf=nil,
               instantiate=true)
  fail TypeError unless name.is_a?(String)
  fail TypeError unless name.length > 0
  @name = name

  fail TypeError unless level.is_a?(Integer) || level.nil?
  @level = level

  fail TypeError unless vrf.is_a?(String) || vrf.nil?
  @vrf = vrf

  create if instantiate
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#vrfObject (readonly)

Returns the value of attribute vrf.



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

def vrf
  @vrf
end

Class Method Details

.syslogserversObject



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/cisco_node_utils/syslog_server.rb', line 53

def self.syslogservers
  hash = {}
  syslogservers_list = config_get('syslog_server', 'server')
  return hash if syslogservers_list.nil?

  syslogservers_list.each do |id|
    # The YAML regex isn't specific enough for some platforms,
    # so we have to do further checking.
    begin
      IPAddr.new(id)
    rescue
      next
    end

    level = config_get('syslog_server', 'level', id)
    level = level[0] if level.is_a?(Array)
    level = LEVEL_TO_NUM[level] if platform == :ios_xr

    vrf = config_get('syslog_server', 'vrf', id)
    vrf = vrf[0] if vrf.is_a?(Array)

    hash[id] = SyslogServer.new(id, level, vrf, false)
  end

  hash
end

Instance Method Details

#==(other) ⇒ Object



80
81
82
# File 'lib/cisco_node_utils/syslog_server.rb', line 80

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

#createObject



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

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.nil? ? '' : "severity #{NUM_TO_LEVEL[level]}",
               vrf:   vrf.nil? ? '' : "vrf #{vrf}",
              )
  else
    config_set('syslog_server',
               'server',
               state: '',
               ip:    "#{name}",
               level: level.nil? ? '' : "#{level}",
               vrf:   vrf.nil? ? '' : "use-vrf #{vrf}",
              )
  end
end

#destroy(duplicate_vrfs = []) ⇒ Object



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

def destroy(duplicate_vrfs=[])
  if platform == :ios_xr
    if duplicate_vrfs.empty?
      config_set('syslog_server',
                 'server',
                 state: 'no',
                 ip:    "#{name}",
                 level: '',
                 vrf:   vrf.nil? ? '' : "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: '',
               vrf:   '',
              )
  end
end