Debian Codegraph @

contrib/mrtg-mail/README

raw
1 Using Mailstats with MRTG
2
3 # 17/04/97: These files are based on a post
4 # made to the MRTG discussion list.
5
6 # I was then asked if I would contribute them to the distribution.
7 # Now I've cleaned up one or two minor things, but the message below
8 # is essentially the same as the distribution I have contributed.
9
10 # I also posted this to comp.mail.sendmail some time back.
11
12 # rachel polanskis <[email protected]>
13
14
15
16
17 This is some experimenting I have done using sendmail and mrtg.
18
19 Currently I am using Solaris 2.5.1 as the mailhost, and then plotting
20 the goodies using mrtg-2.0 on Linux 1.2.13 and displaying the graphs
21 on the linux box.
22
23 There are some caveats on my method for plotting these charts:
24
25 1: I use a previously undefined port and run the Solaris
26 /bin/mailstats program out of inetd.
27 For this reason, I can only assume the mailstat on Solaris
28 works - I have tried no other platforms.
29
30 * Q: Is there a security risk involved in running Solaris Mailstat
31 out of inetd?
32
33 2: You must have Perl-5.002 or better installed on the plotting host.
34
35 3: You must have MRTG-2.0 installed - you must know how to use it!
36
37 4: I am only interested in SMTP connections, and my scripts only
38 plot In and Out, since mrtg can only plot 2 lines per graph.
39 If you want any more than that - experiment! And then tell me
40 since I am only learning.
41
42 Here goes:
43
44 1: Ensure you have "/bin/mailstat" on your system.
45
46 2: Enable "/etc/mail/sendmail.st"
47 To do this you can just make sure the entry for "sendmail.st" in
48 "sendmail.cf" is uncommented, and then do the command:
49
50 # touch /etc/mail/sendmail.st
51
52 And restart sendmail:
53
54 # /etc/init.d/sendmail stop
55 # /etc/init.d/sendmail start
56
57
58 3: Create the following shell-script:
59
60 #!/bin/sh
61 # smtp-stats: exec Solaris mailstats
62 #
63 if [ -x "/bin/mailstats" ]
64 then
65 exec /bin/mailstats -f/etc/mail/sendmail.st
66 fi
67
68 I run this out of /opt/local/bin, and call it "smtp-stats"
69
70 4: Now add the following to /etc/services:
71
72 # /etc/services
73 #
74 smtp-stats 7256/tcp # smtp-stats
75
76 I used port 7256 as it was undefined. You might like to select something
77 else.
78
79 5: Add the following to /etc/inetd.conf:
80
81 # /etc/inetd.conf
82 #
83 smtp-stats stream tcp nowait root \
84 /opt/local/bin/smtp-stats smtp-stats
85
86
87 ***Ensure that the above is all on *one* line. The sample above
88 is broken on 2 lines for legibility!!
89
90
91 6: Restart inetd
92
93
94 7: Test the port. you should see the following:
95
96 # telnet localhost 7256
97 Trying 127.0.0.1...
98 Connected to localhost.
99 Escape character is '^]'.
100 Statistics from Thu Feb 27 01:12:47 1997
101 M msgsfr bytes_from msgsto bytes_to Mailer
102 0 0 0K 96 393K prog
103 3 34 94K 0 0K local
104 5 93 474K 31 47K esmtp
105 ========================================
106 T 127 568K 127 440K
107 Connection closed by foreign host.
108
109
110 OK!!
111
112 The line we are interested in is line 5: The "esmtp" line.
113 This is the line we will use to get our stats.
114
115 You might want to adjust this depending on how many mailer progs
116 you use...
117
118 I find the Totals to be useless, since it includes a lot of filtering
119 from procmail. I am only interested in SMTP connections...
120
121 ####################################
122 Stage 2: MRTG
123
124
125 Here is a perl script I wrote to glom the values of the "esmtp" line (5)
126 and massage them into something mrtg understands.
127
128 A file called "mailstats.old" is written to /tmp
129 to allow for doing my sums.
130
131 I am a Perl Newbie - please don't be offended at my code.
132 I would like it if someone can help me with better methods one day!
133
134 #!/usr/bin/perl
135 #
136 # rachel polanskis - 240297
137 # this script relies on the sendmail.st file being activated.
138 # the data is called via "mailstats" which comes with SunOS/Solaris.
139 # mailstats is run out of inetd, on port 7256.
140 #
141 require 5.002;
142 use strict;
143 use Socket;
144
145 my ($remote, $port, $iaddr, $paddr, $proto, $line, $count, $oldfrm, $oldto, $curfrm, $curto, $msgsfrm, $msgsto, $a, $b, $c, $d);
146
147 ##
148 # open the old stats file for reading - we use this to get the differences
149 #
150 open (OLD,"</tmp/mailstat.old") or die "can't open file!\n";
151
152 # read the old data
153 #
154 while (<OLD>) {
155 if ($. == "1") {
156 $count = $_;
157 }
158 ($oldfrm, $oldto) = split (' ',$count);
159 }
160 close (OLD);
161
162 ##
163 # Straight out of the blue Camel - pp. 349
164 #
165 # Change these next 2 lines to suit yourself.
166 #
167 $remote = "juno";
168 $port = "7256";
169
170 #
171 if ($port =~ /\D/) {$port = getservbyname ($port, 'tcp')}
172 die "No port" unless $port;
173
174 $iaddr = inet_aton($remote) or die "no host: $remote";
175 $paddr = sockaddr_in($port, $iaddr);
176
177 $proto = getprotobyname ('tcp');
178
179 socket (SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
180 connect (SOCK, $paddr) or die "connect: $!";
181
182 ##
183 # munge the output data
184 #
185 while (<SOCK>) {
186
187 if ($. == "5" ) {
188 $line = $_;
189 ($a, $curfrm, $b, $curto, $c, $d) = split(' ',$line);
190
191 # do some sums
192 $msgsfrm = $curfrm - $oldfrm;
193 $msgsto = $curto - $oldto;
194 chomp $msgsfrm;
195 chomp $msgsto;
196
197 # open the old file for overwrite
198 open (OLD,">/tmp/mailstat.old") or die "can't open file!\n";
199
200 # print the data for mrtg
201 print "$msgsfrm\n$msgsto\n1\njuno\n";
202
203 # print the data to the old file
204 print OLD "$curfrm $curto\n";
205
206 } #endif
207
208 }
209
210 close (SOCK) or die "close: $!";
211 exit;
212
213
214 ##########################
215
216 Now, Here is my mrtg.cfg file for mail.
217 I call it "mail.cfg"
218 I run mrtg from cron on my Linux box every 10 minutes to plot the stats.
219 It is up to the individual what they do with their own setup.
220
221 #
222 # Mail: MRTG.cfg file for sendmail stats on mailhost.
223 #
224
225 WorkDir: /usr/local/etc/httpd/htdocs/stats/mrtg
226 Interval: 10
227 #---------------------------------------------------------------
228 Target[mail]: `/usr/local/bin/mrtg/mailstats`
229 MaxBytes[mail]: 150
230 Options[mail]: gauge
231 Title[mail]: Juno Sendmail Statistics
232 PageTop[mail]: Juno Sendmail Statistics</H1>
233 XSize[mail]: 500
234 Supress[mail]: my
235 YSize[mail]: 200
236 WithPeak[mail]: dwmy
237 YLegend[mail]: No. of messages
238 ShortLegend[mail]: messages
239 LegendI[mail]: &nbsp;Incoming:
240 LegendO[mail]: &nbsp;Outgoing:
241
242
243
244 That's it!!
245
246 You should have no problems running this stuff if you follow my
247 instructions.
248
249 Any comments, advice or interesting suggestions
250 would be much appreciated.
251
252 Do with my little hack as you like, as long as the developers
253 of sendmail, perl, and mrtg get some credit, as well as lil ol me
254 for dreaming this up!!!
255
256
257 have fun...
258
259 rachel polanskis <[email protected]> 17/04/97
260