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


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

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']
  @facility = opts['facility']

  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

#facilityObject (readonly)

Returns the value of attribute facility


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

def facility
  @facility
end

#levelObject (readonly)

Returns the value of attribute level


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

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port


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

def port
  @port
end

#severity_levelObject (readonly)

Returns the value of attribute severity_level


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

def severity_level
  @severity_level
end

#vrfObject (readonly)

Returns the value of attribute vrf


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

def vrf
  @vrf
end

Class Method Details

.syslogserversObject


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

def self.syslogservers
  keys = %w(name level port vrf facility 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?
    value_hash['facility'] = 'local7' if value_hash['facility'].nil?
    hash[id[0]] = SyslogServer.new(value_hash, false)
  end

  hash
end

Instance Method Details

#==(other) ⇒ Object


77
78
79
# File 'lib/cisco_node_utils/syslog_server.rb', line 77

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

#createObject


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

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}" : '',
               facility: @facility ? "facility #{@facility}" : '',
              )
  end
end

#destroy(duplicate_vrfs = []) ⇒ Object


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

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:      '',
               facility: '',
              )
  end
end