?
Solved

file counter

Posted on 2005-04-11
11
Medium Priority
?
166 Views
Last Modified: 2010-03-05
i want to read counter value from different server

i m running my cgi on Box A
$c = File::CounterFile->new("/l/apache-1.3.28-20031223/cgi-bin/out-of-service/__noofhits");     will not work because

the problem is /l/apache-1.3.28-20031223/cgi-bin/out-of-service/__noofhits is on box B

how do i do that
0
Comment
Question by:shahrahulb
  • 4
  • 4
9 Comments
 
LVL 1

Author Comment

by:shahrahulb
ID: 13753136
also is there any fater way of reading the counter, because i have to read 4 such counters from different server on my home page
0
 
LVL 18

Expert Comment

by:kandura
ID: 13753442
Consider using a database.
0
 
LVL 1

Author Comment

by:shahrahulb
ID: 13753456
that's fine....

but how do i read the counter value which is present on diffferent server
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:shahrahulb
ID: 13753514
got it :-)

first i will scp to my box A and then read it
0
 
LVL 1

Author Comment

by:shahrahulb
ID: 13753537
i m doing scp 'fole from A' 'file at B'

while doing scp is there a way to find if the file at b really exists or not...becuase if the file does not exists, i don't want my cgi script to break
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13753643
for checking existence on A

$ret_code=system('ssh -l target_user_name_if_required [[ -f /path/to/file ]]')
then check value of $ret_code, if non zero, file doesnt exist on A.

for checking existence on B

$ret_code=system('[[ -f /path/to/file ]]')
then check value of $ret_code, if non zero, file doesnt exist on B.
0
 
LVL 18

Accepted Solution

by:
kandura earned 2000 total points
ID: 13754187
I'd set up a database on one server, make sure all your web servers can connect to it, and then do everything through DBI. I'd abstract it out into a module, too.

Something like this would probably work in Mysql:

### table definition

    create table `counters` (
        counter_id  varchar(64) not null primary key,
        value       int unsigned not null default 0
    );


         
Here's a counter class that uses a database.


    package My::DBI::Counter;
    use DBI;
    use Carp   qw(carp croak);
    use strict;

    use overload (
            '++' => \&inc,
            '--' => \&dec,
            '""' => \&value,
            fallback=>1,
            );

    our ($DSN, $LOGIN, $PASSWORD);

    =pod

    =head1 My::DBI::Counter - named counters via DBI

    =head1 Synopsis

        use My::DBI::Counter;
        $c = new My::DBI::Counter(countername => 'my counter');
        print $c->value;
        $c->inc;

    =head1 Description

    This module creates and maintains named counters in a database table named counters.
    Connection settings can be set in the constructor, or by using the package variables $DSN, $LOGIN and $PASSWORD.
    It supports operator overloading for increment (++), decrement (--) and stringification ("").
    It should perform well in persistent environments, since it uses the connect_cached and prepare_cached methods of DBI.

    =head1 Methods

    =over

    =item new

    Creates a new counter instance. Accepts named parameters for the counter name, the dbi connection string, dbi login and dbi password:

        countername - required
        dsn         - overrides $My::DBI::Counter::DSN
        login       - overrides $My::DBI::Counter::LOGIN
        password    - overrides $My::DBI::Counter::PASSWORD

        Examples:
        $c = new My::DBI::Counter(countername => 'my counter');
        $c = new My::DBI::Counter(countername => 'my counter, dsn=>'dbi:mysql:mydb', login=>'username', password=>'secret');

    =cut

    sub new {
        my $pkg = shift;

        my %opts          = @_;

        my $self = {
            dsn         => $opts{dsn}      || $DSN,
            login       => $opts{login}    || $LOGIN,
            password    => $opts{password} || $PASSWORD,
            countername => $opts{countername},
            };

        bless $self, $pkg;

        $self->_init;

        $self;
    }

    sub _init {
        my $self = shift;
        my $db = DBI->connect_cached($self->{dsn}, $self->{login}, $self->{password}, {PrintError=>0, RaiseError=>1});
        $self->{db} = $db;

        # create counter record if not exists
        eval {
            $db->do(q{insert into counters (counter_id) values (?)}, undef, $self->{countername});
        };
    }

    sub db {
        my $self = shift;

        ### TODO: add connection checking here    
        return $self->{db};
    }

    sub add {
        my ($self, $add) = @_;
        my $dbh     = $self->db;
        my $sth_set = $dbh->prepare_cached(q{update counters set value=value+? where counter_id=?});
        $sth_set->execute($add, $self->{countername});
    }

    =pod

    =item inc

    increases the counter by one.

        $c->inc;
        # or using overload:
        $c++;

    =cut

    sub inc {
        my $self = shift;
        $self->add(1);
    }

    =pod

    =item dec

    decreases the counter by one.

        $c->dec;
        # or using overload:
        $c--;

    =cut

    sub dec {
        my $self = shift;
        $self->add(-1);
    }

    =pod

    =item value

    returns the current value of the counter.

        print $c->value;
        # or using overload:
        print $c;

    =cut

    sub value {
        my $self    = shift;
        my $dbh     = $self->db;
        my $sth_get = $dbh->prepare_cached(q{select value from counters where counter_id=?});

        $sth_get->execute($self->{countername});
        my ($v) = $sth_get->fetchrow_array;
        $sth_get->finish;

        return $v;
    }

    =pod

    =back

    =head1 See also

    L<File::CounterFile|http://search.cpan.org/~gaas/File-CounterFile-1.04/CounterFile.pm>

    =head1 Copyright

    Copyright (c) 2005 Kandura. All rights reserved.

    This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

    =head1 Author

    Kandura L<ee@rhesa.com>.

    =cut

    1;
0
 
LVL 18

Expert Comment

by:kandura
ID: 13754199
sample code:

    use My::DBI::Counter;

    $My::DBI::Counter::DSN         = 'dbi:mysql:test';
    $My::DBI::Counter::LOGIN       = 'user';
    $My::DBI::Counter::PASSWORD    = 'pass';

    my $c = new My::DBI::Counter(countername => 'test');
   
    print "value is $c$/";
    $c++;
    print "value is ", $c->value, $/;
    $c->inc;
    print "value is ", $c, $/;
0
 
LVL 18

Expert Comment

by:kandura
ID: 13795072
I'd like to report that I've released this module on CPAN as DBIx::Counter.

See http://search.cpan.org/~rhesa/DBIx-Counter-0.01/ for details, docs, etc.

Now it's only a "cpan DBIx::Counter" away :-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question