From 6a51993296a58bfb404fc66008352185c122b566 Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Sun, 12 Feb 2017 17:35:09 -0800 Subject: [PATCH] update readme, remove tcp/icmp --- cli/main.go | 8 -- example.config.yml | 61 ++++++++++++-- icmp.go | 5 -- monitor.go | 2 +- monitor_test.go | 7 ++ readme.md | 193 +++++++++++++++++++++++++++++---------------- tcp.go | 15 ---- 7 files changed, 188 insertions(+), 103 deletions(-) delete mode 100644 icmp.go create mode 100644 monitor_test.go delete mode 100644 tcp.go diff --git a/cli/main.go b/cli/main.go index 4385031..8d13975 100644 --- a/cli/main.go +++ b/cli/main.go @@ -179,14 +179,6 @@ func getConfiguration(path string) (*cachet.CachetMonitor, error) { var s cachet.DNSMonitor err = mapstructure.Decode(rawMonitor, &s) t = &s - case "icmp": - var s cachet.ICMPMonitor - err = mapstructure.Decode(rawMonitor, &s) - t = &s - case "tcp": - var s cachet.TCPMonitor - err = mapstructure.Decode(rawMonitor, &s) - t = &s default: logrus.Errorf("Invalid monitor type (index: %d) %v", index, monType) continue diff --git a/example.config.yml b/example.config.yml index 2b24df4..fc4e32a 100644 --- a/example.config.yml +++ b/example.config.yml @@ -1,14 +1,65 @@ api: + # cachet url url: https://demo.cachethq.io/api/v1 + # cachet api token token: 9yMHsdioQosnyVK4iCVR + insecure: false +# https://golang.org/src/time/format.go#L57 +date_format: 02/01/2006 15:04:05 MST monitors: + # http monitor example - name: google - target: https://google.com - threshold: 80 + # test url + target: http://localhost:2000/ping + # strict certificate checking for https + strict: true + # HTTP method + method: POST + + # set to update component (either component_id or metric_id are required) component_id: 1 - interval: 10 - timeout: 5 + # set to post lag to cachet metric (graph) + metric_id: 4 + + # custom templates (see readme for details) + template: + investigating: + subject: "{{ .Monitor.Name }} - {{ .SystemName }}" + message: "{{ .Monitor.Name }} check **failed** (server time: {{ .now }})\n\n{{ .FailReason }}" + fixed: + subject: "I HAVE BEEN FIXED" + + # seconds between checks + interval: 1 + # seconds for timeout + timeout: 1 + # If % of downtime is over this threshold, open an incident + threshold: 80 + + # custom HTTP headers headers: Authorization: Basic + # expected status code (either status code or body must be supplied) expected_status_code: 200 - strict: true \ No newline at end of file + # regex to match body + expected_body: "P.*NG" + # dns monitor example + - name: dns + # fqdn + target: matej.me. + # question type (A/AAAA/CNAME/...) + question: mx + type: dns + # set component_id/metric_id + component_id: 2 + # poll every 1s + interval: 1 + timeout: 1 + # custom DNS server (defaults to system) + dns: 8.8.4.4:53 + answers: + # exact/regex check + - regex: [1-9] alt[1-9].aspmx.l.google.com. + - exact: 10 aspmx2.googlemail.com. + - exact: 1 aspmx.l.google.com. + - exact: 10 aspmx3.googlemail.com. \ No newline at end of file diff --git a/icmp.go b/icmp.go deleted file mode 100644 index a2a9a41..0000000 --- a/icmp.go +++ /dev/null @@ -1,5 +0,0 @@ -package cachet - -type ICMPMonitor struct { - AbstractMonitor `mapstructure:",squash"` -} diff --git a/monitor.go b/monitor.go index 8ee480e..e508c83 100644 --- a/monitor.go +++ b/monitor.go @@ -28,7 +28,7 @@ type AbstractMonitor struct { Name string Target string - // (default)http, tcp, dns, icmp + // (default)http / dns Type string Strict bool diff --git a/monitor_test.go b/monitor_test.go new file mode 100644 index 0000000..c75df01 --- /dev/null +++ b/monitor_test.go @@ -0,0 +1,7 @@ +package cachet + +import ( + "testing" +) + +func TestAnalyseData(t *testing.T) {} diff --git a/readme.md b/readme.md index a77d037..0420608 100644 --- a/readme.md +++ b/readme.md @@ -1,100 +1,155 @@ ![screenshot](https://castawaylabs.github.io/cachet-monitor/screenshot.png) -Features --------- +## Features - [x] Creates & Resolves Incidents -- [x] Check URLs by response code and/or body contents - [x] Posts monitor lag to cachet graphs +- [x] HTTP Checks (body/status code) +- [x] DNS Checks - [x] Updates Component to Partial Outage -- [x] Updates Component to Major Outage if already in Partial Outage (works well with distributed monitoring) +- [x] Updates Component to Major Outage if already in Partial Outage (works with distributed monitors) - [x] Can be run on multiple servers and geo regions -Configuration -------------- +## Example Configuration -``` -{ - // URL for the API. Note: Must end with /api/v1 - "api_url": "https:///api/v1", - // Your API token for Cachet - "api_token": "", - // optional, false default, set if your certificate is self-signed/untrusted - "insecure_api": false, - "monitors": [{ - // required, friendly name for your monitor - "name": "Name of your monitor", - // required, url to probe - "url": "Ping URL", - // optional, http method (defaults GET) - "method": "get", - // optional, http Headers to add (default none) - "headers": [ - // specify Name and Value of Http-Header, eg. Authorization - { "name": "Authorization", "value": "Basic " } - ], - // self-signed ssl certificate - "strict_tls": true, - // seconds between checks - "interval": 10, - // seconds for http timeout - "timeout": 5, - // post lag to cachet metric (graph) - // note either metric ID or component ID are required - "metric_id": , - // post incidents to this component - "component_id": , - // If % of downtime is over this threshold, open an incident - "threshold": 80, - // optional, expected status code (either status code or body must be supplied) - "expected_status_code": 200, - // optional, regular expression to match body content - "expected_body": "P.*NG" - }], - // optional, system name to identify bot (uses hostname by default) - "system_name": "", - // optional, defaults to stdout - "log_path": "" -} +```yaml +api: + # cachet url + url: https://demo.cachethq.io/api/v1 + # cachet api token + token: 9yMHsdioQosnyVK4iCVR + insecure: false +# https://golang.org/src/time/format.go#L57 +date_format: 02/01/2006 15:04:05 MST +monitors: + # http monitor example + - name: google + # test url + target: http://localhost:2000/ping + # strict certificate checking for https + strict: true + # HTTP method + method: POST + + # set to update component (either component_id or metric_id are required) + component_id: 1 + # set to post lag to cachet metric (graph) + metric_id: 4 + + # custom templates (see readme for details) + template: + investigating: + subject: "{{ .Monitor.Name }} - {{ .SystemName }}" + message: "{{ .Monitor.Name }} check **failed** (server time: {{ .now }})\n\n{{ .FailReason }}" + fixed: + subject: "I HAVE BEEN FIXED" + + # seconds between checks + interval: 1 + # seconds for timeout + timeout: 1 + # If % of downtime is over this threshold, open an incident + threshold: 80 + + # custom HTTP headers + headers: + Authorization: Basic + # expected status code (either status code or body must be supplied) + expected_status_code: 200 + # regex to match body + expected_body: "P.*NG" + # dns monitor example + - name: dns + # fqdn + target: matej.me. + # question type (A/AAAA/CNAME/...) + question: mx + type: dns + # set component_id/metric_id + component_id: 2 + # poll every 1s + interval: 1 + timeout: 1 + # custom DNS server (defaults to system) + dns: 8.8.4.4:53 + answers: + # exact/regex check + - regex: [1-9] alt[1-9].aspmx.l.google.com. + - exact: 10 aspmx2.googlemail.com. + - exact: 1 aspmx.l.google.com. + - exact: 10 aspmx3.googlemail.com. ``` -Installation ------------- +## Installation 1. Download binary from [release page](https://github.com/CastawayLabs/cachet-monitor/releases) -2. Create your configuration ([example](https://raw.githubusercontent.com/CastawayLabs/cachet-monitor/master/example.config.json)) -3. `cachet-monitor -c /etc/cachet-monitor.config.json` +2. Create a configuration +3. `cachet-monitor -c /etc/cachet-monitor.yaml` pro tip: run in background using `nohup cachet-monitor 2>&1 > /var/log/cachet-monitor.log &` ``` -Usage of cachet-monitor: - -c="/etc/cachet-monitor.config.json": Config path - -log="": Log path - -name="": System Name +Usage: + cachet-monitor (-c PATH | --config PATH) [--log=LOGPATH] [--name=NAME] [--immediate] + cachet-monitor -h | --help | --version + cachet-monitor print-config + +Arguments: + PATH path to config.json + LOGPATH path to log output (defaults to STDOUT) + NAME name of this logger + +Examples: + cachet-monitor -c /root/cachet-monitor.json + cachet-monitor -c /root/cachet-monitor.json --log=/var/log/cachet-monitor.log --name="development machine" + +Options: + -c PATH.json --config PATH Path to configuration file + -h --help Show this screen. + --version Show version + --immediate Tick immediately (by default waits for first defined interval) + print-config Print example configuration + +Environment varaibles: + CACHET_API override API url from configuration + CACHET_TOKEN override API token from configuration + CACHET_DEV set to enable dev logging ``` -Environment variables ---------------------- +## Templates -| Name | Example Value | Description | -| ------------ | ------------------------------ | --------------------------- | -| CACHET_API | http://demo.cachethq.io/api/v1 | URL endpoint for cachet api | -| CACHET_TOKEN | APIToken123 | API Authentication token | -| CACHET_DEV | 1 | Strips logging | +This package makes use of [`text/template`](https://godoc.org/text/template). -Vision and goals ----------------- +The following variables are available: + +| Root objects | +| ------------- | ----------------- +| `.SystemName` | system name +| `.API` | `api` object from configuration +| `.Monitor` | `monitor` object from configuration +| `.now` | formatted date string + +| Monitor variables | +| ------------------ | +| `.Name` | +| `.Target` | +| `.Type` | +| `.Strict` | +| `.MetricID` | +| ... | + +All monitor variables are available from `monitor.go` + +## Vision and goals We made this tool because we felt the need to have our own monitoring software (leveraging on Cachet). The idea is a stateless program which collects data and pushes it to a central cachet instance. This gives us power to have an army of geographically distributed loggers and reveal issues in both latency & downtime on client websites. -Package usage -------------- +## Package usage -When using `cachet-monitor` as a package in another program, you should follow what `cli/main.go` does. It is important to call `ValidateConfiguration` on `CachetMonitor` and all the monitors inside. +When using `cachet-monitor` as a package in another program, you should follow what `cli/main.go` does. It is important to call `Validate` on `CachetMonitor` and all the monitors inside. [API Documentation](https://godoc.org/github.com/CastawayLabs/cachet-monitor) diff --git a/tcp.go b/tcp.go deleted file mode 100644 index fa34f70..0000000 --- a/tcp.go +++ /dev/null @@ -1,15 +0,0 @@ -package cachet - -type TCPMonitor struct { - AbstractMonitor `mapstructure:",squash"` - - // same as output from net.JoinHostPort - // defaults to parsed config from /etc/resolv.conf when empty - DNSServer string - - // Will be converted to FQDN - Domain string - Type string - // expected answers (regex) - Expect []string -}