Monitoring::Sneck

SYNOPSIS

sneck -u [-C <cache file>] [-f <config file>] [-p] [-i] [-d] [-q] [-l|-L] [-P <pid dir>] [-r] [-T <seconds>] [-s <signal>] [-k|-K]

sneck -c [-C <cache file>] [-f <config file>] [-b]

sneck [-f <config file>] [-p] [-i] [-r] [-T <seconds>] [-s <signal>] [-k|-K]

sneck -t [-f <config file>]

FLAGS

The cache file, PID dir, locking, and check timeout settings may also be set in the config file. Flags override those. See OPTIONS.

-f config_file

The config file to use.

Files ending in .yaml or .yml are read as YAML, which needs YAML::XS.

Default :: /usr/local/etc/sneck.conf

-p

Pretty it in a nicely formatted format.

-C cache_file

The cache file to use.

Overrides cache_file in the config.

Default :: /var/cache/sneck.cache

A secondary cache file based on this name is also created. By default it is /var/cache/sneck.cache.snmp and always holds the GZip+BASE64 compressed version.

-u

Update the cache file. Will also print the was written to it.

-c

Print the cache file. Please note that -p or -i won't affect this as this flag only reads/prints the cache file.

-b

When used with -c, print the LibreNMS style GZip+BASE64 compressed cache instead.

-i

Includes the config file used.

-d

Print debugging info if called with -u.

-q

Don't print the results for -u. Exit quietly.

-l

Enable locking for -u so more than one instance can't run at a time. Overrides locking in the config.

The PID file is sneck.pid in the directory set by -P.

-L

Disable locking, overriding locking in the config. Can not be used with -l.

-P pid_dir

The directory for the PID file used by -l. The user running sneck must be able to write to it.

Overrides pid_dir in the config.

Default :: /var/run

-r

Run any restarts whose checks failed. Without this, restarts are only reported as restarts disabled. Meant to be used with -u from cron. Never use it from snmpd.

Restart state, used for min_interval and max_retries, is kept in the cache file name with .restarts added, so by default /var/cache/sneck.cache.restarts.

Use locking with this to make sure two runs can't restart things at the same time.

-T seconds

Seconds to wait on each check before giving up on it. Overrides check_timeout in the config.

Default :: 30

-s signal

Signal to send a check on timeout, such as TERM. Overrides check_timeout_signal in the config. none sends no signal, even if the config sets one.

Default :: none

-k

Also send the timeout signal to all child processes of a check. Overrides check_kill_sub_pids in the config. Does nothing without a timeout signal. This is the default.

-K

Do not send the timeout signal to child processes of a check. Overrides check_kill_sub_pids in the config. Can not be used with -k.

-t

Test the config file. Prints any errors and warnings, then exits 0 if there are no errors and 1 if there are or the file can't be read. Warnings, such as a check using a undefined variable, do not affect the exit code.

CONFIG FORMAT

The sneck format is described below. Files ending in .yaml or .yml use the YAML format instead, described under YAML CONFIG.

Each line has leading spaces and tabs removed before it is looked at. A trailing \r is also removed, so files with CRLF line endings work.

Blank lines are ignored.

Lines starting with # are comments and are ignored.

Names are made up of A-Z, a-z, 0-9, and _.

Any other sort of line is an error. Every bad line is reported, along with its line number.

Variables are used in commands in the form %NAME%. A reference to a variable that is not defined is left as written and produces a warning, as it may just be part of the command, such as date +%Y%m%d. Use -t to see warnings.

Option, variable, check, debug check, and restart names may not be redefined.

EXAMPLE CONFIG

env PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
# this is a comment
GEOM_DEV=foo
geom_foo|/usr/local/libexec/nagios/check_geom mirror %GEOM_DEV%
does_not_exist|/bin/this_will_error yup... that it will
    
does_not_exist_2|/usr/bin/env /bin/this_will_also_error

#includes route info
%routes|netstat -rn

The first line sets the %ENV variable PATH.

The second is ignored as it is a comment.

The third sets the variable GEOM_DEV to 'foo'

The fourth creates a check named geom_foo that calls check_geom_mirror with the variable supplied to it being the value specified by the variable GEOM_DEV.

