http://qs321.pair.com?node_id=299681

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello all!

I wrote a C program that inserts log messages from Linux syslog to mysql database. Now I wish to implement in Perl programs similar to Linux tail and less. tail for monitoring messages from now till Ctrl-c and less for viewing messages from given time to given time.

1. What Perl modules are recommended here?
2. Once I have a good module less seems not to complicated to implement (just quering the database and piping into Linux less). tail seems the real problem. Anyone has an idea how to do this? or is there something similar already doing that?

Thanks

Replies are listed 'Best First'.
Re: Mysql and tail
by matthewb (Curate) on Oct 16, 2003 at 06:50 UTC

    MySQL allows the non-standard `LIMIT' keyword in your SQL statements. Look it up in the MySQL docs.

    You could emulate `tail' by doing a SELECT with an ORDER BY clause and then LIMIT N at the end.

    MB
Re: Mysql and tail
by Taulmarill (Deacon) on Oct 16, 2003 at 08:44 UTC
    1. What Perl modules are recommended here?
    the modules you are looking for are DBI and DBD::MySQL.
Re: Mysql and tail
by sauoq (Abbot) on Oct 16, 2003 at 07:32 UTC
    tail for monitoring messages from now till Ctrl-c

    That statement makes me think that you are interested specifically in emulating tail -f. File::Tail exists. You probably won't be able to use it directly as your data resides in a database, but you might find the code to be helpful. Essentially, you are going to need to sit in a loop and poll your db every so often for changes. Nothing real difficult in that.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Mysql and tail
by hydo (Monk) on Oct 16, 2003 at 12:45 UTC
    || 1. What Perl modules are recommended here?

    | the modules you are looking for are DBI and DBD::MySQL.

    I would also suggest looking at Class::DBI. It's really really nice.

    As for how I would do it, and this is completely untested but, I would probably insert a column just for keeping track of line numbers. The program that pulls the logs in can increment this easily and you can do the same on the 'tail'-ish program.

    When your dbtail is run, it finds the highest number in that column, backs up say 5 lines or so, displays that to the highest number. Then in a loop or something like that, query for a higher number than your last count.

    I'm pretty tired and that could very well not work at all, but at the moment it seems like a good start.

Re: Mysql and tail
by Abigail-II (Bishop) on Oct 16, 2003 at 08:22 UTC
    That's a very strange concept to do for a database. But, if your database has triggers, you could set up an insert trigger that inserts new rows in a temporary table that you can query, or, if your database supports calls to outside programs, call an outside program.

    However, MySQL, being a rather limited database, doesn't have triggers.

    Anyway, this isn't a Perl problem at all, is it?

    Abigail