2006-03-27 05:37:27 +02:00
#!/usr/bin/perl
2007-08-28 17:09:04 +02:00
###############################################################################
2007-06-07 12:30:03 +02:00
# Pandora FMS DB Management
2007-08-28 17:09:04 +02:00
###############################################################################
2012-04-18 19:04:53 +02:00
# Copyright (c) 2005-2012 Artica Soluciones Tecnologicas S.L
2007-06-07 12:30:03 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
2007-08-28 17:09:04 +02:00
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
###############################################################################
2006-03-27 05:37:27 +02:00
# Includes list
use strict ;
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
use Time::Local ; # DateTime basic manipulation
2008-04-09 16:45:12 +02:00
use DBI ; # DB interface with MySQL
2010-05-19 Ramon Novoa <rnovoa@artica.es>
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/Server.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/GIS.pm, lib/PandoraFMS/WMIServer.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/ProducerConsumerServer.pm,
lib/PandoraFMS/PredictionServer.pm, lib/PandoraFMS/Core.pm,
lib/PandoraFMS/ReconServer.pm, lib/PandoraFMS/DataServer.pm,
bin/pandora_server, util/pandora_db.pl, util/gpx2pandora_agent_data.pl,
util/pandora_manage.pl, util/pandora_dbstress.pl: Added the default library path
used by RPM and DEB packages.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2755 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-05-19 18:59:00 +02:00
use POSIX qw( strftime ) ;
# Default lib dir for RPM and DEB packages
use lib '/usr/lib/perl5' ;
2009-01-12 17:20:33 +01:00
use PandoraFMS::Tools ;
2009-01-26 16:05:33 +01:00
use PandoraFMS::DB ;
2006-03-27 05:37:27 +02:00
2009-12-14 10:37:41 +01:00
# version: define current version
2012-12-17 18:11:00 +01:00
my $ version = "5.0dev PS121217" ;
2009-12-18 12:26:13 +01:00
# Pandora server configuration
my % conf ;
2006-03-27 05:37:27 +02:00
2010-02-08 02:02:59 +01:00
my $ BIG_OPERATION_STEP = 100 ; # Long operations are divided in XX steps for performance
2006-03-27 05:37:27 +02:00
# FLUSH in each IO
2009-01-12 03:44:39 +01:00
$| = 0 ;
2006-03-27 05:37:27 +02:00
2009-12-18 12:26:13 +01:00
# Init
pandora_init ( \ % conf ) ;
2006-03-27 05:37:27 +02:00
2009-12-18 12:26:13 +01:00
# Read config file
pandora_load_config ( \ % conf ) ;
2006-03-27 05:37:27 +02:00
2009-12-18 12:26:13 +01:00
# Load enterprise module
if ( enterprise_load ( \ % conf ) == 0 ) {
print " [*] Pandora FMS Enterprise module not available.\n\n" ;
} else {
print " [*] Pandora FMS Enterprise module loaded.\n\n" ;
}
2006-03-27 05:37:27 +02:00
2009-12-18 12:26:13 +01:00
# Connect to the DB
2012-04-20 05:56:09 +02:00
my $ dbh = db_connect ( 'mysql' , $ conf { 'dbname' } , $ conf { 'dbhost' } , $ conf { 'dbport' } , $ conf { 'dbuser' } , $ conf { 'dbpass' } ) ;
2009-12-18 12:26:13 +01:00
my $ history_dbh = ( $ conf { '_history_db_enabled' } eq '1' ) ? db_connect ( 'mysql' , $ conf { '_history_db_name' } ,
2009-12-18 14:43:02 +01:00
$ conf { '_history_db_host' } , '3306' , $ conf { '_history_db_user' } , $ conf { '_history_db_pass' } ) : undef ;
2009-12-18 12:26:13 +01:00
# Main
pandoradb_main ( \ % conf , $ dbh , $ history_dbh ) ;
# Cleanup and exit
db_disconnect ( $ history_dbh ) if defined ( $ history_dbh ) ;
db_disconnect ( $ dbh ) ;
exit ;
2006-03-27 05:37:27 +02:00
2007-08-28 17:09:04 +02:00
###############################################################################
2009-12-18 12:26:13 +01:00
# Delete old data from the database.
2007-08-28 17:09:04 +02:00
###############################################################################
2009-12-18 12:26:13 +01:00
sub pandora_purgedb ($$) {
my ( $ conf , $ dbh ) = @ _ ;
2006-03-27 05:37:27 +02:00
# 1) Obtain last value for date limit
# 2) Delete all elements below date limit
# 3) Insert last value in date_limit position
2009-12-18 12:26:13 +01:00
# Calculate limit for deletion, today - $conf->{'_days_purge'}
2010-03-10 17:03:38 +01:00
2009-11-01 21:43:04 +01:00
my $ timestamp = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) ;
2008-06-13 18:42:35 +02:00
2009-12-18 14:43:02 +01:00
my $ ulimit_access_timestamp = time ( ) - 86400 ;
2009-12-18 12:26:13 +01:00
my $ ulimit_timestamp = time ( ) - ( 86400 * $ conf - > { '_days_purge' } ) ;
2009-11-01 21:43:04 +01:00
my $ limit_timestamp = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( $ ulimit_timestamp ) ) ;
2009-09-07 02:14:43 +02:00
2010-02-08 02:02:59 +01:00
my $ first_mark ;
my $ total_time ;
my $ purge_steps ;
2010-03-10 17:03:38 +01:00
# Numeric data deletion
2010-02-08 02:02:59 +01:00
print "[PURGE] Deleting old data... \n" ;
2013-01-08 18:56:32 +01:00
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
# This could be very timing consuming, so make this operation in $BIG_OPERATION_STEP
# steps (100 fixed by default)
2010-02-08 02:02:59 +01:00
# Starting from the oldest record on the table
2013-01-08 18:56:32 +01:00
2010-02-08 02:02:59 +01:00
$ first_mark = get_db_value ( $ dbh , 'SELECT utimestamp FROM tagente_datos ORDER BY utimestamp ASC LIMIT 1' ) ;
2010-04-26 13:50:30 +02:00
if ( defined ( $ first_mark ) ) {
$ total_time = $ ulimit_timestamp - $ first_mark ;
$ purge_steps = int ( $ total_time / $ BIG_OPERATION_STEP ) ;
for ( my $ ax = 1 ; $ ax <= $ BIG_OPERATION_STEP ; $ ax + + ) {
2012-09-26 13:35:33 +02:00
db_do ( $ dbh , "DELETE FROM tagente_datos WHERE utimestamp < " . ( $ first_mark + ( $ purge_steps * $ ax ) ) . " AND utimestamp >= " . $ first_mark ) ;
2010-04-26 13:50:30 +02:00
print "[PURGE] Data deletion Progress %$ax .. \r" ;
}
print "\n" ;
} else {
print "[PURGE] No data in tagente_datos\n" ;
2010-03-10 17:03:38 +01:00
}
2010-03-19 15:54:16 +01:00
2012-12-17 18:11:00 +01:00
# Delete inventory data, only if enterprise is enabled
# We use the same value than regular data purge interval
if ( enterprise_load ( \ % conf ) != 0 ) {
print "[PURGE] Deleting old inventory data... \n" ;
# This could be very timing consuming, so make
# this operation in $BIG_OPERATION_STEP
# steps (100 fixed by default)
# Starting from the oldest record on the table
$ first_mark = get_db_value ( $ dbh , 'SELECT utimestamp FROM tagente_datos_inventory ORDER BY utimestamp ASC LIMIT 1' ) ;
if ( defined ( $ first_mark ) ) {
$ total_time = $ ulimit_timestamp - $ first_mark ;
$ purge_steps = int ( $ total_time / $ BIG_OPERATION_STEP ) ;
for ( my $ ax = 1 ; $ ax <= $ BIG_OPERATION_STEP ; $ ax + + ) {
db_do ( $ dbh , "DELETE FROM tagente_datos_inventory WHERE utimestamp < " . ( $ first_mark + ( $ purge_steps * $ ax ) ) . " AND utimestamp >= " . $ first_mark ) ;
print "[PURGE] Inventory data deletion Progress %$ax .. \r" ;
}
print "\n" ;
} else {
print "[PURGE] No data in tagente_datos_inventory\n" ;
}
}
2010-03-19 15:54:16 +01:00
#
# Now the log4x data
#
$ first_mark = get_db_value ( $ dbh , 'SELECT utimestamp FROM tagente_datos_log4x ORDER BY utimestamp ASC LIMIT 1' ) ;
2010-04-26 13:50:30 +02:00
if ( defined ( $ first_mark ) ) {
$ total_time = $ ulimit_timestamp - $ first_mark ;
$ purge_steps = int ( $ total_time / $ BIG_OPERATION_STEP ) ;
for ( my $ ax = 1 ; $ ax <= $ BIG_OPERATION_STEP ; $ ax + + ) {
2012-09-26 13:35:33 +02:00
db_do ( $ dbh , "DELETE FROM tagente_datos_log4x WHERE utimestamp < " . ( $ first_mark + ( $ purge_steps * $ ax ) ) . " AND utimestamp >= " . $ first_mark ) ;
2010-04-26 13:50:30 +02:00
print "[PURGE] Log4x data deletion progress %$ax .. \r" ;
}
print "\n" ;
} else {
print "[PURGE] No data in tagente_datos_log4x\n" ;
2010-03-19 15:54:16 +01:00
}
2010-03-10 17:03:38 +01:00
# String data deletion
print "[PURGE] Deleting old string data... \n" ;
if ( ! defined ( $ conf - > { '_string_purge' } ) ) {
$ conf - > { '_string_purge' } = 7 ;
}
my $ string_limit = time ( ) - 86400 * $ conf - > { '_string_purge' } ;
$ first_mark = get_db_value ( $ dbh , 'SELECT utimestamp FROM tagente_datos_string ORDER BY utimestamp ASC LIMIT 1' ) ;
2010-04-26 13:50:30 +02:00
if ( defined ( $ first_mark ) ) {
$ total_time = $ string_limit - $ first_mark ;
$ purge_steps = int ( $ total_time / $ BIG_OPERATION_STEP ) ;
for ( my $ ax = 1 ; $ ax <= $ BIG_OPERATION_STEP ; $ ax + + ) {
2012-09-26 13:35:33 +02:00
db_do ( $ dbh , "DELETE FROM tagente_datos_string WHERE utimestamp < " . ( $ first_mark + ( $ purge_steps * $ ax ) ) . " AND utimestamp >= " . $ first_mark ) ;
2010-04-26 13:50:30 +02:00
print "[PURGE] String deletion Progress %$ax .. \r" ;
}
print "\n" ;
} else {
print "[PURGE] No data in tagente_datos_string\n" ;
2010-02-08 02:02:59 +01:00
}
2010-03-10 17:03:38 +01:00
# Delete event data
if ( ! defined ( $ conf - > { '_event_purge' } ) ) {
$ conf - > { '_event_purge' } = 10 ;
}
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
2010-03-10 17:03:38 +01:00
my $ event_limit = time ( ) - 86400 * $ conf - > { '_event_purge' } ;
2013-01-16 16:56:31 +01:00
my $ events_table = 'tevento' ;
# If is installed enterprise version and enabled metaconsole,
# check the events history copy and set the name of the metaconsole events table
if ( defined ( $ conf - > { '_enterprise_installed' } ) && $ conf - > { '_enterprise_installed' } eq '1' &&
defined ( $ conf - > { '_metaconsole' } ) && $ conf - > { '_metaconsole' } eq '1' ) {
# If events history is enabled, save the new events (not validated or in process) to history database
if ( defined ( $ conf - > { '_metaconsole_events_history' } ) && $ conf - > { '_metaconsole_events_history' } eq '1' ) {
print "[PURGE] Moving old not validated events to history table (More than " . $ conf - > { '_event_purge' } . " days)... \n" ;
my @ events = get_db_rows ( $ dbh , 'SELECT * FROM tmetaconsole_event WHERE estado = 0 AND utimestamp < ?' , $ event_limit ) ;
foreach my $ event ( @ events ) {
db_process_insert ( $ dbh , 'id_evento' , 'tmetaconsole_event_history' , $ event ) ;
}
}
$ events_table = 'tmetaconsole_event' ;
}
print "[PURGE] Deleting old event data at $events_table table (More than " . $ conf - > { '_event_purge' } . " days)... \n" ;
# Delete with buffer to avoid problems with performance
my $ buffer = 1000 ;
my $ events_to_delete = get_db_value ( $ dbh , "SELECT COUNT(*) FROM $events_table WHERE utimestamp < ?" , $ event_limit ) ;
while ( 1 ) {
db_do ( $ dbh , "DELETE FROM $events_table WHERE utimestamp < ? LIMIT ?" , $ event_limit , $ buffer ) ;
if ( $ events_to_delete <= $ buffer ) {
last ;
}
else {
$ events_to_delete = $ events_to_delete - $ buffer ;
}
}
2010-03-10 17:03:38 +01:00
# Delete audit data
if ( ! defined ( $ conf - > { '_audit_purge' } ) ) {
$ conf - > { '_audit_purge' } = 7 ;
}
print "[PURGE] Deleting old audit data (More than " . $ conf - > { '_audit_purge' } . " days)... \n" ;
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
2010-03-10 17:03:38 +01:00
my $ audit_limit = time ( ) - 86400 * $ conf - > { '_audit_purge' } ;
db_do ( $ dbh , "DELETE FROM tsesion WHERE utimestamp < $audit_limit" ) ;
# Delete SNMP trap data
if ( ! defined ( $ conf - > { '_trap_purge' } ) ) {
$ conf - > { '_trap_purge' } = 7 ;
}
my $ trap_limit = time ( ) - 86400 * $ conf - > { '_trap_purge' } ;
$ trap_limit = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( $ trap_limit ) ) ;
2012-12-17 18:11:00 +01:00
print "[PURGE] Deleting old SNMP traps data (More than " . $ conf - > { '__trap_purge' } . " days)... \n" ;
2010-03-10 17:03:38 +01:00
db_do ( $ dbh , "DELETE FROM ttrap WHERE timestamp < '$trap_limit'" ) ;
2011-05-23 13:55:08 +02:00
# Delete policy queue data
enterprise_hook ( "pandora_purge_policy_queue" , [ $ dbh , $ conf ] ) ;
2010-03-10 17:03:38 +01:00
# Delete GIS data
if ( ! defined ( $ conf - > { '_gis_purge' } ) ) {
$ conf - > { '_gis_purge' } = 15 ;
}
my $ gis_limit = time ( ) - 86400 * $ conf - > { '_gis_purge' } ;
$ gis_limit = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( $ gis_limit ) ) ;
2012-12-17 18:11:00 +01:00
print "[PURGE] Deleting old GID data (More than " . $ conf - > { '__gis_purge' } . " days)... \n" ;
2010-04-26 13:50:30 +02:00
db_do ( $ dbh , "DELETE FROM tgis_data_history WHERE end_timestamp < '$gis_limit'" ) ;
2010-03-10 17:03:38 +01:00
# Delete pending modules
print "[PURGE] Delete pending deleted modules (data table)...\n" ;
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
my @ deleted_modules = get_db_rows ( $ dbh , 'SELECT id_agente_modulo FROM tagente_modulo WHERE delete_pending = 1' ) ;
foreach my $ module ( @ deleted_modules ) {
2010-09-03 12:58:57 +02:00
my $ buffer = 1000 ;
my $ id_module = $ module - > { 'id_agente_modulo' } ;
2012-12-17 18:11:00 +01:00
print " + Deleting data for module " . $ id_module . "\n" ;
2010-09-03 12:58:57 +02:00
while ( 1 ) {
my $ nstate = get_db_value ( $ dbh , 'SELECT count(id_agente_modulo) FROM tagente_estado WHERE id_agente_modulo=?' , $ id_module ) ;
2011-11-15 00:47:47 +01:00
if ( $ nstate == 0 ) {
2010-09-03 12:58:57 +02:00
last ;
}
if ( $ nstate > 0 ) {
db_do ( $ dbh , 'DELETE FROM tagente_estado WHERE id_agente_modulo=? LIMIT ?' , $ id_module , $ buffer ) ;
}
}
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
}
print "[PURGE] Delete pending deleted modules (status, module table)...\n" ;
db_do ( $ dbh , "DELETE FROM tagente_estado WHERE id_agente_modulo IN (SELECT id_agente_modulo FROM tagente_modulo WHERE delete_pending = 1)" ) ;
db_do ( $ dbh , "DELETE FROM tagente_modulo WHERE delete_pending = 1" ) ;
2009-09-16 02:02:23 +02:00
print "[PURGE] Deleting old access data (More than 24hr) \n" ;
2010-02-08 02:02:59 +01:00
$ first_mark = get_db_value ( $ dbh , 'SELECT utimestamp FROM tagent_access ORDER BY utimestamp ASC LIMIT 1' ) ;
2010-04-26 13:50:30 +02:00
if ( defined ( $ first_mark ) ) {
$ total_time = $ ulimit_access_timestamp - $ first_mark ;
$ purge_steps = int ( $ total_time / $ BIG_OPERATION_STEP ) ;
for ( my $ ax = 1 ; $ ax <= $ BIG_OPERATION_STEP ; $ ax + + ) {
2012-09-26 13:35:33 +02:00
db_do ( $ dbh , "DELETE FROM tagent_access WHERE utimestamp < " . ( $ first_mark + ( $ purge_steps * $ ax ) ) . " AND utimestamp >= " . $ first_mark ) ;
2010-04-26 13:50:30 +02:00
print "[PURGE] Agent access deletion progress %$ax .. \r" ;
}
print "\n" ;
} else {
print "[PURGE] No data in tagente_access\n" ;
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
}
2012-03-20 13:23:39 +01:00
#Purge the reports
print "[PURGE] Delete contents in report that have some deleted modules...\n" ;
db_do ( $ dbh , "DELETE FROM treport_content WHERE id_agent_module NOT IN (SELECT id_agente_modulo FROM tagente_modulo) AND id_agent_module != 0;" ) ;
db_do ( $ dbh , "DELETE FROM treport_content_item WHERE id_agent_module NOT IN (SELECT id_agente_modulo FROM tagente_modulo) AND id_agent_module != 0;" ) ;
db_do ( $ dbh , "DELETE FROM treport_content_sla_combined WHERE id_agent_module NOT IN (SELECT id_agente_modulo FROM tagente_modulo) AND id_agent_module != 0;" ) ;
print "[PURGE] Delete contents in report that have some deleted agents...\n" ;
db_do ( $ dbh , "DELETE FROM treport_content WHERE id_agent NOT IN (SELECT id_agente FROM tagente) AND id_agent != 0;" ) ;
2012-04-17 13:38:41 +02:00
print "[PURGE] Delete empty contents in report (like SLA or Exception)...\n" ;
2012-03-20 13:23:39 +01:00
db_do ( $ dbh , "DELETE FROM treport_content WHERE type LIKE 'exception' AND id_rc NOT IN (SELECT id_report_content FROM treport_content_item);" ) ;
db_do ( $ dbh , "DELETE FROM treport_content WHERE type LIKE 'sla' AND id_rc NOT IN (SELECT id_report_content FROM treport_content_sla_combined);" ) ;
2006-03-27 05:37:27 +02:00
}
2007-08-28 17:09:04 +02:00
###############################################################################
2009-12-18 12:26:13 +01:00
# Compact agent data.
2007-08-28 17:09:04 +02:00
###############################################################################
2009-12-18 12:26:13 +01:00
sub pandora_compactdb ($$) {
my ( $ conf , $ dbh ) = @ _ ;
2006-03-27 05:37:27 +02:00
2008-04-24 18:24:55 +02:00
my % count_hash ;
my % id_agent_hash ;
my % value_hash ;
2011-06-06 18:33:32 +02:00
return if ( $ conf - > { '_days_compact' } == 0 || $ conf - > { '_step_compact' } < 1 ) ;
2008-04-24 18:24:55 +02:00
# Compact interval length in seconds
2009-12-18 12:26:13 +01:00
# $conf->{'_step_compact'} varies between 1 (36 samples/day) and
2008-04-24 18:24:55 +02:00
# 20 (1.8 samples/day)
2009-12-18 12:26:13 +01:00
my $ step = $ conf - > { '_step_compact' } * 2400 ;
2008-04-24 18:24:55 +02:00
# The oldest timestamp will be the lower limit
2009-12-18 12:26:13 +01:00
my $ limit_utime = get_db_value ( $ dbh , 'SELECT min(utimestamp) as min FROM tagente_datos' ) ;
return unless ( defined ( $ limit_utime ) && $ limit_utime > 0 ) ;
2008-04-24 18:24:55 +02:00
# Calculate the start date
2011-06-06 18:33:32 +02:00
my $ start_utime = time ( ) - $ conf - > { '_days_compact' } * 24 * 60 * 60 ;
2008-04-24 18:24:55 +02:00
my $ stop_utime ;
2009-12-18 12:26:13 +01:00
print "[COMPACT] Compacting data until " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( $ start_utime ) ) . "\n" ;
2008-04-24 18:24:55 +02:00
# Prepare the query to retrieve data from an interval
while ( 1 ) {
# Calculate the stop date for the interval
$ stop_utime = $ start_utime - $ step ;
# Out of limits
2009-12-18 12:26:13 +01:00
return if ( $ start_utime < $ limit_utime ) ;
2007-08-28 17:09:04 +02:00
2009-12-18 12:26:13 +01:00
my @ data = get_db_rows ( $ dbh , 'SELECT * FROM tagente_datos WHERE utimestamp < ? AND utimestamp >= ?' , $ start_utime , $ stop_utime ) ;
# No data, move to the next interval
if ( $# data == 0 ) {
2008-04-24 18:24:55 +02:00
$ start_utime = $ stop_utime ;
next ;
}
# Get interval data
2009-12-18 12:26:13 +01:00
foreach my $ data ( @ data ) {
2008-04-24 18:24:55 +02:00
my $ id_module = $ data - > { 'id_agente_modulo' } ;
if ( ! defined ( $ value_hash { $ id_module } ) ) {
$ value_hash { $ id_module } = 0 ;
$ count_hash { $ id_module } = 0 ;
if ( ! defined ( $ id_agent_hash { $ id_module } ) ) {
$ id_agent_hash { $ id_module } = $ data - > { 'id_agente' } ;
}
2006-03-27 05:37:27 +02:00
}
2008-04-24 18:24:55 +02:00
$ value_hash { $ id_module } += $ data - > { 'datos' } ;
$ count_hash { $ id_module } + + ;
2006-03-27 05:37:27 +02:00
}
2008-04-24 18:24:55 +02:00
# Delete interval from the database
2009-12-18 12:26:13 +01:00
db_do ( $ dbh , 'DELETE FROM tagente_datos WHERE utimestamp < ? AND utimestamp >= ?' , $ start_utime , $ stop_utime ) ;
2008-04-24 18:24:55 +02:00
# Insert interval average value
foreach my $ key ( keys ( % value_hash ) ) {
$ value_hash { $ key } /= $ count_hash { $ key } ;
2009-12-18 12:26:13 +01:00
db_do ( $ dbh , 'INSERT INTO tagente_datos (id_agente_modulo, datos, utimestamp) VALUES (?, ?, ?)' , $ key , $ value_hash { $ key } , $ stop_utime ) ;
2008-04-24 18:24:55 +02:00
delete ( $ value_hash { $ key } ) ;
delete ( $ count_hash { $ key } ) ;
}
# Move to the next interval
$ start_utime = $ stop_utime ;
2007-08-28 17:09:04 +02:00
}
2006-03-27 05:37:27 +02:00
}
##############################################################################
2009-12-18 12:26:13 +01:00
# Check command line parameters.
2006-03-27 05:37:27 +02:00
##############################################################################
2009-12-18 12:26:13 +01:00
sub pandora_init ($) {
my $ conf = shift ;
2006-03-27 05:37:27 +02:00
2009-12-18 14:43:02 +01:00
print "\nPandora FMS DB Tool $version Copyright (c) 2004-2009 Artica ST\n" ;
2007-08-02 19:25:59 +02:00
print "This program is Free Software, licensed under the terms of GPL License v2\n" ;
2009-12-18 12:26:13 +01:00
print "You can download latest versions and documentation at http://www.pandorafms.org\n\n" ;
2006-03-27 05:37:27 +02:00
# Load config file from command line
2009-12-18 12:26:13 +01:00
help_screen ( ) if ( $# ARGV < 0 ) ;
2012-04-17 13:38:41 +02:00
$ conf - > { '_pandora_path' } = shift ( @ ARGV ) ;
# If there are valid parameters store it
foreach my $ param ( @ ARGV ) {
2009-12-18 12:26:13 +01:00
# help!
help_screen ( ) if ( $ param =~ m/--*h\w*\z/i ) ;
if ( $ param =~ m/-p\z/i ) {
$ conf - > { '_onlypurge' } = 1 ;
2012-04-17 13:38:41 +02:00
}
2012-04-18 11:24:56 +02:00
elsif ( $ param =~ m/-v\z/i ) {
2012-04-17 13:38:41 +02:00
$ conf - > { '_verbose' } = 1 ;
}
2012-04-18 11:24:56 +02:00
elsif ( $ param =~ m/-q\z/i ) {
2012-04-17 13:38:41 +02:00
$ conf - > { '_quiet' } = 1 ;
}
2012-04-18 11:24:56 +02:00
elsif ( $ param =~ m/-d\z/i ) {
2012-04-17 13:38:41 +02:00
$ conf - > { '_debug' } = 1 ;
2009-12-18 12:26:13 +01:00
}
2006-03-27 05:37:27 +02:00
}
2009-12-18 12:26:13 +01:00
help_screen ( ) if ( $ conf - > { '_pandora_path' } eq '' ) ;
2006-03-27 05:37:27 +02:00
}
##############################################################################
2009-12-18 12:26:13 +01:00
# Read external configuration file.
2006-03-27 05:37:27 +02:00
##############################################################################
2009-12-18 12:26:13 +01:00
sub pandora_load_config ($) {
my $ conf = shift ;
# Read conf file
open ( CFG , '< ' . $ conf - > { '_pandora_path' } ) or die ( "[ERROR] Could not open configuration file: $!\n" ) ;
while ( my $ line = <CFG> ) {
2010-07-20 13:51:12 +02:00
next unless ( $ line =~ /^(\S+)\s+(.*)\s+$/ ) ;
2011-09-23 14:04:18 +02:00
$ conf - > { $ 1 } = clean_blank ( $ 2 ) ;
2006-03-27 05:37:27 +02:00
}
close ( CFG ) ;
2009-12-18 14:43:02 +01:00
# Check conf tokens
2009-12-18 12:26:13 +01:00
foreach my $ param ( 'dbuser' , 'dbpass' , 'dbname' , 'dbhost' , 'log_file' ) {
2009-12-18 14:43:02 +01:00
die ( "[ERROR] Bad config values. Make sure " . $ conf - > { '_pandora_path' } . " is a valid config file.\n\n" ) unless defined ( $ conf - > { $ param } ) ;
2006-03-27 05:37:27 +02:00
}
2012-05-21 08:37:12 +02:00
$ conf - > { 'dbport' } = '3306' unless defined ( $ conf - > { 'dbport' } ) ;
2009-12-18 12:26:13 +01:00
# Read additional tokens from the DB
2012-04-20 05:56:09 +02:00
my $ dbh = db_connect ( 'mysql' , $ conf - > { 'dbname' } , $ conf - > { 'dbhost' } , $ conf - > { 'dbport' } , $ conf - > { 'dbuser' } , $ conf - > { 'dbpass' } ) ;
2010-03-10 17:03:38 +01:00
$ conf - > { '_event_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'event_purge'" ) ;
$ conf - > { '_trap_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'trap_purge'" ) ;
$ conf - > { '_audit_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'audit_purge'" ) ;
2012-09-26 13:35:33 +02:00
$ conf - > { '_string_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'string_purge'" ) ;
2010-03-10 17:03:38 +01:00
$ conf - > { '_gis_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'gis_purge'" ) ;
2009-12-18 12:26:13 +01:00
$ conf - > { '_days_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'days_purge'" ) ;
$ conf - > { '_days_compact' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'days_compact'" ) ;
$ conf - > { '_step_compact' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'step_compact'" ) ;
$ conf - > { '_history_db_enabled' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_enabled'" ) ;
$ conf - > { '_history_db_host' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_host'" ) ;
$ conf - > { '_history_db_port' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_port'" ) ;
$ conf - > { '_history_db_name' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_name'" ) ;
$ conf - > { '_history_db_user' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_user'" ) ;
$ conf - > { '_history_db_pass' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_pass'" ) ;
$ conf - > { '_history_db_days' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_days'" ) ;
$ conf - > { '_history_db_step' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_step'" ) ;
$ conf - > { '_history_db_delay' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_delay'" ) ;
2011-07-08 13:11:36 +02:00
$ conf - > { '_days_delete_unknown' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'days_delete_unknown'" ) ;
2013-01-16 16:56:31 +01:00
$ conf - > { '_enterprise_installed' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'enterprise_installed'" ) ;
$ conf - > { '_metaconsole' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'metaconsole'" ) ;
$ conf - > { '_metaconsole_events_history' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'metaconsole_events_history'" ) ;
2009-12-18 12:26:13 +01:00
db_disconnect ( $ dbh ) ;
2009-12-18 14:43:02 +01:00
printf "Pandora DB now initialized and running (PURGE=" . $ conf - > { '_days_purge' } . " days, COMPACT=$conf->{'_days_compact'} days, STEP=" . $ conf - > { '_step_compact' } . ") ... \n\n" ;
2006-03-27 05:37:27 +02:00
}
2010-03-10 17:03:38 +01:00
###############################################################################
# Check database integrity
###############################################################################
sub pandora_checkdb_integrity {
my $ dbh = shift ;
print "[INTEGRITY] Cleaning up group stats \n" ;
# Delete all records on tgroup_stats
db_do ( $ dbh , 'DELETE FROM tgroup_stat' ) ;
print "[INTEGRITY] Deleting non-used IP addresses \n" ;
# Delete all non-used IP addresses from taddress
db_do ( $ dbh , 'DELETE FROM taddress WHERE id_a NOT IN (SELECT id_a FROM taddress_agent)' ) ;
print "[INTEGRITY] Deleting orphan alerts \n" ;
# Delete alerts assigned to inexistant modules
db_do ( $ dbh , 'DELETE FROM talert_template_modules WHERE id_agent_module NOT IN (SELECT id_agente_modulo FROM tagente_modulo)' ) ;
print "[INTEGRITY] Deleting orphan modules \n" ;
# Delete orphan modules in tagente_modulo
2010-05-12 17:33:19 +02:00
db_do ( $ dbh , 'DELETE FROM tagente_modulo WHERE id_agente NOT IN (SELECT id_agente FROM tagente)' ) ;
2010-03-10 17:03:38 +01:00
# Delete orphan modules in tagente_estado
2010-05-12 17:33:19 +02:00
db_do ( $ dbh , 'DELETE FROM tagente_estado WHERE id_agente NOT IN (SELECT id_agente FROM tagente)' ) ;
2010-03-10 17:03:38 +01:00
# Delete orphan data_inc reference records
db_do ( $ dbh , 'DELETE FROM tagente_datos_inc WHERE id_agente_modulo NOT IN (SELECT id_agente_modulo FROM tagente_modulo)' ) ;
2010-05-12 17:33:19 +02:00
# Delete all non-used IP addresses from taddress
db_do ( $ dbh , 'DELETE FROM taddress WHERE id_a NOT IN (SELECT id_a FROM taddress_agent)' ) ;
# Check enterprise tables
enterprise_hook ( 'pandora_checkdb_integrity_enterprise' , [ $ dbh ] ) ;
2010-03-10 17:03:38 +01:00
}
2007-08-28 17:09:04 +02:00
###############################################################################
2009-12-18 12:26:13 +01:00
# Check database consistency.
2007-08-28 17:09:04 +02:00
###############################################################################
sub pandora_checkdb_consistency {
2009-12-18 12:26:13 +01:00
my $ dbh = shift ;
2007-08-28 17:09:04 +02:00
# 1. Check for modules that do not have tagente_estado but have tagente_module
2009-12-18 12:26:13 +01:00
print "[CHECKDB] Deleting non-init data... \n" ;
2012-12-21 15:42:15 +01:00
my @ modules = get_db_rows ( $ dbh , 'SELECT id_agente_modulo,id_agente FROM tagente_estado WHERE estado = 4' ) ;
2009-12-18 12:26:13 +01:00
foreach my $ module ( @ modules ) {
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
# Skip policy modules
2011-12-12 11:55:40 +01:00
my $ is_policy_module = enterprise_hook ( 'is_policy_module' , [ $ dbh , $ id_agente_modulo ] ) ;
next if ( defined ( $ is_policy_module ) && $ is_policy_module ) ;
2009-12-18 12:26:13 +01:00
2012-05-29 09:12:09 +02:00
# Skip if agent is disabled
my $ is_agent_disabled = get_db_value ( $ dbh , 'SELECT disabled FROM tagente WHERE id_agente = ?' , $ module - > { 'id_agente' } ) ;
next if ( defined ( $ is_agent_disabled ) && $ is_agent_disabled ) ;
# Skip if module is disabled
my $ is_module_disabled = get_db_value ( $ dbh , 'SELECT disabled FROM tagente_modulo WHERE id_agente_modulo = ?' , $ module - > { 'id_agente_modulo' } ) ;
next if ( defined ( $ is_module_disabled ) && $ is_module_disabled ) ;
2009-12-18 12:26:13 +01:00
# Delete the module
2012-05-29 09:12:09 +02:00
db_do ( $ dbh , 'DELETE FROM tagente_modulo WHERE id_agente_modulo = ?' , $ id_agente_modulo ) ;
2012-11-14 19:45:40 +01:00
# Update the module status count
db_do ( $ dbh , 'UPDATE tagente SET total_count=total_count-1, notinit_count=notinit_count-1 WHERE id_agente=?' , $ module - > { 'id_agente' } ) ;
2009-12-18 12:26:13 +01:00
# Delete any alerts associated to the module
2012-05-29 09:12:09 +02:00
db_do ( $ dbh , 'DELETE FROM talert_template_modules WHERE id_agent_module = ?' , $ id_agente_modulo ) ;
2009-12-14 10:37:41 +01:00
}
2009-12-18 12:26:13 +01:00
2011-07-08 13:11:36 +02:00
print "[CHECKDB] Deleting unknown data (More than " . $ conf { '_days_delete_unknown' } . " days)... \n" ;
if ( $ conf { '_days_delete_unknown' } > 0 ) {
2012-11-14 19:45:40 +01:00
my @ modules = get_db_rows ( $ dbh , 'SELECT tagente_modulo.id_agente_modulo, tagente_modulo.id_agente FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND estado = 3 AND utimestamp < UNIX_TIMESTAMP() - ?' , 86400 * $ conf { '_days_delete_unknown' } ) ;
2011-07-08 13:11:36 +02:00
foreach my $ module ( @ modules ) {
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
# Skip policy modules
2011-12-12 11:55:40 +01:00
my $ is_policy_module = enterprise_hook ( 'is_policy_module' , [ $ dbh , $ id_agente_modulo ] ) ;
next if ( defined ( $ is_policy_module ) && $ is_policy_module ) ;
2011-07-08 13:11:36 +02:00
# Delete the module
db_do ( $ dbh , 'DELETE FROM tagente_modulo WHERE disabled = 0 AND id_agente_modulo = ?' , $ id_agente_modulo ) ; ;
2012-11-14 19:45:40 +01:00
# Update the module status count
db_do ( $ dbh , 'UPDATE tagente SET total_count=total_count-1, unknown_count=unknown_count-1 WHERE id_agente=?' , $ module - > { 'id_agente' } ) ;
2011-07-08 13:11:36 +02:00
# Delete any alerts associated to the module
2012-03-08 08:12:22 +01:00
db_do ( $ dbh , 'DELETE FROM talert_template_modules WHERE id_agent_module = ? AND NOT EXISTS (SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente_modulo = ?)' , $ id_agente_modulo , $ id_agente_modulo ) ;
2011-07-08 13:11:36 +02:00
}
}
2009-12-18 12:26:13 +01:00
print "[CHECKDB] Checking database consistency (Missing status)... \n" ;
@ modules = get_db_rows ( $ dbh , 'SELECT * FROM tagente_modulo' ) ;
foreach my $ module ( @ modules ) {
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
my $ id_agente = $ module - > { 'id_agente' } ;
# check if exist in tagente_estado and create if not
my $ count = get_db_value ( $ dbh , 'SELECT COUNT(*) FROM tagente_estado WHERE id_agente_modulo = ?' , $ id_agente_modulo ) ;
next if ( defined ( $ count ) && $ count > 0 ) ;
2011-04-13 Ramon Novoa <rnovoa@artica.es>
* lib/PandoraFMS/SNMPServer.pm,
lib/PandoraFMS/Config.pm,
lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/WMIServer.pm,
lib/PandoraFMS/PluginServer.pm,
lib/PandoraFMS/DB.pm,
lib/PandoraFMS/ProducerConsumerServer.pm,
lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm,
lib/PandoraFMS/ReconServer.pm,
bin/pandora_server,
util/pandora_db.pl,
util/pandora_manage.pl,
util/recon_scripts/snmpdevices.pl: Adding support for PostgreSQL and
Oracle (not complete).
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4213 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2011-04-13 18:28:59 +02:00
db_do ( $ dbh , 'INSERT INTO tagente_estado (id_agente_modulo, datos, timestamp, estado, id_agente, last_try, utimestamp, current_interval, running_by, last_execution_try) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' , $ id_agente_modulo , 0 , '1970-01-01 00:00:00' , 1 , $ id_agente , '1970-01-01 00:00:00' , 0 , 0 , 0 , 0 ) ;
2009-12-18 12:26:13 +01:00
print "[CHECKDB] Inserting module $id_agente_modulo in state table \n" ;
2007-08-28 17:09:04 +02:00
}
2009-12-18 12:26:13 +01:00
print "[CHECKDB] Checking database consistency (Missing module)... \n" ;
2007-08-28 17:09:04 +02:00
# 2. Check for modules in tagente_estado that do not have tagente_modulo, if there is any, delete it
2009-09-30 18:24:02 +02:00
2009-12-18 12:26:13 +01:00
@ modules = get_db_rows ( $ dbh , 'SELECT * FROM tagente_estado' ) ;
foreach my $ module ( @ modules ) {
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
# check if exist in tagente_estado and create if not
my $ count = get_db_value ( $ dbh , 'SELECT COUNT(*) FROM tagente_modulo WHERE id_agente_modulo = ?' , $ id_agente_modulo ) ;
next if ( defined ( $ count ) && $ count > 0 ) ;
2007-08-28 17:09:04 +02:00
2009-12-18 12:26:13 +01:00
db_do ( $ dbh , 'DELETE FROM tagente_estado WHERE id_agente_modulo = ?' , $ id_agente_modulo ) ;
print "[CHECKDB] Deleting non-existing module $id_agente_modulo in state table \n" ;
}
2012-11-14 19:45:40 +01:00
print "[CHECKDB] Checking module status count... \n" ;
2012-11-15 16:13:13 +01:00
#my @agents = get_db_rows ($dbh, 'SELECT id_agente FROM tagente WHERE normal_count+warning_count+critical_count+unknown_count+notinit_count<>total_count');
# Try to update module status count for all agents. Further performance tests need to be done.
my @ agents = get_db_rows ( $ dbh , 'SELECT id_agente FROM tagente' ) ;
2012-11-14 19:45:40 +01:00
foreach my $ agent ( @ agents ) {
my $ id_agente = $ agent - > { 'id_agente' } ;
2013-01-08 18:56:32 +01:00
db_do ( $ dbh , 'UPDATE tagente SET normal_count=(SELECT COUNT(*) FROM tagente_estado WHERE id_agente=' . $ id_agente . ' AND estado = 0 AND disabled = 0 ) ,
2012-11-28 15:33:26 +01:00
critical_count = ( SELECT COUNT ( * ) FROM tagente_estado WHERE id_agente = ' . $id_agente . ' AND estado = 1 AND disabled = 0 ) ,
warning_count = ( SELECT COUNT ( * ) FROM tagente_estado WHERE id_agente = ' . $id_agente . ' AND estado = 2 AND disabled = 0 ) ,
unknown_count = ( SELECT COUNT ( * ) FROM tagente_estado WHERE id_agente = ' . $id_agente . ' AND estado = 3 AND disabled = 0 ) ,
2012-12-24 13:15:51 +01:00
notinit_count = ( SELECT COUNT ( * ) FROM tagente_estado WHERE id_agente = ' . $id_agente . ' AND estado = 4 AND disabled = 0 ) ,
2012-11-28 15:33:26 +01:00
total_count = ( SELECT COUNT ( * ) FROM tagente_estado WHERE id_agente = ' . $id_agente . ' AND disabled = 0 )
2012-11-14 19:45:40 +01:00
WHERE id_agente = ' . $ id_agente ) ;
}
2007-08-28 17:09:04 +02:00
}
2006-03-27 05:37:27 +02:00
##############################################################################
2009-12-18 12:26:13 +01:00
# Print a help screen and exit.
2006-03-27 05:37:27 +02:00
##############################################################################
sub help_screen {
2010-02-08 02:02:59 +01:00
print "Usage: $0 <path to pandora_server.conf> [options]\n\n" ;
2012-12-17 18:11:00 +01:00
print "\t\t-p Only purge and consistency check, skip compact.\n\n" ;
exit - 1 ;
2006-03-27 05:37:27 +02:00
}
2007-08-28 17:09:04 +02:00
###############################################################################
2009-12-18 12:26:13 +01:00
# Main
2007-08-28 17:09:04 +02:00
###############################################################################
2009-12-18 12:26:13 +01:00
sub pandoradb_main ($$$) {
my ( $ conf , $ dbh , $ history_dbh ) = @ _ ;
2009-02-03 14:16:31 +01:00
2009-12-18 12:26:13 +01:00
print "Starting at " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) . "\n" ;
2009-09-18 19:29:18 +02:00
2009-12-18 12:26:13 +01:00
# Purge
pandora_purgedb ( $ conf , $ dbh ) ;
# Consistency check
pandora_checkdb_consistency ( $ dbh ) ;
2010-03-10 17:03:38 +01:00
# Maintain Referential integrity and other stuff
pandora_checkdb_integrity ( $ dbh ) ;
2010-02-10 Sancho Lerena <slerena@artica.es>
* bin/pandora_server: Better error management, avoiding to show Enterprise
loading errors, and showing in console critical errors, helping to identify
silly errors like DB credentials, etc.
Added usage of pandora_get_sharedconfig() to get configuration from DB.
* Config.pm: Updated build. Added pandora_get_sharedconfig(). Force three
config tokens to be readed from DB, using default values if cannot be readen
from database (agentaccess, realtimestat and stats_interval).
* Core.pm: Added new parameter to pandora_event() to pass status of the
event (validated, not validated). Implemented auto-validation of low
priority events: normal/ok events will be always shown as autovalidated, and
warning and normal events will be validated with critical events, and when
a module is restored (normal/ok), all it's warning/critical pending events
will be validated.
Fixed also bug with FF Threshold who doesnt' trigger events until FF + 1
instead FF value.
* Server.pm,
NetworkServer.pm,
Dataserver.pm,
ReconServer.pm: Raise event with status 0 (non validated) by default.
* NetworkServer.pm: Implemented support for SNMP v3.
* util/pandora_db.pm: Very important upgrade to this script. Now will
delete all huge tables (tagente_datos, tagente_datos_string and
tagent_acccess) using several independent blocks (by default 100) avoiding
mysql locks which happen in the past.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2341 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2010-02-10 18:32:10 +01:00
# Move old data to the history DB
if ( defined ( $ history_dbh ) ) {
undef ( $ history_dbh ) unless defined ( enterprise_hook ( 'pandora_historydb' , [ $ dbh , $ history_dbh , $ conf - > { '_history_db_days' } , $ conf - > { '_history_db_step' } , $ conf - > { '_history_db_delay' } ] ) ) ;
}
# Compact on if enable and DaysCompact are below DaysPurge
if ( ( $ conf - > { '_onlypurge' } == 0 ) && ( $ conf - > { '_days_compact' } < $ conf - > { '_days_purge' } ) ) {
2009-12-18 12:26:13 +01:00
pandora_compactdb ( $ conf , defined ( $ history_dbh ) ? $ history_dbh : $ dbh ) ;
}
2010-02-18 18:21:40 +01:00
# Update tconfig with last time of database maintance time (now)
db_do ( $ dbh , "DELETE FROM tconfig WHERE token = 'db_maintance'" ) ;
db_do ( $ dbh , "INSERT INTO tconfig (token, value) VALUES ('db_maintance', '" . time ( ) . "')" ) ;
2012-12-17 18:11:00 +01:00
print "\nEnding at " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) . "\n" ;
exit 0 ;
2006-03-27 05:37:27 +02:00
}