root / plugins / network / whois @ 7046ba80
Historique | Voir | Annoter | Télécharger (3,07 ko)
| 1 | 4ce9e742 | Olivier Mehani | #!/bin/sh |
|---|---|---|---|
| 2 | # shellcheck shell=dash |
||
| 3 | # -*- sh -*- |
||
| 4 | |||
| 5 | : << =cut |
||
| 6 | |||
| 7 | =head1 NAME |
||
| 8 | |||
| 9 | 7046ba80 | ruliane | whois - Plugin to monitor expiry dates of domains in the WHOIS database |
| 10 | 4ce9e742 | Olivier Mehani | |
| 11 | Though the plugin will be called every 5 minutes, it will keep a cache, and |
||
| 12 | 63505c50 | ruliane | only query the WHOIS database C<cache_expiry_mins> minutes. |
| 13 | 4ce9e742 | Olivier Mehani | |
| 14 | =head1 CONFIGURATION |
||
| 15 | |||
| 16 | 63505c50 | ruliane | First, create a section in your munin-node configuration files. The domain envvar |
| 17 | is mandatory, others are optional. |
||
| 18 | 4ce9e742 | Olivier Mehani | |
| 19 | 63505c50 | ruliane | [whois] |
| 20 | env.domains example.com example.org |
||
| 21 | 4ce9e742 | Olivier Mehani | env.extract_re s/PATTERN/REPL/ |
| 22 | env.warning_days <default: 7 days> |
||
| 23 | env.critical_days <default: 3 days> |
||
| 24 | f06ee36a | ruliane | env.cache_expiry_mins <default: 60 minutes> |
| 25 | 4ce9e742 | Olivier Mehani | |
| 26 | 63505c50 | ruliane | |
| 27 | C<domains> is a space-separated list of domains to be checked. |
||
| 28 | 4ce9e742 | Olivier Mehani | The C<extract_re> will be used in C<sed> to extract the relevant expiry date. It |
| 29 | default to C<s/^.*[Ee]xpir.*: //>. Only lines for which a replacement has |
||
| 30 | happened will be considered, and the pattern should take care to only output |
||
| 31 | one line. While the default RegExp just finds a leader pattern and removes it, |
||
| 32 | it is possible to write more complex logic to format the date. The extracted |
||
| 33 | date needs to be in a format supported by C<date(1)>'s C<--date> parameter. |
||
| 34 | |||
| 35 | 63505c50 | ruliane | Then create a symlink to enable this plugin: |
| 36 | |||
| 37 | ln -s /path/to/whois /etc/munin/plugins/ |
||
| 38 | |||
| 39 | |||
| 40 | 4ce9e742 | Olivier Mehani | =head1 AUTHOR |
| 41 | |||
| 42 | Olivier Mehani |
||
| 43 | |||
| 44 | Copyright (C) 2020 Olivier Mehani <shtrom+munin@ssji.net> |
||
| 45 | |||
| 46 | =head1 LICENSE |
||
| 47 | |||
| 48 | SPDX-License-Identifier: GPL-3.0-or-later |
||
| 49 | |||
| 50 | =head1 MAGIC MARKERS |
||
| 51 | |||
| 52 | #%# family=manual |
||
| 53 | |||
| 54 | =cut |
||
| 55 | |||
| 56 | set -eu |
||
| 57 | |||
| 58 | 63505c50 | ruliane | # shellcheck disable=SC1091 |
| 59 | 4ce9e742 | Olivier Mehani | . "${MUNIN_LIBDIR}/plugins/plugin.sh"
|
| 60 | |||
| 61 | if [ "${MUNIN_DEBUG:-0}" = 1 ]; then
|
||
| 62 | set -x |
||
| 63 | fi |
||
| 64 | |||
| 65 | 63505c50 | ruliane | DOMAINS=${domains:-""}
|
| 66 | 4ce9e742 | Olivier Mehani | EXTRACT_RE=${extract_re:-'s/^.*[Ee]xpir.*: //'}
|
| 67 | WARNING=${warning_days:-7}
|
||
| 68 | CRITICAL=${critical_days:-3}
|
||
| 69 | f06ee36a | ruliane | CACHE_EXPIRY=${cache_expiry_mins:-60}
|
| 70 | 4ce9e742 | Olivier Mehani | |
| 71 | # Args: domain name (optional, for title) |
||
| 72 | graph_config() {
|
||
| 73 | cat << EOF |
||
| 74 | 63505c50 | ruliane | graph_title Domain registration expiry |
| 75 | 4ce9e742 | Olivier Mehani | graph_category network |
| 76 | graph_vlabel days |
||
| 77 | EOF |
||
| 78 | } |
||
| 79 | |||
| 80 | # Args: domain name |
||
| 81 | # Separated from graph_config so we can easily extend the plugin to support multiple domains. |
||
| 82 | config() {
|
||
| 83 | 63505c50 | ruliane | local NAME |
| 84 | 4ce9e742 | Olivier Mehani | local FIELDNAME |
| 85 | 63505c50 | ruliane | for NAME in $DOMAINS |
| 86 | do |
||
| 87 | FIELDNAME="$(clean_fieldname "${NAME}")"
|
||
| 88 | 4ce9e742 | Olivier Mehani | |
| 89 | 63505c50 | ruliane | cat << EOF |
| 90 | ${FIELDNAME}.label ${NAME}
|
||
| 91 | 4ce9e742 | Olivier Mehani | ${FIELDNAME}.cdef ${FIELDNAME},86400,/
|
| 92 | ${FIELDNAME}.warning ${WARNING}:
|
||
| 93 | ${FIELDNAME}.critical ${CRITICAL}:
|
||
| 94 | EOF |
||
| 95 | 63505c50 | ruliane | done |
| 96 | 4ce9e742 | Olivier Mehani | } |
| 97 | |||
| 98 | # Args: domain name |
||
| 99 | fetch() {
|
||
| 100 | 63505c50 | ruliane | local NAME |
| 101 | 4ce9e742 | Olivier Mehani | local FIELDNAME |
| 102 | local CACHEFILE |
||
| 103 | |||
| 104 | 63505c50 | ruliane | for NAME in $DOMAINS |
| 105 | do |
||
| 106 | FIELDNAME="$(clean_fieldname "${NAME}")"
|
||
| 107 | 4ce9e742 | Olivier Mehani | |
| 108 | 63505c50 | ruliane | CACHEFILE="${MUNIN_PLUGSTATE}/$(basename "${0}").${FIELDNAME}.cache"
|
| 109 | 4ce9e742 | Olivier Mehani | |
| 110 | 63505c50 | ruliane | if [ -z "$(find "${CACHEFILE}" -mmin -"${CACHE_EXPIRY}" 2>/dev/null)" ]; then
|
| 111 | EXPIRY="$(whois "${NAME}" 2>/dev/null | sed -n "${EXTRACT_RE}p;T;q")" # T;q exits after printing the first match
|
||
| 112 | DELTA_TS=U |
||
| 113 | if [ -n "${EXPIRY}" ]; then
|
||
| 114 | EXPIRY_TS="$(date +%s -d "${EXPIRY}")"
|
||
| 115 | 4ce9e742 | Olivier Mehani | |
| 116 | 63505c50 | ruliane | NOW_TS="$(date +%s)" |
| 117 | DELTA_TS=$((EXPIRY_TS-NOW_TS)) |
||
| 118 | fi |
||
| 119 | 4ce9e742 | Olivier Mehani | |
| 120 | 63505c50 | ruliane | echo "${FIELDNAME}.value ${DELTA_TS}" > "${CACHEFILE}"
|
| 121 | fi |
||
| 122 | 4ce9e742 | Olivier Mehani | |
| 123 | 63505c50 | ruliane | cat "${CACHEFILE}"
|
| 124 | done |
||
| 125 | 4ce9e742 | Olivier Mehani | } |
| 126 | |||
| 127 | main() {
|
||
| 128 | local MODE="${1:-}"
|
||
| 129 | local NAME |
||
| 130 | |||
| 131 | NAME="$(echo "${0}" | sed 's/.*_//')"
|
||
| 132 | |||
| 133 | case "${MODE}" in
|
||
| 134 | 'config') |
||
| 135 | graph_config "${NAME}"
|
||
| 136 | config "${NAME}"
|
||
| 137 | ;; |
||
| 138 | *) |
||
| 139 | fetch "${NAME}"
|
||
| 140 | ;; |
||
| 141 | esac |
||
| 142 | } |
||
| 143 | |||
| 144 | main "$@" |
