summaryrefslogtreecommitdiff
path: root/src/conf_mode/syslog.py
blob: f652cf3d0ddef8a889b466915abd42c5bc871c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
#
# Copyright (C) 2018 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

import sys
import os
import re
import jinja2

from vyos.config import Config
from vyos import ConfigError

########### config templates

#### /etc/rsyslog.d/vyos-rsyslog.conf ###
configs = '''
## generated by syslog.py ##
## file based logging
{% for file in files %}
$outchannel {{file}},{{files[file]['log-file']}},{{files[file]['max-size']}},{{files[file]['action-on-max-size']}}
{{files[file]['selectors']}} :omfile:${{file}}
{% endfor %}
{% if console %}
## console logging
{% for con in console %}
{{console[con]['selectors']}} /dev/console
{% endfor %}
{% endif %}
{% if hosts %}
## remote logging
{% for host in hosts %}
{% if hosts[host]['proto'] == 'tcp' %}
{{hosts[host]['selectors']}} @@{{host}}
{% else %}
{{hosts[host]['selectors']}} @{{host}}
{% endif %}
{% endfor %}
{% endif %}
{% if user %}
{% for u in user %}
{{user[u]['selectors']}} :omusrmsg:{{u}}
{% endfor %}
{% endif %}
'''

logrotate_configs = '''
{% for file in files %}
{{files[file]['log-file']}} {
  missingok
  notifempty
  create
  rotate  {{files[file]['max-files']}}
  size={{ files[file]['max-size']//1024}}k
  postrotate
    invoke-rc.d rsyslog rotate > /dev/null
  endscript
}
{% endfor %}
'''
############# config templates end

def get_config():
  c = Config()
  if not c.exists('system syslog'):
    return None
  c.set_level('system syslog')  

  config_data = {
    'files' : {},
    'console' : {},
    'hosts' : {},
    'user'  : {}
  }

  #####
  # /etc/rsyslog.d/vyos-rsyslog.conf
  # 'set system syslog global'
  #####
  config_data['files'].update(
    {
      'global'  : {
        'log-file' : '/var/log/messages',
        'max-size'  : 262144,
        'action-on-max-size'  : '/usr/sbin/logrotate /etc/logrotate.d/vyos-rsyslog',
        'selectors'  : '*.notice;local7.debug',
        'max-files' : '5'
      }
    }
  )

  if c.exists('global facility'):
    config_data['files']['global']['selectors'] = generate_selectors(c, 'global facility')
  if c.exists('global archive size'):
    config_data['files']['global']['max-size'] = int(c.return_value('global archive size'))* 1024
  if c.exists('global archive files'):
    config_data['files']['global']['max-files'] = c.return_value('global archive files')

  ###
  # set system syslog file
  ###

  if c.exists('file'):
    filenames = c.list_nodes('file')
    for filename in filenames:
      config_data['files'].update(
        {
          filename  : { 
            'log-file' : '/var/log/user/' + filename,
            'max-files' : '5',
            'action-on-max-size'  : '/usr/sbin/logrotate /etc/logrotate.d/' + filename,
            'selectors'  : '*.err',
            'max-size'  : 262144     
          }
        }
      )

      if c.exists('file ' + filename + ' facility'):
        config_data['files'][filename]['selectors']  = generate_selectors(c, 'file ' + filename + ' facility')
      if c.exists('file ' + filename + ' archive size'):
       config_data['files'][filename]['max-size']  = int(c.return_value('file ' + filename + ' archive size'))* 1024
      if c.exists('file ' + filename + ' archive files'):
        config_data['files'][filename]['max-files'] = c.return_value('file ' + filename + ' archive files')

  ## set system syslog console
  if c.exists('console'):
    config_data['console']  = {
      '/dev/console' : {
        'selectors' : '*.err'
      }
    }
    
  for f in c.list_nodes('console facility'):
    if c.exists('console facility ' + f + ' level'):
      config_data['console'] = {
        '/dev/console' : {
          'selectors' : generate_selectors(c, 'console facility')
        }
      }
    
  ## set system syslog host
  if c.exists('host'):
    proto = 'udp'
    rhosts = c.list_nodes('host')
    for rhost in rhosts:
      for fac in c.list_nodes('host ' + rhost + ' facility'):
        if c.exists('host ' + rhost + ' facility ' + fac + ' protocol'):
          proto = c.return_value('host ' + rhost + ' facility ' + fac + ' protocol')

      config_data['hosts'].update(
        {
          rhost : {
           'selectors' : generate_selectors(c, 'host ' + rhost + ' facility'), 
            'proto' : proto
          } 
        }
      )

  ## set system syslog user
  if c.exists('user'):
    usrs = c.list_nodes('user')
    for usr in usrs:
      config_data['user'].update(
        {
          usr : {
            'selectors' : generate_selectors(c, 'user ' + usr + ' facility')
          }
        }
      )

  return config_data

