Git Product home page Git Product logo

sensu-check-log's Introduction

Sensu Bonsai Asset Go Test goreleaser

sensu-check-log

Table of Contents

Overview

sensu-check-log is a Sensu Check and log file analyzer plugin for Sensu Go. The program scans a set of log files, checks for matches, and sends a special alert event to the agent events API when a match is detected.

The check itself will return a 0 status, unless execution fails for some reason (ex: if one of the files can not be read)

Usage examples

sensu-check-log

Check Log

Usage:
  sensu-check-log [flags]
  sensu-check-log [command]

Available Commands:
  help        Help about any command
  version     Print the version number of this plugin

Flags:
  -d, --state-directory string       Directory where check will hold state for each processed log file. Note: checks using different match expressions should use different state directories to avoid conflict. (Required)
  -f, --log-file string              Log file to check. (Required if --log-file-expr not used)
  -e, --log-file-expr string         Log file regexp to check. (Required if --log-file not used)
  -m, --match-expr string            RE2 regexp matcher expression. (required)
  -p, --log-path string              Log path for basis of log file regexp. Only finds files under this path. (Required if --log-file-expr used) (default "/var/log/")
  -W, --warning-only                 Only issue warning status if matches are found
  -w, --warning-threshold int        Minimum match count that results in an warning (default 1)
  -C, --critical-only                Only issue critical status if matches are found
  -c, --critical-threshold int       Minimum match count that results in an warning (default 5)
  -b, --max-bytes int                Max number of bytes to read (0 means unlimited).
  -a, --analyzer-procs int           Number of parallel analyzer processes per file. 
  -t, --check-name-template string   Check name to use in generated events (default "{{ .Check.Name }}-alert")
  -u, --events-api-url string        Agent Events API URL. (default "http://localhost:3031/events")
  -D, --disable-event-generation     Disable event generation, send results to stdout instead.
  -I, --ignore-initial-run           Suppresses alerts for any matches found on the first run of the plugin.
  -M, --missing-ok                   Suppresses error if selected log files are missing 
  -i, --invert-thesholds             Invert warning and critical threshold values, making them minimum values to alert on
  -r, --reset-state                  Allow automatic state reset if match expression changes, instead of failing.
  -n, --dry-run                      Suppress generation of events and report intended actions instead. (implies verbose)
  -v, --verbose                      Verbose output, useful for testing.
      --output-matching-string       Include detailed information about each matching line in output
      --force-read-from-start        Ignore cached file offset in state directory and read file(s) from beginning.
  -h, --help                         help for sensu-check-log

Environment variables

Argument Environment Variable
--state-directory CHECK_LOG_STATE_DIRECTORY
--log-file CHECK_LOG_FILE
--log-file-expr CHECK_LOG_FILE_EXPR
--log-path CHECK_LOG_PATH
--match-expr CHECK_LOG_MATCH_EXPR
--warning-only CHECK_LOG_WARNING_ONLY
--warning-threshold CHECK_LOG_WARNING_THRESHOLD
--critical-only CHECK_LOG_CRITICAL_ONLY
--critical-threshold CHECK_LOG_CRITICAL_THRESHOLD
--max-bytes CHECK_LOG_MAX_BYTES
--analyzer-procs CHECK_LOG_ANALYZER_PROCS
--check-name-template CHECK_LOG_CHECK_NAME_TEMPLATE
--events-api-url CHECK_LOG_EVENTS_API_URL
--disable-event-generation CHECK_LOG_DISABLE_EVENT_GENERATION
--ignore-initial-run CHECK_LOG_IGNORE_INITIAL_RUN
--missing-ok CHECK_LOG_MISSING_OK
--invert-thresholds CHECK_LOG_INVERT_THRESHOLDS
--reset-state CHECK_LOG_RESET_STATE

Event generation

By default, sensu-check-log will attempt to create a new alert event if a log match is found for any of the files selected to be checked. This makes it possible for the check to run repeatedly without automatically resolving alerts generated from previously found log matches. The primary event associated with the sensu-check-log can still be used to detect operational faults such as a missing log file, or errors writing into the state directory.

The generated alert event is created using the local Sensu agent's event api url. You can disable event generation by using --disable-event-generation or --dry-run arguments

Note: Event generation requires Sensu Go check configuration stdin:true

Check Name Template

This check provides options for using a golang template aware string to populate the check name in the generated event. By default the check name is populated using a template that modifies the calling check name from the event passed into the command from stdin. More information on template syntax and format can be found in the documentation

Annotations

All arguments for these checks are tunable on a per entity or check basis based on annotations. The annotations keyspace for this collection of checks is sensu.io/plugins/sensu-check-log/config. You can make use of annotation overrides when the check is configured with stdin: true.

NOTE: Due to check token substituion, supplying a template value such as for check-name-template as a check annotation requires that you place the desired template as a golang string literal (enlcosed in backticks) within another template definition. This does not apply to entity annotations.

Examples

