Add nosystemd build tag

This allows building the exporter without systemd headers, eg:

    env CGO_ENABLED=0 go build -tags nosystemd

Closes #11
This commit is contained in:
Silke 2018-04-15 15:03:36 +02:00
parent b572eeeb93
commit 0c405faf87
3 changed files with 66 additions and 31 deletions

21
nosystemd.go Normal file
View File

@ -0,0 +1,21 @@
// +build nosystemd
// This file contains stubs to support non-systemd use
package main
import "io"
type Journal struct {
io.Closer
Path string
}
func systemdFlags(enable *bool, unit, slice, path *string) {}
func NewJournal(unit, slice, path string) (*Journal, error) {
return nil, nil
}
func (e *PostfixExporter) CollectLogfileFromJournal() error {
return nil
}

View File

@ -404,29 +404,6 @@ func (e *PostfixExporter) CollectLogfileFromFile(path string) error {
return fd.Truncate(0) return fd.Truncate(0)
} }
// CollectLogfileFromJournal Collects entries from the systemd journal.
func (e *PostfixExporter) CollectLogfileFromJournal() error {
e.journal.Lock()
defer e.journal.Unlock()
r := e.journal.Wait(time.Duration(1) * time.Second)
if r < 0 {
log.Print("error while waiting for journal!")
}
for {
m, c, err := e.journal.NextMessage()
if err != nil {
return err
}
if c == 0 {
break
}
e.CollectFromLogline(m)
}
return nil
}
// NewPostfixExporter creates a new Postfix exporter instance. // NewPostfixExporter creates a new Postfix exporter instance.
func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal) (*PostfixExporter, error) { func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal) (*PostfixExporter, error) {
return &PostfixExporter{ return &PostfixExporter{
@ -641,22 +618,20 @@ func main() {
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
postfixShowqPath = flag.String("postfix.showq_path", "/var/spool/postfix/public/showq", "Path at which Postfix places its showq socket.") postfixShowqPath = flag.String("postfix.showq_path", "/var/spool/postfix/public/showq", "Path at which Postfix places its showq socket.")
postfixLogfilePath = flag.String("postfix.logfile_path", "/var/log/postfix_exporter_input.log", "Path where Postfix writes log entries. This file will be truncated by this exporter.") postfixLogfilePath = flag.String("postfix.logfile_path", "/var/log/postfix_exporter_input.log", "Path where Postfix writes log entries. This file will be truncated by this exporter.")
systemdEnable = flag.Bool("systemd.enable", false, "Read from the systemd journal instead of log")
systemdUnit = flag.String("systemd.unit", "postfix.service", "Name of the Postfix systemd unit.") systemdEnable bool
systemdSlice = flag.String("systemd.slice", "", "Name of the Postfix systemd slice. Overrides the systemd unit.") systemdUnit, systemdSlice, systemdJournalPath string
systemdJournalPath = flag.String("systemd.journal_path", "", "Path to the systemd journal")
) )
systemdFlags(&systemdEnable, &systemdUnit, &systemdSlice, &systemdJournalPath)
flag.Parse() flag.Parse()
var journal *Journal var journal *Journal
if *systemdEnable { if systemdEnable {
var err error var err error
journal, err = NewJournal(*systemdUnit, *systemdSlice, *systemdJournalPath) journal, err = NewJournal(systemdUnit, systemdSlice, systemdJournalPath)
if err != nil { if err != nil {
log.Fatalf("Error opening systemd journal: %s", err) log.Fatalf("Error opening systemd journal: %s", err)
} }
// Start at end of journal
journal.SeekRealtimeUsec(uint64(time.Now().UnixNano() / 1000))
defer journal.Close() defer journal.Close()
} }

View File

@ -1,7 +1,11 @@
// +build !nosystemd
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log"
"sync" "sync"
"time" "time"
@ -40,6 +44,10 @@ func NewJournal(unit, slice, path string) (j *Journal, err error) {
return return
} }
} }
// Start at end of journal
j.SeekRealtimeUsec(uint64(time.Now().UnixNano() / 1000))
return return
} }
@ -76,3 +84,34 @@ func (j *Journal) NextMessage() (s string, c uint64, err error) {
return return
} }
// systemdFlags sets the flags for use with systemd
func systemdFlags(enable *bool, unit, slice, path *string) {
flag.BoolVar(enable, "systemd.enable", false, "Read from the systemd journal instead of log")
flag.StringVar(unit, "systemd.unit", "postfix.service", "Name of the Postfix systemd unit.")
flag.StringVar(slice, "systemd.slice", "", "Name of the Postfix systemd slice. Overrides the systemd unit.")
flag.StringVar(path, "systemd.journal_path", "", "Path to the systemd journal")
}
// CollectLogfileFromJournal Collects entries from the systemd journal.
func (e *PostfixExporter) CollectLogfileFromJournal() error {
e.journal.Lock()
defer e.journal.Unlock()
r := e.journal.Wait(time.Duration(1) * time.Second)
if r < 0 {
log.Print("error while waiting for journal!")
}
for {
m, c, err := e.journal.NextMessage()
if err != nil {
return err
}
if c == 0 {
break
}
e.CollectFromLogline(m)
}
return nil
}