def generate_selectors(c, config_node):
## protocols and security are being mapped here
## for backward compatibility with old configs
## security and protocol mappings can be removed later
  if c.is_tag(config_node):
    nodes = c.list_nodes(config_node)
    selectors = ""
    for node in nodes:
      lvl = c.return_value( config_node + ' ' + node + ' level')
      if lvl == None:
        lvl = "err"
      if lvl == 'all':
        lvl = '*'
      if node == 'all' and node != nodes[-1]:
        selectors += "*." + lvl + ";"
      elif node == 'all':
        selectors += "*." + lvl
      elif node != nodes[-1]:
        if node == 'protocols':
          node = 'local7'
        if node == 'security':
          node = 'auth'
        selectors += node + "." + lvl + ";"
      else:
        if node == 'protocols':
          node = 'local7'
        if node == 'security':
          node = 'auth'
        selectors += node + "." + lvl
    return selectors

def generate(c):
  tmpl = jinja2.Template(configs, trim_blocks=True)
  config_text = tmpl.render(c)
  #print (config_text)
  with open('/etc/rsyslog.d/vyos-rsyslog.conf', 'w') as f:
    f.write(config_text)

  ## eventually write for each file its own logrotate file, since size is defined it shouldn't matter
  tmpl = jinja2.Template(logrotate_configs, trim_blocks=True)
  config_text = tmpl.render(c)
  #print (config_text)
  with open('/etc/logrotate.d/vyos-rsyslog', 'w') as f:
    f.write(config_text)

def verify(c):
  #
  # /etc/rsyslog.conf is generated somewhere and copied over the original (exists in /opt/vyatta/etc/rsyslog.conf)
  # it interferes with the global logging, to make sure we are using a single base, template is enforced here 
  #

  if not os.path.islink('/etc/rsyslog.conf'):
    os.remove('/etc/rsyslog.conf')
    os.symlink('/usr/share/vyos/templates/rsyslog/rsyslog.conf', '/etc/rsyslog.conf')

  # /var/log/vyos-rsyslog were the old files, we may want to clean those up, but currently there
  # is a chance that someone still needs it, so I don't automatically remove them 

  if c == None:
    return None

  fac = ['*','auth','authpriv','cron','daemon','kern','lpr','mail','mark','news','protocols','security',\
        'syslog','user','uucp','local0','local1','local2','local3','local4','local5','local6','local7']
  lvl = ['emerg','alert','crit','err','warning','notice','info','debug','*']
  for conf in c:
    if c[conf]:
      for item in c[conf]:
        for s in c[conf][item]['selectors'].split(";"):
          f = re.sub("\..*$","",s)
          if f not in fac:
            print (c[conf])
            raise ConfigError('Invalid facility ' + s + ' set in '+ conf + ' ' + item)
          l = re.sub("^.+\.","",s)
          if l not in lvl:
            raise ConfigError('Invalid logging level ' + s + ' set in '+ conf + ' ' + item)

def apply(c):
  ### vyatta-log.conf is being generated somewhere
  ### this is just a quick hack to remove the old configfile
  
  if os.path.exists('/etc/rsyslog.d/vyatta-log.conf'):
    os.remove('/etc/rsyslog.d/vyatta-log.conf')
  os.system("sudo systemctl restart rsyslog >/dev/null")

if __name__ == '__main__':
  try:
    c = get_config()
    verify(c)
    generate(c)
    apply(c)
  except ConfigError as e:
    print(e)
    sys.exit(1)