2018-07-23 12:51:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-07-16 12:54:48 +00:00
|
|
|
"context"
|
2018-07-23 12:51:32 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-07-23 14:20:50 +00:00
|
|
|
"path/filepath"
|
2018-07-23 12:51:32 +00:00
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-07-21 17:58:44 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/rconfig/v2"
|
2018-07-23 12:51:32 +00:00
|
|
|
)
|
|
|
|
|
2018-07-24 11:36:12 +00:00
|
|
|
const (
|
2024-07-16 12:54:48 +00:00
|
|
|
cleanupInterval = 10 * time.Second
|
|
|
|
logFolderPerms = 0o750
|
2018-07-24 11:36:12 +00:00
|
|
|
)
|
2018-07-23 12:51:32 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
|
|
|
DisableLog bool `flag:"no-log" default:"false" description:"Disable response body logging"`
|
|
|
|
Interval time.Duration `flag:"interval,i" default:"1s" description:"Check interval"`
|
2018-07-23 14:20:57 +00:00
|
|
|
LogDir string `flag:"log-dir,l" default:"/tmp/resp-log/" description:"Directory to log non-matched requests to"`
|
2024-07-16 12:54:48 +00:00
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
2018-07-23 12:51:32 +00:00
|
|
|
LogRetention time.Duration `flag:"log-retention" default:"24h" description:"When to clean up file from log-dir"`
|
|
|
|
Match string `flag:"match,m" default:".*" description:"RegExp to match the response body against to validate it"`
|
|
|
|
Timeout time.Duration `flag:"timeout,t" default:"30s" description:"Timeout for the request"`
|
|
|
|
URL string `flag:"url,u" default:"" description:"URL to query" validate:"nonzero"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
func initApp() error {
|
|
|
|
rconfig.AutoEnv(true)
|
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
|
|
|
return fmt.Errorf("parsing cli options: %w", err)
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing log-level: %w", err)
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
2024-07-16 12:54:48 +00:00
|
|
|
logrus.SetLevel(l)
|
2018-07-23 12:51:32 +00:00
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
return nil
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
if err = initApp(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("initializing app")
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
2024-07-16 12:54:48 +00:00
|
|
|
logrus.WithField("version", version).Info("webcheck")
|
2018-07-23 12:51:32 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
matcher, err := regexp.Compile(cfg.Match)
|
|
|
|
if err != nil {
|
2024-07-16 12:54:48 +00:00
|
|
|
logrus.WithError(err).Fatal("compiling matcher RegExp")
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastResult := newCheckResult(statusUnknown, "Uninitialized", 0)
|
|
|
|
|
2018-07-23 14:20:50 +00:00
|
|
|
go cleanupLogFiles()
|
|
|
|
|
2018-07-23 12:51:32 +00:00
|
|
|
for range time.Tick(cfg.Interval) {
|
|
|
|
var (
|
|
|
|
body *bytes.Buffer
|
|
|
|
result *checkResult
|
|
|
|
)
|
|
|
|
|
|
|
|
if !cfg.DisableLog {
|
|
|
|
body = new(bytes.Buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = doCheck(cfg.URL, matcher, body)
|
|
|
|
|
|
|
|
if !result.Equals(lastResult) {
|
2024-07-16 12:54:48 +00:00
|
|
|
fmt.Println() //nolint:forbidigo
|
2018-07-23 12:51:32 +00:00
|
|
|
lastResult = result
|
|
|
|
|
|
|
|
if result.Status == statusFailed {
|
|
|
|
fn, err := dumpRequest(body)
|
|
|
|
if err != nil {
|
2024-07-16 12:54:48 +00:00
|
|
|
logrus.WithError(err).Fatal("logging request")
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
lastResult.DumpFile = fn
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-24 11:36:12 +00:00
|
|
|
lastResult.AddDuration(result.Durations.GetCurrent())
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
if err = lastResult.Print(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("displaying status")
|
|
|
|
}
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:20:50 +00:00
|
|
|
func cleanupLogFiles() {
|
2024-07-16 12:54:48 +00:00
|
|
|
for range time.Tick(cleanupInterval) {
|
2018-07-24 09:02:49 +00:00
|
|
|
if info, err := os.Stat(cfg.LogDir); err != nil || !info.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:20:50 +00:00
|
|
|
if err := filepath.Walk(cfg.LogDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() && time.Since(info.ModTime()) > cfg.LogRetention {
|
|
|
|
return os.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2024-07-16 12:54:48 +00:00
|
|
|
fmt.Println() //nolint:forbidigo
|
|
|
|
logrus.WithError(err).Error("cleaning up logs")
|
2018-07-23 14:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:51:32 +00:00
|
|
|
func doCheck(url string, match *regexp.Regexp, responseBody io.Writer) *checkResult {
|
2024-07-16 12:54:48 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
2018-07-23 12:51:32 +00:00
|
|
|
|
|
|
|
respStart := time.Now()
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
respDuration := time.Since(respStart)
|
|
|
|
if err != nil {
|
|
|
|
return newCheckResult(
|
|
|
|
statusFailed,
|
|
|
|
fmt.Sprintf("HTTP request failed: %s", err),
|
|
|
|
respDuration,
|
|
|
|
)
|
|
|
|
}
|
2024-07-16 12:54:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing response body (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2018-07-23 12:51:32 +00:00
|
|
|
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
if _, err = io.Copy(body, resp.Body); err != nil {
|
|
|
|
return newCheckResult(
|
|
|
|
statusFailed,
|
|
|
|
"Was not able to read response body",
|
|
|
|
respDuration,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody != nil {
|
|
|
|
if err = resp.Header.Write(responseBody); err != nil {
|
|
|
|
return newCheckResult(
|
|
|
|
statusFailed,
|
|
|
|
"Was not able to copy headers",
|
|
|
|
respDuration,
|
|
|
|
)
|
|
|
|
}
|
2024-07-16 12:54:48 +00:00
|
|
|
|
|
|
|
if _, err = responseBody.Write(append([]byte{'\n'}, body.Bytes()...)); err != nil {
|
2018-07-23 12:51:32 +00:00
|
|
|
return newCheckResult(
|
|
|
|
statusFailed,
|
|
|
|
"Was not able to copy body",
|
|
|
|
respDuration,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
|
|
return newCheckResult(
|
|
|
|
statusFailed,
|
|
|
|
fmt.Sprintf("Status code was != 2xx: %d", resp.StatusCode),
|
|
|
|
respDuration,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !match.Match(body.Bytes()) {
|
|
|
|
return newCheckResult(
|
|
|
|
statusFailed,
|
|
|
|
"Response body does not match regexp",
|
|
|
|
respDuration,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newCheckResult(
|
|
|
|
statusOk,
|
|
|
|
fmt.Sprintf("Status was %d and text matched", resp.StatusCode),
|
|
|
|
respDuration,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpRequest(body io.Reader) (string, error) {
|
|
|
|
if body == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
if err := os.MkdirAll(cfg.LogDir, logFolderPerms); err != nil {
|
|
|
|
return "", fmt.Errorf("creating log folder: %w", err)
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
f, err := os.CreateTemp(cfg.LogDir, "resp")
|
2018-07-23 12:51:32 +00:00
|
|
|
if err != nil {
|
2024-07-16 12:54:48 +00:00
|
|
|
return "", fmt.Errorf("creating log file: %w", err)
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|
2024-07-16 12:54:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing log file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2018-07-23 12:51:32 +00:00
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
if _, err = io.Copy(f, body); err != nil {
|
|
|
|
return "", fmt.Errorf("copying request body: %w", err)
|
|
|
|
}
|
2018-07-23 12:51:32 +00:00
|
|
|
|
2024-07-16 12:54:48 +00:00
|
|
|
return f.Name(), nil
|
2018-07-23 12:51:32 +00:00
|
|
|
}
|