Merge pull request #13 from silkeh/systemd-build-tag

Add nosystemd build tag
This commit is contained in:
Bart Vercoulen 2018-05-04 12:00:00 +02:00 committed by GitHub
commit 2f94d55e93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
}
// 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.
func NewPostfixExporter(showqPath string, logfilePath string, journal *Journal) (*PostfixExporter, error) {
return &PostfixExporter{
@ -641,22 +618,20 @@ func main() {
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.")
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.")
systemdSlice = flag.String("systemd.slice", "", "Name of the Postfix systemd slice. Overrides the systemd unit.")
systemdJournalPath = flag.String("systemd.journal_path", "", "Path to the systemd journal")
systemdEnable bool
systemdUnit, systemdSlice, systemdJournalPath string
)
systemdFlags(&systemdEnable, &systemdUnit, &systemdSlice, &systemdJournalPath)
flag.Parse()
var journal *Journal
if *systemdEnable {
if systemdEnable {
var err error
journal, err = NewJournal(*systemdUnit, *systemdSlice, *systemdJournalPath)
journal, err = NewJournal(systemdUnit, systemdSlice, systemdJournalPath)
if err != nil {
log.Fatalf("Error opening systemd journal: %s", err)
}
// Start at end of journal
journal.SeekRealtimeUsec(uint64(time.Now().UnixNano() / 1000))
defer journal.Close()
}

View File

@ -1,7 +1,11 @@
// +build !nosystemd
package main
import (
"flag"
"fmt"
"log"
"sync"
"time"
@ -40,6 +44,10 @@ func NewJournal(unit, slice, path string) (j *Journal, err error) {
return
}
}
// Start at end of journal
j.SeekRealtimeUsec(uint64(time.Now().UnixNano() / 1000))
return
}
@ -76,3 +84,34 @@ func (j *Journal) NextMessage() (s string, c uint64, err error) {
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
}