Projet

Général

Profil

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

root / plugins / network / whois @ 02451d8f

Historique | Voir | Annoter | Télécharger (3,38 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 domains 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
17
envvar is mandatory, others are optional. Optionally, a specific WHOIS server
18
to query for a given domain can be specified with the @WHOIS_SERVER suffix
19

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

    
27

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

    
36
Then create a symlink to enable this plugin:
37

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

    
40

    
41
=head1 AUTHOR
42

    
43
Olivier Mehani
44

    
45
Copyright (C) 2020,2021 Olivier Mehani <shtrom+munin@ssji.net>
46

    
47
=head1 LICENSE
48

    
49
SPDX-License-Identifier: GPL-3.0-or-later
50

    
51
=head1 MAGIC MARKERS
52

    
53
 #%# family=manual
54

    
55
=cut
56

    
57
set -eu
58

    
59
# shellcheck disable=SC1091
60
. "${MUNIN_LIBDIR}/plugins/plugin.sh"
61

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

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

    
72
# Args: domain name (optional, for title)
73
graph_config() {
74
	cat << EOF
75
graph_title Domain registration expiry
76
graph_category network
77
graph_vlabel days
78
graph_args --units-exponent 0
79
EOF
80
}
81

    
82
# Args: domain name
83
# Separated from graph_config so we can easily extend the plugin to support multiple domains.
84
config() {
85
	local NAME
86
	local FIELDNAME
87
	for NAME in $DOMAINS
88
	do
89
		NAME="$(echo "${NAME}" | cut -d @ -f 1)"
90

    
91
		FIELDNAME="$(clean_fieldname "${NAME}")"
92

    
93
		cat << EOF
94
${FIELDNAME}.label ${NAME}
95
${FIELDNAME}.cdef ${FIELDNAME},86400,/
96
${FIELDNAME}.warning ${WARNING}:
97
${FIELDNAME}.critical ${CRITICAL}:
98
EOF
99
	done
100
}
101

    
102
# Args: domain name
103
fetch() {
104
	local NAME
105
	local SERVER
106
	local FIELDNAME
107
	local CACHEFILE
108

    
109
	for NAME in $DOMAINS
110
	do
111
		SERVER=''
112
		if echo "${NAME}" | grep -q '@'; then
113
			SERVER="$(echo "${NAME}" | cut -d @ -f 2)"
114
			NAME="$(echo "${NAME}" | cut -d @ -f 1)"
115
		fi
116

    
117
		FIELDNAME="$(clean_fieldname "${NAME}")"
118

    
119
		CACHEFILE="${MUNIN_PLUGSTATE}/$(basename "${0}").${FIELDNAME}.cache"
120

    
121
		if [ -z "$(find "${CACHEFILE}" -mmin -"${CACHE_EXPIRY}" 2>/dev/null)" ]; then
122
			EXPIRY="$(whois "${NAME}" "${SERVER:+-h${SERVER}}" 2>/dev/null | sed -n "${EXTRACT_RE}p;T;q")" # T;q exits after printing the first match
123
			DELTA_TS=U
124
			if [ -n "${EXPIRY}" ]; then
125
				EXPIRY_TS="$(date +%s -d "${EXPIRY}")"
126

    
127
				NOW_TS="$(date +%s)"
128
				DELTA_TS=$((EXPIRY_TS-NOW_TS))
129
			fi
130

    
131
			echo "${FIELDNAME}.value ${DELTA_TS}" > "${CACHEFILE}"
132
		fi
133

    
134
		cat "${CACHEFILE}"
135
	done
136
}
137

    
138
main() {
139
	local MODE="${1:-}"
140

    
141
	case "${MODE}" in
142
		'config')
143
			graph_config
144
			config
145
			;;
146
		*)
147
			fetch
148
			;;
149
	esac
150
}
151

    
152
main "$@"