Commit 24b036e5 authored by Sebastian Stark's avatar Sebastian Stark

-notify option to send error reports via email

  - if mainExitCode() is != 0 snaprd will send an email to the notify
  address containg the exit code and the last 25 lines of log
parent 1ab28a50
......@@ -94,6 +94,22 @@ Basic operation:
2016-09-14 Wednesday 20:32:29 (1s, 10m0s)
```
E-Mail Notification
-------------------
If you add the `-notify` option to the run sub-command you will get an email
in case of a problem. Use it like this:
```
> snaprd run -notify root <other options...>
```
If snaprd has a severe problem it will stop execution and send an email to the
specified address, along with the last few lines of log output.
Sending happens through use of the standard mail(1) command, make sure your
system is configured accordingly.
Stopping
--------
......
......@@ -53,6 +53,7 @@ type Config struct {
SchedFile string
MinPercSpace float64
MinGiBSpace int
Notify string
}
// WriteCache writes the global configuration to disk as a json file.
......@@ -167,6 +168,9 @@ func loadConfig() (*Config, error) {
flags.IntVar(&(config.MinGiBSpace),
"minGbSpace", 0,
"if set, keep at least x GiB of the snapshots filesystem free")
flags.StringVar(&(config.Notify),
"notify", "",
"specify an email address to send reports")
if err := flags.Parse(os.Args[2:]); err != nil {
return nil, err
......
package main
import (
"io/ioutil"
"log"
"os/exec"
)
func NotifyMail(to, msg string) {
SendMail(to, "snaprd notice", msg)
}
func SendMail(to, subject, msg string) {
sendmail := exec.Command("mail", "-s", subject, to)
stdin, err := sendmail.StdinPipe()
if err != nil {
log.Println(err)
return
}
stdout, err := sendmail.StdoutPipe()
if err != nil {
log.Println(err)
return
}
sendmail.Start()
stdin.Write([]byte(msg))
stdin.Write([]byte("\n"))
stdin.Close()
ioutil.ReadAll(stdout)
sendmail.Wait()
log.Printf("sending notification to %s done\n", to)
}
......@@ -9,6 +9,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
......@@ -251,10 +252,9 @@ func subcmdList(cl clock) {
}
}
func mainExitCode() int {
rio := newRingIO(os.Stderr, 5, 100)
logger = log.New(rio, "", log.Ldate|log.Ltime|log.Lshortfile)
log.SetOutput(rio)
func mainExitCode(logIO io.Writer) int {
logger = log.New(logIO, "", log.Ldate|log.Ltime|log.Lshortfile)
log.SetOutput(logIO)
var err error
if config, err = loadConfig(); err != nil || config == nil {
if err == flag.ErrHelp {
......@@ -286,5 +286,12 @@ func mainExitCode() int {
}
func main() {
os.Exit(mainExitCode())
rio := newRingIO(os.Stderr, 25, 100)
exitCode := mainExitCode(rio)
if exitCode != 0 && config.Notify != "" {
mail := fmt.Sprintf("snaprd exited with return value %d.\nLatest log output:\n\n%s",
exitCode, rio.GetAsText())
NotifyMail(config.Notify, mail)
}
os.Exit(exitCode)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment