def generate_localsyslog_configs()

in AzureMonitorAgent/agent.py [0:0]


def generate_localsyslog_configs(uses_gcs = False, uses_mcs = False):
    """
    Install local syslog configuration files if not present and restart syslog
    """

    # don't deploy any configuration if no control plane is configured
    if not uses_gcs and not uses_mcs:
        return
    
    public_settings, _ = get_settings()
    syslog_port = ''
    if os.path.isfile(AMASyslogPortFilePath):
        f = open(AMASyslogPortFilePath, "r")
        syslog_port = f.read()
        f.close()
        
    useSyslogTcp = False
    
    # always use syslog tcp port, unless 
    # - the distro is Red Hat based and doesn't have semanage
    #   these distros seem to have SELinux on by default and we shouldn't be installing semanage ourselves
    if not os.path.exists('/etc/selinux/config'):
        useSyslogTcp = True
    else:        
        sedisabled, _ = run_command_and_log('getenforce | grep -i "Disabled"',log_cmd=False, log_output=False)
        if sedisabled == 0:
            useSyslogTcp = True
        else:            
            check_semanage, _ = run_command_and_log("which semanage",log_cmd=False, log_output=False)
            if check_semanage == 0 and syslog_port != '':
                syslogPortEnabled, _ = run_command_and_log('grep -Rnw /var/lib/selinux -e syslogd_port_t | grep ' + syslog_port,log_cmd=False, log_output=False)
                if syslogPortEnabled != 0:                    
                    # also check SELinux config paths for Oracle/RH
                    syslogPortEnabled, _ = run_command_and_log('grep -Rnw /etc/selinux -e syslogd_port_t | grep ' + syslog_port,log_cmd=False, log_output=False)
                    if syslogPortEnabled != 0:                    
                        # allow the syslog port in SELinux
                        run_command_and_log('semanage port -a -t syslogd_port_t -p tcp ' + syslog_port,log_cmd=False, log_output=False)
                useSyslogTcp = True   
        
    # 1P tenants use omuxsock, so keep using that for customers using 1P
    if useSyslogTcp == True and syslog_port != '':
        if os.path.exists('/etc/rsyslog.d/'):            
            restartRequired = False
            if uses_gcs and not os.path.exists('/etc/rsyslog.d/05-azuremonitoragent-loadomuxsock.conf'):
                copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/rsyslogconf/05-azuremonitoragent-loadomuxsock.conf","/etc/rsyslog.d/05-azuremonitoragent-loadomuxsock.conf")
                restartRequired = True
            
            if not os.path.exists('/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf'):
                if os.path.exists('/etc/rsyslog.d/05-azuremonitoragent-loadomuxsock.conf'):
                    os.remove("/etc/rsyslog.d/05-azuremonitoragent-loadomuxsock.conf")
                if os.path.exists('/etc/rsyslog.d/10-azuremonitoragent.conf'):
                    os.remove("/etc/rsyslog.d/10-azuremonitoragent.conf")
                copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/rsyslogconf/10-azuremonitoragent-omfwd.conf","/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf")
                os.chmod('/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf', stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH)
                restartRequired = True                
            
            portSetting = 'Port="' + syslog_port + '"'
            defaultPortSetting = 'Port="28330"'
            portUpdated = False
            with open('/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf') as f:
                if portSetting not in f.read():
                    portUpdated = True

            if portUpdated == True:
                copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/rsyslogconf/10-azuremonitoragent-omfwd.conf","/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf")
                with contextlib.closing(fileinput.FileInput('/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf', inplace=True, backup='.bak')) as file:
                    for line in file:
                        print(line.replace(defaultPortSetting, portSetting), end='')
                os.chmod('/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf', stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH)
                restartRequired = True
            
            if restartRequired == True:
                run_command_and_log(get_service_command("rsyslog", "restart"))
                hutil_log_info("Installed local syslog configuration files and restarted syslog")

        if os.path.exists('/etc/syslog-ng/syslog-ng.conf'):
            restartRequired = False
            if not os.path.exists('/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf'):
                if os.path.exists('/etc/syslog-ng/conf.d/azuremonitoragent.conf'):
                    os.remove("/etc/syslog-ng/conf.d/azuremonitoragent.conf")
                syslog_ng_confpath = os.path.join('/etc/syslog-ng/', 'conf.d')
                if not os.path.exists(syslog_ng_confpath):
                    os.makedirs(syslog_ng_confpath)
                copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/syslog-ngconf/azuremonitoragent-tcp.conf","/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf")
                os.chmod('/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf', stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH)
                restartRequired = True

            portSetting = "port(" + syslog_port + ")"
            defaultPortSetting = "port(28330)"
            portUpdated = False
            with open('/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf') as f:
                if portSetting not in f.read():
                    portUpdated = True

            if portUpdated == True:
                copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/syslog-ngconf/azuremonitoragent-tcp.conf","/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf")
                with contextlib.closing(fileinput.FileInput('/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf', inplace=True, backup='.bak')) as file:
                    for line in file:
                        print(line.replace(defaultPortSetting, portSetting), end='')
                os.chmod('/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf', stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH)
                restartRequired = True
            
            if restartRequired == True:
                run_command_and_log(get_service_command("syslog-ng", "restart"))
                hutil_log_info("Installed local syslog configuration files and restarted syslog")    
    else:
        if os.path.exists('/etc/rsyslog.d/') and not os.path.exists('/etc/rsyslog.d/10-azuremonitoragent.conf'):
            if os.path.exists('/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf'):
                os.remove("/etc/rsyslog.d/10-azuremonitoragent-omfwd.conf")
            copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/rsyslogconf/05-azuremonitoragent-loadomuxsock.conf","/etc/rsyslog.d/05-azuremonitoragent-loadomuxsock.conf")
            copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/rsyslogconf/10-azuremonitoragent.conf","/etc/rsyslog.d/10-azuremonitoragent.conf")
            os.chmod('/etc/rsyslog.d/05-azuremonitoragent-loadomuxsock.conf', stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH)
            os.chmod('/etc/rsyslog.d/10-azuremonitoragent.conf', stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH)
            run_command_and_log(get_service_command("rsyslog", "restart"))
            hutil_log_info("Installed local syslog configuration files and restarted syslog")

        if os.path.exists('/etc/syslog-ng/syslog-ng.conf') and not os.path.exists('/etc/syslog-ng/conf.d/azuremonitoragent.conf'):
            if os.path.exists('/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf'):
                os.remove("/etc/syslog-ng/conf.d/azuremonitoragent-tcp.conf")
            syslog_ng_confpath = os.path.join('/etc/syslog-ng/', 'conf.d')
            if not os.path.exists(syslog_ng_confpath):
                os.makedirs(syslog_ng_confpath)
            copyfile("/etc/opt/microsoft/azuremonitoragent/syslog/syslog-ngconf/azuremonitoragent.conf","/etc/syslog-ng/conf.d/azuremonitoragent.conf")
            os.chmod('/etc/syslog-ng/conf.d/azuremonitoragent.conf', stat.S_IRGRP | stat.S_IRUSR | stat.S_IWUSR | stat.S_IROTH)
            run_command_and_log(get_service_command("syslog-ng", "restart"))
            hutil_log_info("Installed local syslog configuration files and restarted syslog")