Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to improve Syslog daemon detection. #26

Merged
merged 4 commits into from Aug 31, 2013
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Improve syslog daemon detection

* No longer need to figure out distro before we decide to run
  `dpkg` or `rpm` to find which version of a daemon we are running

* Add test for replacement method that detects the syslog daemon.
  • Loading branch information
♥ Ivan Tam ♥
♥ Ivan Tam ♥ committed Aug 30, 2013
commit c1d05cdb70a896be1a9498080473557566a13788
@@ -14,7 +14,7 @@
# (c) Copyright Loggly 2013.
######################################################################

import os
import os, os.path
import platform
import re
import sys
@@ -366,30 +366,43 @@ def get_syslog_process_name(product_name):
'rsyslog': RSYSLOG_PROCESS,
}.get(product_name.lower(), PROD_UNSUPPORTED)

def get_syslog_version(distro_id):
def get_syslog_version(verify_config_paths=False):
"""
Determine which syslog version is installed
Guess the syslog daemon and version number,
returns a singleton list containing a tuple: [(daemon, version_string)]
if verify_config_paths is set, this will also verify the configuration file
and/or paths are present on this system
"""
LOGGER.debug("Reading installed Syslog versions....")
if distro_id == OS_UBUNTU:
command = r"dpkg -l \*sys\*log\* | grep ^ii"
pattern = r'ii\s+(rsyslog|syslog-ng)\s+(\d+\.\d+)'
elif distro_id == OS_FEDORA:
command = "rpm -qa | grep -i 'sys' | grep -i 'log'"
pattern = r'(rsyslog|syslog-ng)-(\d+\.\d+)'
elif distro_id == OS_RHEL:
command = "rpm -qa | grep -i 'sys' | grep -i 'log'"
pattern = r'(rsyslog|syslog-ng)-(\d+\.\d+)'
elif distro_id == OS_CENTOS:
command = "rpm -qa | grep -i 'sys' | grep -i 'log'"
pattern = r'(rsyslog|syslog-ng)-(\d+\.\d+)'
else:
return []

output = os.popen(command).read()
compiled_regex = re.compile(pattern, re.MULTILINE | re.IGNORECASE)
SYSLOG_PATHS = [
# (syslog type, command, conf path, conf.d path)
("rsyslog", "rsyslogd -v", "/etc/rsyslog.conf", "/etc/rsyslog.d/"),
("syslog-ng", "syslog-ng --version", "/etc/syslog-ng/syslog-ng.conf", "/etc/syslog-ng/")
]

for s in SYSLOG_PATHS:

p = subprocess.Popen(s[1], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p.wait()
if p.returncode != 0:
continue

version_line = p.stdout.readlines()[0]

if verify_config_paths:
if not os.path.isfile(s[2]) or not os.path.isdir(s[3]):
continue

# extract the version number from the daemon's version report,
# only cares about the major and minor version numbers and ignores
# the point number.
version_pattern = r'%s (\d+\.+\d)' % s[0]
version_string = re.search(version_pattern, version_line).group(1)

return [(s[0], version_string)]

return compiled_regex.findall(output)
return []

def get_user_type():
"""
@@ -426,7 +439,7 @@ def get_environment_details():
'distro_id': distro_id,
'version': version,
'id': version_id,
'syslog_versions': get_syslog_version(distro_id),
'syslog_versions': get_syslog_version(),
'supported_syslog_versions': {},
'operating_system': "%s-%s(%s)" % distribution
}
@@ -850,8 +863,7 @@ def syslog_config_file_content(syslog_id, source, authorization_details):
configured_source = source
source_created = ''
if len(configured_source) <= 0:
syslog_version = get_selected_syslog_version(syslog_id,
get_syslog_version(get_os_id(platform.linux_distribution()[0])))
syslog_version = get_selected_syslog_version(syslog_id, get_syslog_version())

if syslog_version > float(3.2):
source_created = (SYSLOG_NG_SOURCE_TEXT_ABOVE_3_2
@@ -0,0 +1,38 @@
import unittest, platform

confsys = __import__("configure-syslog")

# "mock" out the LOGGER object
class Amorphous(object):
def __getattr__(self, name):
return lambda x: x

confsys.LOGGER = Amorphous()

class TestConfigureSyslog(unittest.TestCase):

def test_get_syslog_version(self):

r = confsys.get_syslog_version()
t = r[0]

self.assertTrue(len(r) > 0)
self.assertTrue(isinstance(t, tuple) )

self.assertTrue(t[0] in ['rsyslog', 'syslog-ng'] )

# versions should only be of xx.yy form; no more, no less
self.assertEquals(2, len(t[1].split('.')) )

def skip_test_new_old_equality(self):

new_get = confsys.new_get_syslog_version()

distro_name, version, version_id = platform.linux_distribution()
distro_id = confsys.get_os_id(distro_name)
old_get = confsys.get_syslog_version(distro_id)

self.assertEquals( new_get, old_get)

if __name__ == "__main__":
unittest.main()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.