From 1fcf9f1291171248ed6d3bf74aa8b5d7ff5d229d Mon Sep 17 00:00:00 2001 From: Christopher Ramey Date: Tue, 11 Aug 2020 03:48:40 -0800 Subject: [PATCH] Initial commit --- .gitignore | 2 + main.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ sample.config | 11 ++++++ 3 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 main.go create mode 100644 sample.config diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f041f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +alrm +*.swp diff --git a/main.go b/main.go new file mode 100644 index 0000000..eece646 --- /dev/null +++ b/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "fmt" + "os" + "bufio" + "strings" + "unicode/utf8" +) + +func main(){ + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "filename required\n") + os.Exit(1) + } + + file, err := os.Open(os.Args[1]) + if err != nil { + fmt.Fprintf(os.Stderr, "cannot open %s: %s\n", + os.Args[1], err.Error()) + os.Exit(1) + } + defer file.Close() + + lscan := bufio.NewScanner(file) + lscan.Split(bufio.ScanLines) + for lscan.Scan() { + line := lscan.Text() + // Ignore comments + if len(line) < 1 || line[0] == '#' { + continue + } + + wscan := bufio.NewScanner(strings.NewReader(line)) + wscan.Split(ScanWords) + for wscan.Scan() { + word := wscan.Text() + fmt.Printf("[%s] ", word) + } + } + fmt.Printf("\n") +} + +func ScanWords(data []byte, atEOF bool) (int, []byte, error) { + start := 0 + quote := int32(0) + for start < len(data) { + r, w := utf8.DecodeRune(data[start:]) + if !isSpace(r) { + if isQuote(r) { + quote = r + start += w + } + break + } + start += w + } + + for i := start; i < len(data); { + r, w := utf8.DecodeRune(data[i:]) + + if (quote > 0 && quote == r) || (quote == 0 && isSpace(r)) { + return i + w, data[start:i], nil + } + + i += w + } + + if atEOF && len(data) > start { + return len(data), data[start:], nil + } + return start, nil, nil +} + +func isQuote(r rune) bool { + switch r { + case '\u0022', '\u0027': + return true + } + return false +} + +func isSpace(r rune) bool { + if r <= '\u00FF' { + switch r { + case ' ', '\t', '\n', '\v', '\f', '\r': + return true + case '\u0085', '\u00A0': + return true + } + return false + } + + if '\u2000' <= r && r <= '\u200a' { + return true + } + + switch r { + case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000': + return true + } + return false +} diff --git a/sample.config b/sample.config new file mode 100644 index 0000000..9b3140e --- /dev/null +++ b/sample.config @@ -0,0 +1,11 @@ +set alert + +monitor group + host www1.example.com address 10.11.135.101 + # check allows for a check using a host/address + check ping + host www2.example.com address 10.11.135.102 + check ping + +# Address is optional, derived from host +monitor host monkey.com