github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/log/syslog/testing/syslogconf.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"bytes"
     8  	"text/template"
     9  
    10  	gc "launchpad.net/gocheck"
    11  )
    12  
    13  var expectedAccumulateSyslogConfTemplate = `
    14  $ModLoad imfile
    15  
    16  $InputFilePersistStateInterval 50
    17  $InputFilePollInterval 5
    18  $InputFileName /var/log/juju{{.Namespace}}/{{.MachineTag}}.log
    19  $InputFileTag juju{{.Namespace}}-{{.MachineTag}}:
    20  $InputFileStateFile {{.MachineTag}}{{.Namespace}}
    21  $InputRunFileMonitor
    22  
    23  $ModLoad imtcp
    24  $DefaultNetstreamDriver gtls
    25  $DefaultNetstreamDriverCAFile /var/log/juju{{.Namespace}}/ca-cert.pem
    26  $DefaultNetstreamDriverCertFile /var/log/juju{{.Namespace}}/rsyslog-cert.pem
    27  $DefaultNetstreamDriverKeyFile /var/log/juju{{.Namespace}}/rsyslog-key.pem
    28  $InputTCPServerStreamDriverAuthMode anon
    29  $InputTCPServerStreamDriverMode 1 # run driver in TLS-only mode
    30  $InputTCPServerRun {{.Port}}
    31  
    32  # Messages received from remote rsyslog machines have messages prefixed with a space,
    33  # so add one in for local messages too if needed.
    34  $template JujuLogFormat{{.Namespace}},"%syslogtag:{{.Offset}}:$%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
    35  
    36  $FileCreateMode 0644
    37  :syslogtag, startswith, "juju{{.Namespace}}-" /var/log/juju{{.Namespace}}/all-machines.log;JujuLogFormat{{.Namespace}}
    38  & ~
    39  $FileCreateMode 0640
    40  `
    41  
    42  type templateArgs struct {
    43  	MachineTag  string
    44  	LogDir      string
    45  	Namespace   string
    46  	BootstrapIP string
    47  	Port        int
    48  	Offset      int
    49  }
    50  
    51  // ExpectedAccumulateSyslogConf returns the expected content for a rsyslog file on a state server.
    52  func ExpectedAccumulateSyslogConf(c *gc.C, machineTag, namespace string, port int) string {
    53  	if namespace != "" {
    54  		namespace = "-" + namespace
    55  	}
    56  	t := template.Must(template.New("").Parse(expectedAccumulateSyslogConfTemplate))
    57  	var conf bytes.Buffer
    58  	err := t.Execute(&conf, templateArgs{
    59  		MachineTag: machineTag,
    60  		Namespace:  namespace,
    61  		Offset:     len("juju-") + len(namespace) + 1,
    62  		Port:       port,
    63  	})
    64  	c.Assert(err, gc.IsNil)
    65  	return conf.String()
    66  }
    67  
    68  var expectedForwardSyslogConfTemplate = `
    69  $ModLoad imfile
    70  
    71  # Enable reliable forwarding.
    72  $ActionQueueType LinkedList
    73  $ActionQueueFileName {{.MachineTag}}{{.Namespace}}
    74  $ActionResumeRetryCount -1
    75  $ActionQueueSaveOnShutdown on
    76  
    77  $InputFilePersistStateInterval 50
    78  $InputFilePollInterval 5
    79  $InputFileName {{.LogDir}}/{{.MachineTag}}.log
    80  $InputFileTag juju{{.Namespace}}-{{.MachineTag}}:
    81  $InputFileStateFile {{.MachineTag}}{{.Namespace}}
    82  $InputRunFileMonitor
    83  
    84  $DefaultNetstreamDriver gtls
    85  $DefaultNetstreamDriverCAFile {{.LogDir}}/ca-cert.pem
    86  $ActionSendStreamDriverAuthMode anon
    87  $ActionSendStreamDriverMode 1 # run driver in TLS-only mode
    88  
    89  $template LongTagForwardFormat,"<%PRI%>%TIMESTAMP:::date-rfc3339% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg%"
    90  
    91  :syslogtag, startswith, "juju{{.Namespace}}-" @@{{.BootstrapIP}}:{{.Port}};LongTagForwardFormat
    92  & ~
    93  `
    94  
    95  // ExpectedForwardSyslogConf returns the expected content for a rsyslog file on a host machine.
    96  func ExpectedForwardSyslogConf(c *gc.C, machineTag, logDir, namespace, bootstrapIP string, port int) string {
    97  	if namespace != "" {
    98  		namespace = "-" + namespace
    99  	}
   100  	t := template.Must(template.New("").Parse(expectedForwardSyslogConfTemplate))
   101  	var conf bytes.Buffer
   102  	err := t.Execute(&conf, templateArgs{
   103  		MachineTag:  machineTag,
   104  		LogDir:      logDir,
   105  		Namespace:   namespace,
   106  		BootstrapIP: bootstrapIP,
   107  		Port:        port,
   108  	})
   109  	c.Assert(err, gc.IsNil)
   110  	return conf.String()
   111  }