mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-27 07:44:35 +02:00
2012-07-24 Miguel de Dios <miguel.dedios@artica.es>
* lib/PandoraFMS/Core.pm, lib/PandoraFMS/Tools.pm, lib/PandoraFMS/PredictionServer.pm: cleaned source code style. * lib/PandoraFMS/DB.pm: added functions "get_agent_status", "get_agent_modules" and "get_agentmodule_status". git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6803 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
parent
b063fc814c
commit
28bca04079
@ -1,3 +1,11 @@
|
|||||||
|
2012-07-24 Miguel de Dios <miguel.dedios@artica.es>
|
||||||
|
|
||||||
|
* lib/PandoraFMS/Core.pm, lib/PandoraFMS/Tools.pm,
|
||||||
|
lib/PandoraFMS/PredictionServer.pm: cleaned source code style.
|
||||||
|
|
||||||
|
* lib/PandoraFMS/DB.pm: added functions "get_agent_status",
|
||||||
|
"get_agent_modules" and "get_agentmodule_status".
|
||||||
|
|
||||||
2012-07-16 Dario Rodriguez <dario.rodriguez@artica.es>
|
2012-07-16 Dario Rodriguez <dario.rodriguez@artica.es>
|
||||||
|
|
||||||
* lib/PandoraFMS/Core.pm: Updated statistic queries from php code
|
* lib/PandoraFMS/Core.pm: Updated statistic queries from php code
|
||||||
|
@ -872,7 +872,11 @@ sub pandora_process_module ($$$$$$$$$;$) {
|
|||||||
my ($pa_config, $data_object, $agent, $module, $module_type,
|
my ($pa_config, $data_object, $agent, $module, $module_type,
|
||||||
$timestamp, $utimestamp, $server_id, $dbh, $extra_macros) = @_;
|
$timestamp, $utimestamp, $server_id, $dbh, $extra_macros) = @_;
|
||||||
|
|
||||||
logger($pa_config, "Processing module '" . safe_output($module->{'nombre'}) . "' for agent " . (defined ($agent) && $agent ne '' ? "'" . safe_output($agent->{'nombre'}) . "'" : 'ID ' . $module->{'id_agente'}) . ".", 10);
|
logger($pa_config,
|
||||||
|
"Processing module '" . safe_output($module->{'nombre'}) .
|
||||||
|
"' for agent " .
|
||||||
|
(defined ($agent) && $agent ne '' ? "'" . safe_output($agent->{'nombre'}) . "'" : 'ID ' . $module->{'id_agente'}) . ".",
|
||||||
|
1);
|
||||||
|
|
||||||
# Get agent information
|
# Get agent information
|
||||||
if (! defined ($agent) || $agent eq '') {
|
if (! defined ($agent) || $agent eq '') {
|
||||||
|
@ -22,6 +22,8 @@ use warnings;
|
|||||||
use DBI;
|
use DBI;
|
||||||
use PandoraFMS::Tools;
|
use PandoraFMS::Tools;
|
||||||
|
|
||||||
|
#use Data::Dumper;
|
||||||
|
|
||||||
require Exporter;
|
require Exporter;
|
||||||
|
|
||||||
our @ISA = ("Exporter");
|
our @ISA = ("Exporter");
|
||||||
@ -73,6 +75,9 @@ our @EXPORT = qw(
|
|||||||
get_user_exists
|
get_user_exists
|
||||||
is_agent_address
|
is_agent_address
|
||||||
is_group_disabled
|
is_group_disabled
|
||||||
|
get_agent_status
|
||||||
|
get_agent_modules
|
||||||
|
get_agentmodule_status
|
||||||
);
|
);
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
@ -96,7 +101,8 @@ sub db_connect ($$$$$$) {
|
|||||||
$dbh->{'mysql_enable_utf8'} = 1;
|
$dbh->{'mysql_enable_utf8'} = 1;
|
||||||
|
|
||||||
return $dbh;
|
return $dbh;
|
||||||
} elsif ($rdbms eq 'postgresql') {
|
}
|
||||||
|
elsif ($rdbms eq 'postgresql') {
|
||||||
$RDBMS = 'postgresql';
|
$RDBMS = 'postgresql';
|
||||||
|
|
||||||
# Connect to PostgreSQL
|
# Connect to PostgreSQL
|
||||||
@ -188,55 +194,169 @@ sub get_os_id ($$) {
|
|||||||
sub get_agent_group ($$) {
|
sub get_agent_group ($$) {
|
||||||
my ($dbh, $agent_id) = @_;
|
my ($dbh, $agent_id) = @_;
|
||||||
|
|
||||||
my $group_id = get_db_value ($dbh, "SELECT id_grupo FROM tagente WHERE id_agente = ?", $agent_id);
|
my $group_id = get_db_value ($dbh, "SELECT id_grupo
|
||||||
|
FROM tagente
|
||||||
|
WHERE id_agente = ?", $agent_id);
|
||||||
return 0 unless defined ($group_id);
|
return 0 unless defined ($group_id);
|
||||||
|
|
||||||
return $group_id;
|
return $group_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## SUB get_agent_name (agent_id)
|
## SUB get_agent_name (agent_id)
|
||||||
## Return agent name, given "agent_id"
|
## Return agent name, given "agent_id"
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub get_agent_name ($$) {
|
sub get_agent_name ($$) {
|
||||||
my ($dbh, $agent_id) = @_;
|
my ($dbh, $agent_id) = @_;
|
||||||
|
|
||||||
return get_db_value ($dbh, "SELECT nombre FROM tagente WHERE id_agente = ?", $agent_id);
|
return get_db_value ($dbh, "SELECT nombre
|
||||||
|
FROM tagente
|
||||||
|
WHERE id_agente = ?", $agent_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
|
## SUB agents_get_modules (agent_id, fields, filters)
|
||||||
|
## Return the list of modules, given "agent_id"
|
||||||
|
########################################################################
|
||||||
|
sub get_agent_modules ($$$$$) {
|
||||||
|
my ($pa_config, $dbh, $agent_id, $fields, $filters) = @_;
|
||||||
|
|
||||||
|
my $str_filter = '';
|
||||||
|
|
||||||
|
foreach my $key (keys $filters) {
|
||||||
|
$str_filter .= ' AND ' . $key . " = " . $filters->{$key};
|
||||||
|
}
|
||||||
|
|
||||||
|
my @rows = get_db_rows($dbh, "SELECT *
|
||||||
|
FROM tagente_modulo
|
||||||
|
WHERE id_agente = ?" . $str_filter, $agent_id);
|
||||||
|
|
||||||
|
return @rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################################
|
||||||
|
## SUB get_agentmodule_status (agent_module_id)
|
||||||
|
## Return agent module status. given "agent_module_id"
|
||||||
|
########################################################################
|
||||||
|
sub get_agentmodule_status($$$) {
|
||||||
|
my ($pa_config, $dbh, $agent_module_id) = @_;
|
||||||
|
|
||||||
|
my $status = get_db_value($dbh, 'SELECT estado
|
||||||
|
FROM tagente_estado
|
||||||
|
WHERE id_agente_modulo = ?', $agent_module_id);
|
||||||
|
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
########################################################################
|
||||||
|
## SUB get_get_status (agent_id)
|
||||||
|
## Return agent status, given "agent_id"
|
||||||
|
########################################################################
|
||||||
|
sub get_agent_status ($$$) {
|
||||||
|
my ($pa_config, $dbh, $agent_id) = @_;
|
||||||
|
|
||||||
|
my @modules = get_agent_modules ($pa_config, $dbh,
|
||||||
|
$agent_id, 'id_agente_modulo', {'disabled' => 0});
|
||||||
|
#logger($pa_config, Dumper(@modules), 5);
|
||||||
|
|
||||||
|
# The status are:
|
||||||
|
# 3 -> AGENT_MODULE_STATUS_UNKNOW
|
||||||
|
# 4 -> AGENT_MODULE_STATUS_CRITICAL_ALERT
|
||||||
|
# 1 -> AGENT_MODULE_STATUS_CRITICAL_BAD
|
||||||
|
# 2 -> AGENT_MODULE_STATUS_WARNING
|
||||||
|
# 0 -> AGENT_MODULE_STATUS_NORMAL
|
||||||
|
|
||||||
|
my $module_status = 3;
|
||||||
|
my $modules_async = 0;
|
||||||
|
foreach my $module (@modules) {
|
||||||
|
my $m_status = get_agentmodule_status($pa_config, $dbh,
|
||||||
|
$module->{'id_agente_modulo'});
|
||||||
|
|
||||||
|
#This is the order to check
|
||||||
|
# AGENT_MODULE_STATUS_CRITICAL_ALERT
|
||||||
|
# AGENT_MODULE_STATUS_CRITICAL_BAD
|
||||||
|
# AGENT_MODULE_STATUS_WARNING
|
||||||
|
# AGENT_MODULE_STATUS_UNKNOW
|
||||||
|
# AGENT_MODULE_STATUS_NORMAL
|
||||||
|
if ($m_status == 4) {
|
||||||
|
$module_status = 4;
|
||||||
|
}
|
||||||
|
elsif ($module_status != 4) {
|
||||||
|
if ($m_status == 1) {
|
||||||
|
$module_status = 1;
|
||||||
|
}
|
||||||
|
elsif ($module_status != 1) {
|
||||||
|
if ($m_status == 2) {
|
||||||
|
$module_status = 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ($m_status == 0) {
|
||||||
|
$module_status = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
my $module_type = get_db_value($dbh, 'SELECT id_tipo_modulo
|
||||||
|
FROM tagente_modulo
|
||||||
|
WHERE id_agente_modulo = ?', $module->{'id_agente_modulo'});
|
||||||
|
|
||||||
|
if (($module_type >= 21 && $module_type <= 23) ||
|
||||||
|
$module_type == 100) {
|
||||||
|
$modules_async++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
my $count_modules = scalar(@modules);
|
||||||
|
|
||||||
|
# If all the modules are asynchronous or keep alive, the group cannot be unknown
|
||||||
|
if ($modules_async < $count_modules) {
|
||||||
|
my $last_contact = get_db_value($dbh,
|
||||||
|
'SELECT (UNIX_TIMESTAMP(ultimo_contacto) + (intervalo * 2)) AS last_contact
|
||||||
|
FROM tagente WHERE id_agente = ?', $agent_id);
|
||||||
|
|
||||||
|
if ($last_contact < time ()) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $module_status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
########################################################################
|
||||||
## SUB get_module_agent_id (agent_module_id)
|
## SUB get_module_agent_id (agent_module_id)
|
||||||
## Return agent id, given "agent_module_id"
|
## Return agent id, given "agent_module_id"
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub get_module_agent_id ($$) {
|
sub get_module_agent_id ($$) {
|
||||||
my ($dbh, $agent_module_id) = @_;
|
my ($dbh, $agent_module_id) = @_;
|
||||||
|
|
||||||
return get_db_value ($dbh, "SELECT id_agente FROM tagente_modulo WHERE id_agente_modulo = ?", $agent_module_id);
|
return get_db_value ($dbh, "SELECT id_agente FROM tagente_modulo WHERE id_agente_modulo = ?", $agent_module_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## SUB get_agent_address (id_agente)
|
## SUB get_agent_address (id_agente)
|
||||||
## Return agent address, given "agent_id"
|
## Return agent address, given "agent_id"
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub get_agent_address ($$) {
|
sub get_agent_address ($$) {
|
||||||
my ($dbh, $agent_id) = @_;
|
my ($dbh, $agent_id) = @_;
|
||||||
|
|
||||||
return get_db_value ($dbh, "SELECT direccion FROM tagente WHERE id_agente = ?", $agent_id);
|
return get_db_value ($dbh, "SELECT direccion FROM tagente WHERE id_agente = ?", $agent_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## SUB get_module_name(module_id)
|
## SUB get_module_name(module_id)
|
||||||
## Return the module name, given "module_id"
|
## Return the module name, given "module_id"
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub get_module_name ($$) {
|
sub get_module_name ($$) {
|
||||||
my ($dbh, $module_id) = @_;
|
my ($dbh, $module_id) = @_;
|
||||||
|
|
||||||
return get_db_value ($dbh, "SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = ?", $module_id);
|
return get_db_value ($dbh, "SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = ?", $module_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## Return module id given the module name and agent id.
|
## Return module id given the module name and agent id.
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub get_agent_module_id ($$$) {
|
sub get_agent_module_id ($$$) {
|
||||||
my ($dbh, $module_name, $agent_id) = @_;
|
my ($dbh, $module_name, $agent_id) = @_;
|
||||||
|
|
||||||
@ -364,12 +484,12 @@ sub get_group_name ($$) {
|
|||||||
return get_db_value ($dbh, "SELECT nombre FROM tgrupo WHERE id_grupo = ?", $group_id);
|
return get_db_value ($dbh, "SELECT nombre FROM tgrupo WHERE id_grupo = ?", $group_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## Get a single column returned by an SQL query as a hash reference.
|
## Get a single column returned by an SQL query as a hash reference.
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub get_db_value ($$;@) {
|
sub get_db_value ($$;@) {
|
||||||
my ($dbh, $query, @values) = @_;
|
my ($dbh, $query, @values) = @_;
|
||||||
my @rows;
|
#my @rows;
|
||||||
|
|
||||||
# Cache statements
|
# Cache statements
|
||||||
my $sth = $dbh->prepare_cached($query);
|
my $sth = $dbh->prepare_cached($query);
|
||||||
@ -383,6 +503,7 @@ sub get_db_value ($$;@) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$sth->finish();
|
$sth->finish();
|
||||||
|
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,7 +513,7 @@ sub get_db_value ($$;@) {
|
|||||||
##########################################################################
|
##########################################################################
|
||||||
sub get_db_single_row ($$;@) {
|
sub get_db_single_row ($$;@) {
|
||||||
my ($dbh, $query, @values) = @_;
|
my ($dbh, $query, @values) = @_;
|
||||||
my @rows;
|
#my @rows;
|
||||||
|
|
||||||
# Cache statements
|
# Cache statements
|
||||||
my $sth = $dbh->prepare_cached($query);
|
my $sth = $dbh->prepare_cached($query);
|
||||||
@ -407,6 +528,7 @@ sub get_db_single_row ($$;@) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$sth->finish();
|
$sth->finish();
|
||||||
|
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -426,7 +548,8 @@ sub get_db_rows ($$;@) {
|
|||||||
while (my $row = $sth->fetchrow_hashref()) {
|
while (my $row = $sth->fetchrow_hashref()) {
|
||||||
if ($RDBMS eq 'oracle') {
|
if ($RDBMS eq 'oracle') {
|
||||||
push (@rows, {map { lc ($_) => $row->{$_} } keys (%{$row})});
|
push (@rows, {map { lc ($_) => $row->{$_} } keys (%{$row})});
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
push (@rows, $row);
|
push (@rows, $row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -576,7 +699,11 @@ sub add_new_address_agent ($$$) {
|
|||||||
sub get_addr_id ($$) {
|
sub get_addr_id ($$) {
|
||||||
my ($dbh, $addr) = @_;
|
my ($dbh, $addr) = @_;
|
||||||
|
|
||||||
my $addr_id = get_db_value ($dbh, 'SELECT id_a FROM taddress WHERE ip = ?', $addr);
|
my $addr_id = get_db_value ($dbh,
|
||||||
|
'SELECT id_a
|
||||||
|
FROM taddress
|
||||||
|
WHERE ip = ?', $addr);
|
||||||
|
|
||||||
return (defined ($addr_id) ? $addr_id : -1);
|
return (defined ($addr_id) ? $addr_id : -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -587,7 +714,11 @@ sub get_addr_id ($$) {
|
|||||||
sub get_agent_addr_id ($$$) {
|
sub get_agent_addr_id ($$$) {
|
||||||
my ($dbh, $addr_id, $agent_id) = @_;
|
my ($dbh, $addr_id, $agent_id) = @_;
|
||||||
|
|
||||||
my $agent_addr_id = get_db_value ($dbh, 'SELECT id_ag FROM taddress_agent WHERE id_a = ? AND id_agent = ?', $addr_id, $agent_id);
|
my $agent_addr_id = get_db_value ($dbh,
|
||||||
|
'SELECT id_ag
|
||||||
|
FROM taddress_agent
|
||||||
|
WHERE id_a = ?
|
||||||
|
AND id_agent = ?', $addr_id, $agent_id);
|
||||||
return (defined ($agent_addr_id) ? $agent_addr_id : -1);
|
return (defined ($agent_addr_id) ? $agent_addr_id : -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -608,7 +739,8 @@ sub db_do ($$;@) {
|
|||||||
sub is_agent_address ($$$) {
|
sub is_agent_address ($$$) {
|
||||||
my ($dbh, $id_agent, $id_addr) = @_;
|
my ($dbh, $id_agent, $id_addr) = @_;
|
||||||
|
|
||||||
my $id_ag = get_db_value ($dbh, 'SELECT id_ag FROM taddress_agent
|
my $id_ag = get_db_value ($dbh, 'SELECT id_ag
|
||||||
|
FROM taddress_agent
|
||||||
WHERE id_a = ?
|
WHERE id_a = ?
|
||||||
AND id_agent = ?', $id_addr, $id_agent);
|
AND id_agent = ?', $id_addr, $id_agent);
|
||||||
|
|
||||||
@ -671,8 +803,8 @@ sub db_concat ($$) {
|
|||||||
my ($element1, $element2) = @_;
|
my ($element1, $element2) = @_;
|
||||||
|
|
||||||
return " concat(" . $element1 . ", ' '," . $element2 . ") " if ($RDBMS eq 'mysql');
|
return " concat(" . $element1 . ", ' '," . $element2 . ") " if ($RDBMS eq 'mysql');
|
||||||
return " " . $element1 . " || ' ' || " . $element2 . " " if ($RDBMS eq 'oracle' or $RDBMS eq 'postgresql');
|
|
||||||
|
|
||||||
|
return " " . $element1 . " || ' ' || " . $element2 . " " if ($RDBMS eq 'oracle' or $RDBMS eq 'postgresql');
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package PandoraFMS::PredictionServer;
|
package PandoraFMS::PredictionServer;
|
||||||
##########################################################################
|
########################################################################
|
||||||
# Pandora FMS Prediction Server.
|
# Pandora FMS Prediction Server.
|
||||||
# Pandora FMS. the Flexible Monitoring System. http://www.pandorafms.org
|
# Pandora FMS. the Flexible Monitoring System. http://www.pandorafms.org
|
||||||
##########################################################################
|
########################################################################
|
||||||
# Copyright (c) 2005-2009 Artica Soluciones Tecnologicas S.L
|
# Copyright (c) 2005-2009 Artica Soluciones Tecnologicas S.L
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or
|
# This program is free software; you can redistribute it and/or
|
||||||
@ -36,6 +36,9 @@ use PandoraFMS::DB;
|
|||||||
use PandoraFMS::Core;
|
use PandoraFMS::Core;
|
||||||
use PandoraFMS::ProducerConsumerServer;
|
use PandoraFMS::ProducerConsumerServer;
|
||||||
|
|
||||||
|
#For debug
|
||||||
|
#use Data::Dumper;
|
||||||
|
|
||||||
# Inherits from PandoraFMS::ProducerConsumerServer
|
# Inherits from PandoraFMS::ProducerConsumerServer
|
||||||
our @ISA = qw(PandoraFMS::ProducerConsumerServer);
|
our @ISA = qw(PandoraFMS::ProducerConsumerServer);
|
||||||
|
|
||||||
@ -45,9 +48,9 @@ my %PendingTasks :shared;
|
|||||||
my $Sem :shared = Thread::Semaphore->new;
|
my $Sem :shared = Thread::Semaphore->new;
|
||||||
my $TaskSem :shared = Thread::Semaphore->new (0);
|
my $TaskSem :shared = Thread::Semaphore->new (0);
|
||||||
|
|
||||||
########################################################################################
|
########################################################################
|
||||||
# Prediction Server class constructor.
|
# Prediction Server class constructor.
|
||||||
########################################################################################
|
########################################################################
|
||||||
sub new ($$;$) {
|
sub new ($$;$) {
|
||||||
my ($class, $config, $dbh) = @_;
|
my ($class, $config, $dbh) = @_;
|
||||||
|
|
||||||
@ -57,12 +60,13 @@ sub new ($$;$) {
|
|||||||
my $self = $class->SUPER::new($config, 5, \&PandoraFMS::PredictionServer::data_producer, \&PandoraFMS::PredictionServer::data_consumer, $dbh);
|
my $self = $class->SUPER::new($config, 5, \&PandoraFMS::PredictionServer::data_producer, \&PandoraFMS::PredictionServer::data_consumer, $dbh);
|
||||||
|
|
||||||
bless $self, $class;
|
bless $self, $class;
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
########################################################################
|
||||||
# Run.
|
# Run.
|
||||||
###############################################################################
|
########################################################################
|
||||||
sub run ($) {
|
sub run ($) {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $pa_config = $self->getConfig ();
|
my $pa_config = $self->getConfig ();
|
||||||
@ -72,9 +76,9 @@ sub run ($) {
|
|||||||
$self->SUPER::run (\@TaskQueue, \%PendingTasks, $Sem, $TaskSem);
|
$self->SUPER::run (\@TaskQueue, \%PendingTasks, $Sem, $TaskSem);
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
########################################################################
|
||||||
# Data producer.
|
# Data producer.
|
||||||
###############################################################################
|
########################################################################
|
||||||
sub data_producer ($) {
|
sub data_producer ($) {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my ($pa_config, $dbh) = ($self->getConfig (), $self->getDBH ());
|
my ($pa_config, $dbh) = ($self->getConfig (), $self->getDBH ());
|
||||||
@ -83,7 +87,8 @@ sub data_producer ($) {
|
|||||||
my @rows;
|
my @rows;
|
||||||
|
|
||||||
if ($pa_config->{'pandora_master'} != 1) {
|
if ($pa_config->{'pandora_master'} != 1) {
|
||||||
@rows = get_db_rows ($dbh, 'SELECT tagente_modulo.id_agente_modulo, tagente_modulo.flag, last_execution_try
|
@rows = get_db_rows ($dbh, 'SELECT tagente_modulo.id_agente_modulo,
|
||||||
|
tagente_modulo.flag, last_execution_try
|
||||||
FROM tagente, tagente_modulo, tagente_estado
|
FROM tagente, tagente_modulo, tagente_estado
|
||||||
WHERE server_name = ?
|
WHERE server_name = ?
|
||||||
AND tagente_modulo.id_agente = tagente.id_agente
|
AND tagente_modulo.id_agente = tagente.id_agente
|
||||||
@ -95,17 +100,23 @@ sub data_producer ($) {
|
|||||||
AND (tagente_modulo.flag = 1
|
AND (tagente_modulo.flag = 1
|
||||||
OR (tagente_estado.last_execution_try + tagente_estado.current_interval) < UNIX_TIMESTAMP())
|
OR (tagente_estado.last_execution_try + tagente_estado.current_interval) < UNIX_TIMESTAMP())
|
||||||
ORDER BY last_execution_try ASC ', $pa_config->{'servername'});
|
ORDER BY last_execution_try ASC ', $pa_config->{'servername'});
|
||||||
} else {
|
}
|
||||||
@rows = get_db_rows ($dbh, 'SELECT DISTINCT(tagente_modulo.id_agente_modulo), tagente_modulo.flag, last_execution_try
|
else {
|
||||||
|
@rows = get_db_rows ($dbh, 'SELECT DISTINCT(tagente_modulo.id_agente_modulo),
|
||||||
|
tagente_modulo.flag, last_execution_try
|
||||||
FROM tagente, tagente_modulo, tagente_estado
|
FROM tagente, tagente_modulo, tagente_estado
|
||||||
WHERE ((server_name = ?) OR (server_name = ANY(SELECT name FROM tserver WHERE status = 0)))
|
WHERE ((server_name = ?)
|
||||||
|
OR (server_name = ANY(SELECT name
|
||||||
|
FROM tserver
|
||||||
|
WHERE status = 0)))
|
||||||
AND tagente_modulo.id_agente = tagente.id_agente
|
AND tagente_modulo.id_agente = tagente.id_agente
|
||||||
AND tagente.disabled = 0
|
AND tagente.disabled = 0
|
||||||
AND tagente_modulo.disabled = 0
|
AND tagente_modulo.disabled = 0
|
||||||
AND tagente_modulo.prediction_module != 0
|
AND tagente_modulo.prediction_module != 0
|
||||||
AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
|
AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
|
||||||
AND tagente_modulo.id_modulo = 5
|
AND tagente_modulo.id_modulo = 5
|
||||||
AND (tagente_modulo.flag = 1 OR (tagente_estado.last_execution_try + tagente_estado.current_interval) < UNIX_TIMESTAMP())
|
AND (tagente_modulo.flag = 1
|
||||||
|
OR (tagente_estado.last_execution_try + tagente_estado.current_interval) < UNIX_TIMESTAMP())
|
||||||
ORDER BY last_execution_try ASC', $pa_config->{'servername'});
|
ORDER BY last_execution_try ASC', $pa_config->{'servername'});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,29 +133,32 @@ sub data_producer ($) {
|
|||||||
return @tasks;
|
return @tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
########################################################################
|
||||||
# Data consumer.
|
# Data consumer.
|
||||||
###############################################################################
|
########################################################################
|
||||||
sub data_consumer ($$) {
|
sub data_consumer ($$) {
|
||||||
my ($self, $task) = @_;
|
my ($self, $task) = @_;
|
||||||
|
|
||||||
exec_prediction_module ($self->getConfig (), $task, $self->getServerID (), $self->getDBH ());
|
exec_prediction_module ($self->getConfig (), $task, $self->getServerID (), $self->getDBH ());
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# Execute prediction module.
|
# Execute prediction module.
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub exec_prediction_module ($$$$) {
|
sub exec_prediction_module ($$$$) {
|
||||||
my ($pa_config, $id_am, $server_id, $dbh) = @_;
|
my ($pa_config, $id_am, $server_id, $dbh) = @_;
|
||||||
|
|
||||||
# Get a full hash for agent_module record reference ($agent_module)
|
# Get a full hash for agent_module record reference ($agent_module)
|
||||||
my $agent_module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente_modulo = ?', $id_am);
|
my $agent_module = get_db_single_row ($dbh, 'SELECT *
|
||||||
|
FROM tagente_modulo
|
||||||
|
WHERE id_agente_modulo = ?', $id_am);
|
||||||
return unless defined $agent_module;
|
return unless defined $agent_module;
|
||||||
|
|
||||||
# Service modules
|
# Service modules
|
||||||
if ($agent_module->{'prediction_module'} == 2) {
|
if ($agent_module->{'prediction_module'} == 2) {
|
||||||
logger ($pa_config, "Executing service module " . $agent_module->{'nombre'}, 10);
|
logger ($pa_config, "Executing service module " . $agent_module->{'nombre'}, 5);
|
||||||
enterprise_hook ('exec_service_module', [$pa_config, $agent_module, $server_id, $dbh]);
|
enterprise_hook ('exec_service_module', [$pa_config, $agent_module, $server_id, $dbh]);
|
||||||
|
logger ($pa_config, "End execution", 5);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,18 +225,31 @@ sub exec_prediction_module ($$$$) {
|
|||||||
$temp1 = $week_utimestamp[$i] + $agent_module->{'module_interval'};
|
$temp1 = $week_utimestamp[$i] + $agent_module->{'module_interval'};
|
||||||
# Get data for week $i in the past
|
# Get data for week $i in the past
|
||||||
|
|
||||||
$average_interval = get_db_value ($dbh, 'SELECT AVG(datos) FROM tagente_datos WHERE id_agente_modulo = ? AND utimestamp > ? AND utimestamp < ?', $target_module->{'id_agente_modulo'}, $week_utimestamp[$i], $temp1);
|
$average_interval = get_db_value ($dbh, 'SELECT AVG(datos)
|
||||||
|
FROM tagente_datos
|
||||||
|
WHERE id_agente_modulo = ?
|
||||||
|
AND utimestamp > ?
|
||||||
|
AND utimestamp < ?', $target_module->{'id_agente_modulo'}, $week_utimestamp[$i], $temp1);
|
||||||
|
|
||||||
# Need to get data outside interval because no data.
|
# Need to get data outside interval because no data.
|
||||||
if (!(defined($average_interval)) || ($average_interval == 0)) {
|
if (!(defined($average_interval)) || ($average_interval == 0)) {
|
||||||
$last_data = get_db_value ($dbh, 'SELECT datos FROM tagente_datos WHERE id_agente_modulo = ? AND utimestamp > ? LIMIT 1', $target_module->{'id_agente_modulo'}, $week_utimestamp[$i]);
|
$last_data = get_db_value ($dbh, 'SELECT datos
|
||||||
|
FROM tagente_datos
|
||||||
|
WHERE id_agente_modulo = ?
|
||||||
|
AND utimestamp > ?
|
||||||
|
LIMIT 1', $target_module->{'id_agente_modulo'}, $week_utimestamp[$i]);
|
||||||
next unless defined ($last_data);
|
next unless defined ($last_data);
|
||||||
$first_data = get_db_value ($dbh, 'SELECT datos FROM tagente_datos WHERE id_agente_modulo = ? AND utimestamp < ? LIMIT 1', $target_module->{'id_agente_modulo'}, $temp1);
|
$first_data = get_db_value ($dbh, 'SELECT datos
|
||||||
|
FROM tagente_datos
|
||||||
|
WHERE id_agente_modulo = ?
|
||||||
|
AND utimestamp < ?
|
||||||
|
LIMIT 1', $target_module->{'id_agente_modulo'}, $temp1);
|
||||||
next unless defined ($first_data);
|
next unless defined ($first_data);
|
||||||
$sum_data++ if ($last_data != 0);
|
$sum_data++ if ($last_data != 0);
|
||||||
$sum_data++ if ($first_data != 0);
|
$sum_data++ if ($first_data != 0);
|
||||||
$week_data[$i] = ($sum_data > 0) ? (($last_data + $first_data) / $sum_data) : 0;
|
$week_data[$i] = ($sum_data > 0) ? (($last_data + $first_data) / $sum_data) : 0;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
$week_data[$i] = $average_interval;
|
$week_data[$i] = $average_interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,13 +277,17 @@ sub exec_prediction_module ($$$$) {
|
|||||||
}
|
}
|
||||||
$typical_deviation = ($n > 1) ? sqrt ($typical_deviation / ($n-1)) : 0;
|
$typical_deviation = ($n > 1) ? sqrt ($typical_deviation / ($n-1)) : 0;
|
||||||
|
|
||||||
my $current_value = get_db_value ($dbh, 'SELECT datos FROM tagente_estado WHERE id_agente_modulo = ?', $target_module->{'id_agente_modulo'});
|
my $current_value = get_db_value ($dbh, 'SELECT datos
|
||||||
|
FROM tagente_estado
|
||||||
|
WHERE id_agente_modulo = ?', $target_module->{'id_agente_modulo'});
|
||||||
if ( ($current_value > ($average - $typical_deviation)) && ($current_value < ($average + $typical_deviation)) ){
|
if ( ($current_value > ($average - $typical_deviation)) && ($current_value < ($average + $typical_deviation)) ){
|
||||||
$module_data = 1; # OK !!
|
$module_data = 1; # OK !!
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
$module_data = 0; # Out of predictions
|
$module_data = 0; # Out of predictions
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
# Prediction based on data
|
# Prediction based on data
|
||||||
$module_data = $average;
|
$module_data = $average;
|
||||||
}
|
}
|
||||||
@ -264,7 +295,9 @@ sub exec_prediction_module ($$$$) {
|
|||||||
my %data = ("data" => $module_data);
|
my %data = ("data" => $module_data);
|
||||||
pandora_process_module ($pa_config, \%data, '', $agent_module, '', $timestamp, $utimestamp, $server_id, $dbh);
|
pandora_process_module ($pa_config, \%data, '', $agent_module, '', $timestamp, $utimestamp, $server_id, $dbh);
|
||||||
|
|
||||||
my $agent_os_version = get_db_value ($dbh, 'SELECT os_version FROM tagente WHERE id_agente = ?', $agent_module->{'id_agente'});
|
my $agent_os_version = get_db_value ($dbh, 'SELECT os_version
|
||||||
|
FROM tagente
|
||||||
|
WHERE id_agente = ?', $agent_module->{'id_agente'});
|
||||||
|
|
||||||
if ($agent_os_version eq ''){
|
if ($agent_os_version eq ''){
|
||||||
$agent_os_version = $pa_config->{'servername'}.'_Prediction';
|
$agent_os_version = $pa_config->{'servername'}.'_Prediction';
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package PandoraFMS::Tools;
|
package PandoraFMS::Tools;
|
||||||
##########################################################################
|
########################################################################
|
||||||
# Tools Package
|
# Tools Package
|
||||||
# Pandora FMS. the Flexible Monitoring System. http://www.pandorafms.org
|
# Pandora FMS. the Flexible Monitoring System. http://www.pandorafms.org
|
||||||
##########################################################################
|
########################################################################
|
||||||
# Copyright (c) 2005-2011 Artica Soluciones Tecnologicas S.L
|
# Copyright (c) 2005-2011 Artica Soluciones Tecnologicas S.L
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or
|
# This program is free software; you can redistribute it and/or
|
||||||
@ -68,10 +68,10 @@ our @EXPORT = qw(
|
|||||||
safe_output
|
safe_output
|
||||||
);
|
);
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## SUB pandora_trash_ascii
|
## SUB pandora_trash_ascii
|
||||||
# Generate random ascii strings with variable lenght
|
# Generate random ascii strings with variable lenght
|
||||||
##########################################################################
|
########################################################################
|
||||||
|
|
||||||
sub pandora_trash_ascii {
|
sub pandora_trash_ascii {
|
||||||
my $config_depth = $_[0];
|
my $config_depth = $_[0];
|
||||||
@ -84,9 +84,9 @@ sub pandora_trash_ascii {
|
|||||||
return $output
|
return $output
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## Convert the $value encode in html entity to clear char string.
|
## Convert the $value encode in html entity to clear char string.
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub safe_input($) {
|
sub safe_input($) {
|
||||||
my $value = shift;
|
my $value = shift;
|
||||||
|
|
||||||
@ -131,9 +131,9 @@ sub safe_input($) {
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
## Convert the html entities to value encode to rebuild char string.
|
## Convert the html entities to value encode to rebuild char string.
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub safe_output($) {
|
sub safe_output($) {
|
||||||
my $value = shift;
|
my $value = shift;
|
||||||
|
|
||||||
@ -213,10 +213,10 @@ sub get_html_entities {
|
|||||||
|
|
||||||
return \%trans;
|
return \%trans;
|
||||||
}
|
}
|
||||||
##########################################################################
|
########################################################################
|
||||||
# SUB ascii_to_html (string)
|
# SUB ascii_to_html (string)
|
||||||
# Convert an ascii string to hexadecimal
|
# Convert an ascii string to hexadecimal
|
||||||
##########################################################################
|
########################################################################
|
||||||
|
|
||||||
sub ascii_to_html($) {
|
sub ascii_to_html($) {
|
||||||
my $ascii = shift;
|
my $ascii = shift;
|
||||||
@ -225,10 +225,10 @@ sub ascii_to_html($) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# SUB pandora_get_os (string)
|
# SUB pandora_get_os (string)
|
||||||
# Detect OS using a string, and return id_os
|
# Detect OS using a string, and return id_os
|
||||||
##########################################################################
|
########################################################################
|
||||||
|
|
||||||
sub pandora_get_os ($) {
|
sub pandora_get_os ($) {
|
||||||
my $command = $_[0];
|
my $command = $_[0];
|
||||||
@ -278,15 +278,16 @@ sub pandora_get_os ($) {
|
|||||||
else {
|
else {
|
||||||
return 10; # Unknown / Other
|
return 10; # Unknown / Other
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return 10;
|
return 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# Sub daemonize ()
|
# Sub daemonize ()
|
||||||
# Put program in background (for daemon mode)
|
# Put program in background (for daemon mode)
|
||||||
##########################################################################
|
########################################################################
|
||||||
|
|
||||||
sub pandora_daemonize {
|
sub pandora_daemonize {
|
||||||
my $pa_config = $_[0];
|
my $pa_config = $_[0];
|
||||||
@ -300,7 +301,6 @@ sub pandora_daemonize {
|
|||||||
|
|
||||||
# Store PID of this process in file presented by config token
|
# Store PID of this process in file presented by config token
|
||||||
if ($pa_config->{'PID'} ne "") {
|
if ($pa_config->{'PID'} ne "") {
|
||||||
|
|
||||||
if ( -e $pa_config->{'PID'} && open (FILE, $pa_config->{'PID'})) {
|
if ( -e $pa_config->{'PID'} && open (FILE, $pa_config->{'PID'})) {
|
||||||
$pid = <FILE> + 0;
|
$pid = <FILE> + 0;
|
||||||
close FILE;
|
close FILE;
|
||||||
@ -325,14 +325,14 @@ sub pandora_daemonize {
|
|||||||
# -------------------------------------------+
|
# -------------------------------------------+
|
||||||
|
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# SUB pandora_sendmail
|
# SUB pandora_sendmail
|
||||||
# Send a mail, connecting directly to MTA
|
# Send a mail, connecting directly to MTA
|
||||||
# param1 - config hash
|
# param1 - config hash
|
||||||
# param2 - Destination email addres
|
# param2 - Destination email addres
|
||||||
# param3 - Email subject
|
# param3 - Email subject
|
||||||
# param4 - Email Message body
|
# param4 - Email Message body
|
||||||
##########################################################################
|
########################################################################
|
||||||
|
|
||||||
sub pandora_sendmail {
|
sub pandora_sendmail {
|
||||||
|
|
||||||
@ -366,13 +366,13 @@ sub pandora_sendmail {
|
|||||||
|
|
||||||
if (sendmail %mail) {
|
if (sendmail %mail) {
|
||||||
return;
|
return;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
logger ($pa_config, "[ERROR] Sending email to $to_address with subject $subject", 1);
|
logger ($pa_config, "[ERROR] Sending email to $to_address with subject $subject", 1);
|
||||||
if (defined($Mail::Sendmail::error)){
|
if (defined($Mail::Sendmail::error)){
|
||||||
logger ($pa_config, "ERROR Code: $Mail::Sendmail::error", 5);
|
logger ($pa_config, "ERROR Code: $Mail::Sendmail::error", 5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
@ -394,7 +394,8 @@ sub is_numeric {
|
|||||||
my $NUMBER = qr{ ($SIGN?) ($DIGITS) }xms;
|
my $NUMBER = qr{ ($SIGN?) ($DIGITS) }xms;
|
||||||
if ( $val !~ /^${NUMBER}$/ ) {
|
if ( $val !~ /^${NUMBER}$/ ) {
|
||||||
return 0; #Non-numeric
|
return 0; #Non-numeric
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return 1; #Numeric
|
return 1; #Numeric
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -429,16 +430,17 @@ sub md5check {
|
|||||||
if ($buf =~ /$buf2/ ) {
|
if ($buf =~ /$buf2/ ) {
|
||||||
#print "MD5 Correct";
|
#print "MD5 Correct";
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
#print "MD5 Incorrect";
|
#print "MD5 Incorrect";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# SUB logger (pa_config, message, level)
|
# SUB logger (pa_config, message, level)
|
||||||
# Log to file
|
# Log to file
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub logger ($$;$) {
|
sub logger ($$;$) {
|
||||||
my ($pa_config, $message, $level) = @_;
|
my ($pa_config, $message, $level) = @_;
|
||||||
|
|
||||||
@ -457,9 +459,9 @@ sub logger ($$;$) {
|
|||||||
close (FILE);
|
close (FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# limpia_cadena (string) - Purge a string for any forbidden characters (esc, etc)
|
# limpia_cadena (string) - Purge a string for any forbidden characters (esc, etc)
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub limpia_cadena {
|
sub limpia_cadena {
|
||||||
my $micadena;
|
my $micadena;
|
||||||
$micadena = $_[0];
|
$micadena = $_[0];
|
||||||
@ -467,14 +469,15 @@ sub limpia_cadena {
|
|||||||
$micadena =~ s/[^\-\:\;\.\,\_\s\a\*\=\(\)a-zA-Z0-9]//g;
|
$micadena =~ s/[^\-\:\;\.\,\_\s\a\*\=\(\)a-zA-Z0-9]//g;
|
||||||
$micadena =~ s/[\n\l\f]//g;
|
$micadena =~ s/[\n\l\f]//g;
|
||||||
return $micadena;
|
return $micadena;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# clean_blank (string) - Purge a string for any blank spaces in it
|
# clean_blank (string) - Purge a string for any blank spaces in it
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub clean_blank {
|
sub clean_blank {
|
||||||
my $input = $_[0];
|
my $input = $_[0];
|
||||||
$input =~ s/\s//g;
|
$input =~ s/\s//g;
|
||||||
@ -553,9 +556,9 @@ sub enterprise_hook ($$) {
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
##########################################################################
|
########################################################################
|
||||||
# Prints a message to STDOUT at the given log level.
|
# Prints a message to STDOUT at the given log level.
|
||||||
##########################################################################
|
########################################################################
|
||||||
sub print_message ($$$) {
|
sub print_message ($$$) {
|
||||||
my ($pa_config, $message, $log_level) = @_;
|
my ($pa_config, $message, $log_level) = @_;
|
||||||
|
|
||||||
@ -581,10 +584,10 @@ sub get_tag_value ($$$) {
|
|||||||
return $def_value;
|
return $def_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
########################################################################
|
||||||
# Initialize some variables needed by the MD5 algorithm.
|
# Initialize some variables needed by the MD5 algorithm.
|
||||||
# See http://en.wikipedia.org/wiki/MD5#Pseudocode.
|
# See http://en.wikipedia.org/wiki/MD5#Pseudocode.
|
||||||
###############################################################################
|
########################################################################
|
||||||
my (@R, @K);
|
my (@R, @K);
|
||||||
sub md5_init () {
|
sub md5_init () {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user