#!/usr/bin/perl

    use Carp;
    use strict;
    use Asterisk::AMI;

    my $ret = undef;
    if ( ! eval "require Asterisk::AMI;" ) {
        $ret = "Asterisk::AMI not found";
    };

    if ( $ARGV[ 0 ] and $ARGV[ 0 ] eq "config" ) {
        print "graph_title Asterisk Fax - Cancelled Faxes (T.38 and G.711)\n";
        print "graph_args --base 1000 -l 0\n";
        print "graph_vlabel Number of Cancelled Faxes\n";
        print "graph_category other\n";
        print "t38_cancelled.draw AREA\n";
        print "t38_cancelled.label Cancelled T.38 Faxes\n";
        print "g711_cancelled.draw AREA\n";
        print "g711_cancelled.label Cancelled G.711 Faxes\n";
        exit ( 0 );
    };

    #my $host     = exists $ENV{ 'host' } ? $ENV{ 'host' } : "127.0.0.1";
    #my $port     = exists $ENV{ 'port' } ? $ENV{ 'port' } : "5038";
    #my $username = $ENV{ 'username' };
    #my $secret   = $ENV{ 'secret'   };

    my $username = 'manager';
    my $host     = '192.168.1.70';
    my $port     = '5038';
    my $secret   = 'insecure';
    my $timeout  = '5';

    my $astman = Asterisk::AMI->new(PeerAddr => "$host",
                                    PeerPort => "$port",
                                    Username => "$username",
                                    Secret   => "$secret"
    );

    croak "Unable to connect to asterisk" unless ( $astman );
    my $actionid = $astman->send_action({ Action => 'Command',
                                          Command => 'fax show stats',
                                          $timeout});
    my $response = $astman->get_response( $actionid );
    my $arrayref = $response->{CMD};
    my $null     = qq{};
    my ( %faxstats, $section );
    foreach my $line ( @$arrayref ) {
        next if ( ( ! $line ) || ( $line =~ /-----------/ ) );
        my ( $key, $value ) = split( ':', $line );
        $section = $key if ( $value eq $null );
        $key     =~ s/\s+$//g;
        $value   =~ s/^\s+//g;
        $faxstats{ "$section" }{ "$key" } = $value if ( $value ne $null );
    };
    $astman->disconnect;

    print "t38_cancelled.value $faxstats{'Digium T.38'}{'Canceled'}\n";
    print "g711_cancelled.value $faxstats{'Digium G.711'}{'Canceled'}\n";

    exit( 0 );



