Zimbra notes/Domainreport

From Federal Burro of Information
Jump to navigationJump to search

print out a csv report of domains.

#!/usr/bin/perl -w

use strict;
use Net::LDAP;
use Data::Dumper;

my $VERBOSE = 0;
my $host = "ldap1";
my $base = "";
my $mydn = "uid=zimbra,cn=admins,cn=zimbra";
my $pass = "zoenoe";
my $ldap; # the connection object.
my $mesg; # the message returned when searches are done.
my @status = qw(active closed locked maintenance total);
my %domain; # the hash where the results are stored.

$ldap = Net::LDAP->new($host) or die "Can't bind to ldap: $!\n";

$VERBOSE && print "Binding...\n";
$mesg = $ldap->bind( $mydn, password => $pass );
$mesg->code && die $mesg->error;
$VERBOSE && print "Bound ok\n";

($mesg) = $ldap->search( base => $base, filter => "(objectClass=zimbraDomain)" );
$mesg->code && warn $mesg->error;
# my $max = $mesg->count;
# print "Number of entries found for objectClass=zimbraDomain  $max\n";

foreach my $entry ($mesg->entries) {
    my $thisdomain                                    = join(" ", $entry->get_value( "zimbraDomainName"));
    $domain{$thisdomain}->{'zimbraDomainType'}        = join(" ",$entry->get_value( "zimbraDomainType") );
    $domain{$thisdomain}->{'zimbraId'}                = join(" ",$entry->get_value( "zimbraId" ) );
    $domain{$thisdomain}->{'zimbraDomainMaxAccounts'} = join(" ",$entry->get_value( 'zimbraDomainMaxAccounts' ) ) ;
    $domain{$thisdomain}->{'dn'}                      = $entry->dn();
    if ( join(" ",$entry->get_value( "zimbraDomainType") ) eq 'alias' ) {
        $domain{$thisdomain}->{'zimbraDomainAliasTargetId'} = join(" ",$entry->get_value('zimbraDomainAliasTargetId'));
    }
}

print "Domain,Max,active,closed,locked,maintenance,total,aliases\n";
foreach my $thisdomain ( sort keys %domain ) {
    if ( $domain{$thisdomain}->{'zimbraDomainType'} eq 'local' ) {
        print "$thisdomain,";
        print $domain{$thisdomain}->{'zimbraDomainMaxAccounts'}.",";
        ($mesg) = $ldap->search( base => $domain{$thisdomain}->{'dn'},filter => "(objectClass=zimbraAccount)");
        $mesg->code && warn $mesg->error;
        $domain{$thisdomain}->{'total'} = $mesg->count;
        foreach my $entry ($mesg->entries) {
            $domain{$thisdomain}->{ join(" ",$entry->get_value( 'zimbraAccountStatus' )) }++;
        }
        foreach my $status ( @status ) {
            if ( $domain{$thisdomain}->{$status} ) {
                print $domain{$thisdomain}->{$status}.",";
            } else {
                $domain{$thisdomain}->{$status} = 0;
                print $domain{$thisdomain}->{$status}.",";
            }
        }
        my $aliaslist ='';
        $domain{$thisdomain}->{'aliascount'} = 0;
        foreach my $aliassearch ( sort keys %domain ) {
            if ( $domain{$aliassearch}->{'zimbraDomainType'} eq 'alias' ) {
                if ( $domain{$aliassearch}->{'zimbraDomainAliasTargetId'} eq $domain{$thisdomain}->{'zimbraId'}){
                    $aliaslist .= $aliassearch." ";
                    $domain{$thisdomain}->{'aliascount'}++;
                }
            }
        }
        if ( $domain{$thisdomain}->{'aliascount'} > 0 ) {
            print ",".$aliaslist;
        } else {
        }
        print "\n";
    }
}