Projet

Général

Profil

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

root / plugins / other / pmta_ @ e5ecd263

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

1
#!/bin/sh
2
#######################################################################################################################
3
#
4
#  =  PowerMTA Munin multi-monitor  =
5
#
6
#  -- developer:	aaron.zauner@emarsys.com
7
#  -- start date:	11.11.2011
8
#
9
#
10
#  =  LICENSE  =
11
#
12
#  Copyright (c) 2011, emarsys eMarketing Systems AG
13
#  All rights reserved.
14
#
15
#  Redistribution and use in source and binary forms, with or without modification, are permitted provided that  
16
#  the following conditions are met:
17
#
18
#      Redistributions of source code must retain the above copyright notice, this list of conditions and the  
19
#      following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list  
20
#      of conditions and the following disclaimer in the documentation and/or other materials provided with the  
21
#      distribution. Neither the name of the emarsys eMarketing Systems AG nor the names of its contributors may  
22
#      be used to endorse or promote products derived from this software without specific prior written permission.
23
#
24
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
25
#  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
26
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
28
#  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
29
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  
30
#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
#
32
#
33
#######################################################################################################################
34
#
35
#  -> "xpath" cli tool required (ships with your linux distribution)
36
#  -> install:
37
#
38
#		o check config (/etc/munin/plugin-conf.d) set "env.REMOTEHOST" and "env.PORT", e.g.:
39
#		  [pmta*]
40
#			env.REMOTEHOST 127.0.0.1
41
#			env.PORT 8080
42
#
43
#		o chmod +x pmta_
44
#
45
#		o link:
46
#		  lrwxrwxrwx 1 root root    5 2011-11-14 14:52 pmta_traffic_in -> pmta_
47
#		  lrwxrwxrwx 1 root root    5 2011-11-14 14:52 pmta_traffic_out -> pmta_
48
#		  lrwxrwxrwx 1 root root    5 2011-11-14 14:52 pmta_conn_in -> pmta_
49
#		  lrwxrwxrwx 1 root root    5 2011-11-14 14:52 pmta_conn_out -> pmta_
50
#		  lrwxrwxrwx 1 root root    5 2011-11-14 14:52 pmta_queue_domains -> pmta_
51
#		  lrwxrwxrwx 1 root root    5 2011-11-14 14:52 pmta_queue_recipients -> pmta_
52
#		  lrwxrwxrwx 1 root root    5 2011-11-14 14:52 pmta_queue_megabytes -> pmta_
53
#		  lrwxrwxrwx 1 root root    5 2011-11-15 16:31 pmta_top_domains -> pmta_
54
#
55
#		o alternative multi-host configuration:
56
#		   you can also link the pmta_ script to various hosts and configure munin to respond 
57
#		   to multiple REMOTEHOSTs, just link pmta_ in this schema and configure munin
58
#		   accordingly - DO NOT USE DOTS IN YOUR IDENTIFIER VARIABLE, USE UNDERSCORE INSTEAD!
59
#		   for example:
60
#			lrwxrwxrwx 1 root root    5 2011-11-18 12:18 mailhost1_domain_com_pmta_top_domains -> pmta_
61
#			lrwxrwxrwx 1 root root    5 2011-11-18 12:18 mailhost2_domain_com_pmta_top_domains -> pmta_
62
#			lrwxrwxrwx 1 root root    5 2011-11-18 12:18 mailhost3_domain_com_pmta_top_domains -> pmta_
63
#			lrwxrwxrwx 1 root root    5 2011-11-18 12:18 mailhost4_domain_com_pmta_top_domains -> pmta_
64
#		  [...]
65
#
66
#		  conf.: 
67
#		   [mailhost1_domain_com_pmta*]
68
#				env.REMOTEHOST somehostORip
69
#				env.PORT portnumber
70
#		   [mailhost2_domain_com_pmta*]
71
#				env.REMOTEHOST someotherhostORip
72
#				env.PORT portnumber
73
#		  [...]
74
#
75
#		o restart munin.
76
#
77
###################################################################################################################
78
# debug (munin-run --pidebug --debug <script>):
79
if [ "$MUNIN_DEBUG" = "1" ]; then
80
	set -vx
