Category Archives: Other

Postfix quota notification email script

If you want to take care about your users quota and your storage space, check this …

1. Create new file (for example quota_notify inside /usr/local/sbin/ )

2. Copy next content inside quota_notify

#!/usr/bin/perl -w
 
# Author <jps@tntmax.com>
#
# This script assumes that virtual_mailbox_base in defined
# in postfix's main.cf file. This directory is assumed to contain
# directories which themselves contain your virtual user's maildirs.
# For example:
#
# -----------/
#            |
#            |
#    home/vmail/domains/
#        |          |
#        |          |
#  example.com/  foo.com/
#                   |
#                   |
#           -----------------
#           |       |       |
#           |       |       |
#         user1/   user2/  user3/
#                           |
#                           |
#                        maildirsize
#
 
use strict;
 
my $POSTFIX_CF = "/etc/postfix/main.cf";
my $MAILPROG = "/usr/sbin/sendmail -t";
my $WARNPERCENT = 80;
my @POSTMASTERS = ('postmaster@mydomain.tld');
my $CONAME = 'Company ....';
my $COADDR = 'postmaster@mydomain.tld';
my $SUADDR = 'postmaster@mydomain.tld';
my $MAIL_REPORT = 1;
my $MAIL_WARNING = 1;
 
#get virtual mailbox base from postfix config
open(PCF, "< $POSTFIX_CF") or die $!;
my $mboxBase;
while (<PCF>) {
   next unless /virtual_mailbox_base\s*=\s*(.*)\s*/;
   $mboxBase = $1;
}
close(PCF);
 
#assume one level of subdirectories for domain names
my @domains;
opendir(DIR, $mboxBase) or die $!;
while (defined(my $name = readdir(DIR))) {
   next if $name =~ /^\.\.?$/;        #skip '.' and '..'
   next unless (-d "$mboxBase/$name");
   push(@domains, $name);
}
closedir(DIR);
#iterate through domains for username/maildirsize files
my @users;
chdir($mboxBase);
foreach my $domain (@domains) {
        opendir(DIR, $domain) or die $!;
        while (defined(my $name = readdir(DIR))) {
           next if $name =~ /^\.\.?$/;        #skip '.' and '..'
           next unless (-d "$domain/$name");
      push(@users, {"$name\@$domain" => "$mboxBase/$domain/$name"});
        }
}
closedir(DIR);
 
 
#get user quotas and percent used
my (%lusers, $report);
foreach my $href (@users) {
   foreach my $user (keys %$href) {
      my $quotafile = "$href->{$user}/maildirsize";
      next unless (-f $quotafile);
      open(QF, "< $quotafile") or die $!;
      my ($firstln, $quota, $used);
      while (<QF>) {
         my $line = $_;
              if (! $firstln) {
                 $firstln = 1;
                 die "Error: corrupt quotafile $quotafile"
                    unless ($line =~ /^(\d+)S/);
                 $quota = $1;
            last if (! $quota);
            next;
         }
         die "Error: corrupt quotafile $quotafile"
            unless ($line =~ /\s*(-?\d+)/);
         $used += $1;
      }
      close(QF);
      next if (! $used);
      my $percent = int($used / $quota * 100);
      $lusers{$user} = $percent unless not $percent;
   }
}
 
 
#send a report to the postmasters
if ($MAIL_REPORT) {
   open(MAIL, "| $MAILPROG");
   select(MAIL);
   map {print "To: $_\n"} @POSTMASTERS;
   print "From: $COADDR\n";
   print "Subject: Daily Quota Report.\n";
   print "DAILY QUOTA REPORT:\n\n";
   print "----------------------------------------------\n";
   print "| % USAGE |            ACCOUNT NAME          |\n";
   print "----------------------------------------------\n";
   foreach my $luser ( sort { $lusers{$b} <=> $lusers{$a} } keys %lusers ) {
      printf("|   %3d   | %32s |\n", $lusers{$luser}, $luser);
      print "---------------------------------------------\n";
   }
        print "\n--\n";
        print "$CONAME\n";
        close(MAIL);
}
 
#email a warning to people over quota
if ($MAIL_WARNING) {
        foreach my $luser (keys (%lusers)) {
           next unless $lusers{$luser} >= $WARNPERCENT;       # skip those under quota
           open(MAIL, "| $MAILPROG");
           select(MAIL);
           print "To: $luser\n";
      map {print "BCC: $_\n"} @POSTMASTERS;
           print "From: $SUADDR\n";
           print "Subject: WARNING: Your mailbox is $lusers{$luser}% full.\n";
           print "Reply-to: $SUADDR\n";
           print "Your mailbox: $luser is $lusers{$luser}% full.\n\n";
           print "Please consider deleting e-mail and emptying your trash folder to clear some space.\n\n";
           print "Contact <$SUADDR> for further assistance.\n\n";
           print "Thank You.\n\n";
           print "--\n";
           print "$CONAME\n";
           close(MAIL);
        }
}

