Skip to content

Commit

Permalink
Merge pull request #13 from silkeh/systemd-build-tag
Browse files Browse the repository at this point in the history
Add nosystemd build tag
  • Loading branch information
BartVerc authored May 4, 2018
2 parents b572eee + 0c405fa commit 2f94d55
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
21 changes: 21 additions & 0 deletions nosystemd.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 6 additions & 31 deletions postfix_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()
}

Expand Down
39 changes: 39 additions & 0 deletions systemd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// +build !nosystemd

package main

import (
"flag"
"fmt"
"log"
"sync"
"time"

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit 2f94d55

Please sign in to comment.