81
fi
82

    
83
NAME_SELF=`basename ${0}`
84
trap 'echo "\n$NAME_SELF - >> interrupt. \n$NAME_SELF - >> exiting."' 1 2 9 11 15
85

    
86
if [ -z "${REMOTEHOST}" ] || [ -z "${PORT}" ]; then
87
	echo "\n$NAME_SELF - >> no enviroment variable set for host and/or port. \n$NAME_SELF - >> exiting."
88
	exit 1
89
fi
90

    
91
fetch_xml() {
92
	#  o arg1 specifies xpath or false
93
	#  o arg2 specifies sed/regex or false
94
	#  o arg3 specifies category ('status', 'domains',..) or false	
95
	HTTPQUERY="GET /${3}?format=xml HTTP/1.1\n\n"
96
	REMOTE=`echo ${HTTPQUERY} | nc -q 1 -w 5 ${REMOTEHOST} ${PORT} | tail -1`
97
	XML_DATA=${REMOTE}
98
	SED="s/.*<$2>\([^<]*\)<\/$2>.*/\1/p"	
99
	
100
	if [ "$3" = "status" ]; then
101
		RET=`echo $XML_DATA | xpath -q -e $1 | sed -n $SED | cut -f1 -d '.'`
102
	elif [ "$3" = "domains" ]; then
103
		RET=`echo $XML_DATA | xpath -q -e '//domain/*[self::name or self::rcp]' | sed 's/<[^>]*>//g' | sed 's/\./_/g'`
104
	fi	
105
	
106
	if [ "$RET" ]; then
107
		echo $RET
108
		return 0
109
	else
110
		echo 'no_result'
111
		return 1
112
	fi
113
}
114

    
115
case $NAME_SELF in
116
	*pmta_traffic_in | *pmta_traffic_out)
117
		if [ `expr match "$NAME_SELF" ".*pmta_traffic_in"` != 0 ]; then
118
			WHAT="in"
119
		elif [ `expr match "$NAME_SELF" ".*pmta_traffic_out"` != 0 ]; then
120
			WHAT="out"
121
		fi
122
		CONF_TITLE="powermta ${WHAT}bound traffic"
123
		CONF_LABEL="traffic_${WHAT}"
124
		CONF_SELF="traffic_${WHAT}bound"
125
		
126
		if [ -z $1 ]; then			
127
			GET=`fetch_xml //traffic//lastMin/${WHAT} kb status`  
128
			RETURN=`echo $GET / 1024 | bc`
129
			RCPT=`fetch_xml //traffic//lastMin/${WHAT} rcp status`  
130
			MSGS=`fetch_xml //traffic//lastMin/${WHAT} msg status`  
131
			echo "megabytes.value $RETURN"
132
			echo "recipients.value $RCPT"
133
			echo "messages.value $MSGS"			
134
			exit 0
135
		elif [ $1 = config ]; then
136
			echo "megabytes.label megabytes"
137
			echo "megabytes.draw AREA"
138
			echo "megabytes.colour 0000A0"
139
			echo "recipients.label recipients"
140
			echo "recipients.draw STACK"
141
			echo "recipients.colour FF0000"
142
			echo "messages.label messages"
143
			echo "messages.draw STACK"
144
			echo "messages.colour FF6F00"
145
			AUTOLABEL="no"
146
		fi
147
	;;
148
	*pmta_conn_in | *pmta_conn_out)
149
		if [ `expr match "$NAME_SELF" ".*pmta_conn_in"` != 0 ]; then
150
			WHAT="In"
151
		elif [ `expr match "$NAME_SELF" ".*pmta_conn_out"` != 0 ]; then
152
			WHAT="Out"
153
		fi
154
		CONF_TITLE=`echo "powermta ${WHAT}bound connections" | tr [:upper:] [:lower:]`
155
		CONF_LABEL=`echo "conn_${WHAT}" | tr [:upper:] [:lower:]`
156
		CONF_SELF=`echo "connections_${WHAT}bound" | tr [:upper:] [:lower:]`
