syslog format

This commit is contained in:
Paolo Asperti 2022-06-03 10:47:03 +02:00
parent 9aa5e864c7
commit 1098116513
Signed by: paspo
GPG Key ID: 06D46905D19D5182
3 changed files with 21 additions and 0 deletions

View File

@ -17,6 +17,7 @@ var defaults = map[string]interface{}{
"Syslog.Host": "localhost", "Syslog.Host": "localhost",
"Syslog.Port": 514, "Syslog.Port": 514,
"Syslog.Protocol": "udp", "Syslog.Protocol": "udp",
"Syslog.Format": "RFC5424",
} }
func init() { func init() {
@ -46,6 +47,7 @@ func Connect() {
hostname := viper.GetString("Syslog.Host") hostname := viper.GetString("Syslog.Host")
port := viper.GetInt("Syslog.Port") port := viper.GetInt("Syslog.Port")
protocol := viper.GetString("Syslog.Protocol") protocol := viper.GetString("Syslog.Protocol")
format := viper.GetString("Syslog.Format")
conn := fmt.Sprintf("%s:%d", hostname, port) conn := fmt.Sprintf("%s:%d", hostname, port)
logger, err = srslog.Dial(protocol, conn, srslog.LOG_WARNING|srslog.LOG_DAEMON, "openpdu") logger, err = srslog.Dial(protocol, conn, srslog.LOG_WARNING|srslog.LOG_DAEMON, "openpdu")
@ -55,6 +57,13 @@ func Connect() {
go Connect() go Connect()
} }
switch format {
case "RFC3164":
logger.SetFormatter(srslog.RFC3164Formatter)
default: // RFC5424
logger.SetFormatter(srslog.RFC5424Formatter)
}
Connected = true Connected = true
} }

View File

@ -12,6 +12,7 @@ func syslogPage(ctx *macaron.Context) {
ctx.Data["host"] = viper.GetString("Syslog.Host") ctx.Data["host"] = viper.GetString("Syslog.Host")
ctx.Data["port"] = viper.GetInt("Syslog.Port") ctx.Data["port"] = viper.GetInt("Syslog.Port")
ctx.Data["protocol"] = viper.GetString("Syslog.Protocol") ctx.Data["protocol"] = viper.GetString("Syslog.Protocol")
ctx.Data["format"] = viper.GetString("Syslog.Format")
if syslog.Connected { if syslog.Connected {
ctx.Data["status"] = "Connected" ctx.Data["status"] = "Connected"
@ -26,6 +27,7 @@ type SyslogPostForm struct {
Host string `form:"host" binding:"Required"` Host string `form:"host" binding:"Required"`
Port int `form:"port" binding:"Required"` Port int `form:"port" binding:"Required"`
Protocol string `form:"protocol" binding:"Required"` Protocol string `form:"protocol" binding:"Required"`
Format string `form:"format" binding:"Required"`
} }
func syslogPost(ctx *macaron.Context, f SyslogPostForm) { func syslogPost(ctx *macaron.Context, f SyslogPostForm) {
@ -33,6 +35,8 @@ func syslogPost(ctx *macaron.Context, f SyslogPostForm) {
viper.Set("Syslog.Host", strings.ToLower(strings.TrimSpace(f.Host))) viper.Set("Syslog.Host", strings.ToLower(strings.TrimSpace(f.Host)))
viper.Set("Syslog.Port", f.Port) viper.Set("Syslog.Port", f.Port)
viper.Set("Syslog.Protocol", strings.TrimSpace(f.Protocol)) viper.Set("Syslog.Protocol", strings.TrimSpace(f.Protocol))
// TODO: check format, it should be 'RFC5424' or 'RFC3164'
viper.Set("Syslog.Format", strings.TrimSpace(f.Format))
viper.WriteConfig() viper.WriteConfig()
syslogPage(ctx) syslogPage(ctx)

View File

@ -60,6 +60,14 @@
</div> </div>
</div> </div>
<div class="form-group">
<label for="format" class="col-sm-2 control-label">Format</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="format" name="format" placeholder="RFC5424 or RFC3164"
value="{{ format }}">
</div>
</div>
</div> </div>
<!-- /.box-body --> <!-- /.box-body -->