Projet

Général

Profil

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

root / plugins / network / ldap_connections @ 17f78427

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

1
#!/bin/sh
2
#
3
# Plugin to monitor the number of open connexions to LDAP
4
#
5
# $Log: ldap_connections,v $
6
# Revision 1.9  2008/05/20 21:30:34  cvserver
7
# Corrections de bugs
8
#
9
# Revision 1.8  2007/09/03 09:35:37  cvserver
10
# Correction2 pour OpenBSD
11
#
12
# Revision 1.7  2007/09/03 09:01:07  cvserver
13
# Modif pour OpenBSD
14
#
15
# Revision 1.6  2007/03/02 07:52:52  cvserver
16
# pas LISTEN pour les IPs utilis?es (en cas de *:389)
17
#
18
# Revision 1.5  2007/03/01 16:06:53  cvserver
19
# corrections:
20
#  - reinitialisation de $IPS_BOUND au debut de la fonction find_ip_bound
21
#  - precision dans le grep (LISTEN)
22
#
23
# Revision 1.4  2006/09/27 11:56:54  cvserver
24
# + sockets
25
#
26
# Revision 1.3  2006/06/24 23:38:30  cvserver
27
# correction
28
#
29
# Revision 1.2  2006/06/24 23:24:30  cvserver
30
# correction
31
#
32
# Revision 1.1  2006/06/24 23:15:25  cvserver
33
# connexions LDAP pour munin
34
#
35
#
36
# plugin-conf.d/-options:
37
#
38
#       netstat         -- path to netstat executable
39
#       ports           -- ldap ports used (389 and 636)
40
#			   only used ones are graphed
41
#	socket		-- ldapi socket (default: /var/run/openldap/ldapi)
42
#
43
# Parameters:
44
#
45
# 	config   (required)
46
# 	autoconf (optional - used by munin-config)
47
#
48
# Magic markers (Used by munin-config and some installation scripts.
49
# Optional):
50
#
51
#%# family=contrib
52
#%# capabilities=autoconf
53

    
54
NETSTAT=${netstat:-`which netstat`}
55
NETSTAT=${NETSTAT:-/usr/bin/netstat}
56
PORTS=${ports:-389 636}
57
TEMP_FILE=$(mktemp /tmp/munin_ldap.XXXXXX)
58
trap "rm -f ${TEMP_FILE}" EXIT
59
PATH=/bin:/usr/bin:/usr/local/bin
60
SOCKET=${socket:-/var/run/openldap/ldapi}
61

    
62
case $(uname -s) in
63
  *BSD)
64
    NETSTAT_ARGS="-an -ptcp"
65
    FAMILYMARK="-f "
66
    ;;
67
  Linux)
68
    NETSTAT_ARGS="-alnt"
69
    FAMILYMARK="--"
70
    ;;
71
  *)
72
    NETSTAT_ARGS="-an"
73
    FAMILYMARK="-f "
74
    ;;
75
esac
76

    
77
$NETSTAT $NETSTAT_ARGS > $TEMP_FILE
78

    
79
# arg: port
80
find_ips_bound() {
81
  port=$1
82
  IPS_BOUND=""
83
  for i in $(grep "^tcp[46]\{0,1\}\([[:space:]]\{1,\}[[:digit:]]\{1,\}\)\{2\}[[:space:]]\{1,\}\(\([0-9]\)\{1,3\}\.\)\{3\}[0-9]\{1,3\}[\.:]$port[[:space:]].*" $TEMP_FILE | awk '{print $4}' | sed "s/^\(.*\)[\.:]$port$/\1/"); do
84
    echo $IPS_BOUND | grep "$i" > /dev/null || IPS_BOUND=$IPS_BOUND" $i"
85
  done
86
  echo $IPS_BOUND
87
}
88

    
89
# see which port(s) is/are really bound
90
LISTENING_PORTS=""
91
for port in $PORTS; do
92
  find_ips_bound $port > /dev/null && LISTENING_PORTS="$LISTENING_PORTS$port "
93
done
94

    
95
if [ "$1" = "autoconf" ]; then
96
  ONE_LISTENING=""
97
  for port in $PORTS; do
98
    ONE_LISTENING=${ONE_LISTENING}$(find_ips_bound $port)
99
  done
100

    
101
  if [ -n "$ONE_LISTENING" ]; then
102
  	echo yes
103
  	exit 0
104
  else
105
  	echo no '(no slapd listening on '$PORTS')'
106
  	exit 1
107
  fi
108
fi
109

    
110
if [ "$1" = "config" ]; then
111
  echo 'graph_title LDAP connections'
112
  echo 'graph_args -l 0'
113
  echo 'graph_vlabel active connections to ldap by port'
114
  echo 'graph_category network'
115
  for port in $LISTENING_PORTS; do
116
    for ip in $(find_ips_bound $port | sed 's/\./_/g'); do
117
      echo "${ip}_${port}.label ${ip}:${port}"
118
    done
119
  done
120
  if [ -e $SOCKET ]; then
121
    if [ $($NETSTAT -an ${FAMILYMARK}unix | grep $SOCKET | wc -l) -gt 0 ]; then
122
      echo "socket.label ldapi"
123
    fi
124
  fi
125

    
126
  exit 0
127
fi
128

    
129
for port in $LISTENING_PORTS; do
130
  for ip in $(find_ips_bound $port); do
131

    
132
    label=$(printf "%s_%d" "$(echo $ip | tr ':.' '_')" "$port")
133
    connections=$(
134
      awk -v ip_port="${ip}:${port}" \
135
        'BEGIN { counter=0 }
136
         $1 ~ /tcp[46]?/ && $4 == ip_port && $6 == "ESTABLISHED" { counter++ }
137
         END { print counter }' \
138
        $TEMP_FILE
139
    )
140

    
141
    printf "%s.value %d\n" "$label" "$connections"
142
  done
143
done
144
if [ -e "$SOCKET" ]; then
145
  echo "socket.value $($NETSTAT -an ${FAMILYMARK}unix | grep $SOCKET | wc -l | sed 's/[[:space:]]*//g')"
146
fi
147