github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/utils/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  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  )
    13  
    14  var expectedAccumulateSyslogConfTemplate = `
    15  $ModLoad imuxsock
    16  $ModLoad imfile
    17  
    18  # Messages received from remote rsyslog machines have messages prefixed with a space,
    19  # so add one in for local messages too if needed.
    20  $template JujuLogFormat{{.Namespace}},"%syslogtag:{{.Offset}}:$%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
    21  
    22  $template LongTagForwardFormat,"<%PRI%>%TIMESTAMP:::date-rfc3339% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg%"
    23  
    24  
    25  # start: Forwarding rule for foo
    26  $ActionQueueType LinkedList
    27  $ActionQueueFileName {{.MachineTag}}{{.Namespace}}_0
    28  $ActionResumeRetryCount -1
    29  $ActionQueueSaveOnShutdown on
    30  $DefaultNetstreamDriver gtls
    31  $DefaultNetstreamDriverCAFile /var/log/juju{{.Namespace}}/ca-cert.pem
    32  $ActionSendStreamDriverAuthMode anon
    33  $ActionSendStreamDriverMode 1 # run driver in TLS-only mode
    34  
    35  :syslogtag, startswith, "juju{{.Namespace}}-" @@foo:{{.Port}};LongTagForwardFormat
    36  # end: Forwarding rule for foo
    37  
    38  :syslogtag, startswith, "juju{{.Namespace}}-" stop
    39  
    40  $FileCreateMode 0600
    41  
    42  # Maximum size for the log on this outchannel is 512MB
    43  # The command to execute when an outchannel as reached its size limit cannot accept any arguments
    44  # that is why we have created the helper script for executing logrotate.
    45  $outchannel logRotation,/var/log/juju{{.Namespace}}/all-machines.log,536870912,/var/log/juju{{.Namespace}}/logrotate.run
    46  
    47  $RuleSet remote
    48  $FileCreateMode 0600
    49  :syslogtag, startswith, "juju{{.Namespace}}-" :omfile:$logRotation;JujuLogFormat{{.Namespace}}
    50  :syslogtag, startswith, "juju{{.Namespace}}-" stop
    51  $FileCreateMode 0600
    52  
    53  $InputFilePersistStateInterval 50
    54  $InputFilePollInterval 5
    55  $InputFileName /var/log/juju{{.Namespace}}/{{.MachineTag}}.log
    56  $InputFileTag juju{{.Namespace}}-{{.MachineTag}}:
    57  $InputFileStateFile {{.MachineTag}}{{.Namespace}}
    58  $InputRunFileMonitor
    59  
    60  $ModLoad imtcp
    61  $DefaultNetstreamDriver gtls
    62  $DefaultNetstreamDriverCAFile /var/log/juju{{.Namespace}}/ca-cert.pem
    63  $DefaultNetstreamDriverCertFile /var/log/juju{{.Namespace}}/rsyslog-cert.pem
    64  $DefaultNetstreamDriverKeyFile /var/log/juju{{.Namespace}}/rsyslog-key.pem
    65  $InputTCPServerStreamDriverAuthMode anon
    66  $InputTCPServerStreamDriverMode 1 # run driver in TLS-only mode
    67  $InputTCPMaxSessions 10000 # default is 200, all agents connect to all rsyslog daemons
    68  
    69  $InputTCPServerBindRuleset remote
    70  $InputTCPServerRun {{.Port}}
    71  
    72  # switch back to default ruleset for further rules
    73  $RuleSet RSYSLOG_DefaultRuleset
    74  `
    75  
    76  type templateArgs struct {
    77  	MachineTag  string
    78  	LogDir      string
    79  	Namespace   string
    80  	BootstrapIP string
    81  	Port        int
    82  	Offset      int
    83  }
    84  
    85  // ExpectedAccumulateSyslogConf returns the expected content for a rsyslog file on a state server.
    86  func ExpectedAccumulateSyslogConf(c *gc.C, machineTag, namespace string, port int) string {
    87  	if namespace != "" {
    88  		namespace = "-" + namespace
    89  	}
    90  	t := template.Must(template.New("").Parse(expectedAccumulateSyslogConfTemplate))
    91  	var conf bytes.Buffer
    92  	err := t.Execute(&conf, templateArgs{
    93  		MachineTag: machineTag,
    94  		Namespace:  namespace,
    95  		Offset:     len("juju-") + len(namespace) + 1,
    96  		Port:       port,
    97  	})
    98  	c.Assert(err, jc.ErrorIsNil)
    99  	return conf.String()
   100  }
   101  
   102  var expectedForwardSyslogConfTemplate = `
   103  $ModLoad imuxsock
   104  $ModLoad imfile
   105  
   106  $InputFilePersistStateInterval 50
   107  $InputFilePollInterval 5
   108  $InputFileName {{.LogDir}}/{{.MachineTag}}.log
   109  $InputFileTag juju{{.Namespace}}-{{.MachineTag}}:
   110  $InputFileStateFile {{.MachineTag}}{{.Namespace}}
   111  $InputRunFileMonitor
   112  
   113  # start: Forwarding rule for server
   114  $ActionQueueType LinkedList
   115  $ActionQueueFileName {{.MachineTag}}{{.Namespace}}_0
   116  $ActionResumeRetryCount -1
   117  $ActionQueueSaveOnShutdown on
   118  $DefaultNetstreamDriver gtls
   119  $DefaultNetstreamDriverCAFile {{.LogDir}}/ca-cert.pem
   120  $ActionSendStreamDriverAuthMode anon
   121  $ActionSendStreamDriverMode 1 # run driver in TLS-only mode
   122  
   123  $template LongTagForwardFormat,"<%PRI%>%TIMESTAMP:::date-rfc3339% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg%"
   124  :syslogtag, startswith, "juju{{.Namespace}}-" @@{{.BootstrapIP}}:{{.Port}};LongTagForwardFormat
   125  # end: Forwarding rule for server
   126  
   127  & ~
   128  `
   129  
   130  // ExpectedForwardSyslogConf returns the expected content for a rsyslog file on a host machine.
   131  func ExpectedForwardSyslogConf(c *gc.C, machineTag, logDir, namespace, bootstrapIP string, port int) string {
   132  	if namespace != "" {
   133  		namespace = "-" + namespace
   134  	}
   135  	t := template.Must(template.New("").Parse(expectedForwardSyslogConfTemplate))
   136  	var conf bytes.Buffer
   137  	err := t.Execute(&conf, templateArgs{
   138  		MachineTag:  machineTag,
   139  		LogDir:      logDir,
   140  		Namespace:   namespace,
   141  		BootstrapIP: bootstrapIP,
   142  		Port:        port,
   143  	})
   144  	c.Assert(err, jc.ErrorIsNil)
   145  	return conf.String()
   146  }