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 <r.polanskis@nepean.uws.edu.au>
13
14
15
16
17This is some experimenting I have done using sendmail and mrtg.
18
19Currently I am using Solaris 2.5.1 as the mailhost, and then plotting
20the goodies using mrtg-2.0 on Linux 1.2.13 and displaying the graphs
21on the linux box.
22
23There are some caveats on my method for plotting these charts:
24
251: 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
332: You must have Perl-5.002 or better installed on the plotting host.
34
353: You must have MRTG-2.0 installed - you must know how to use it!
36
374: 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
42Here goes:
43
441: Ensure you have "/bin/mailstat" on your system.
45
462: 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
583: 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
68I run this out of /opt/local/bin, and call it "smtp-stats"
69
704: Now add the following to /etc/services:
71
72 # /etc/services
73 #
74 smtp-stats 7256/tcp # smtp-stats
75
76I used port 7256 as it was undefined. You might like to select something
77else.
78
795: 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
88is broken on 2 lines for legibility!!
89
90
916: Restart inetd
92
93
947: Test the port. you should see the following:
95
96# telnet localhost 7256
97Trying 127.0.0.1...
98Connected to localhost.
99Escape character is '^]'.
100Statistics 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
107Connection closed by foreign host.
108
109
110OK!!
111
112The line we are interested in is line 5: The "esmtp" line.
113This is the line we will use to get our stats.
114
115You might want to adjust this depending on how many mailer progs
116you use...
117
118I find the Totals to be useless, since it includes a lot of filtering
119from procmail. I am only interested in SMTP connections...
120
121####################################
122Stage 2: MRTG
123
124
125Here is a perl script I wrote to glom the values of the "esmtp" line (5)
126and massage them into something mrtg understands.
127
128A file called "mailstats.old" is written to /tmp
129to allow for doing my sums.
130
131I am a Perl Newbie - please don't be offended at my code.
132I 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#
141require 5.002;
142use strict;
143use Socket;
144
145my ($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#
150open (OLD,"</tmp/mailstat.old") or die "can't open file!\n";
151
152# read the old data
153#
154while (<OLD>) {
155 if ($. == "1") {
156 $count = $_;
157 }
158 ($oldfrm, $oldto) = split (' ',$count);
159}
160close (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#
171if ($port =~ /\D/) {$port = getservbyname ($port, 'tcp')}
172die "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
179socket (SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
180connect (SOCK, $paddr) or die "connect: $!";
181
182##
183# munge the output data
184#
185while (<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;
194chomp $msgsfrm;
195chomp $msgsto;
196
197# open the old file for overwrite
198open (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
210close (SOCK) or die "close: $!";
211exit;
212
213
214##########################
215
216Now, Here is my mrtg.cfg file for mail.
217I call it "mail.cfg"
218I run mrtg from cron on my Linux box every 10 minutes to plot the stats.
219It 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
225WorkDir: /usr/local/etc/httpd/htdocs/stats/mrtg
226Interval: 10
227#---------------------------------------------------------------
228Target[mail]: `/usr/local/bin/mrtg/mailstats`
229MaxBytes[mail]: 150
230Options[mail]: gauge
231Title[mail]: Juno Sendmail Statistics
232PageTop[mail]: Juno Sendmail Statistics</H1>
233XSize[mail]: 500
234Supress[mail]: my
235YSize[mail]: 200
236WithPeak[mail]: dwmy
237YLegend[mail]: No. of messages
238ShortLegend[mail]: messages
239LegendI[mail]: Incoming:
240LegendO[mail]: Outgoing:
241
242
243
244That's it!!
245
246You should have no problems running this stuff if you follow my
247instructions.
248
249Any comments, advice or interesting suggestions
250would be much appreciated.
251
252Do with my little hack as you like, as long as the developers
253of sendmail, perl, and mrtg get some credit, as well as lil ol me
254for dreaming this up!!!
255
256
257have fun...
258
259rachel polanskis <r.polanskis@nepean.uws.edu.au> 17/04/97
260
261