Add syslog notifier

This commit is contained in:
Julien Riou 2018-06-24 16:10:26 +02:00
parent 23a54e1ceb
commit 85207e0a77
No known key found for this signature in database
GPG key ID: BA3E15176E45E85D
5 changed files with 97 additions and 0 deletions

View file

@ -28,6 +28,8 @@ type Config struct {
ActiveTimeout float64 `yaml:"active-timeout"`
LogFile string `yaml:"log-file"`
PidFile string `yaml:"pid-file"`
SyslogIdent string `yaml:"syslog-ident"`
SyslogFacility string `yaml:"syslog-facility"`
}
func init() {

View file

@ -10,6 +10,7 @@ import (
"log"
"os"
"os/signal"
"regexp"
"strconv"
"sync"
"syscall"
@ -36,6 +37,8 @@ func main() {
flag.Float64Var(&config.ActiveTimeout, "active-timeout", 0, "Time for active connections to be terminated in seconds")
flag.StringVar(&config.LogFile, "log-file", "", "Write logs to a file")
flag.StringVar(&config.PidFile, "pid-file", "", "Write process id into a file")
flag.StringVar(&config.SyslogIdent, "syslog-ident", "pgterminate", "Define syslog tag")
flag.StringVar(&config.SyslogFacility, "syslog-facility", "", "Define syslog facility from LOCAL0 to LOCAL7")
flag.Parse()
if *version {
@ -63,6 +66,14 @@ func main() {
log.Fatalln("Parameter -active-timeout or -idle-timeout required")
}
if config.SyslogFacility != "" {
matched, err := regexp.MatchString("^LOCAL[0-7]$", config.SyslogFacility)
base.Panic(err)
if !matched {
log.Fatalln("Syslog facility must range from LOCAL0 to LOCAL7")
}
}
if config.PidFile != "" {
writePid(config.PidFile)
defer removePid(config.PidFile)

View file

@ -9,3 +9,5 @@ idle-timeout: 300
active-timeout: 10
log-file: /var/log/pgterminate/pgterminate.log
pid-file: /var/run/pgterminate/pgterminate.pid
#syslog-ident: pgterminate
#syslog-facility: LOCAL0

View file

@ -16,5 +16,8 @@ func NewNotifier(ctx *base.Context) Notifier {
if ctx.Config.LogFile != "" {
return NewFile(ctx.Config.LogFile, ctx.Sessions)
}
if ctx.Config.SyslogFacility != "" {
return NewSyslog(ctx.Config.SyslogFacility, ctx.Config.SyslogIdent, ctx.Sessions)
}
return NewConsole(ctx.Sessions)
}

79
notifier/syslog.go Normal file
View file

@ -0,0 +1,79 @@
package notifier
import (
"fmt"
"github.com/jouir/pgterminate/base"
"log/syslog"
)
// Syslog notifier
type Syslog struct {
sessions chan base.Session
ident string
priority syslog.Priority
writer *syslog.Writer
}
// NewSyslog creates a syslog notifier
func NewSyslog(facility string, ident string, sessions chan base.Session) Notifier {
var priority syslog.Priority
switch facility {
case "LOCAL0":
priority = syslog.LOG_INFO | syslog.LOG_LOCAL0
case "LOCAL1":
priority = syslog.LOG_INFO | syslog.LOG_LOCAL1
case "LOCAL2":
priority = syslog.LOG_INFO | syslog.LOG_LOCAL2
case "LOCAL3":
priority = syslog.LOG_INFO | syslog.LOG_LOCAL3
case "LOCAL4":
priority = syslog.LOG_INFO | syslog.LOG_LOCAL4
case "LOCAL5":
priority = syslog.LOG_INFO | syslog.LOG_LOCAL5
case "LOCAL6":
priority = syslog.LOG_INFO | syslog.LOG_LOCAL6
default: // LOCAL7
priority = syslog.LOG_INFO | syslog.LOG_LOCAL7
}
return &Syslog{
sessions: sessions,
ident: ident,
priority: priority,
}
}
// Run starts syslog notifier
func (s *Syslog) Run() {
var err error
if s.writer, err = syslog.New(s.priority, s.ident); err != nil {
base.Panic(err)
}
for session := range s.sessions {
s.writer.Info(fmt.Sprintf("%s", session))
}
}
// Reload disconnect from syslog daemon and re-connect
// Executed when receiving SIGHUP signal
func (s *Syslog) Reload() {
if s.writer != nil {
s.disconnect()
s.connect()
}
}
// connect to syslog daemon
func (s *Syslog) connect() {
var err error
if s.writer, err = syslog.New(s.priority, s.ident); err != nil {
base.Panic(err)
}
}
// disconnect from syslog daemon
func (s *Syslog) disconnect() {
var err error
if err = s.writer.Close(); err != nil {
base.Panic(err)
}
}