Projet

Général

Profil

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

root / plugins / network / whois @ f2261ed9

Historique | Voir | Annoter | Télécharger (2,92 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 every hour.
13

    
14
=head1 CONFIGURATION
15

    
16
None is needed, just
17

    
18
	ln -s /path/to/whois_ whois_domain.tld
19

    
20
in Munin's installed plugins directory will suffice to start monitoring
21
C<domain.tld>.
22

    
23
However, a few parameters, all optional, can be set if needed
24

    
25
	[whois_domain.tld]
26
	env.extract_re s/PATTERN/REPL/
27
	env.warning_days <default: 7 days>
28
	env.critical_days <default: 3 days>
29
	env.cache_expiry_mins <default: 60 minutes>
30

    
31
The C<extract_re> will be used in C<sed> to extract the relevant expiry date. It
32
default to C<s/^.*[Ee]xpir.*: //>. Only lines for which a replacement has
33
happened will be considered, and the pattern should take care to only output
34
one line. While the default RegExp just finds a leader pattern and removes it,
35
it is possible to write more complex logic to format the date. The extracted
36
date needs to be in a format supported by C<date(1)>'s C<--date> parameter.
37

    
38
=head1 AUTHOR
39

    
40
Olivier Mehani
41

    
42
Copyright (C) 2020 Olivier Mehani <shtrom+munin@ssji.net>
43

    
44
=head1 LICENSE
45

    
46
SPDX-License-Identifier: GPL-3.0-or-later
47

    
48
=head1 MAGIC MARKERS
49

    
50
 #%# family=manual
51

    
52
=cut
53

    
54
set -eu
55

    
56
# shellcheck disable=SC1090
57
. "${MUNIN_LIBDIR}/plugins/plugin.sh"
58

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

    
63
EXTRACT_RE=${extract_re:-'s/^.*[Ee]xpir.*: //'}
64
WARNING=${warning_days:-7}
65
CRITICAL=${critical_days:-3}
66
CACHE_EXPIRY=${cache_expiry_mins:-60}
67

    
68
# Args: domain name (optional, for title)
69
graph_config() {
70
	NAMETITLE=${1:+ for ${1}}
71
	cat << EOF
72
graph_title Domain registration expiry${NAMETITLE}
73
graph_category network
74
graph_vlabel days
75
EOF
76
}
77

    
78
# Args: domain name
79
# Separated from graph_config so we can easily extend the plugin to support multiple domains.
80
config() {
81
	local NAME=${1}
82
	local FIELDNAME
83

    
84
	FIELDNAME="$(clean_fieldname "${NAME}")"
85

    
86
	cat << EOF
87
${FIELDNAME}.label expiry
88
${FIELDNAME}.cdef ${FIELDNAME},86400,/
89
${FIELDNAME}.warning ${WARNING}:
90
${FIELDNAME}.critical ${CRITICAL}:
91
EOF
92
}
93

    
94
# Args: domain name
95
fetch() {
96
	local NAME=${1}
97
	local FIELDNAME
98
	local CACHEFILE
99

    
100
	FIELDNAME="$(clean_fieldname "${NAME}")"
101

    
102
	CACHEFILE="${MUNIN_PLUGSTATE}/$(basename "${0}").${FIELDNAME}.cache"
103

    
104
	if [ -z "$(find "${CACHEFILE}" -mmin -"${CACHE_EXPIRY}" 2>/dev/null)" ]; then
105
		EXPIRY="$(whois "${NAME}" 2>/dev/null | sed -n "${EXTRACT_RE}p;T;q")" # T;q exits after printing the first match
106
		DELTA_TS=U
107
		if [ -n "${EXPIRY}" ]; then
108
			EXPIRY_TS="$(date +%s -d "${EXPIRY}")"
109

    
110
			NOW_TS="$(date +%s)"
111
			DELTA_TS=$((EXPIRY_TS-NOW_TS))
112
		fi
113

    
114
		echo "${FIELDNAME}.value ${DELTA_TS}" > "${CACHEFILE}"
115
	fi
116

    
117
	cat "${CACHEFILE}"
118
}
119

    
120
main() {
121
	local MODE="${1:-}"
122
	local NAME
123

    
124
	NAME="$(echo "${0}" | sed 's/.*_//')"
125

    
126
	case "${MODE}" in
127
		'config')
128
			graph_config "${NAME}"
129
			config "${NAME}"
130
			;;
131
		*)
132
			fetch "${NAME}"
133
			;;
134
	esac
135
}
136

    
137
main "$@"