#!/usr/bin/perl 
# Reads number of registered customers in argument ast_hostname (information read from a PBS server)
#############################
# my modules
use strict;
use warnings;
use POSIX;
use Switch;
use DBI;
use Socket;
#############################
# Globals

my $astman;
my $DB_HOST  = 'pbsXX.switzernet.com';
my $DB_USER  = 'login';
my $DB_PASS  = 'password';
my $DB_NAME  = 'porta-sip';
my $DB_TABLE = 'location';

my $ast_hostname = $ARGV[0];

my $dbh;
my $sqlQuery;
if ($DB_PASS ne '') {
  $dbh = DBI->connect_cached("DBI:mysql:$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS);
} else {
  $dbh = DBI->connect_cached("DBI:mysql:$DB_NAME;host=$DB_HOST", $DB_USER);
}
if( ! $dbh ) {
  print  -1;
  return -1;
}

$ast_hostname =~ m/^([0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3})$/;
if (!defined $1) {
  $ast_hostname = inet_ntoa(inet_aton($ast_hostname));
}

my $return   = 0;
my $query    = "select count(*) from ".$DB_TABLE." where domain = '".$ast_hostname."' and last_modified > DATE_SUB(NOW(), INTERVAL 60 MINUTE)";

$sqlQuery = $dbh->prepare($query) or $return = -1;
if ($return == 0) {
  $sqlQuery->execute or $return = -1;
}
my @row_ary  = $sqlQuery->fetchrow_array if ($return == 0);
$return      = $row_ary[0];
$sqlQuery->finish;
print $return;
$dbh->disconnect ;

