2014-07-25 Miguel de Dios <miguel.dedios@artica.es>

* lib/PandoraFMS/GIS.pm, lib/PandoraFMS/DB.pm,
	lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
	lib/PandoraFMS/DataServer.pm: tiny fixes for to improve the support
	of PostgreSQL.




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@10356 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
mdtrooper 2014-07-25 13:15:24 +00:00
parent b4992dcbc5
commit df64061283
5 changed files with 198 additions and 117 deletions

View File

@ -1201,8 +1201,10 @@ sub pandora_planned_downtime_disabled_once_stop($$) {
# Stop executed downtimes (enable agents and disable_agents_alerts)
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_downtime != "quiet" AND type_execution="once"
AND executed = 1 AND date_to <= ?', $utimestamp);
WHERE type_downtime != ' . $RDBMS_QUOTE_STRING. 'quiet' . $RDBMS_QUOTE_STRING. '
AND type_execution = ' . $RDBMS_QUOTE_STRING. 'once' . $RDBMS_QUOTE_STRING. '
AND executed = 1
AND date_to <= ?', $utimestamp);
foreach my $downtime (@downtimes) {
@ -1234,7 +1236,8 @@ sub pandora_planned_downtime_disabled_once_start($$) {
# Start pending downtimes (disable agents and disable_agents_alerts)
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_downtime != "quiet" AND type_execution="once"
WHERE type_downtime != ' . $RDBMS_QUOTE_STRING . 'quiet' . $RDBMS_QUOTE_STRING . '
AND type_execution = ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING . '
AND executed = 0 AND date_from <= ?
AND date_to >= ?', $utimestamp, $utimestamp);
@ -1431,7 +1434,8 @@ sub pandora_planned_downtime_quiet_once_stop($$) {
# Stop pending downtimes
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_downtime = "quiet" AND type_execution="once"
WHERE type_downtime = ' . $RDBMS_QUOTE_STRING . 'quiet' . $RDBMS_QUOTE_STRING . '
AND type_execution = ' . $RDBMS_QUOTE_STRING. 'once' . $RDBMS_QUOTE_STRING . '
AND executed = 1 AND date_to <= ?', $utimestamp);
foreach my $downtime (@downtimes) {
@ -1471,7 +1475,8 @@ sub pandora_planned_downtime_quiet_once_start($$) {
# Start pending downtimes
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_downtime = "quiet" AND type_execution="once"
WHERE type_downtime = ' . $RDBMS_QUOTE_STRING . 'quiet' . $RDBMS_QUOTE_STRING . '
AND type_execution = ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING . '
AND executed = 0 AND date_from <= ?
AND date_to >= ?', $utimestamp, $utimestamp);
@ -1525,7 +1530,7 @@ sub pandora_planned_downtime_monthly_start($$) {
# Start pending downtimes
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_periodicity="monthly"
WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'monthly' . $RDBMS_QUOTE_STRING . '
AND executed = 0
AND periodically_day_from <= ?
AND periodically_day_to >= ?',
@ -1627,9 +1632,9 @@ sub pandora_planned_downtime_monthly_stop($$) {
# Start pending downtimes
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_periodicity = "monthly"
WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'monthly' . $RDBMS_QUOTE_STRING . '
AND executed = 1
AND type_execution <> "once"');
AND type_execution <> ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING);
foreach my $downtime (@downtimes) {
#Convert to identical type.
@ -1703,7 +1708,7 @@ sub pandora_planned_downtime_weekly_start($$) {
# Start pending downtimes
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_periodicity="weekly"
WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'weekly' . $RDBMS_QUOTE_STRING . '
AND executed = 0');
foreach my $downtime (@downtimes) {
@ -1825,8 +1830,8 @@ sub pandora_planned_downtime_weekly_stop($$) {
# Start pending downtimes
my @downtimes = get_db_rows($dbh, 'SELECT *
FROM tplanned_downtime
WHERE type_periodicity = "weekly"
AND type_execution <> "once"
WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'weekly' . $RDBMS_QUOTE_STRING . '
AND type_execution <> ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING . '
AND executed = 1');
foreach my $downtime (@downtimes) {
@ -2145,7 +2150,7 @@ sub pandora_update_gis_data ($$$$$$$$$) {
Create a template module.
=cut
##########################################################################
########################################################################
sub pandora_create_template_module ($$$$;$$$) {
my ($pa_config, $dbh, $id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby) = @_;
@ -2154,16 +2159,27 @@ sub pandora_create_template_module ($$$$;$$$) {
$standby = 0 unless defined $standby;
my $module_name = get_module_name($dbh, $id_agent_module);
return db_insert ($dbh, 'id', "INSERT INTO talert_template_modules (`id_agent_module`, `id_alert_template`, `id_policy_alerts`, `disabled`, `standby`, `last_reference`) VALUES (?, ?, ?, ?, ?, ?)", $id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby, time);
return db_insert ($dbh,
'id',
"INSERT INTO talert_template_modules(
" . $RDBMS_QUOTE . "id_agent_module" . $RDBMS_QUOTE . ",
" . $RDBMS_QUOTE . "id_alert_template" . $RDBMS_QUOTE . ",
" . $RDBMS_QUOTE . "id_policy_alerts" . $RDBMS_QUOTE . ",
" . $RDBMS_QUOTE . "disabled" . $RDBMS_QUOTE . ",
" . $RDBMS_QUOTE . "standby" . $RDBMS_QUOTE . ",
" . $RDBMS_QUOTE . "last_reference" . $RDBMS_QUOTE . ")
VALUES (?, ?, ?, ?, ?, ?)",
$id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby, time);
}
##########################################################################
########################################################################
=head2 C<< pandora_update_template_module(I<$pa_config>, I<$dbh>, I<$id_alert>, I<$id_policy_alerts>, I<$disabled>, I<$standby>) >>
Update a template module.
=cut
##########################################################################
########################################################################
sub pandora_update_template_module ($$$;$$$) {
my ($pa_config, $dbh, $id_alert, $id_policy_alerts, $disabled, $standby) = @_;
@ -2172,16 +2188,22 @@ sub pandora_update_template_module ($$$;$$$) {
$disabled = 0 unless defined $disabled;
$standby = 0 unless defined $standby;
db_do ($dbh, "UPDATE talert_template_modules SET `id_policy_alerts` = ?, `disabled` = ?, `standby` = ? WHERE id = ?", $id_policy_alerts, $disabled, $standby, $id_alert);
db_do ($dbh,
"UPDATE talert_template_modules
SET " . $RDBMS_QUOTE . "id_policy_alerts" . $RDBMS_QUOTE . " = ?,
" . $RDBMS_QUOTE . "disabled" . $RDBMS_QUOTE . " = ?,
" . $RDBMS_QUOTE . "standby" . $RDBMS_QUOTE . " = ?
WHERE id = ?",
$id_policy_alerts, $disabled, $standby, $id_alert);
}
##########################################################################
########################################################################
=head2 C<< pandora_create_template_module_action(I<$pa_config>, I<$parameters>, I<$dbh>) >>
Create a template action.
=cut
##########################################################################
########################################################################
sub pandora_create_template_module_action ($$$) {
my ($pa_config, $parameters, $dbh) = @_;
@ -2192,26 +2214,26 @@ sub pandora_create_template_module_action ($$$) {
return $action_id;
}
##########################################################################
########################################################################
=head2 C<< pandora_delete_all_template_module_actions(I<$dbh>, I<$template_module_id>) >>
Delete all actions of policy template module.
=cut
##########################################################################
########################################################################
sub pandora_delete_all_template_module_actions ($$) {
my ($dbh, $template_module_id) = @_;
return db_do ($dbh, 'DELETE FROM talert_template_module_actions WHERE id_alert_template_module = ?', $template_module_id);
}
##########################################################################
########################################################################
=head2 C<< pandora_update_agent_address(I<$pa_config>, I<$agent_id>, I<$address>, I<$dbh>) >>
Update the address of an agent.
=cut
##########################################################################
########################################################################
sub pandora_update_agent_address ($$$$$) {
my ($pa_config, $agent_id, $agent_name, $address, $dbh) = @_;
@ -2306,9 +2328,13 @@ sub pandora_create_module ($$$$$$$$$$) {
$status = 0;
}
my $module_id = db_insert($dbh, 'id_agente_modulo', 'INSERT INTO tagente_modulo (id_agente, id_tipo_modulo, nombre, max, min, post_process, descripcion, module_interval, id_modulo)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_id, $module_type_id, safe_input($module_name), $max, $min, $post_process, $description, $interval);
db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, id_agente, estado, last_status, last_known_status, last_try) VALUES (?, ?, ?, ?, ?, \'1970-01-01 00:00:00\')', $module_id, $agent_id, $status, $status, $status);
my $module_id = db_insert($dbh, 'id_agente_modulo',
'INSERT INTO tagente_modulo (id_agente, id_tipo_modulo, nombre, max, min, post_process, descripcion, module_interval, id_modulo)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)',
$agent_id, $module_type_id, safe_input($module_name), $max, $min, $post_process, $description, $interval);
db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, id_agente, estado, last_status, last_known_status, last_try)
VALUES (?, ?, ?, ?, ?, \'1970-01-01 00:00:00\')',
$module_id, $agent_id, $status, $status, $status);
# Update the module status count. When the module is created disabled dont do it
pandora_mark_agent_for_module_update ($dbh, $agent_id);
@ -2514,8 +2540,16 @@ sub pandora_create_module_tags ($$$$) {
}
foreach my $tag_name (split (',', $serialized_tags)) {
my $tag_id = get_db_value ($dbh, "SELECT id_tag FROM ttag WHERE name = ?", $tag_name);
db_insert ($dbh, 'id_tag', "INSERT INTO ttag_module (`id_tag`, `id_agente_modulo`) VALUES (?, ?)", $tag_id, $id_agent_module);
my $tag_id = get_db_value ($dbh,
"SELECT id_tag FROM ttag WHERE name = ?", $tag_name);
db_insert ($dbh,
'id_tag',
"INSERT INTO ttag_module(
" . $RDBMS_QUOTE . "id_tag" . $RDBMS_QUOTE . ",
" . $RDBMS_QUOTE . "id_agente_modulo" . $RDBMS_QUOTE . ")
VALUES (?, ?)",
$tag_id, $id_agent_module);
}
}
@ -3990,11 +4024,24 @@ sub pandora_self_monitoring ($$) {
my $queued_modules = get_db_value ($dbh, "SELECT SUM(queued_modules) FROM tserver WHERE name = '".$pa_config->{"servername"}."'");
if (!defined($queued_modules)){
if (!defined($queued_modules)) {
$queued_modules = 0;
}
my $dbmaintance = get_db_value ($dbh, "SELECT COUNT(*) FROM tconfig WHERE token = 'db_maintance' AND value > UNIX_TIMESTAMP() - 86400");
my $dbmaintance;
if ($RDBMS eq 'postgresql') {
$dbmaintance = get_db_value ($dbh,
"SELECT COUNT(*)
FROM tconfig
WHERE token = 'db_maintance'
AND NULLIF(value, '')::int > UNIX_TIMESTAMP() - 86400");
}
else {
$dbmaintance = get_db_value ($dbh,
"SELECT COUNT(*)
FROM tconfig
WHERE token = 'db_maintance' AND value > UNIX_TIMESTAMP() - 86400");
}
$xml_output .=" <module>";
$xml_output .=" <name>Database Maintenance</name>";

View File

@ -86,14 +86,18 @@ our @EXPORT = qw(
get_agentmodule_data
$RDBMS
$RDBMS_QUOTE
$RDBMS_QUOTE_STRING
);
# Relational database management system in use
our $RDBMS = '';
# Character used to quote reserved words in the current RDBMS
# For fields, character used to quote reserved words in the current RDBMS
our $RDBMS_QUOTE = '';
# For strings, Character used to quote in the current RDBMS
our $RDBMS_QUOTE_STRING = '';
##########################################################################
## Connect to the DB.
##########################################################################
@ -103,6 +107,7 @@ sub db_connect ($$$$$$) {
if ($rdbms eq 'mysql') {
$RDBMS = 'mysql';
$RDBMS_QUOTE = '`';
$RDBMS_QUOTE_STRING = '"';
# Connect to MySQL
my $dbh = DBI->connect("DBI:mysql:$db_name:$db_host:$db_port", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
@ -119,15 +124,18 @@ sub db_connect ($$$$$$) {
elsif ($rdbms eq 'postgresql') {
$RDBMS = 'postgresql';
$RDBMS_QUOTE = '"';
$RDBMS_QUOTE_STRING = "'";
# Connect to PostgreSQL
my $dbh = DBI->connect("DBI:Pg:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
return undef unless defined ($dbh);
return $dbh;
} elsif ($rdbms eq 'oracle') {
}
elsif ($rdbms eq 'oracle') {
$RDBMS = 'oracle';
$RDBMS_QUOTE = '"';
$RDBMS_QUOTE_STRING = '"';
# Connect to Oracle
my $dbh = DBI->connect("DBI:Oracle:dbname=$db_name;host=$db_host;port=$db_port;sid=pandora", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
@ -641,6 +649,7 @@ sub db_insert ($$$;@) {
my ($dbh, $index, $query, @values) = @_;
my $insert_id = undef;
# MySQL
if ($RDBMS eq 'mysql') {
$dbh->do($query, undef, @values);
@ -684,9 +693,9 @@ sub get_alert_template_module_id ($$$) {
return defined ($rc) ? $rc : -1;
}
##########################################################################
########################################################################
## SQL insert. Returns the ID of the inserted row.
##########################################################################
########################################################################
sub db_process_insert($$$$;@) {
my ($dbh, $index, $table, $parameters, @values) = @_;
@ -711,9 +720,13 @@ sub db_process_insert($$$$;@) {
}
$wildcards = '('.$wildcards.')';
my $columns_string = join('`,`',@columns_array);
my $columns_string = join($RDBMS_QUOTE . ',' . $RDBMS_QUOTE,
@columns_array);
my $res = db_insert ($dbh,
$index,
"INSERT INTO $table (" . $RDBMS_QUOTE . $columns_string . $RDBMS_QUOTE . ") VALUES " . $wildcards, @values_array);
my $res = db_insert ($dbh, $index, "INSERT INTO $table (`".$columns_string."`) VALUES ".$wildcards, @values_array);
return $res;
}
@ -740,7 +753,8 @@ sub db_process_update($$$$$;@) {
if ($i > 0 && $i <= $#values_array) {
$fields = $fields.',';
}
$fields = $fields." `$columns_array[$i]` = ?";
$fields = $fields .
" " . $RDBMS_QUOTE . "$columns_array[$i]" . $RDBMS_QUOTE . " = ?";
}
push(@values_array, $where_value);
@ -752,28 +766,30 @@ sub db_process_update($$$$$;@) {
return $res;
}
##########################################################################
########################################################################
# Add the given address to taddress.
##########################################################################
########################################################################
sub add_address ($$) {
my ($dbh, $ip_address) = @_;
return db_insert ($dbh, 'id_a', 'INSERT INTO taddress (ip) VALUES (?)', $ip_address);
}
##########################################################################
########################################################################
# Assign the new address to the agent
##########################################################################
########################################################################
sub add_new_address_agent ($$$) {
my ($dbh, $addr_id, $agent_id) = @_;
db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`)
db_do ($dbh, 'INSERT INTO taddress_agent (' .
$RDBMS_QUOTE . 'id_a' . $RDBMS_QUOTE . ', ' .
$RDBMS_QUOTE. 'id_agent' . $RDBMS_QUOTE. ')
VALUES (?, ?)', $addr_id, $agent_id);
}
##########################################################################
########################################################################
# Return the ID of the given address, -1 if it does not exist.
##########################################################################
########################################################################
sub get_addr_id ($$) {
my ($dbh, $addr) = @_;
@ -825,9 +841,9 @@ sub is_agent_address ($$$) {
return (defined ($id_ag)) ? $id_ag : 0;
}
##########################################################################
########################################################################
## Quote the given string.
##########################################################################
########################################################################
sub db_string ($) {
my $string = shift;
@ -856,7 +872,10 @@ sub db_text ($) {
sub get_alert_template_name ($$) {
my ($dbh, $alert_id) = @_;
return get_db_value ($dbh, "SELECT name FROM talert_templates, talert_template_modules WHERE talert_templates.id = talert_template_modules.id_alert_template AND talert_template_modules.id = ?", $alert_id);
return get_db_value ($dbh, "SELECT name
FROM talert_templates, talert_template_modules
WHERE talert_templates.id = talert_template_modules.id_alert_template
AND talert_template_modules.id = ?", $alert_id);
}
########################################################################

View File

@ -626,7 +626,8 @@ sub process_module_data ($$$$$$$$$) {
}
}
} else {
}
else {
# Control NULL columns
$module->{'descripcion'} = '' unless defined ($module->{'descripcion'});
$module->{'extended_info'} = '' unless defined ($module->{'extended_info'});

View File

@ -147,11 +147,23 @@ B<Returns>: I<undef> if there is not information available or a B<hash> with:
sub get_reverse_geoip_sql($$$) {
my ($pa_config,$ip_addr, $dbh) = @_;
my $id_range = get_db_value($dbh, 'SELECT `id_range` FROM tgis_reverse_geoip_ranges WHERE INET_ATON(?) >= `first_IP_decimal` AND INET_ATON(?) <= `last_IP_decimal` LIMIT 1', $ip_addr, $ip_addr);
my $id_range = get_db_value($dbh,
'SELECT ' . $RDBMS_QUOTE . 'id_range' . $RDBMS_QUOTE . '
FROM tgis_reverse_geoip_ranges
WHERE INET_ATON(?) >= ' . $RDBMS_QUOTE . 'first_IP_decimal' . $RDBMS_QUOTE . '
AND INET_ATON(?) <= ' . $RDBMS_QUOTE . 'last_IP_decimal ' . $RDBMS_QUOTE . '
LIMIT 1', $ip_addr, $ip_addr);
if (defined($id_range)) {
logger($pa_config,"Range id of '$ip_addr' is '$id_range'", 8);
my $region_info = get_db_single_row($dbh, 'SELECT * FROM tgis_reverse_geoip_info WHERE `id_range` = ?',$id_range);
my $region_info = get_db_single_row($dbh,
'SELECT *
FROM tgis_reverse_geoip_info
WHERE ' . $RDBMS_QUOTE . 'id_range ' . $RDBMS_QUOTE . ' = ?',
$id_range);
logger($pa_config, "region info of id_range '$id_range' is: country:".$region_info->{'country_name'}." region:".$region_info->{'region'}." city:".$region_info->{'city'}." longitude:".$region_info->{'longitude'}." latitude:".$region_info->{'longitude'}, 8);
return $region_info;
}
return undef;

View File

@ -286,7 +286,8 @@ sub data_consumer ($$) {
# Assign the new address to the agent
my $agent_addr_id = get_agent_addr_id ($dbh, $addr_id, $agent_id);
if ($agent_addr_id <= 0) {
db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`)
db_do ($dbh, 'INSERT INTO taddress_agent (' . $RDBMS_QUOTE . 'id_a' . $RDBMS_QUOTE . ', ' .
$RDBMS_QUOTE . 'id_agent' . $RDBMS_QUOTE . ')
VALUES (?, ?)', $addr_id, $agent_id);
}
@ -392,7 +393,8 @@ sub get_host_parent {
# Create the host
my $agent_id = pandora_create_agent ($pa_config, $pa_config->{'servername'}, $host_name, $host_addr, $group, $parent_id, $id_os, '', 300, $dbh);
$agent_id = 0 unless defined ($parent_id);
db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`)
db_do ($dbh, 'INSERT INTO taddress_agent (' . $RDBMS_QUOTE . 'id_a' . $RDBMS_QUOTE . ',' .
$RDBMS_QUOTE . 'id_agent' . $RDBMS_QUOTE . ')
VALUES (?, ?)', $addr_id, $agent_id);
# Move to the next host