| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Author by Rick Horowitz <[email protected]> 1998-12-16 |
| 4 |
# Inspired by, and may show remnants of code from: |
| 5 |
# "mailstats" by Petter Reinholdtsen <[email protected]> |
| 6 |
# dated 1997-07-09 and part of mrtg2.54c dist |
| 7 |
# Petter credits Rachel Polanskis <[email protected]> for |
| 8 |
# the original. |
| 9 |
# Type "perldoc mrtg-mailstat" from a shell prompt for more information |
| 10 |
# on this script and its use. |
| 11 |
# |
| 12 |
|
| 13 |
use strict; |
| 14 |
use Socket; |
| 15 |
use Getopt::Long; |
| 16 |
|
| 17 |
# IMPORTANT: THIS NEXT LINE ENSURE THAT CASE MATTERS ON OPTIONS!!! |
| 18 |
$Getopt::Long::ignorecase = 0; |
| 19 |
|
| 20 |
use vars qw($datafile $server $sourceport $oldfrm $oldto $help |
| 21 |
$newfrm $newto $uptime $mailtype $logbase); |
| 22 |
|
| 23 |
# Assign options. ("Illegal" options will be possible, |
| 24 |
# but we'll warn about them via Usage().) |
| 25 |
# h = runs the perldoc program to show the mrtg-mailstat manual |
| 26 |
# m = specifies the type of mail we're checking for |
| 27 |
# p = specifies the port for reaching the mailstats on the mailserver |
| 28 |
# s = specifies the location of the mailserver |
| 29 |
# See the mrtg-mailstat man page for details. |
| 30 |
GetOptions("help" => \$help, |
| 31 |
"logbase=s" => \$logbase, |
| 32 |
"mailtype=s" => \$mailtype, |
| 33 |
"port=i" => \$sourceport, |
| 34 |
"server=s" => \$server, |
| 35 |
"<>" => \&Usage); |
| 36 |
|
| 37 |
# HARDCODING SECTION -- MODIFY THESE ONLY IF YOU KNOW WHAT YOU ARE DOING!!! |
| 38 |
# Set these ONLY if you don't wish to use command line options. For more |
| 39 |
# information, type 'mrtg-mailstat -h' or 'perldoc mrtg-mailstat' from a |
| 40 |
# shell prompt. |
| 41 |
$logbase = '/tmp' unless $logbase; |
| 42 |
$mailtype = '' unless $mailtype; |
| 43 |
$server = '' unless $server; |
| 44 |
$sourceport = '' unless $sourceport; |
| 45 |
|
| 46 |
# Location for keeping mail statistics. The numbers stored here are |
| 47 |
# compared to the new numbers at each run of this script. |
| 48 |
$datafile = "$logbase/${mailtype}-mailstat.old"; |
| 49 |
|
| 50 |
# Core portions of the program. Most of retained code from original |
| 51 |
# comes into play here. |
| 52 |
GetHelp() if $help; |
| 53 |
Usage() unless ($mailtype && $server && $sourceport); |
| 54 |
|
| 55 |
($oldfrm, $oldto) = GetOldStats($datafile); |
| 56 |
($newfrm, $newto, $uptime) = GetNewStats($server, $sourceport); |
| 57 |
|
| 58 |
PutOldStats($datafile, $newfrm, $newto) || warn "$0: Unable to save stats to $datafile"; |
| 59 |
|
| 60 |
print $newfrm-$oldfrm,"\n",$newto-$oldto,"\n","$uptime\nthe mail server\n" if ($oldfrm); |
| 61 |
|
| 62 |
## |
| 63 |
# Returns first line of file given as param split on (a single) space |
| 64 |
sub GetOldStats { |
| 65 |
my($datafile) = @_; |
| 66 |
|
| 67 |
if (-e $datafile) { |
| 68 |
open(OLD, $datafile) || warn "$0: Unable to open $datafile for reading"; |
| 69 |
my($line) = <OLD>; |
| 70 |
close(OLD); |
| 71 |
return split(/ /, $line); |
| 72 |
} else { |
| 73 |
return(0, 0); |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
sub GetNewStats { |
| 79 |
my($server, $sourceport) = @_; |
| 80 |
my(@output, $port, $proto, $iaddr, $paddr, $tempuptime, $curfrm, $curto, $uptime); |
| 81 |
if ( $server eq "localhost" ) { |
| 82 |
@output = `/usr/sbin/mailstats`; |
| 83 |
chomp(@output); |
| 84 |
} else { |
| 85 |
if ($sourceport =~ /\D/) { |
| 86 |
$port = getservbyname ($sourceport, 'tcp'); |
| 87 |
} else { |
| 88 |
$port = $sourceport; |
| 89 |
} |
| 90 |
die "$0: Bad port \"$sourceport\"" unless ($port); |
| 91 |
$proto = getprotobyname ('tcp') || die "$0: Bad prototype tcp"; |
| 92 |
|
| 93 |
$iaddr = inet_aton($server) or die "$0: no host \"$server\""; |
| 94 |
$paddr = sockaddr_in($port, $iaddr); |
| 95 |
|
| 96 |
socket (SOCK, PF_INET, SOCK_STREAM, $proto) or die "$0: socket error $!"; |
| 97 |
connect (SOCK, $paddr) or die "$0: connect error $!"; |
| 98 |
|
| 99 |
while (<SOCK>) { |
| 100 |
push(@output, $_); |
| 101 |
} |
| 102 |
close(SOCK) || warn "$0: socket close error $!"; |
| 103 |
} |
| 104 |
|
| 105 |
# Parse only the interesting lines. |
| 106 |
foreach (@output) { |
| 107 |
($curfrm, $curto) = (split(/ +/))[2,4] if (/$mailtype/); |
| 108 |
if (/UPTIME.*?up\s*(.*?), [0-9]* user/) { |
| 109 |
$uptime = $1; |
| 110 |
$uptime =~ s/:([0-9]*)/ hours, $1 minutes/; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
# This line ensures no negative numbers. See "perldoc mrtg-mailstat". |
| 115 |
($curfrm, $curto) = noNeg($curfrm, $curto); |
| 116 |
|
| 117 |
return ($curfrm, $curto, $uptime); |
| 118 |
|
| 119 |
} # End GetNewStats(). |
| 120 |
|
| 121 |
# Saves the information to the log file. (Note: Overwrites old contents |
| 122 |
# which aren't needed anymore, anyway.) |
| 123 |
sub PutOldStats { |
| 124 |
my($datafile, $frm, $to) = @_; |
| 125 |
open(STAT, ">$datafile") || return ""; |
| 126 |
print STAT "$frm $to\n"; |
| 127 |
close(STAT); |
| 128 |
return "1"; |
| 129 |
} # End PutOldStats(). |
| 130 |
|
| 131 |
# Negative values should only occur the first time the script is ever run. |
| 132 |
# These must be converted to positives so subsequent runs of the script |
| 133 |
# correctly compute new values. |
| 134 |
sub noNeg { |
| 135 |
|
| 136 |
my ($frm, $to) = @_; |
| 137 |
|
| 138 |
$frm = -($frm) if ($frm < 0); |
| 139 |
$to = -($to) if ($to < 0); |
| 140 |
|
| 141 |
return($frm, $to); |
| 142 |
|
| 143 |
} # End noNeg(). |
| 144 |
|
| 145 |
# Usage() simply checks to see that no "illegal" options |
| 146 |
# have been entered. If the program is thus improperly |
| 147 |
# called, it issues a notice explaining how to read the |
| 148 |
# man page for this program. |
| 149 |
sub Usage { |
| 150 |
|
| 151 |
my $option = shift; |
| 152 |
|
| 153 |
print STDERR "You may wish to read the documentation \n"; |
| 154 |
print STDERR "(type \"perldoc mrtg-mailstat\" or \"mrtg-mailstat -h\")\n"; |
| 155 |
|
| 156 |
exit(5); |
| 157 |
|
| 158 |
} # End Usage(). |
| 159 |
|
| 160 |
sub GetHelp { |
| 161 |
|
| 162 |
system("perldoc mrtg-mailstat"); |
| 163 |
exit(0); |
| 164 |
|
| 165 |
} # End GetHelp(). |
| 166 |
|
| 167 |
__END__ |
| 168 |
|
| 169 |
=head1 NAME |
| 170 |
|
| 171 |
mrtg-mailstat - Mail Statistics Collector for MRTG |
| 172 |
|
| 173 |
=head1 SYNOPSIS |
| 174 |
|
| 175 |
mrtg-mailstat S<[B<-h>]> S<[B<-m> I<mailtype>]> S<[B<-p> I<sourceport>]> |
| 176 |
S<[B<-s> I<server>]> |
| 177 |
|
| 178 |
=head1 DESCRIPTION |
| 179 |
|
| 180 |
The B<mrtg-mailstat> program retrieves statistical information from the |
| 181 |
mail server. It does not matter whether the script is run on the mail |
| 182 |
server itself, or on a machine remote from the mail server. |
| 183 |
|
| 184 |
=head1 S< Running mrtg-mailstat > |
| 185 |
|
| 186 |
S< > |
| 187 |
The proper way to run B<mrtg-mailstat> is to call it from an MRTG configuration |
| 188 |
file. The following provides an example of a valid configuration file entry: |
| 189 |
|
| 190 |
=over 2 |
| 191 |
|
| 192 |
=item |
| 193 |
|
| 194 |
C<S<Target[smtp-mail]: `mrtg-mailstat -m smtp -p 7258 -s mail.techstop.com`> |
| 195 |
S<MaxBytes[smtp-mail]: 150 > |
| 196 |
S<Options[smtp-mail]: gauge > |
| 197 |
S<Title[smtp-mail]: TechStop Support Services SMTP Mail Statistics> |
| 198 |
S<PageTop[smtp-mail]: E<lt>H1E<gt>TechStop SMTP Mail Statistics E<lt>/H1E<gt>> |
| 199 |
S<XSize[smtp-mail]: 500 > |
| 200 |
S<YSize[smtp-mail]: 200 > |
| 201 |
S<WithPeak[smtp-mail]: dwmy> |
| 202 |
S<YLegend[smtp-mail]: No. of messages> |
| 203 |
S<ShortLegend[smtp-mail]: messages> |
| 204 |
S<LegendI[smtp-mail]: Incoming:> |
| 205 |
S<LegendO[smtp-mail]: Outgoing: > > |
| 206 |
|
| 207 |
=back |
| 208 |
|
| 209 |
This makes certain assumptions which are outlined below under I<Dependencies>. |
| 210 |
|
| 211 |
=head1 S< Switches > |
| 212 |
|
| 213 |
S< > |
| 214 |
|
| 215 |
=head2 Hardcoding versus Using Switches |
| 216 |
|
| 217 |
It is not necessary to provide switches to the program, if you prefer to "hardcode" |
| 218 |
the information in the script itself. "Hardcoding" allows you to call the script |
| 219 |
with less (or no) flags; information needed by the script, and not provided by a |
| 220 |
flag, must be "hardcoded" by you before this will work. (This was provided for |
| 221 |
diehards who hate the flexibility of switches. I provided enough flexibility for |
| 222 |
them to be inflexible. ;)) |
| 223 |
|
| 224 |
To "hardcode" your information into the script, look for the section near the |
| 225 |
top of the script labeled "HARDCODING SECTION". Change the "null" entries |
| 226 |
represented by the double double quotes ("") so that they contain the appropriate |
| 227 |
values. |
| 228 |
|
| 229 |
Example: |
| 230 |
|
| 231 |
|
| 232 |
=over 4 |
| 233 |
|
| 234 |
=item |
| 235 |
|
| 236 |
C<$server = "" unless $server; > |
| 237 |
|
| 238 |
=back |
| 239 |
|
| 240 |
becomes |
| 241 |
|
| 242 |
=over 4 |
| 243 |
|
| 244 |
=item |
| 245 |
|
| 246 |
C<$server = "mail.techstop.com" unless $server; > |
| 247 |
|
| 248 |
=back |
| 249 |
|
| 250 |
As stated, this is not necessary; it is not even preferred. You may, |
| 251 |
and probably should, use the switches in the next subsection. |
| 252 |
|
| 253 |
=head2 Invoking Switches |
| 254 |
|
| 255 |
B<mrtg-mailstat> switches adhere to the POSIX syntax for command-line |
| 256 |
options. This means options can take a variety of forms. |
| 257 |
For example, to specify running B<mrtg-mailstat> against port C<7258> |
| 258 |
of the mail server to obtain reports on smtp mail (see "I<mailtype> |
| 259 |
under I<Available Switches> below) you could use any of the following forms: |
| 260 |
|
| 261 |
=over 5 |
| 262 |
|
| 263 |
=item |
| 264 |
|
| 265 |
C<% mrtg-mailstat -m smtp -p 7258> |
| 266 |
|
| 267 |
=item |
| 268 |
|
| 269 |
C<% mrtg-mailstat -m=smtp -p=7258> |
| 270 |
|
| 271 |
=item |
| 272 |
|
| 273 |
C<% mrtg-mailstat -mailtype smtp -port 7258> |
| 274 |
|
| 275 |
=item |
| 276 |
|
| 277 |
C<% mrtg-mailstat -mailtype=smtp -port=7258> |
| 278 |
|
| 279 |
=item |
| 280 |
|
| 281 |
C<% mrtg-mailstat --m smtp --p 7258> |
| 282 |
|
| 283 |
=item |
| 284 |
|
| 285 |
C<% mrtg-mailstat --m=smtp --p=7258> |
| 286 |
|
| 287 |
=item |
| 288 |
|
| 289 |
C<% mrtg-mailstat --mailtype smtp --port 7258> |
| 290 |
|
| 291 |
=item |
| 292 |
|
| 293 |
C<% mrtg-mailstat --mailtype=smtp --port=7258> |
| 294 |
|
| 295 |
=back |
| 296 |
|
| 297 |
Any of the last four forms are preferred. One way of doing options |
| 298 |
which is I<not> possible is: |
| 299 |
|
| 300 |
=over 5 |
| 301 |
|
| 302 |
=item |
| 303 |
|
| 304 |
C<% mrtg-mailstat --msmtp --p7258> |
| 305 |
|
| 306 |
=back |
| 307 |
|
| 308 |
Use of that last form may cause results other than you expected. |
| 309 |
(Important Note: The above examples would also require that the |
| 310 |
server be "hardcoded" into the script as noted in the subsection on |
| 311 |
I<Hardcoding versus Using Switches> above.) |
| 312 |
|
| 313 |
=head2 Available Switches |
| 314 |
|
| 315 |
The following switches are available: |
| 316 |
|
| 317 |
=over 2 |
| 318 |
|
| 319 |
=item B<--h[elp]> |
| 320 |
|
| 321 |
The B<-h> flag causes B<mrtg-mailstat> to print this manual to |
| 322 |
standard output. For obvious reasons, this is not normally included in an |
| 323 |
MRTG configuration file, nor invoked by cron. |
| 324 |
|
| 325 |
=item B<--l[ogbase]> |
| 326 |
|
| 327 |
The B<-l> flag can be used to specify the directory in which to store the |
| 328 |
file that holds the values from each run. By default, this file is created |
| 329 |
in I</tmp>, but if you need to store it in another directory, this is the |
| 330 |
flag for you. By the way, if the file doesn't exist, one is created. A side-effect |
| 331 |
of this is that if you try to use one "logbase" one time, and another the next, |
| 332 |
the program has no way of warning you. It simply creates a new file, and uses zero |
| 333 |
values to begin its calculations. This can have strange consequences for your |
| 334 |
mail statistics. Try to remember where you store your log, or use the hardcoding |
| 335 |
option (see I<Hardcoding versus Using Switches> above). |
| 336 |
|
| 337 |
=item B<--m[ailtype]> |
| 338 |
|
| 339 |
The B<-m> flag is used to specify the mail type. The two |
| 340 |
types reported by I</usr/bin/mailstat> are "esmtp" and "local". The |
| 341 |
"esmtp" line reports traffic between the mail server and other systems. |
| 342 |
The "local" line reports traffic between users on the mail server. For |
| 343 |
most smaller networks, this likely means that "esmtp" reports mail between |
| 344 |
their network and the Internet, and "local" reports mail between users |
| 345 |
within the small network's organization. Possible values for this flag are |
| 346 |
"smtp" and "local". B<Note the flag takes "smtp" and not "esmtp".> |
| 347 |
|
| 348 |
=item B<--p[ort]> |
| 349 |
|
| 350 |
The B<-p> flag specifies the port whence the statistics are obtained. See |
| 351 |
the I<Dependencies> section below. |
| 352 |
|
| 353 |
=item B<--s[erver]> |
| 354 |
|
| 355 |
The B<-s> flag specifies the mail server. This can be either a DNS name, |
| 356 |
or an IP number. |
| 357 |
|
| 358 |
=back |
| 359 |
|
| 360 |
=head1 DEPENDENCIES |
| 361 |
|
| 362 |
The B<mrtg-mailstat> program makes certain assumptions. The following is |
| 363 |
quoted with a few changes from the documentation for the original B<mailstat> script |
| 364 |
upon which B<mrtg-mailstat> is based. In particular, it is important to note that |
| 365 |
the original instructions were Solaris-centric. Your system may have different commands |
| 366 |
for starting and stopping the mail server, and your paths may be different (i.e., may |
| 367 |
use /usr/local instead of /opt, or whatever). You are assumed to be able to figure that |
| 368 |
out yourself. |
| 369 |
|
| 370 |
=over 2 |
| 371 |
|
| 372 |
=item 1: Ensure you have I</bin/mailstat> (or I</usr/sbin/mailstat>)on your system. |
| 373 |
|
| 374 |
=item 2: Enable I</var/log/sendmail.st> |
| 375 |
|
| 376 |
To do this you can just make sure the entry for "sendmail.st" in |
| 377 |
"sendmail.cf" is uncommented. On the BSD/OS 3.1 system mrtg-mailstat was |
| 378 |
tested on, the /etc/sendmail.cf entries look like this: |
| 379 |
|
| 380 |
OS/var/log/sendmail.st |
| 381 |
O StatusFile=/var/log/sendmail.st |
| 382 |
|
| 383 |
|
| 384 |
Then do the command: |
| 385 |
|
| 386 |
# touch /var/log/sendmail.st |
| 387 |
|
| 388 |
And restart sendmail: |
| 389 |
|
| 390 |
# /etc/init.d/sendmail stop |
| 391 |
# /etc/init.d/sendmail start |
| 392 |
|
| 393 |
|
| 394 |
=item 3: Create the following shell-script (B<Note!> This section is different than the original version!): |
| 395 |
|
| 396 |
#!/bin/sh |
| 397 |
# |
| 398 |
# smtp-stats: invoke mailstats from a wrapper for use in inetd. |
| 399 |
# |
| 400 |
PATH=/bin:/sbin |
| 401 |
if [ -x "/usr/sbin/mailstats" ] |
| 402 |
then |
| 403 |
temp1=`/usr/sbin/mailstats -f/var/log/sendmail.st` |
| 404 |
fi |
| 405 |
|
| 406 |
if [ -x "/usr/bin/uptime" ] |
| 407 |
then |
| 408 |
temp2=`/usr/bin/uptime` |
| 409 |
fi |
| 410 |
|
| 411 |
echo -e "$temp1\nUPTIME: $temp2" |
| 412 |
|
| 413 |
I run this out of /usr/local/bin, and call it "smtp-stats" |
| 414 |
|
| 415 |
=item 4: Now add the following to /etc/services: |
| 416 |
|
| 417 |
# /etc/services |
| 418 |
# |
| 419 |
smtp-stats 7256/tcp # smtp-stats |
| 420 |
|
| 421 |
I used port 7256 as it was undefined. You might like to select something |
| 422 |
else. |
| 423 |
|
| 424 |
=item 5: Add the following to /etc/inetd.conf: |
| 425 |
|
| 426 |
# /etc/inetd.conf |
| 427 |
# |
| 428 |
smtp-stats stream tcp nowait root \ |
| 429 |
/usr/local/bin/smtp-stats smtp-stats |
| 430 |
|
| 431 |
|
| 432 |
***Ensure that the above is all on *one* line. The sample above |
| 433 |
is broken on 2 lines for legibility!! |
| 434 |
|
| 435 |
|
| 436 |
=item 6: Restart inetd |
| 437 |
|
| 438 |
|
| 439 |
=item 7: Test the port. you should see the following: |
| 440 |
|
| 441 |
S< > |
| 442 |
S<unixhost: {26} % telnet mailserver 7258 > |
| 443 |
S<Trying 192.168.1.8... > |
| 444 |
S<Connected to mailserver.yourdomain.com. > |
| 445 |
S<Escape character is '^]'. > |
| 446 |
S<Statistics from Wed Jan 14 03:10:34 1998 > |
| 447 |
S< M msgsfr bytes_from msgsto bytes_to Mailer > |
| 448 |
S< 3 79372 3623347K 143989 6954437K local > |
| 449 |
S< 5 9941 483230K 6669 652586K esmtp > |
| 450 |
S<======================================== > |
| 451 |
S< T 89313 4106577K 150658 7607023K > |
| 452 |
S<UPTIME: 9:02AM up 52 days, 48 mins, 2 users, load averages: 0.57, 0.56, 0.43> |
| 453 |
S<Connection closed by foreign host. > |
| 454 |
|
| 455 |
|
| 456 |
=back |
| 457 |
|
| 458 |
=head1 FILES |
| 459 |
|
| 460 |
Strictly speaking, you needn't worry about the files. If you've created |
| 461 |
the smtp-stats script noted above, and made the appropriate changes, the |
| 462 |
only other I<file> you need to be concerned with is B<mrtg-mailstat> itself. |
| 463 |
|
| 464 |
For completeness sake, I list all the files with which B<mrtg-mailstat> may |
| 465 |
depend upon, utilize, create, or use. |
| 466 |
|
| 467 |
=over 5 |
| 468 |
|
| 469 |
=item |
| 470 |
|
| 471 |
I<mrtg-mailstat> |
| 472 |
|
| 473 |
=item |
| 474 |
|
| 475 |
I</etc/sendmail.cf> |
| 476 |
|
| 477 |
=item |
| 478 |
|
| 479 |
I</usr/local/bin/smtp-stats> |
| 480 |
|
| 481 |
=item |
| 482 |
|
| 483 |
I</usr/sbin/mailstats> |
| 484 |
|
| 485 |
=item |
| 486 |
|
| 487 |
I</usr/bin/uptime> |
| 488 |
|
| 489 |
=item |
| 490 |
|
| 491 |
I</var/log/sendmail.st> |
| 492 |
|
| 493 |
=item |
| 494 |
|
| 495 |
I<$logbase/${mailtype}-mailstat.old> |
| 496 |
|
| 497 |
=back |
| 498 |
|
| 499 |
|
| 500 |
=head1 AUTHOR |
| 501 |
|
| 502 |
This B<mrtg-mailstat> program was written by Rick Horowitz <[email protected]>, |
| 503 |
tested, and released to the MRTG mailing list on December 16, 1998. It was |
| 504 |
inspired by, and contains remnants of code from: |
| 505 |
|
| 506 |
=over 10 |
| 507 |
|
| 508 |
=item |
| 509 |
|
| 510 |
"mailstats" by Petter Reinholdtsen <[email protected]> |
| 511 |
dated 1997-07-09 and part of mrtg2.54c dist |
| 512 |
|
| 513 |
=back |
| 514 |
|
| 515 |
Petter credits Rachel Polanskis <[email protected]> for |
| 516 |
the original. |
| 517 |
|
| 518 |
=cut |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|