The fith is a example of an error that will show what will happen when you call to a file that does not exit.

The sixth line will be ignored as it is blank.

The seventh is a example of another command erroring.

When you run it, you will notice that errors for lines 4 and 5 are printed to STDERR. For this reason you should use '2> /dev/null' when calling it from snmpd or '2> /dev/null > /dev/null' when calling from cron.

YAML CONFIG

YAML configs need YAML::XS, which is optional and only loaded when a YAML config is used.

env:
  PATH: /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
vars:
  GEOM_DEV: foo
checks:
  geom_foo: /usr/local/libexec/nagios/check_geom mirror %GEOM_DEV%
  does_not_exist: /bin/this_will_error yup... that it will
debugs:
  routes: netstat -rn

Any other top level key is an error. Any section may be left out or empty.

Names, variables, and undefined variable warnings work the same as the sneck format. Errors and warnings point to a path, such as checks.geom_foo, as YAML::XS does not give line numbers.

Values must be strings or numbers. An empty value is an empty string for env and vars and an error for checks and debugs.

Some things to watch for.

OPTIONS

Options are settings for sneck itself. Flags override them.

Any other option is an error.

$cache_file=/var/db/sneck/sneck.cache
$pid_dir=/var/run/sneck
$locking=1
$check_timeout=60
$check_timeout_signal=TERM
options:
  cache_file: /var/db/sneck/sneck.cache
  pid_dir: /var/run/sneck
  locking: true
  check_timeout: 60
  check_timeout_signal: TERM

RESTARTS

A restart is a command run when enough of the checks it watches fail. They only run with -r. Otherwise they are just reported.

http_check|/usr/local/libexec/nagios/check_http -H localhost
php_check|/usr/local/libexec/nagios/check_procs -C php-fpm -c 1:
@php_fpm|checks=php_check|/usr/sbin/service php_fpm restart
@httpd|checks=http_check,php_check threshold=2 depends=php_fpm cascade=1 timeout=60|/usr/sbin/service apache24 restart
restarts:
  php_fpm:
    command: /usr/sbin/service php_fpm restart
    checks: [php_check]
  httpd:
    command: /usr/sbin/service apache24 restart
    checks: [http_check, php_check]
    threshold: 2
    depends: [php_fpm]
    cascade: true
    timeout: 60

Critical always counts as failed. Ok and warning never do.

A restart that ran and failed sets .data.alert and adds a line to .data.alertString, along with any output from the command.

USAGE

snmpd just needs to print the cache. The simplest way is to cat the GZip+BASE64 compressed cache, which avoids starting perl at all.

extend sneck /bin/cat /var/cache/sneck.cache.snmp

sneck -c may be used instead. It reports a missing cache file as error JSON instead of printing nothing, but has to start perl and read the config on each poll.

extend sneck /usr/bin/env PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin /usr/local/bin/sneck -c

If the cache file is changed via cache_file or -C, cat its .snmp file instead, or give -c the same -C.

Then just setup a entry in like cron such as below.

*/5 * * * * /usr/bin/env PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin  /usr/local/bin/sneck -u 2> /dev/null > /dev/null

Most likely want to run it once per polling interval.

You can use it in a non-cached manner with out cron, but this will result in a longer polling time for LibreNMS or the like when it queries it.

RETURN HASH/JSON

The generated JSON/hash is as below in jpath notation.

For the following $name is the name of the check ran.

For the following $name is the name of the debug check ran.

For the following $name is the name of the restart. Every restart has an entry, even when restarts are disabled.

INSTALLING

FreeBSD

pkg install p5-JSON p5-JSON-XS p5-File-Slurp p5-Proc-PID-File p5-DateTime-Event-Cron p5-App-cpanminus
cpanminus Monitoring::Sneck

Debian

apt-get install libjson-perl libjson-xs-perl libfile-slurp-perl libproc-pid-file-perl libdatetime-event-cron-perl cpanminus
cpanminus Monitoring::Sneck

YAML Support

For YAML configs, also install YAML::XS.

# FreeBSD
pkg install p5-YAML-LibYAML

# Debian
apt-get install libyaml-libyaml-perl

From Src

perl Makefile.PL
make
make test
make install