Configuring NXlog with python


Hello folks,
I’ve finally started working with python for one of our new automatization systems, saltstack. I’ve been experimenting with saltstack recently so I don’t have much experience in this technology. Anyway, I’ve created a script that allows you to configure a log aggregation server (such as Graylog or Flume) on your NXlog client. The script verifies if a hostname or IP address is already configured using a regex formula and if a configuration is not found, allows you to configure the server:

import re
import logging
import subprocess


logger = logging.getLogger(__name__)

def checkconf():
    k = 0
    file = ‘C:\\Program Files (x86)\\nxlog\\conf\\nxlog.conf’
    f=open(file,”r”)
    for line in f:
        if re.match(“^\s*Host\s*([a-z]|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})”, line):
            k = 1
    if k == 1:
        return True
    else:
        return False

def set(flumeip=”127.0.0.1″):
    configfile = ‘C:\\Program Files (x86)\\nxlog\\conf\\nxlog.conf’
    oldpattern = “Host “
    newpattern = “Host ” + flumeip


    if checkconf() == True:
        logger.info(“Nxlog configuration already found, skipping”)
    else:
        f = open(configfile,’r’)
        filedata = f.read()
        f.close()
        newdata = filedata.replace(oldpattern,newpattern)
        f = open(configfile,’w’)
        f.write(newdata)
        f.close()
        subprocess.call(‘net stop “nxlog”‘)
        logger.info(“NXlog service Stopped”)
        subprocess.call(‘net start “nxlog”‘)
        logger.info(“NXlog service Started”)
        logger.info(“IP configured for the NXlog client”)
        return True

checkconf() function verifies if the server is already configured and returns either True or False if a match is found or not.
set() function checks the returned value of checkconf() and then either skips the configuration if a match is found or adds the IP/hostname and then restarts the NXlog service.
That’s about all for this article, if you have any misunderstandings don’t hesitate to post a comment in my dedicated section.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s