Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / whois @ 63505c50

Historique | Voir | Annoter | Télécharger (3,07 ko)

1
#!/bin/sh
2
# shellcheck shell=dash
3
# -*- sh -*-
4

    
5
: << =cut
6

    
7
=head1 NAME
8

    
9
whois - Plugin to monitor expiry dates of domain in the WHOIS database
10

    
11
Though the plugin will be called every 5 minutes, it will keep a cache, and
12
only query the WHOIS database C<cache_expiry_mins> minutes.
13

    
14
=head1 CONFIGURATION
15

    
16
First, create a section in your munin-node configuration files. The domain envvar
17
is mandatory, others are optional.
18

    
19
	[whois]
20
	env.domains example.com example.org
21
	env.extract_re s/PATTERN/REPL/
22
	env.warning_days <default: 7 days>
23
	env.critical_days <default: 3 days>
24
	env.cache_expiry_mins <default: 60 minutes>
25

    
26

    
27
C<domains> is a space-separated list of domains to be checked.
28
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
Then create a symlink to enable this plugin:
36

    
37
	ln -s /path/to/whois /etc/munin/plugins/
38

    
39

    
40
=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
# shellcheck disable=SC1091
59
. "${MUNIN_LIBDIR}/plugins/plugin.sh"
60

    
61
if [ "${MUNIN_DEBUG:-0}" = 1 ]; then
62
	set -x
63
fi
64

    
65
DOMAINS=${domains:-""}
66
EXTRACT_RE=${extract_re:-'s/^.*[Ee]xpir.*: //'}
67
WARNING=${warning_days:-7}
68
CRITICAL=${critical_days:-3}
69
CACHE_EXPIRY=${cache_expiry_mins:-60}
70

    
71
# Args: domain name (optional, for title)
72
graph_config() {
73
	cat << EOF
74
graph_title Domain registration expiry
75
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
	local NAME
84
	local FIELDNAME
85
	for NAME in $DOMAINS
86
	do
87
		FIELDNAME="$(clean_fieldname "${NAME}")"
88

    
89
		cat << EOF
90
${FIELDNAME}.label ${NAME}
91
${FIELDNAME}.cdef ${FIELDNAME},86400,/
92
${FIELDNAME}.warning ${WARNING}:
93
${FIELDNAME}.critical ${CRITICAL}:
94
EOF
95
	done
96
}
97

    
98
# Args: domain name
99
fetch() {
100
	local NAME
101
	local FIELDNAME
102
	local CACHEFILE
103

    
104
	for NAME in $DOMAINS
105
	do
106
		FIELDNAME="$(clean_fieldname "${NAME}")"
107

    
108
		CACHEFILE="${MUNIN_PLUGSTATE}/$(basename "${0}").${FIELDNAME}.cache"
109

    
110
		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

    
116
				NOW_TS="$(date +%s)"
117
				DELTA_TS=$((EXPIRY_TS-NOW_TS))
118
			fi
119

    
120
			echo "${FIELDNAME}.value ${DELTA_TS}" > "${CACHEFILE}"
121
		fi
122

    
123
		cat "${CACHEFILE}"
124
	done
125
}
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 "$@"