To customize the event api url as an entity annotation, you could use a sensu-agent configuration snippet similar to this:

# /etc/sensu/agent.yml example
annotations:
  sensu.io/plugins/sensu-check-log/config/events-api-url: 'http://127.0.0.1:7342'

Configuration

Asset registration

Sensu Assets are the best way to make use of this plugin. If you're not using an asset, please consider doing so! If you're using sensuctl 5.13 with Sensu Backend 5.13 or later, you can use the following command to add the asset:

sensuctl asset add sensu/sensu-check-log

If you're using an earlier version of sensuctl, you can find the asset on the [Bonsai Asset Index][https://bonsai.sensu.io/assets/sensu/sensu-check-log].

Check definition

sensu-check-log

Example of configuring a check configuration to match the word 'error' in a case-insensitive manner using RE compatible regexp syntax

---
type: CheckConfig
api_version: core/v2
metadata:
  name: sensu-check-log
spec:
  command: sensu-check-log -f /var/log/messages.log -m "(?i)error" -d /tmp/sensu-check-log-error/
  stdin: true
  runtime_assets:
  - sensu/sensu-check-log

Example of configuring a check configuration to match lines without the word 'success' in a case-insensitive manner using RE compatible regexp syntax

---
type: CheckConfig
api_version: core/v2
metadata:
  name: sensu-check-log
spec:
  command: sensu-check-log -f /var/log/messages.log -m "(?i)success" -i -d /tmp/sensu-check-log-not-success/
  stdin: true
  runtime_assets:
  - sensu/sensu-check-log

Example of configuring a check configuration to match lines with the word 'error' in a case-insensitive manner for all log filepaths under /var/log ending with webserver-.*/access.log using RE compatible regexp syntax

---
type: CheckConfig
api_version: core/v2
metadata:
  name: sensu-check-log
spec:
  command: sensu-check-log -p /var/log/ -e "webserver-.*/access.log$" -m "(?i)error" -d /tmp/sensu-check-access-log-error/
  stdin: true
  runtime_assets:
  - sensu/sensu-check-log

Installation from source

The preferred way of installing and deploying this plugin is to use it as an Asset. If you would like to compile and install the plugin from source or contribute to it, download the latest version or create an executable script from this source.

From the local path of the sensu-check-log repository:

go build

Additional notes

Contributing

For more information about contributing to this plugin, see Contributing.

sensu-check-log's People

Contributors

echlebek avatar jspaleta avatar portertech avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sensu-check-log's Issues

Aggregate/evaluate log matches over time period

User stories:

  • Alert when the count (or total) of regex matches exceeds a threshold during a time period (e.g. if more than 10 aggregate log matches in 15 mins).

  • Alert when the count (or total) of regex matches is below a threshold during a time period (e.g. if 10 aggregate log matches are expected in 30 mins and only 5 are observed).

Can we achieve this via a more robust state file (or state db via boltdb)?

Get project to MVP status

Minimum requirements:

  • On positive match, transmit event to agent events API
  • Configurable state file support that informs the current byte offset
  • -max-bytes flag
  • Packaging

Fix inverse matching logic

Inverse matching boolean logic is unuseful.

Inverse should map to logic that warns/critical "less than" instead of "greater than" count match

Whenever we run this check, it throws error: creating event

Whenever we execute the sensu-check-log, it throws an error. Here's the image for reference
error_screenshot

Additionally, Here are the few item you would require to replicate the issue

  1. Log file : sample.log
  2. Check_Command: sensu-check-log --log-file /var/tmp/sample.log --match-expr "java.lang.OutOfMemoryError" --state-directory /var/tmp/ --reset-state --critical-threshold 1 --critical-only --warning-threshold 0 --ignore-initial-run --verbose
  3. Check_config: check.yml.txt

Wrong return status when processing multiple files

From customer report

Test setup

  1. set up first log file with matching lines
echo "error" > /tmp/test-logs/first-test.log
  1. setup second log file without matching lines
echo "success" > /tmp/test-logs/first-test.log
  1. run dry run command test using -p and -e option to make sure both files are found
./sensu-check-log -d /tmp/test-state-directory -p /tmp/test-logs -e "log$" -m "error" --disable-event-generation --dry-run ; echo $?
  1. return status should be non-zero but it is not

Optionally suppress alerts on first run

When deploying this check against existing log files, the first run may report any number of matches which could be quite old. This is resulting in user requests to suppress alerts for any matches found on the first run of the plugin.

Because this plugin uses a state file to keep track of the position reached in last execution, it seems to me that we can trivially add a flag which suppresses alerts when there's no pre-existing state file.

Add a `mtime` style flag to filter out log files based on modification time.

In internal ref https://secure.helpscout.net/conversation/1825973406/28629?folderId=5845954, it was mentioned that there is a feature in Tivoli's log check that allows users to filter out logs based on the modification time. The user provided the following help text to illustrate the features that the Tivoli log check provides.

