Projet

Général

Profil

Révision 4ce9e742

ID4ce9e742515fb38dc5045a7aaa9c99bac7a62dd1
Parent 254eb56d
Enfant c0108c95

Ajouté par Olivier Mehani il y a environ 5 ans

[whois_] Plugin to monitor domain registration expiries

This plugin only monitors one domain per instance, but should be easily
extensible to monitor several at once if need be.

Signed-off-by: Olivier Mehani <>

Voir les différences:

plugins/network/whois_
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

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

  
37
=head1 AUTHOR
38

  
39
Olivier Mehani
40

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

  
43
=head1 LICENSE
44

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

  
47
=head1 MAGIC MARKERS
48

  
49
 #%# family=manual
50

  
51
=cut
52

  
53
set -eu
54

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

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

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

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

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

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

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

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

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

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

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

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

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

  
116
	cat "${CACHEFILE}"
117
}
118

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

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

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

  
136
main "$@"

Formats disponibles : Unified diff