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