FileComparisonMode
Specifies which log files are monitored when more than one matches a wildcard pattern. The
following values are available:
CompareByAllMatches
This value is the default behavior. All files that match the wildcard pattern that is
specified in LogSources are monitored.
CompareByLastUpdate
Of the files that match the wildcard pattern that is specified in LogSources, the file with
the most recent last update timestamp is monitored.
CompareBySize
Of the two or more files that match the file name pattern criteria, the bigger file is
selected for monitoring. Do not use CompareBySize with multiple matching files that are
being updated at the same time and increasing their file sizes. If the largest file is subject
to frequent change, monitoring might continually restart at the beginning of the newly
selected file. Instead, use CompareBySize when there is a set of matching files, but only
one is active and being updated at any specific time.
CompareByCreationTime
Of the files that match the wildcard

Add better error handling when incorrect/non-existent values are provided the state dir flag

In internal ref https://secure.helpscout.net/conversation/2016338466/29933?folderId=4465878, the customer points out the lack of error handling when stating a non-existent state directory leading to a much more difficult time when troubleshooting. The error output is the generic help text/syntax error pasted below.

sensu-check-log [flags] sensu-check-log [command] Available Commands: help Help about any command version Print the version number of this plugin Flags: -a, --analyzer-procs int Number of parallel analyzer processes per file. (default 4) -t, --check-name-template string Check name to use in generated events (default "{{ .Check.Name }}-alert") -C, --critical-only Only issue critical status if matches are found -c, --critical-threshold int Minimum match count that results in an warning (default 5) -D, --disable-event-generation Disable event generation, send results to stdout instead. -n, --dry-run Suppress generation of events and report intended actions instead. (implies verbose) -u, --events-api-url string Agent Events API URL. (default http://127.0.0.1:4041/events) -h, --help help for sensu-check-log -I, --ignore-initial-run Suppresses alerts for any matches found

Add support for log metrics

Add support for extracting actual metrics reported in a log file, or generating/calculating metrics based on some trend or "pattern" in a log file (e.g. "N errors in the last 10 seconds"). The latter capability more or less already exists via the -match flag, but it would need to produce event.metrics as a result.

Add support to generate an event per log file

Feature request from customer needs design discussion.

Desired use case explanation

Plugin now supports processing multiple files using a filename regexp pattern, and a state directory to hold per file path information, and will generate a single event capturing the result information.

The request is to generate an unique event for each processed log file path.

The technical problem

each generated event needs to have an unique check name, but file paths as check names might not be suitable, as file paths can be arbitrarily long and have special characters that can not be used in the check names currently.

Any attempt to squash the file path into an uuid makes the check name potentially unreadable to a human who needs to parse the event. Any attempt to shorten the filepath into a reasonable length check name runs the risk of destroying check name uniqueness causes events to overwrite each other.

Make matching string optional in output

The situation

The good

matching is regexp based, having the matching string in the output can be very useful to tune the regexp when receive false positive

The bad

matching string degrades readability of the json output because it can be a long unbounded string.

The ugly

because this check plugin uses regexp to match both file names and log line strings, just providing a simple summary of error counts is operationally insufficient to be actionable. The output must at a minimum show which files matched.

Straw solution

  1. remove matching string from output by default
  2. add cmdline line argument to opt-in to matching string

Support inverse case

This plugin currently raises an alert when the specified regexp is matched.

We understand from some customers that they have a use for the inverse case, meaning they want to be alerted when specified patterns are not present in the log.

Add support for regex paths

It would be helpful to be able to specify a regex path/multiple files to look at, specifically for applications that write logs to dated folders. For example:

C:\Program Files\Application\logs\YYYY-MM-DD\log.txt

Being able to present the check configuration as the following would be nice.

sensu-check-log -log "C:\Program Files\Application\logs\*\*.txt" -match ERROR -state "C:\etc\sensu\log_state\Application"

This was previously accomplished with a modified version of the log plugin for Sensu Core, but given the current Ruby runtime does not support Windows that guy no longer functions.

Clean up dry-run report output

dry-run output report is confusing for operators.
Need to rework the output to clear up confusion on expected event status ok,warn,critical

Update readme to meet community plugin style guidelines

Style guide is published here: https://github.com/sensu-plugins/community/blob/master/PLUGIN_STYLEGUIDE.md

Specifically, this readme would be more useful with an example check configuration. Here's one where I've captured sensu-backend logs into /tmp and I'm checking them for error:

type: CheckConfig
api_version: core/v2
metadata:
  name: tmp-backend-log
spec:
  check_hooks: null
  command: sensu-check-log -match error -state /tmp/backend.log.state -log /tmp/backend.log
  interval: 10
  publish: true
  runtime_assets:
  - sensu-check-log
  stdin: true
  subscriptions:
  - backend
  timeout: 10

It would be my preference to include an example which will be more immediately applicable to a wide cross section of users, but in most cases the standard log files on a Linux system are not readable by non-root users.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.