157
		if [ -z $1 ]; then			
158
			GET=`fetch_xml //conn//smtp${WHAT} cur status`  
159
			echo "${CONF_SELF}.value $GET"
160
			exit 0 
161
		elif [ $1 = config ]; then
162
			echo "${CONF_SELF}.label ${WHAT}bound connections"
163
			echo "${CONF_SELF}.draw LINE1"
164
			echo "${CONF_SELF}.colour FF0000"
165
			AUTOLABEL="no"
166
		fi
167
	;;
168
	*pmta_queue_domains | *pmta_queue_recipients | *pmta_queue_megabytes)		
169
		if [ `expr match "$NAME_SELF" ".*pmta_queue_domains"` != 0 ]; then
170
			WHAT="domains"
171
		elif [ `expr match "$NAME_SELF" ".*pmta_queue_recipients"` != 0 ]; then
172
			WHAT="recipients"
173
		elif [ `expr match "$NAME_SELF" ".*pmta_queue_megabytes"` != 0 ]; then
174
			WHAT="megabytes"
175
			UNIT_KB="true"
176
		fi
177
		CONF_TITLE="powermta ${WHAT} in queue"
178
		CONF_LABEL="queue_${WHAT}"	
179
		CONF_SELF="${WHAT}"		
180
		
181
		if [ -z $1 ]; then
182
			if [ !$UNIT_KB ]; then
183
				GET=`fetch_xml //queue/smtp dom status`  
184
				echo "${CONF_SELF}.value $GET"
185
				exit 0 
186
			else	
187
				GET=`fetch_xml //queue/smtp kb status`  
188
				RETURN=`echo $GET / 1024 | bc`
189
				echo "${CONF_SELF}.value $RETURN"
190
				exit 0 
191
			fi
192
		fi	
193
	;;
194
	*pmta_top_domains)
195
		CONF_TITLE="powermta top 10 domains by recipients"
196
		CONF_LABEL="top_domains"
197
		CONF_SELF="domains"
198
		AUTOLABEL="no"
199
		
200
		GET=`fetch_xml false false domains`
201
		if [ -z $1 ]; then
202
			for values in $GET; do
203
				if [ $values -ge 0 2>/dev/null ]; then
204
					echo $values
205
				else
206
					echo -n "$values.value#"
207
				fi			
208
			done | tr ' ' '\n' | tr '#' ' ' # restore newline - replace hashtag with whitespace
209
			exit 0
210
		elif [ $1 = config ]; then
211
			for values in $GET; do
212
				[ $values -ge 0 2>/dev/null ] || echo "$values.label $values\n$values.draw LINE2"
213
			done
214
		fi
215
	;;
216
esac
217

    
218
case $1 in
219
	suggest)
220
		if [ $NAME_SELF = "pmta_" ]; then
221
			# suggest plugins to munin:
222
			echo "traffic_in\ntraffic_out\nconn_in\nconn_out\nqueue_domains\nqueue_recipients\nqueue_megabytes\ntop_domains"
223
			exit 0
224
        fi
225
	;;
226
	config)
227
		if [ -n "$CONF_TITLE" ] && [ -n "$CONF_LABEL" ]; then
228
			echo "graph_category PowerMTA"
229
			echo "graph_title $CONF_TITLE on ${REMOTEHOST}"
230
			echo "graph_vlabel $CONF_LABEL"
231
			echo "graph_scale yes"
232
			echo "graph_width 1400"
233
			echo "graph_height 450"
234
			echo "graph_args --step 10 --rigid --base 1000 -l 0"
235
			if [ "$AUTOLABEL" != "no" ]; then
236
				echo "${CONF_SELF}.label $CONF_LABEL"
237
				echo "${CONF_SELF}.draw AREA"
238
				echo "${CONF_SELF}.colour FF6F00"
239
			fi
240
		fi
241
		exit 0
242
	;;	
243
	autoconf)
244
		# tell munin this script supports autoconfiguration:
245
		echo "yes"
246
	;;	
247
	*)
248
		exit 1
249
	;;
250
esac
251
#EOF