您的位置:首页 > 其它

SaltStack源码分析之使用logrotate模块

2015-03-31 20:06 381 查看
logrotate模块用于处理日志文件轮转的相关任务

# -*- coding: utf-8 -*-
'''
Module for managing logrotate.
'''

# Import python libs
import os
import logging

# Import salt libs
import salt.utils

log = logging.getLogger(__name__)
default_conf = '/etc/logrotate.conf'

# Define a function alias in order not to shadow built-in's
__func_alias__ = {
'set_': 'set'
}


def __virtual__():
'''
Only work on POSIX-like systems
'''
if salt.utils.is_windows():
return False
return True
判断操作系统的类型,如果是windows就不使用这个模块
判断是否是windows的函数
def is_windows():
'''
Simple function to return if a host is Windows or not
'''
return sys.platform.startswith('win')


def _parse_conf(conf_file=default_conf):
'''
Parse a logrotate configuration file.

Includes will also be parsed, and their configuration will be stored in the
return dict, as if they were part of the main config file. A dict of which
configs came from which includes will be stored in the 'include files' dict
inside the return dict, for later reference by the user or module.
'''
ret = {}
mode = 'single'
multi_name = ''
multi = {}
with salt.utils.fopen(conf_file, 'r') as ifile:
for line in ifile:
line = line.strip()
if not line:
continue
if line.startswith('#'):
continue

comps = line.split()
if '{' in line and '}' not in line:
mode = 'multi'
multi_name = comps[0]
continue
if '}' in line:
mode = 'single'
ret[multi_name] = multi
multi_name = ''
multi = {}
continue

if mode == 'single':
key = ret
else:
key = multi

if comps[0] == 'include':
if 'include files' not in ret:
ret['include files'] = {}
for include in os.listdir(comps[1]):
if include not in ret['include files']:
ret['include files'][include] = []
include_path = '{0}/{1}'.format(comps[1], include)
include_conf = _parse_conf(include_path)
for file_key in include_conf:
ret[file_key] = include_conf[file_key]
ret['include files'][include].append(file_key)

if len(comps) > 1:
key[comps[0]] = ' '.join(comps[1:])
else:
key[comps[0]] = True
return ret
解析配置文件

def show_conf(conf_file=default_conf):
'''
Show parsed configuration

CLI Example:

.. code-block:: bash

salt '*' logrotate.show_conf
'''
return _parse_conf(conf_file)
显示配置文件
$ sudo salt 'gintama-qa-server' logrotate.show_conf
gintama-qa-server:
----------
/var/log/btmp:
----------
create:
0600 root utmp
missingok:
True
monthly:
True
rotate:
1
/var/log/cron:
True
/var/log/dracut.log:
----------
create:
0600 root root
missingok:
True
notifempty:
True
size:
30k
yearly:
True
/var/log/httpd/*log:
----------
/sbin/service:
httpd reload > /dev/null 2>/dev/null || true
delaycompress:
True
endscript:
True
missingok:
True
notifempty:
True
postrotate:
True
sharedscripts:
True
/var/log/maillog:
True
/var/log/messages:
True
/var/log/salt/cloud:
----------
compress:
True
missingok:
True
notifempty:
True
rotate:
5
weekly:
True
/var/log/salt/key:
----------
compress:
True
missingok:
True
notifempty:
True
rotate:
5
weekly:
True
/var/log/salt/master:
----------
compress:
True
missingok:
True
notifempty:
True
rotate:
5
weekly:
True
/var/log/salt/minion:
----------
compress:
True
missingok:
True
notifempty:
True
rotate:
5
weekly:
True
/var/log/salt/ssh:
----------
compress:
True
missingok:
True
notifempty:
True
rotate:
5
weekly:
True
/var/log/secure:
True
/var/log/spooler:
True
/var/log/wtmp:
----------
create:
0664 root utmp
minsize:
1M
monthly:
True
rotate:
1
/var/log/yum.log:
----------
create:
0600 root root
missingok:
True
notifempty:
True
yearly:
True
create:
True
dateext:
True
include:
/etc/logrotate.d
include files:
----------
dracut:
- /var/log/dracut.log
httpd:
- /var/log/httpd/*log
salt:
- /var/log/salt/master
- /var/log/salt/ssh
- /var/log/salt/cloud
- /var/log/salt/key
- /var/log/salt/minion
syslog:
- /var/log/secure
- /var/log/messages
- /var/log/maillog
- /var/log/cron
- /var/log/spooler
- {
yum:
- /var/log/yum.log
rotate:
4
weekly:
True
{:
----------
/bin/kill:
-HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript:
True
postrotate:
True
sharedscripts:
True


def set_(key, value, setting=None, conf_file=default_conf):
'''
Set a new value for a specific configuration line

CLI Example:

.. code-block:: bash

salt '*' logrotate.set rotate 2

Can also be used to set a single value inside a multiline configuration
block. For instance, to change rotate in the following block::

/var/log/wtmp {
monthly
create 0664 root root
rotate 1
}

Use the following command:

.. code-block:: bash

salt '*' logrotate.set /var/log/wtmp rotate 2

This module also has the ability to scan files inside an include directory,
and make changes in the appropriate file.
'''
conf = _parse_conf(conf_file)
for include in conf['include files']:
if key in conf['include files'][include]:
conf_file = os.path.join(conf['include'], include)

if isinstance(conf[key], dict) and not setting:
return (
'Error: {0} includes a dict, and a specific setting inside the '
'dict was not declared'.format(key)
)

if setting:
if isinstance(conf[key], str):
return ('Error: A setting for a dict was declared, but the '
'configuration line given is not a dict')
# We're going to be rewriting an entire stanza
stanza = conf[key]
if value == 'False':
del stanza[value]
else:
stanza[value] = setting
new_line = _dict_to_stanza(key, stanza)
log.debug(stanza)
log.debug(new_line)
log.debug(key)
__salt__['file.psed'](conf_file,
'{0}.*{{.*}}'.format(key),
new_line)
else:
# This is the new config line that will be set
if value == 'True':
new_line = key
elif value == 'False':
new_line = ''
else:
new_line = '{0} {1}'.format(key, value)

log.debug(conf_file)
log.debug(key)
log.debug(new_line)
__salt__['file.psed'](conf_file,
'^{0}.*'.format(key),
new_line,
flags='gM')
设置配置项

sudo salt 'gintama-qa-server' logrotate.set rotate 4

def _dict_to_stanza(key, stanza):
'''
Convert a dict to a multi-line stanza
'''
ret = ''
for skey in stanza:
if stanza[skey] is True:
stanza[skey] = ''
ret += '    {0} {1}\n'.format(skey, stanza[skey])
return '{0} {{\n{1}}}'.format(key, ret)


将一个字典数据转换成多行字段
本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1627054
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: