#!/usr/bin/perl  -w
use strict;
use DBI;
#definition of variables
my $db="db_name";
my $host="localhost";
my $user="user";
my $password="password";
my $interval = 60;
my $time = localtime time;
my $pretime = localtime (time - $interval * 60);

my $state = "OK";

my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);

#connect to MySQL database
my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",$user,$password, {PrintError => 0});

if (!$dbh) {
        print "CRITICAL ERROR - Unable to connect to database $db with user $user - $DBI::errstr";
        exit $ERRORS{"CRITICAL"};
}

my $stmt = "SELECT *
            FROM table1, table2
            WHERE timestamp < localtime
               && timestamp > DATE_SUB(NOW(), INTERVAL $interval MINUTE)
               && ...";

#prepare the query
my $sth = $dbh->prepare($stmt);

#execute the query
if (!$sth->execute()) {
        print "CRITICAL ERROR - Unable to execute query on $db";
        exit $ERRORS{"CRITICAL"};
}

my $results = $sth->rows;

if ($sth->err()) {
        print "CRITICAL ERROR - Error in retrieving results on $db";
        exit $ERRORS{"CRITICAL"};
}

#Check for new entry count
if ($results == 1) {
        print "OK - $results new entry in $db (0&lt;=entries&lt;=30 in $interval minutes)";
        print "<br>timestamp: $time";
} elsif ($results <= 30) {
        print "OK - $results new entries in $db (0&lt;=entry&lt;=30 in $interval minutes)";
        print "<br>timestamp: $time";
} elsif ($results <= 60) {
        print "WARNING - $results new entries in $db (30&lt;entries&lt;= 60 in $interval minutes)";
        print "<br>timestamp: $time";
        $state = "WARNING";
} else {
        print "CRITICAL - $results new entries in $db (entries&gt;60 in $interval minutes)";
        print "<br>timestamp: $time";
        $state = "CRITICAL";
}

$sth->finish();
$dbh->disconnect();
exit $ERRORS{$state};