3. Edit next lines inside and set up your postmaster email address, percentage, etc

my $POSTFIX_CF = "/etc/postfix/main.cf";
my $MAILPROG = "/usr/sbin/sendmail -t";
my $WARNPERCENT = 80;
my @POSTMASTERS = ('postmaster@mydomain.tld');
my $CONAME = 'Company ....';
my $COADDR = 'postmaster@mydomain.tld';
my $SUADDR = 'postmaster@mydomain.tld';
my $MAIL_REPORT = 1;
my $MAIL_WARNING = 1;

4. set chmod 755 on quota_notify (# chmod 755 /usr/local/sbin/quota_notify)

5. add next line to crontab so it will be executed at 0:00 every day.

0 0 * * * /usr/local/sbin/quota_notify &> /dev/null

Keep in mind that this script has only reporting purpose and it will not reject any email when user mailbox is over quota. For this you will need Postfix with quota patch.

This post was inspired by this LINK.

MySQL Workbench

MySQL Workbench is a cross-platform, visual database design tool developed by MySQL. It is the highly anticipated successor application of the DBDesigner4 project. MySQL Workbench will be available as a native GUI tool on Window, Linux and OS X.

MySQL Workbench 5.2 RC provides:
1. Data Modeling
2. Query (upgrade from MySQL Query Browser)
3. Admin (upgrade from MySQL Administrator)

Please get your copy from this page

RPMforge howto

The RPMforge project is an open source project that provides RPM packages for various distributions (RH, CentOS, Fedora, …). To enable RPMforge you can install the rpmforge-release package for your distribution.

For RHEL5 / CentOS-5:
i386

# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm
# rpm -Uvh rpmforge-release-0.5.1-1.el5.rf.i386.rpm

x86_64

# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm
# rpm -Uvh rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm

For older releases (RHEL 4 / CentOS 4) please follow the instructions available on RPMrepo Wiki

Set date and time from command prompt

If the Linux server time and date is wrong and you need to set it to new values from the shell prompt, you should use date command. You must login as root user to use date command. Also from command prompt you can check and set system clock (hwclock command).

Use the following syntax to set new data and time:

# date -s "16 MAR 2010 09:36:00"

Also you can use next syntax:

date set="16 MAR 2010 09:36:00"

After you set date and time, it is good idea to check your system clock with

# hwclock --show

If there is some difference you can sync system date and time with

# hwclock --systohc

tftp server on CentOS/RHEL/Fedora

Trivial File Transfer Protocol (TFTP) is a file transfer protocol, with the functionality of a very basic form of File Transfer Protocol (FTP). It was first defined in 1980 and it is used for operations like firmware upgrade on network devices. This post won’t be the history lesson :). To install tftp server on RH based distros, follow the next steps

Install tftp-server with

# yum install tftp-server xinetd

Now you will need to set up some things before you can start and use tftp server

# nano /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

To enable tftp server, set “disable” to no. Also, check server_args. In this case, all files will be/must be in /var/lib/tftpboot. If you have any problems with permissions, try to chmod 777 /var/lib/tftpboot and fix perms later.

Restart xinetd to apply settings you just made

# service xinetd restart

Setting “disable” to yes will disable tftp server and for security reasons, you should disable tftp whenever you don’t need it.

TFTP has been implemented on top of the User Datagram Protocol (UDP) using port number 69. TFTP is designed to be small and easy to implement, therefore, lacks most of the features of a regular FTP. TFTP only reads and writes files (or mail) from/to a remote server. It cannot list directories, and currently has no provisions for user authentication.

In TFTP, any transfer begins with a request to read or write a file, which also serves to request a connection. If the server grants the request, the connection is opened and the file is sent in fixed length blocks of 512 bytes. Each data packet contains one block of data, and must be acknowledged by an acknowledgment packet before the next packet can be sent. A data packet of less than 512 bytes signals termination of a transfer. If a packet gets lost in the network, the intended recipient will timeout and may retransmit his last packet (which may be data or an acknowledgment), thus causing the sender of the lost packet to retransmit that lost packet. The sender has to keep just one packet on hand for retransmission, since the lock step acknowledgment guarantees that all older packets have been received. Notice that both machines involved in a transfer are considered senders and receivers. One sends data and receives acknowledgments, the other sends acknowledgments and receives data.

Check dir size howto

It is useful to know how to check the size of a folder. All you need to do is to run next command:

# du -hs /dir/location/

For example:

[root@myserver /]# du -hs /mail/mydomain.com/
837M    /mail/mydomain.com/

In this case, the size is shown in MBs.

Small update…

Hi there… Its been a while since my last post and that seems like a tradition for my blogs… Few posts and then couple months of inactivity… Well, you know, I’m busy, I’m sick, tired etc.. At the end I would like to announce a couple articles about Sysbench, mod_fcgid and other “Nuclear science” utilities…

see ya …