diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..0cffcb348ff9cec9cd41492a9e5c5a41d86ce96e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json \ No newline at end of file diff --git a/config-sample.json b/config-sample.json new file mode 100644 index 0000000000000000000000000000000000000000..161119d214e47355a46c4bde45f14571f24f9443 --- /dev/null +++ b/config-sample.json @@ -0,0 +1,15 @@ +{ + "debug-dest": "os.Stdout", + "debug": false, + "logs": [ + { + "log_path": "", + "webhook_url": "", + "username": "", + "channel": "", + "color": "", + "icon_url": "", + "search": "" + } + ] +} diff --git a/eth-log-alerting.go b/eth-log-alerting.go index 9fc03db8d25f390c4d18e3ae297207025791b40d..35698ff7d2d98a42f4f502b3f944fd8f4371c9a0 100644 --- a/eth-log-alerting.go +++ b/eth-log-alerting.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "flag" "fmt" "log" @@ -15,16 +16,15 @@ import ( "github.com/hpcloud/tail" ) -type attachment struct { - Fallback string `json:"fallback,omitempty"` - Pretext string `json:"pretext,omitempty"` - Text string `json:"text"` - Color string `json:"color,omitempty"` +type config struct { + DebugDest string + Debug bool + Logs logConfigs } -type attachments []attachment +type logConfigs []logConfig -var ( +type logConfig struct { logPath string webhookURL string username string @@ -32,8 +32,27 @@ var ( color string iconURL string searchRegex string +} + +type attachment struct { + Fallback string `json:"fallback,omitempty"` + Pretext string `json:"pretext,omitempty"` + Text string `json:"text"` + Color string `json:"color,omitempty"` +} - mh *matterhook.Client +type attachments []attachment + +var ( + logPath string // Deprecate + webhookURL string // Deprecate + username string // Deprecate + channel string // Deprecate + color string // Deprecate + iconURL string // Deprecate + searchRegex string // Deprecate + + mh *matterhook.Client // Deprecate logger *log.Logger debugDest string @@ -41,17 +60,31 @@ var ( ) func init() { - flag.StringVar(&logPath, "log-path", "", "Log to monitor") - flag.StringVar(&webhookURL, "webhook", "", "Webhook to forward log entries to") - flag.StringVar(&username, "username", "logbot", "Username to post as") - flag.StringVar(&channel, "channel", "", "Channel to post log entries to") - flag.StringVar(&color, "color", "default", "Color for entry, either named or hex with `#`") - flag.StringVar(&iconURL, "icon-url", "", "URL of icon to use for bot") - flag.StringVar(&searchRegex, "search", "", "Search term or regex to match") - flag.StringVar(&debugDest, "debug-dest", "os.Stdout", "Destination for debug and other messages, omit to log to Stdout") - flag.BoolVar(&debug, "debug", false, "Include additional log data for debugging") + var configPath string + // flag.StringVar(&logPath, "log-path", "", "Log to monitor") + // flag.StringVar(&webhookURL, "webhook", "", "Webhook to forward log entries to") + // flag.StringVar(&username, "username", "logbot", "Username to post as") + // flag.StringVar(&channel, "channel", "", "Channel to post log entries to") + // flag.StringVar(&color, "color", "default", "Color for entry, either named or hex with `#`") + // flag.StringVar(&iconURL, "icon-url", "", "URL of icon to use for bot") + // flag.StringVar(&searchRegex, "search", "", "Search term or regex to match") + // flag.StringVar(&debugDest, "debug-dest", "os.Stdout", "Destination for debug and other messages, omit to log to Stdout") + // flag.BoolVar(&debug, "debug", false, "Include additional log data for debugging") + flag.StringVar(&configPath, "config", "./config.json", "Path to configuration file") flag.Parse() + validatePath(&configPath) + + configFile, err := os.Open(configPath) + jsonDecoder := json.NewDecoder(configFile) + config := config{} + err = jsonDecoder.Decode(&config) + if err != nil { + usage() + } + + fmt.Println(fmt.Sprintf("%+v\n", config)) + os.Exit(3) setUpLogger() validatePath(&logPath) @@ -60,35 +93,35 @@ func init() { usage() } - mh = matterhook.New(webhookURL, matterhook.Config{DisableServer: true}) + // mh = matterhook.New(webhookURL, matterhook.Config{DisableServer: true}) } func main() { - logger.Printf("Monitoring %s", logPath) - logger.Printf("Forwarding entries to channel \"%s\" as user \"%s\" at %s", channel, username, webhookURL) + // logger.Printf("Monitoring %s", logPath) + // logger.Printf("Forwarding entries to channel \"%s\" as user \"%s\" at %s", channel, username, webhookURL) sig := make(chan os.Signal, 1) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) - t, err := tail.TailFile(logPath, tail.Config{ - Follow: true, - Location: &tail.SeekInfo{Offset: 0, Whence: 2}, - MustExist: true, - ReOpen: true, - Logger: logger, - }) - if err != nil { - logger.Println(err) - t.Cleanup() - close(sig) - os.Exit(3) - } - - go parseLinesAndSend(t) + // t, err := tail.TailFile(logPath, tail.Config{ + // Follow: true, + // Location: &tail.SeekInfo{Offset: 0, Whence: 2}, + // MustExist: true, + // ReOpen: true, + // Logger: logger, + // }) + // if err != nil { + // logger.Println(err) + // t.Cleanup() + // close(sig) + // os.Exit(3) + // } + + // go parseLinesAndSend(t) caughtSig := <-sig - t.Stop() - t.Cleanup() + // t.Stop() + // t.Cleanup() logger.Printf("Stopping, got signal %s", caughtSig) } @@ -108,21 +141,21 @@ func parseLinesAndSend(t *tail.Tail) { } func sendLine(line *tail.Line) { - atts := attachments{ - attachment{ - Fallback: fmt.Sprintf("New entry in %s", logPath), - Pretext: fmt.Sprintf("In `%s` at `%s`:", logPath, line.Time), - Text: fmt.Sprintf(" %s", line.Text), - Color: color, - }, - } - - mh.Send(matterhook.OMessage{ - Channel: channel, - UserName: username, - Attachments: atts, - IconURL: iconURL, - }) + // atts := attachments{ + // attachment{ + // Fallback: fmt.Sprintf("New entry in %s", logPath), + // Pretext: fmt.Sprintf("In `%s` at `%s`:", logPath, line.Time), + // Text: fmt.Sprintf(" %s", line.Text), + // Color: color, + // }, + // } + + // mh.Send(matterhook.OMessage{ + // Channel: channel, + // UserName: username, + // Attachments: atts, + // IconURL: iconURL, + // }) } func setUpLogger() {