#!/usr/bin/perl

($warning, $critical)=@ARGV;

use DBI;
use POSIX;
use Net::Ping;
use IO::Socket;

$dbh = DBI->connect ( "dbi:mysql:dbname=ocsweb;host=localhost","root", "oppass");
if ( !defined $dbh ) {
	die "Cannot connect to database!\n";
}

$sql="select  ID, DEVICEID, NAME, OSNAME, OSVERSION from hardware";
$sth=$dbh->prepare($sql);
if ( !defined $sth ) {
   die "Cannot prepare statement: $DBI::errstr\n";
   exit(1);
}
$sth->execute;

$count=0;
while ( ($id, $deviceid, $name, $osname, $osversion) = $sth->fetchrow()) {
	print "$id, $deviceid, $name, $osname, $osversion\n";
	@parts=split(/[-]+/,$name);
	$search_string="Server";
	if ($#parts>1) {
		$Unidade=$parts[0];
		$UF=$parts[1];
		$tipo="Estação de Trabalho";
		$sql_update="UPDATE accountinfo SET Unidade='$Unidade', UF='$UF', Tipo='$tipo' where HARDWARE_ID='$id'";
                $sth2=$dbh->prepare($sql_update);
                $sth2->execute();
                print "$sql_update\n";
	}
	elsif ($osname=~/$search_string/) {
		$tipo="Servidor";
		$sql_update="UPDATE accountinfo SET Tipo='$tipo' where HARDWARE_ID='$id'";
		$sth2=$dbh->prepare($sql_update);
		$sth2->execute();
		print "$sql_update\n";
	}
	#print "$sql_update\n";
}
exit(0);

# Function the generates the logfile
sub logger {
   local ($hostname, $ok)=@_;
   $reason="Ok";
   if ($ok==256) {
      $reason="Timeout";
   }
   if ($ok==1) {
      $reason="Down";
   }
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
   $wday++;
   $yday++;
   $isdst++;
   $mon++;
   $year+=1900;
   open(LOGGER, ">>$LOGFILE");
   print LOGGER "$year/$mon/$mday $hour:$min:$sec - $hostname - $reason\n";
   close(LOGGER);
}

