Projet

Général

Profil

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

root / plugins / ssh / hostdenied @ f8801b87

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

1 0dca7e09 L.Schmidt
#!/bin/bash
2
#
3
# Plugin to monitor the number of hosts in /etc/hosts.deny
4
# that are denied access to sshd
5
6
# Copyright (C) 2010 Lothar Schmidt, l.munin@scarydevilmonastery.net
7
#                                    Bushmills on #munin, irc.freenode.net
8
#                                    latest versions on http://scarydevilmonastery.net/munin.cgi
9
#
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation, either version 3 of the License, or
13
# (at your option) any later version.
14
#
15
# This program is distributed in the hope that it will be useful,
16 17f78427 Lars Kruse
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17 0dca7e09 L.Schmidt
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU General Public License for more details.
19
#
20
# You should have received a copy of the GNU General Public License
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
#
23
# ------------------------------------------------------------------------------------------------------
24
# 20100310  v1.01  ls
25 17f78427 Lars Kruse
#               as threatened, shows now "temperatures" of active hosts.deny lines. Recent additions are
26 0dca7e09 L.Schmidt
#               displayed in bright red, turning to blue as older the addition rules are.
27
#               This requires denyhosts to add line to hosts.deny in a specific format. Also, times are currently
28
#               hardcoded, and not a lot of flexibility adjusting them through parameters.
29
#               A line in hosts.deny should come with a comment, looking like:
30
#               # DenyHosts: Sat Mar  6 01:11:57 2010 | sshd: 87.101.51.198
31
#               8 graphs are drawn from that depicting number of rules in 24 h increments. Different colours are
32 17f78427 Lars Kruse
#               assigned to graphs which are <24h, 24-48h, 48-72h ... old. The last (coldest) graph shows rules
33 0dca7e09 L.Schmidt
#               which have been added > 168h ago.
34
#               I'm considerering to change age granularity to hours, rather than days, and plot many graphs (64 or 128,
35 17f78427 Lars Kruse
#               which are nice for colour calculations), showing more of a colour cloud than discernible areas.
36 0dca7e09 L.Schmidt
#               The plugin must have permission to read /etc/hosts.deny, of course.
37 17f78427 Lars Kruse
# 20100308, v1.0, ls
38 0dca7e09 L.Schmidt
#               Will probably add multiple stacked graphs, indicative for addition/removal date of denies,
39
#               instead of a boring single area graph.
40
# ------------------------------------------------------------------------------------------------------
41
42
#%# family=manual
43
#%# capabilities=autoconf
44
45
# ------------------------------------------------------------------------------------------------------
46
DENY="/etc/hosts.deny"
47 f8801b87 Olivier Mehani
NAME="$(basename "$0")"                                        # component of naming temporary files
48 4b2fcbf8 Lars Kruse
STATEFILE="$MUNIN_PLUGSTATE/$NAME.state"
49 0dca7e09 L.Schmidt
COLOUR=(FF0000 DA0024 B60048 91006D 6D0091 4800B6 2400DA 0000FF)  # hot to cold colours
50
# ------------------------------------------------------------------------------------------------------
51
52
run_autoconf()  {
53 f8801b87 Olivier Mehani
        RUN="no (denyhosts not found)"
54
        command -v denyhosts > /dev/null && RUN="yes"             # only run when denyhosts is present
55 0dca7e09 L.Schmidt
        echo "$RUN"
56
}
57
58
59
run_config()  {
60 f8801b87 Olivier Mehani
	cat << EOF
61 e926acaf Olivier Mehani
graph_title Hosts denied sshd access
62
graph_info Hosts denied sshd access in $DENY
63 0dca7e09 L.Schmidt
graph_args --base 1000 -l 0
64
graph_vlabel Hosts denied
65 6aa977b2 dipohl
graph_category security
66 0dca7e09 L.Schmidt
EOF
67 f8801b87 Olivier Mehani
for AGE in {7..0}; do
68
	if [ "${AGE}" = 0 ]; then
69
		echo "age${AGE}.label added last 24h"
70
	else
71
		echo "age${AGE}.label older than $((AGE*24))h"
72
	fi
73
	cat << EOF
74
age${AGE}.draw AREASTACK
75 0dca7e09 L.Schmidt
age${AGE}.colour ${COLOUR[$AGE]}
76
EOF
77
done
78
}
79
80
81
run_fetch()   {
82
   TOTAL=0
83
   NOW=$(date +%s)
84
   sed -n 's/^\# DenyHosts: //;s/ | .*//gp' $DENY |         # strip all but date
85 f8801b87 Olivier Mehani
   while read -r DATE; do
86 0dca7e09 L.Schmidt
      echo $(((NOW - $(date -d "$DATE" +%s))/86400))        # calculate rule age
87 f8801b87 Olivier Mehani
   done > "$STATEFILE"                                      # rather than going through temp file, the age could be
88
   for AGE in {6..0} ; do                                   # used to increment an array element with that index.
89
      COUNT="$(grep -c "^$AGE$" "$STATEFILE")"              # That'd save grepping for counting from temp file.
90 0dca7e09 L.Schmidt
      echo "age${AGE}.value $COUNT"                         # produce values for all but oldest
91
      ((TOTAL+=COUNT))
92
   done
93 f8801b87 Olivier Mehani
   echo "age7.value $(($(grep -c . "$STATEFILE")-TOTAL))"   # all non-printed are older
94
   rm "$STATEFILE"
95 0dca7e09 L.Schmidt
}
96
97 f8801b87 Olivier Mehani
run_"${1:-fetch}"
98 0dca7e09 L.Schmidt
exit 0