App-MonM

 view release on metacpan or  search on metacpan

lib/App/MonM.pm  view on Meta::CPAN

    # Store
    my $db_file = File::Spec->catfile($self->datadir, App::MonM::Store::DB_FILENAME());
    my $store_conf = $self->config("store") || $self->config('dbi') || {file => $db_file};
       $store_conf = {file => $db_file} unless is_hash($store_conf);
    my %store_args = %$store_conf;
    $store_args{file} = $db_file unless ($store_args{file} || $store_args{dsn});
    my $store = App::MonM::Store->new(%store_args);
    $self->{store} = $store;
    #$self->debug(explain($store));

    # Notifier object init
    my %nargs = (config => $self->configobj);
    $self->{notifier} = $NOTIFIER_LOADED && lvalue($self->config("usemonotifier"))
        ? App::MonM::Notifier->new(%nargs)
        : App::MonM::QNotifier->new(%nargs);

    #$self->status($self->raise("Test error"));

    return $self; # CTK requires!
}
sub raise {
    my $self = shift;
    say STDERR red(@_);
    $self->log_error(sprintf(shift, @_));
    return 0;
}
sub store {
    my $self = shift;
    return $self->{store};
}
sub notifier {
    my $self = shift;
    return $self->{notifier};
}

__PACKAGE__->register_handler(
    handler     => "info",
    description => "Show statistic information",
    code => sub {
### CODE:
    my ($self, $meta, @arguments) = @_;
    my $store = $self->store;

    # General info
    printf("Hostname            : %s\n", HOSTNAME);
    printf("MonM version        : %s\n", $self->VERSION);
    printf("Date                : %s\n", _fdate());
    printf("Data dir            : %s\n", $self->datadir);
    printf("Temp dir            : %s\n", $self->tempdir);
    printf("Config file         : %s\n", $self->configfile);
    printf("Config status       : %s\n", $self->conf("loadstatus") ? green("OK") : magenta("ERROR: not loaded"));
    $self->raise($self->configobj->error) if !$self->configobj->status and length($self->configobj->error);
    printf("Notifier class      : %s\n", ref($self->notifier) || magenta("not initialized"));
    #$self->debug(explain($self->config)) if $self->conf("loadstatus") && $self->verbosemode;

    # DB status
    printf("DB DSN              : %s\n", $store->dsn);
    printf("DB status           : %s\n", $store->error ? red("ERROR") : green("OK"));
    my $db_is_ok = $store->error ? 0 : 1;
    if ($db_is_ok && $store->{file} && -e $store->{file}) {
        my $s = File::stat::stat($store->{file})->size;
        printf("DB file             : %s\n", $store->{file});
        printf("DB size             : %s\n", sprintf("%s (%d bytes)", _fbytes($s), $s));
        printf("DB modified         : %s\n", _fdate(File::stat::stat($store->{file})->mtime || 0));
    }
    $self->raise($store->error) unless $db_is_ok;

    # Checkets
    my @checkits = getCheckitByName($self->config("checkit"));
    my $noc = scalar(@checkits);
    printf("Checkits            : %s\n", $noc ? $noc : yellow("none"));
    if ($noc) {
        #print explain(\@checkits);
        my $tbl = Text::SimpleTable->new(
            [20, 'CHECKIT NAME'], # name
            [7,  'TYPE'], # type
            [7,  'TARGET'], # target
            [6,  'INTRVL'], # interval
            [3,  'TRG'], # trigger
            [27, 'RECIPIENTS'], # sendto
        );
        foreach my $ch (@checkits) {
            my $triggers = array($ch, "trigger");
            my $recipients = array($ch, "sendto");
            $tbl->row( # variant_stf($v->{source} // '', $src_len),
                variant_stf($ch->{name} // '', 20),
                $ch->{type} // 'http',
                $ch->{target} // 'status',
                $ch->{interval} || 0,
                scalar(@$triggers),
                join(", ", @$recipients),
            );
        }
        print $tbl->draw();
    }

    # Scheduler
    my $scheduler = App::MonM::Util::Scheduler->new;

    # Channels
    my $channels = $self->notifier->{ch_def} || {};
    my $chcnt = scalar(keys %$channels);
    printf("Channels            : %s\n", $chcnt ? $chcnt : yellow("none"));
    if ($chcnt) {
        my $tbl = Text::SimpleTable->new(
            [20, 'CHANNEL NAME'],
            [7,  'TYPE'],
            [42, 'TO (FROM)'],
            [7,  'ON/NOW'],
        );
        foreach my $ch_name (keys %$channels) {
            my $ch = hash($channels, $ch_name);
            $scheduler->add($ch_name, lvalue($ch, "at"));
            $tbl->row(
                $ch_name,
                lvalue($ch, "type") || '',
                lvalue($ch, "from")
                    ? sprintf("%s (%s)", lvalue($ch, "to") || '', lvalue($ch, "from"))
                    : lvalue($ch, "to") || '',
                sprintf("%s/%s",
                    lvalue($ch, "enable") || lvalue($ch, "enabled") ? 'Yes' : 'No',
                    $scheduler->check($ch_name) ? "Yes" : "No",
                ),
            );



( run in 0.732 second using v1.01-cache-2.11-cpan-39bf76dae61 )