syslog management

This commit is contained in:
Paolo Asperti 2021-10-01 18:46:07 +02:00
parent d32cdb11f2
commit d36e249ce1
Signed by: paspo
GPG Key ID: 06D46905D19D5182
4 changed files with 155 additions and 41 deletions

View File

@ -1,29 +1,66 @@
package syslog
import (
"fmt"
"log"
"time"
"git.openpdu.org/OpenPDU/openpdu/events"
"github.com/RackSec/srslog"
"github.com/spf13/viper"
)
var logger *srslog.Writer
var Connected bool
var defaults = map[string]interface{}{
"Syslog.Host": "localhost",
"Syslog.Port": 514,
"Syslog.Protocol": "udp",
}
func init() {
var err error
for k, v := range defaults {
viper.SetDefault(k, v)
}
Connected = false
events.AddListener("config_changed", Reconfigure)
go Connect()
}
logger, err = srslog.Dial("", "", srslog.LOG_ERR, "openpdu")
// w, err := syslog.Dial("udp", "192.168.0.50:514", syslog.LOG_ERR, "openpdu")
if err != nil {
log.Fatal("failed to connect to syslog:", err)
func Reconfigure() {
if logger != nil {
logger.Close()
}
Notice("Syslog disconnected")
events.FireEvent("syslog_disconnected")
Connected = false
go Connect()
}
func Connect() {
var err error
hostname := viper.GetString("Syslog.Host")
port := viper.GetInt("Syslog.Port")
protocol := viper.GetString("Syslog.Protocol")
conn := fmt.Sprintf("%s:%d", hostname, port)
logger, err = srslog.Dial(protocol, conn, srslog.LOG_WARNING|srslog.LOG_DAEMON, "openpdu")
if err != nil {
Err(fmt.Sprintf("failed to connect to syslog: %s", err.Error()))
time.Sleep(1 * time.Second)
go Connect()
}
Connected = true
}
func Alert(msg string) {
if logger == nil {
log.Printf(msg)
log.Print(msg)
} else {
logger.Alert(msg)
}
@ -31,7 +68,7 @@ func Alert(msg string) {
func Crit(msg string) {
if logger == nil {
log.Printf(msg)
log.Print(msg)
} else {
logger.Crit(msg)
}
@ -39,7 +76,7 @@ func Crit(msg string) {
func Err(msg string) {
if logger == nil {
log.Printf(msg)
log.Print(msg)
} else {
logger.Err(msg)
}
@ -47,7 +84,7 @@ func Err(msg string) {
func Warning(msg string) {
if logger == nil {
log.Printf(msg)
log.Print(msg)
} else {
logger.Warning(msg)
}
@ -55,7 +92,7 @@ func Warning(msg string) {
func Notice(msg string) {
if logger == nil {
log.Printf(msg)
log.Print(msg)
} else {
logger.Notice(msg)
}
@ -63,7 +100,7 @@ func Notice(msg string) {
func Info(msg string) {
if logger == nil {
log.Printf(msg)
log.Print(msg)
} else {
logger.Info(msg)
}
@ -71,7 +108,8 @@ func Info(msg string) {
func Debug(msg string) {
if logger == nil {
log.Printf(msg)
log.Print(msg)
} else {
logger.Debug(msg)
}
logger.Debug(msg)
}

View File

@ -1,7 +1,39 @@
package webui
import "gopkg.in/macaron.v1"
import (
"strings"
"git.openpdu.org/OpenPDU/openpdu/syslog"
"github.com/spf13/viper"
"gopkg.in/macaron.v1"
)
func syslogPage(ctx *macaron.Context) {
ctx.Data["host"] = viper.GetString("Syslog.Host")
ctx.Data["port"] = viper.GetInt("Syslog.Port")
ctx.Data["protocol"] = viper.GetString("Syslog.Protocol")
if syslog.Connected {
ctx.Data["status"] = "Connected"
} else {
ctx.Data["status"] = "Disconnected"
}
ctx.HTML(200, "syslog")
}
type SyslogPostForm struct {
Host string `form:"host" binding:"Required"`
Port int `form:"port" binding:"Required"`
Protocol string `form:"protocol" binding:"Required"`
}
func syslogPost(ctx *macaron.Context, f SyslogPostForm) {
// TODO: check protocol, it should be 'udp' or 'tcp'
viper.Set("Syslog.Host", strings.ToLower(strings.TrimSpace(f.Host)))
viper.Set("Syslog.Port", f.Port)
viper.Set("Syslog.Protocol", strings.TrimSpace(f.Protocol))
viper.WriteConfig()
syslogPage(ctx)
}

View File

@ -32,6 +32,7 @@ func StartServer() {
m.Get("/ups", upsPage)
m.Post("/ups", binding.Bind(UPSPostForm{}), upsPost)
m.Get("/syslog", syslogPage)
m.Post("/syslog", binding.Bind(SyslogPostForm{}), syslogPost)
m.Get("/backup", backupPage)
m.Post("/backup", backupDownload)
m.Get("/json/status", jsonStatus)

View File

@ -2,47 +2,90 @@
<html>
<head>
{% include "common/common-head.html" %}
{% include "common/common-head.html" %}
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<div class="wrapper">
{% include "common/page-header.html" %} {% with pageselected="syslog" %} {% include "common/sidebar-menu.html" %} {% endwith %}
{% include "common/page-header.html" %} {% with pageselected="syslog" %} {% include "common/sidebar-menu.html" %}
{% endwith %}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Main content -->
<section class="content">
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="row">
<div class="col-xs-12">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Syslog configuration</h3>
</div>
<!-- /.box-header -->
</div>
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Syslog configuration</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form class="form-horizontal" method="post">
<div class="box-body">
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="status" disabled="" value="{{ status }}">
</div>
<!-- /.col -->
</div>
<div class="form-group">
<label for="host" class="col-sm-2 control-label">Host</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="host" name="host" placeholder="Hostname or IP address"
value="{{ host }}">
</div>
</div>
<div class="form-group">
<label for="port" class="col-sm-2 control-label">Port</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="port" name="port"
placeholder="syslog port, default 514" value="{{ port }}">
</div>
</div>
<div class="form-group">
<label for="protocol" class="col-sm-2 control-label">Protocol</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="protocol" name="protocol" placeholder="udp or tcp"
value="{{ protocol }}">
</div>
</div>
</div>
<!-- /.row -->
</section>
<!-- /.content -->
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-info pull-right">Save</button>
</div>
<!-- /.box-footer -->
</form>
</div>
</div>
<!-- /.col -->
</div>
<!-- /.content-wrapper -->
{% include "common/footer.html" %}
<!-- /.row -->
</section>
<!-- /.content -->
</div>
<!-- ./wrapper -->
<!-- /.content-wrapper -->
{% include "common/common-js.html" %}
{% include "common/footer.html" %}
</div>
<!-- ./wrapper -->
{% include "common/common-js.html" %}
</body>