From e7b180da0f0e4eab3b7c2b08ba405654baf82588 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 4 Sep 2016 13:31:05 +1200 Subject: Upgrading to latest version These are sanitized from the versions we use at work, so maintaining them is becoming a bit of a pain. I may have to work out some reasonable submodule system. --- nagios-acknowledge.1 | 7 +-- nagios-clear | 117 +++++++++++++++++++++++++++++++++++++++++++ nagios-clear.1 | 33 ++++++++++++ nagios-data-search | 43 +++++++++++++--- nagios-data-search.1 | 12 +++-- nagios-downstream-list.1 | 6 +-- nagios-downtime.1 | 7 +-- nagios-force-check.1 | 6 +-- nagios-notification-switch | 53 ++++++++++++++++++++ nagios-notification-switch.1 | 19 +++++++ nagios-problem-list.1 | 6 +-- nagios-unhandled-list | 4 +- nagios-unhandled-list.1 | 6 +-- nagscripts.bash | 35 ++++++++----- 14 files changed, 313 insertions(+), 41 deletions(-) create mode 100755 nagios-clear create mode 100644 nagios-clear.1 create mode 100755 nagios-notification-switch create mode 100644 nagios-notification-switch.1 diff --git a/nagios-acknowledge.1 b/nagios-acknowledge.1 index e48f646..568cd40 100644 --- a/nagios-acknowledge.1 +++ b/nagios-acknowledge.1 @@ -15,7 +15,7 @@ nac webhost 'DDoS I think, fixing' .br nac -n webhost/HTTP 'Apache HTTPD crashed, fixing' .br -nac authns1,authns2,recdns1,recdns2 'DNS is all buggered' +nac authdns0,authdns1,authdns2,recdns1,recdns2 'DNS is all buggered' .br npl | grep abc-example | nac - 'Power problems at abc-example' .SH DESCRIPTION @@ -37,7 +37,8 @@ parameter is optional. By default, the script won't send an ACKNOWLEDGEMENT notification. Add a -n option if you actually want to do this. .SH SEE ALSO -nagios-data-search(1), nagios-descendent-list(1), nagios-downtime(1), -nagios-force-check(1), nagios-problem-list(1), nagios-unhandled-list(1) +nagios-clear(1), nagios-data-search(1), nagios-descendent-list(1), +nagios-downtime(1), nagios-force-check(1), nagios-problem-list(1), +nagios-unhandled-list(1) .SH AUTHOR Tom Ryder diff --git a/nagios-clear b/nagios-clear new file mode 100755 index 0000000..e8a6de8 --- /dev/null +++ b/nagios-clear @@ -0,0 +1,117 @@ +#!/usr/bin/env bash + +# +# nagios-clear(1) -- Shortcut to removing all active downtime and +# acknowledgements from a host. +# +self=nagios-clear + +# Usage printing function +usage() { + printf 'USAGE: %s [|-] [comment]\n' "$self" +} + +# Process options (just help at the moment) +OPTIND=1 +while getopts 'h' opt ; do + case "$opt" in + h) + usage + exit 0 + ;; + '?') + usage >&2 + exit 1 + ;; + esac +done +shift "$((OPTIND-1))" + +# Bail if too few arguments left; we need at least an object name; the comma is +# optional +if ! (($#)) ; then + usage >&2 + exit 1 +fi + +# Define relatively fixed/guaranteed fields for Nagios command; note that the +# comment has a default of 'no comment given' +now=$(date +%s) +author=${SUDO_USER:-"$USER"} +spec=$1 +comment=${2:-'no comment given'} +cmdfile=${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd} + +# Define the path to the Livestatus socket +socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live} + +# How to get the objects depends on the spec (the first argument) +declare -a objects +case $spec in + + # If the spec is just "-", we just read unique objects from stdin + -) + while read -r object ; do + [[ $object ]] || continue + objects[${#objects[@]}]=$object + done < <(sort -u) + ;; + + # If the spec is anything else, we break it up with commas and read the + # objects that way + *) + IFS=, read -a objects -r < <(printf '%s\n' "$spec") + ;; +esac + +# There must be at least one object +if ! ((${#objects[@]})) ; then + printf '%s: At least one host/service must be given\n' \ + "$self" >&2 + exit 1 +fi + +# All the hosts or services must exist, just to be strict +for object in "${objects[@]}" ; do + nagios-exists "$object" && continue + printf '%s: Host/service %s does not seem to exist\n' \ + "$self" "$object" >&2 + exit 1 +done + +# Write commands to remove acknowledgements for each of the objects +for object in "${objects[@]}" ; do + case $object in + */*) + host=${object%/*} + service=${object##*/} + printf '[%lu] REMOVE_SVC_ACKNOWLEDGEMENT;%s;%s\n' \ + "$now" "$host" "$service" + unixcat "$socket" < "$cmdfile" diff --git a/nagios-clear.1 b/nagios-clear.1 new file mode 100644 index 0000000..80058df --- /dev/null +++ b/nagios-clear.1 @@ -0,0 +1,33 @@ +.TH NAGIOS-CLEAR 1 "Nagios Clear" "Nagscripts" +.SH NAME +.B ncl, nagios-clear +\- Remove all acknowledgements and downtimes for hosts/services +.SH USAGE +.B nagios-clear +-h +.br +.B nagios-clear +.I (HOSTNAME[/SERVICE][,HOSTNAME[,SERVICE],...]|-) +.I [COMMENT] +.SH SYNOPSIS +ncl webhost 'System upgrade was cancelled' +.br +ncl webhost/HTTP 'HTTP upgrade was rescheduled' +.br +ncl authdns0,authdns1,authdns2,recdns1,recdns2 'Turns out these problems are unexpected' +.br +nds abc-example | ncl - 'Not actually a powercut' +.SH DESCRIPTION +.B nagios-clear +will remove all acknowledgements and downtime (both active and pending) for all +of the hosts and/or services given in its first argument. +.PP +Multiple hosts or services can be separated by commas, and the special value - +allows hosts or services to be read from stdin. +.SH SEE ALSO +nagios-acknowledge(1), nagios-clear(1), nagios-data-search(1), +nagios-descendent-list(1), nagios-downtime(1), nagios-force-check(1), +nagios-notification-switch(1), nagios-problem-list(1), +nagios-unhancled-list(1) +.SH AUTHOR +Tom Ryder diff --git a/nagios-data-search b/nagios-data-search index 5be21ca..ffa0bb4 100755 --- a/nagios-data-search +++ b/nagios-data-search @@ -8,6 +8,9 @@ # $ nds abc- def- ghi- # $ nds -s WORDPRESS # +# Add -d to include a unique list of the hosts downstream of any/all of the +# matched hosts. This is incompatible with the -s option. +# # Author: Tom Ryder # Copyright: 2016 # @@ -17,35 +20,46 @@ self=nagios-data-search # Usage printing function usage() { - printf 'USAGE: %s [-hs] STRING\n' "$self" + printf 'USAGE: %s [-h | -d | -s] STRING\n' "$self" } -# By default we search hosts, not services +# By default we search hosts, not services, and don't include downstreams +downstream=0 services=0 # Handle options, just -h help at the moment OPTIND=1 -while getopts 'hs' opt ; do +while getopts 'dhs' opt ; do case "$opt" in h) usage exit 0 ;; + d) + downstream=1 + ;; s) services=1 ;; '?') usage >&2 - exit 1 + exit 2 ;; esac done shift "$((OPTIND-1))" +# There is no such thing as a downstream service (well, not really), so bail if +# the user asked for them +if ((downstream && services)) ; then + printf '%s: Cannot specify both -d and -s\n' "$self" >&2 + exit 2 +fi + # We need exactly one argument after that if ! (($#)) ; then usage >&2 - exit 1 + exit 2 fi # Define the path to the Livestatus socket @@ -69,4 +83,21 @@ Columns: host_name Filter: host_name ~ $search EOF fi -done | sort -V +done | + +# Pass output through a while-read loop that appends the output of ndl(1) for +# this host if downstream is set; otherwise, stdin just gets quietly duplicated +# by this block +while IFS= read -r host ; do + printf '%s\n' "$host" + if ((downstream)) ; then + ndl -- "$host" + fi +done | + +# Sort the output version-wise (so that foo-bar-ap-1 comes before +# foo-bar-ap-10); note that -V is not a POSIX flag (supported by GNU sort) +sort -V | + +# Ensure the output is unique +uniq diff --git a/nagios-data-search.1 b/nagios-data-search.1 index 3c1adf7..807836e 100644 --- a/nagios-data-search.1 +++ b/nagios-data-search.1 @@ -4,16 +4,22 @@ \- List hosts (or services) with names matching at least one of a set of strings .SH USAGE -.B nagios-data-search [-hs] STRING1 [STRING2 ...] +.B nagios-data-search [-d | -h | -s] STRING1 [STRING2 ...] .SH DESCRIPTION .B nagios-data-search will write to standard output the names of hosts matching any of the given arguments as a substring. .P +If -d is given, the host search will include all hosts downstream of all +matched hosts. This is intended for easy piping through ndt(1). +.P If -s is given, services with a host or service description matching any of the strings will be printed instead. +.P +The -d and -s options cannot be used together. .SH SEE ALSO -nagios-acknowledge(1), nagios-downtime(1), nagios-force-check(1), -nagios-notification-switch(1), nagios-problem-list(1), nagios-unhandled-list(1) +nagios-acknowledge(1), nagios-clear(1), nagios-downtime(1), +nagios-force-check(1), nagios-notification-switch(1), nagios-problem-list(1), +nagios-unhandled-list(1) .SH AUTHOR Tom Ryder diff --git a/nagios-downstream-list.1 b/nagios-downstream-list.1 index 9633d97..824b5f2 100644 --- a/nagios-downstream-list.1 +++ b/nagios-downstream-list.1 @@ -10,8 +10,8 @@ will write to standard output the descendent hosts of the host(s) given as the sole argument as read from a MK LiveStatus socket. .SH SEE ALSO -nagios-acknowledge(1), nagios-data-search(1), nagios-downtime(1), -nagios-force-check(1), nagios-notification-switch(1), nagios-problem-list(1), -nagios-unhandled-list(1) +nagios-acknowledge(1), nagios-clear(1), nagios-data-search(1), +nagios-downtime(1), nagios-force-check(1), nagios-notification-switch(1), +nagios-problem-list(1), nagios-unhandled-list(1) .SH AUTHOR Tom Ryder diff --git a/nagios-downtime.1 b/nagios-downtime.1 index 3581651..ae34fd0 100644 --- a/nagios-downtime.1 +++ b/nagios-downtime.1 @@ -16,7 +16,7 @@ ndt webhost now 10pm 'Upgrading to latest Ubuntu release' .PP ndt webhost/HTTP now 10pm 'Upgrading Apache HTTPD' .br -ndt authns1,authns2,recdns1,recdns2 'sunday 3:00am' 'sunday 4:00am' 'Outage for entire DNS system for upgrades' +ndt authdns0,authdns1,authdns2,recdns1,recdns2 'sunday 3:00am' 'sunday 4:00am' 'Outage for entire DNS system for upgrades' .br nds abc-example | ndt - 9am 10am 'Power problems at abc-example' .SH DESCRIPTION @@ -45,8 +45,9 @@ changes. Note that the .I COMMENT parameter is optional. .SH SEE ALSO -nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1), -nagios-force-check(1), nagios-notification-switch(1), nagios-problem-list(1), +nagios-acknowledge(1), nagios-clear(1), nagios-data-search(1), +nagios-descendent-list(1), nagios-force-check(1), +nagios-notification-switch(1), nagios-problem-list(1), nagios-unhandled-list(1) .SH AUTHOR Tom Ryder diff --git a/nagios-force-check.1 b/nagios-force-check.1 index 9836247..d3a5852 100644 --- a/nagios-force-check.1 +++ b/nagios-force-check.1 @@ -45,8 +45,8 @@ npl | while read -r object ; do .br done .SH SEE ALSO -nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1), -nagios-downtime(1), nagios-notification-switch(1), nagios-problem-list(1), -nagios-unhandled-list(1) +nagios-acknowledge(1), nagios-clear(1), nagios-data-search(1), +nagios-descendent-list(1), nagios-downtime(1), nagios-notification-switch(1), +nagios-problem-list(1), nagios-unhandled-list(1) .SH AUTHOR Tom Ryder diff --git a/nagios-notification-switch b/nagios-notification-switch new file mode 100755 index 0000000..a6c2507 --- /dev/null +++ b/nagios-notification-switch @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +# +# nagios-notification-switch(1) -- Turn notifications off or on globally. +# +# $ nns on +# $ nns off +# +# Author: Tom Ryder +# Copyright: 2016 +# + +# Name self +self=nagios-notification-switch + +# Usage printing function +usage() { + printf 'USAGE: %s [on|off]\n' "$self" +} + +# If no args, exit with usage help +if ! (($#)) ; then + usage >&2 + exit 1 +fi + +# Figure out what to do based on first argument +case $1 in + + # Give help in the form of usage information if requested + -h|--help) + usage + ;; + + # Set notifications on + on) + printf '[%lu] ENABLE_NOTIFICATIONS\n' "$(date +%s)" \ + > "${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd}" + ;; + + # Set notifications off + off) + printf '[%lu] DISABLE_NOTIFICATIONS\n' "$(date +%s)" \ + > "${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd}" + ;; + + # Didn't understand the argument; say so and dump usage to stderr + *) + printf 'Unknown argument :%s\n' "$1" + usage >&2 + exit 1 + ;; +esac diff --git a/nagios-notification-switch.1 b/nagios-notification-switch.1 new file mode 100644 index 0000000..5012846 --- /dev/null +++ b/nagios-notification-switch.1 @@ -0,0 +1,19 @@ +.TH NAGIOS-NOTIFICATION-SWITCH 1 "Nagios Notification Switch" "Nagscripts" +.SH NAME +.B nns, nagios-notification-switch +\- Set Nagios notifications off or on globally +.SH USAGE +.B nns on +.br +.B nns off +.SH DESCRIPTION +.B nagios-notification-switch +will either enable or disable ("on" or "off" as sole argument) notifications +for the running Nagios instance, writing to $NAGCMD_FILE (defaults to +/usr/local/nagios/var/rw/nagios.cmd if unset). +.SH SEE ALSO +nagios-acknowledge(1), nagios-clear(1), nagios-data-search(1), +nagios-descendent-list(1), nagios-downtime(1), nagios-force-check(1), +nagios-notification-switch(1), nagios-unhandled-list(1), nagios-problem-list(1) +.SH AUTHOR +Tom Ryder diff --git a/nagios-problem-list.1 b/nagios-problem-list.1 index 8ce7bcc..0c5eb29 100644 --- a/nagios-problem-list.1 +++ b/nagios-problem-list.1 @@ -23,8 +23,8 @@ input. It differs from in that it shows all objects in problem state, including any that are acknowledged or in downtime. .SH SEE ALSO -nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1), -nagios-downtime(1), nagios-force-check(1), nagios-notification-switch(1), -nagios-unhandled-list(1) +nagios-acknowledge(1), nagios-clear(1), nagios-data-search(1), +nagios-descendent-list(1), nagios-downtime(1), nagios-force-check(1), +nagios-notification-switch(1), nagios-unhandled-list(1) .SH AUTHOR Tom Ryder diff --git a/nagios-unhandled-list b/nagios-unhandled-list index d5d97fd..92909cc 100755 --- a/nagios-unhandled-list +++ b/nagios-unhandled-list @@ -1,8 +1,8 @@ #!/usr/bin/env bash # -# nagios-unhandled-list(1) -- List all unhandled hosts and services in a -# problem state. +# nagios-unhandled-list(1) -- List all the hosts and services in a problem state, +# handled or not # # $ npl # diff --git a/nagios-unhandled-list.1 b/nagios-unhandled-list.1 index d67eaec..411350b 100644 --- a/nagios-unhandled-list.1 +++ b/nagios-unhandled-list.1 @@ -23,8 +23,8 @@ input. It differs from in that it shows only objects in unhandled problem state, thereby excluding any that are acknowledged or in downtime. .SH SEE ALSO -nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1), -nagios-downtime(1), nagios-force-check(1), nagios-notification-switch(1), -nagios-problem-list(1) +nagios-acknowledge(1), nagios-clear(1), nagios-data-search(1), +nagios-descendent-list(1), nagios-downtime(1), nagios-force-check(1), +nagios-notification-switch(1), nagios-problem-list(1) .SH AUTHOR Tom Ryder diff --git a/nagscripts.bash b/nagscripts.bash index 5f95575..11ec4fb 100644 --- a/nagscripts.bash +++ b/nagscripts.bash @@ -1,16 +1,27 @@ -# Completion tools for Nagscripts suite -_nagscripts_objects() { - local object +# Source this file to get host and service completion for the Nagscript tools. +_nagios_objects() { + local word=${COMP_WORDS[COMP_CWORD]} + declare -a cmd + case $word in + */*) + cmd=(nagios-data-search -s ^"${word#*/}") + ;; + *) + compopt -o nospace + cmd=(nagios-data-search ^"$word") + ;; + esac while read -r object ; do - [[ $object == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue - COMPREPLY[${#COMPREPLY[@]}]=$object - done < <(cat <(nagios-data-search .) <(nagios-data-search -s .)) - return + case $object in + "$word"*) COMPREPLY[${#COMPREPLY[@]}]=$object ;; + esac + done < <("${cmd[@]}") } -complete -F _nagscripts_objects -o default \ - nagios-acknowledge \ - nagios-data-search \ +complete -F _nagios_objects -o default \ + nagios-acknowledge \ + nagios-clear \ nagios-downstream-list \ - nagios-downtime \ - nagios-exists \ + nagios-data-search \ + nagios-downtime \ + nagios-exists \ nagios-force-check -- cgit v1.2.3