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
###############################################################################
2020-11-27 13:52:35 +01:00
# Copyright (c) 2005-2021 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 ;
2013-05-07 17:38:11 +02:00
use warnings ;
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 ) ;
2013-06-12 15:00:48 +02:00
use File::Path qw( rmtree ) ;
use Time::HiRes qw( usleep ) ;
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
# Default lib dir for RPM and DEB packages
2021-04-15 17:56:02 +02:00
BEGIN { push @ INC , '/usr/lib/perl5' ; }
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
2020-03-19 09:56:34 +01:00
use PandoraFMS::Core ;
2009-01-12 17:20:33 +01:00
use PandoraFMS::Tools ;
2018-05-29 16:50:30 +02:00
use PandoraFMS::Config ;
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
2022-10-20 01:00:21 +02:00
my $ version = "7.0NG.765 Build 221020" ;
2009-12-18 12:26:13 +01:00
# Pandora server configuration
my % conf ;
2006-03-27 05:37:27 +02:00
2022-09-08 10:14:44 +02:00
# History DB configuration
my $ h_conf ;
2014-03-18 11:59:53 +01:00
# Long operations are divided in XX steps for performance
2016-05-27 09:37:58 +02:00
my $ BIG_OPERATION_STEP = 100 ; # 100 is default
2014-03-18 11:59:53 +01:00
# Each long operations has a LIMIT of SMALL_OPERATION_STEP to avoid locks.
#Increate to 3000~5000 in fast systems decrease to 500 or 250 on systems with locks
2016-05-27 09:37:58 +02:00
my $ SMALL_OPERATION_STEP = 1000 ; # 1000 is default
2014-03-18 11:59:53 +01:00
2022-03-28 20:56:05 +02:00
# Timeout for lock acquisition.
my $ LOCK_TIMEOUT = 60 ;
2013-04-24 03:12:50 +02:00
# FLUSH in each IO
$| = 1 ;
2006-03-27 05:37:27 +02:00
2014-03-18 11:59:53 +01:00
########################################################################
2013-06-12 15:00:48 +02:00
# Print the given message with a preceding timestamp.
2014-03-18 11:59:53 +01:00
########################################################################
2013-06-12 15:00:48 +02:00
sub log_message ($$;$) {
my ( $ source , $ message , $ eol ) = @ _ ;
# Set a default end of line
$ eol = "\n" unless defined ( $ eol ) ;
if ( $ source eq '' ) {
print $ message ;
2014-03-18 11:59:53 +01:00
}
else {
2013-06-12 15:00:48 +02:00
print strftime ( "%H:%M:%S" , localtime ( ) ) . ' [' . $ source . '] ' . $ message . $ eol ;
}
}
2014-03-18 11:59:53 +01:00
########################################################################
2009-12-18 12:26:13 +01:00
# Delete old data from the database.
2014-03-18 11:59:53 +01:00
########################################################################
2022-09-08 10:14:44 +02:00
sub pandora_purgedb ($$$) {
my ( $ conf , $ dbh , $ h_conf ) = @ _ ;
2014-03-18 11:59:53 +01:00
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
2014-03-18 11:59:53 +01:00
# Calculate limit for deletion, today - $conf->{'_days_purge'}
2009-11-01 21:43:04 +01:00
my $ timestamp = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) ;
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' } ) ;
2015-01-07 13:47:19 +01:00
my $ first_mark ;
my $ total_time ;
my $ purge_steps ;
my $ purge_count ;
2014-03-18 11:59:53 +01:00
# Delete extended session data
if ( enterprise_load ( \ % conf ) != 0 ) {
db_do ( $ dbh , " DELETE FROM tsesion_extended
2015-07-22 15:36:46 +02:00
WHERE id_sesion NOT IN ( SELECT id_sesion FROM tsesion ) " ) ;
2014-03-18 11:59:53 +01:00
log_message ( 'PURGE' , 'Deleting old extended session data.' ) ;
}
2012-12-17 18:11:00 +01:00
2016-12-14 13:02:10 +01:00
# Delete old inventory data
2017-04-11 05:37:23 +02:00
if ( defined ( $ conf - > { '_inventory_purge' } ) && $ conf - > { '_inventory_purge' } > 0 ) {
2015-01-07 13:47:19 +01:00
if ( enterprise_load ( \ % conf ) != 0 ) {
2016-12-14 13:02:10 +01:00
my $ ulimit_timestamp_inventory = time ( ) - ( 86400 * $ conf - > { '_inventory_purge' } ) ;
2015-01-07 13:47:19 +01:00
log_message ( 'PURGE' , 'Deleting old inventory data.' ) ;
# 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
2015-07-22 15:36:46 +02:00
$ first_mark = get_db_value_limit ( $ dbh , 'SELECT utimestamp FROM tagente_datos_inventory ORDER BY utimestamp ASC' , 1 ) ;
2015-01-07 13:47:19 +01:00
if ( defined ( $ first_mark ) ) {
2016-12-14 13:02:10 +01:00
$ total_time = $ ulimit_timestamp_inventory - $ first_mark ;
2015-01-07 13:47:19 +01:00
$ purge_steps = int ( $ total_time / $ BIG_OPERATION_STEP ) ;
if ( $ purge_steps > 0 ) {
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 ) ;
log_message ( 'PURGE' , "Inventory data deletion Progress %$ax\r" ) ;
# Do a nanosleep here for 0,01 sec
usleep ( 10000 ) ;
}
log_message ( '' , "\n" ) ;
} else {
log_message ( 'PURGE' , 'No data to purge in tagente_datos_inventory.' ) ;
}
2013-06-12 15:00:48 +02:00
} else {
2015-01-07 13:47:19 +01:00
log_message ( 'PURGE' , 'No data in tagente_datos_inventory.' ) ;
2013-06-12 15:00:48 +02:00
}
2015-01-07 13:47:19 +01:00
}
2016-12-14 13:02:10 +01:00
}
# Delete old data
if ( $ conf - > { '_days_purge' } > 0 ) {
2012-12-17 18:11:00 +01:00
2016-12-14 13:02:10 +01:00
# Delete old numeric data
pandora_delete_old_module_data ( $ dbh , 'tagente_datos' , $ ulimit_access_timestamp , $ ulimit_timestamp ) ;
2012-12-17 18:11:00 +01:00
2016-12-14 13:02:10 +01:00
# Delete old export data
pandora_delete_old_export_data ( $ dbh , $ ulimit_timestamp ) ;
# Delete sessions data
2019-08-30 08:47:49 +02:00
pandora_delete_old_session_data ( \ % conf , $ dbh , $ ulimit_timestamp ) ;
2014-03-18 11:59:53 +01:00
}
else {
2015-01-07 13:47:19 +01:00
log_message ( 'PURGE' , 'days_purge is set to 0. Old data will not be deleted.' ) ;
2010-03-19 15:54:16 +01:00
}
2022-09-08 10:14:44 +02:00
pandora_delete_old_tplanned_downtime ( \ % conf , $ dbh , $ h_conf ) ;
2015-01-07 13:47:19 +01:00
# String data deletion
if ( ! defined ( $ conf - > { '_string_purge' } ) ) {
$ conf - > { '_string_purge' } = 7 ;
}
2016-07-05 11:53:43 +02:00
2015-01-07 13:47:19 +01:00
if ( $ conf - > { '_string_purge' } > 0 ) {
$ ulimit_access_timestamp = time ( ) - 86400 ;
2017-10-10 11:26:04 +02:00
$ ulimit_timestamp = time ( ) - ( 86400 * $ conf - > { '_string_purge' } ) ;
2015-01-07 13:47:19 +01:00
pandora_delete_old_module_data ( $ dbh , 'tagente_datos_string' , $ ulimit_access_timestamp , $ ulimit_timestamp ) ;
}
else {
log_message ( 'PURGE' , 'string_purge is set to 0. Old string data will not be deleted.' ) ;
2013-01-16 16:56:31 +01:00
}
2015-01-07 13:47:19 +01:00
# Delete event data
if ( ! defined ( $ conf - > { '_event_purge' } ) ) {
$ conf - > { '_event_purge' } = 10 ;
}
if ( $ conf - > { '_event_purge' } > 0 ) {
my $ event_limit = time ( ) - 86400 * $ conf - > { '_event_purge' } ;
2013-06-12 15:00:48 +02:00
2022-05-31 12:50:43 +02:00
log_message ( 'PURGE' , "Deleting old event data from tevento (More than " . $ conf - > { '_event_purge' } . " days)." , '' ) ;
2010-03-10 17:03:38 +01:00
2015-01-07 13:47:19 +01:00
# Delete with buffer to avoid problems with performance
2022-05-31 12:50:43 +02:00
my $ events_to_delete = get_db_value ( $ dbh , "SELECT COUNT(*) FROM tevento WHERE utimestamp < ?" , $ event_limit ) ;
2015-01-07 13:47:19 +01:00
while ( $ events_to_delete > 0 ) {
2022-05-31 12:50:43 +02:00
db_delete_limit ( $ dbh , 'tevento' , "utimestamp < ?" , $ BIG_OPERATION_STEP , $ event_limit ) ;
2015-01-07 13:47:19 +01:00
$ events_to_delete = $ events_to_delete - $ BIG_OPERATION_STEP ;
# Mark the progress
log_message ( '' , "." ) ;
# Do not overload the MySQL server
2015-03-23 12:54:25 +01:00
usleep ( 10000 ) ;
}
log_message ( '' , "\n" ) ;
2015-01-07 13:47:19 +01:00
}
else {
log_message ( 'PURGE' , 'event_purge is set to 0. Old events will not be deleted.' ) ;
}
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
2015-01-07 13:47:19 +01:00
# Delete audit data
$ conf - > { '_audit_purge' } = 7 if ( ! defined ( $ conf - > { '_audit_purge' } ) ) ;
if ( $ conf - > { '_audit_purge' } > 0 ) {
log_message ( 'PURGE' , "Deleting old audit data (More than " . $ conf - > { '_audit_purge' } . " days)." ) ;
my $ audit_limit = time ( ) - 86400 * $ conf - > { '_audit_purge' } ;
db_do ( $ dbh , "DELETE FROM tsesion WHERE utimestamp < $audit_limit" ) ;
}
else {
log_message ( 'PURGE' , 'audit_purge is set to 0. Old audit data will not be deleted.' ) ;
}
2010-03-10 17:03:38 +01:00
2015-01-07 13:47:19 +01:00
# Delete SNMP trap data
$ conf - > { '_trap_purge' } = 7 if ( ! defined ( $ conf - > { '_trap_purge' } ) ) ;
if ( $ conf - > { '_trap_purge' } > 0 ) {
log_message ( 'PURGE' , "Deleting old SNMP traps (More than " . $ conf - > { '_trap_purge' } . " days)." ) ;
2013-06-12 15:00:48 +02:00
2015-01-07 13:47:19 +01:00
my $ trap_limit = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( time ( ) - 86400 * $ conf - > { '_trap_purge' } ) ) ;
db_do ( $ dbh , "DELETE FROM ttrap WHERE timestamp < '$trap_limit'" ) ;
}
else {
log_message ( 'PURGE' , 'trap_purge is set to 0. Old SNMP traps will not be deleted.' ) ;
}
2022-03-21 14:04:17 +01:00
# Cleanup policy groups pointing to non-existing policies.
enterprise_hook ( 'pandora_policy_group_cleanup' , [ $ dbh ] ) ;
2011-05-23 13:55:08 +02:00
2015-01-07 13:47:19 +01:00
# Delete policy queue data
2011-05-23 13:55:08 +02:00
enterprise_hook ( "pandora_purge_policy_queue" , [ $ dbh , $ conf ] ) ;
2010-03-10 17:03:38 +01:00
2015-01-07 13:47:19 +01:00
# Delete policy queue data
2014-10-29 17:11:51 +01:00
enterprise_hook ( "pandora_purge_service_elements" , [ $ dbh , $ conf ] ) ;
2015-01-07 13:47:19 +01:00
# Delete GIS data
$ conf - > { '_gis_purge' } = 15 if ( ! defined ( $ conf - > { '_gis_purge' } ) ) ;
if ( $ conf - > { '_gis_purge' } > 0 ) {
log_message ( 'PURGE' , "Deleting old GIS data (More than " . $ conf - > { '_gis_purge' } . " days)." ) ;
my $ gis_limit = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( time ( ) - 86400 * $ conf - > { '_gis_purge' } ) ) ;
db_do ( $ dbh , "DELETE FROM tgis_data_history WHERE end_timestamp < '$gis_limit'" ) ;
}
else {
log_message ( 'PURGE' , 'gis_purge is set to 0. Old GIS data will not be deleted.' ) ;
}
2010-03-10 17:03:38 +01:00
2015-01-07 13:47:19 +01:00
# Delete pending modules
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "Deleting pending delete modules (data table)." , '' ) ;
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 ) {
2015-01-07 13:47:19 +01:00
my $ buffer = 1000 ;
my $ id_module = $ module - > { 'id_agente_modulo' } ;
2018-11-12 18:39:14 +01:00
db_do ( $ dbh , 'UPDATE tagente_modulo SET parent_module_id=0 WHERE parent_module_id=?' , $ id_module ) ;
2013-06-12 15:00:48 +02:00
log_message ( '' , "." ) ;
2010-09-03 12:58:57 +02:00
while ( 1 ) {
2013-06-12 15:00:48 +02:00
my $ nstate = get_db_value ( $ dbh , 'SELECT count(id_agente_modulo) FROM tagente_estado WHERE id_agente_modulo=?' , $ id_module ) ;
last if ( $ nstate == 0 ) ;
2010-09-03 12:58:57 +02:00
2015-07-22 15:36:46 +02:00
db_delete_limit ( $ dbh , 'tagente_estado' , 'id_agente_modulo=?' , $ buffer , $ id_module ) ;
2010-09-03 12:58:57 +02: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
}
2013-06-12 15:00:48 +02:00
log_message ( '' , "\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
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "Deleting pending delete modules (status, module table)." ) ;
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
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" ) ;
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "Deleting old access data (More than 24hr)" ) ;
2010-02-08 02:02:59 +01:00
2015-07-22 15:36:46 +02:00
$ first_mark = get_db_value_limit ( $ dbh , 'SELECT utimestamp FROM tagent_access ORDER BY utimestamp ASC' , 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 ) ;
2013-05-27 19:58:00 +02:00
if ( $ purge_steps > 0 ) {
for ( my $ ax = 1 ; $ ax <= $ BIG_OPERATION_STEP ; $ ax + + ) {
db_do ( $ dbh , "DELETE FROM tagent_access WHERE utimestamp < " . ( $ first_mark + ( $ purge_steps * $ ax ) ) . " AND utimestamp >= " . $ first_mark ) ;
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "Agent access deletion progress %$ax" , "\r" ) ;
2013-05-27 19:58:00 +02:00
# Do a nanosleep here for 0,01 sec
usleep ( 10000 ) ;
}
2015-01-07 13:47:19 +01:00
log_message ( '' , "\n" ) ;
2013-05-27 19:58:00 +02:00
} else {
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "No agent access data to purge." ) ;
2010-04-26 13:50:30 +02:00
}
} else {
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "No agent access data." ) ;
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
2014-03-18 11:59:53 +01:00
2013-06-12 15:00:48 +02:00
# Purge the reports
2013-07-10 11:27:21 +02:00
if ( defined ( $ conf - > { '_enterprise_installed' } ) && $ conf - > { '_enterprise_installed' } eq '1' &&
2015-01-07 13:47:19 +01:00
defined ( $ conf - > { '_metaconsole' } ) && $ conf - > { '_metaconsole' } eq '1' ) {
2013-07-10 11:27:21 +02:00
log_message ( 'PURGE' , "Metaconsole enabled, ignoring reports." ) ;
} else {
2014-10-27 15:31:31 +01:00
my @ blacklist_types = ( "'SLA_services'" , "'custom_graph'" , "'sql_graph_vbar'" , "'sql_graph_hbar'" ,
"'sql_graph_pie'" , "'database_serialized'" , "'sql'" , "'inventory'" , "'inventory_changes'" ,
2022-02-21 11:44:31 +01:00
"'netflow_area'" , "'netflow_data'" , "'netflow_summary'" , "'netflow_top_N'" ) ;
2014-10-27 15:31:31 +01:00
my $ blacklist_types_str = join ( ',' , @ blacklist_types ) ;
# Deleted modules
2013-07-10 11:27:21 +02:00
log_message ( 'PURGE' , "Delete contents in report that have some deleted modules." ) ;
2014-10-27 15:31:31 +01:00
db_do ( $ dbh , " DELETE FROM treport_content
WHERE id_agent_module != 0
AND id_agent_module NOT IN ( SELECT id_agente_modulo FROM tagente_modulo )
2015-07-22 15:36:46 +02:00
AND $ { RDBMS_QUOTE } type $ { RDBMS_QUOTE } NOT IN ( $ blacklist_types_str ) " ) ;
2014-10-27 15:31:31 +01:00
db_do ( $ dbh , " DELETE FROM treport_content_item
WHERE id_agent_module != 0
AND id_agent_module NOT IN ( SELECT id_agente_modulo FROM tagente_modulo )
2015-07-22 15:36:46 +02:00
AND id_report_content NOT IN ( SELECT id_rc FROM treport_content WHERE $ { RDBMS_QUOTE } type $ { RDBMS_QUOTE } IN ( $ blacklist_types_str ) ) " ) ;
2014-10-27 15:31:31 +01:00
db_do ( $ dbh , " DELETE FROM treport_content_sla_combined
WHERE id_agent_module != 0
AND id_agent_module NOT IN ( SELECT id_agente_modulo FROM tagente_modulo )
2015-07-22 15:36:46 +02:00
AND id_report_content NOT IN ( SELECT id_rc FROM treport_content WHERE $ { RDBMS_QUOTE } type $ { RDBMS_QUOTE } = 'SLA_services' ) " ) ;
2013-07-10 11:27:21 +02:00
2014-10-27 15:31:31 +01:00
# Deleted agents
2013-07-10 11:27:21 +02:00
log_message ( 'PURGE' , "Delete contents in report that have some deleted agents." ) ;
2014-10-27 15:31:31 +01:00
db_do ( $ dbh , " DELETE FROM treport_content
WHERE id_agent != 0
AND id_agent NOT IN ( SELECT id_agente FROM tagente )
2015-07-22 15:36:46 +02:00
AND $ { RDBMS_QUOTE } type $ { RDBMS_QUOTE } NOT IN ( $ blacklist_types_str ) " ) ;
2013-07-10 11:27:21 +02:00
2014-10-27 15:31:31 +01:00
# Empty contents
2013-07-10 11:27:21 +02:00
log_message ( 'PURGE' , "Delete empty contents in report (like SLA or Exception)." ) ;
2014-10-27 15:31:31 +01:00
db_do ( $ dbh , " DELETE FROM treport_content
2015-07-22 15:36:46 +02:00
WHERE $ { RDBMS_QUOTE } type $ { RDBMS_QUOTE } LIKE 'exception'
AND id_rc NOT IN ( SELECT id_report_content FROM treport_content_item ) " ) ;
2014-10-27 15:31:31 +01:00
db_do ( $ dbh , " DELETE FROM treport_content
2015-07-22 15:36:46 +02:00
WHERE $ { RDBMS_QUOTE } type $ { RDBMS_QUOTE } IN ( 'SLA' , 'SLA_monthly' , 'SLA_services' )
AND id_rc NOT IN ( SELECT id_report_content FROM treport_content_sla_combined ) " ) ;
2013-07-10 11:27:21 +02:00
}
2013-02-13 16:55:41 +01:00
2016-06-03 11:58:13 +02:00
# Delete disabled autodisable agents after some period
log_message ( 'PURGE' , 'Delete autodisabled agents where last contact is bigger than ' . $ conf - > { '_days_autodisable_deletion' } . ' days.' ) ;
db_do ( $ dbh , " DELETE FROM tagente
WHERE UNIX_TIMESTAMP ( ultimo_contacto ) + ? < UNIX_TIMESTAMP ( NOW ( ) )
AND disabled = 1
2016-06-07 17:20:04 +02:00
AND modo = 2 " , $ conf - > { '_days_autodisable_deletion' } * 8600 ) ;
2014-03-18 11:59:53 +01:00
2013-02-13 16:55:41 +01:00
# Delete old netflow data
2015-01-07 13:47:19 +01:00
if ( $ conf - > { '_netflow_max_lifetime' } > 0 ) {
log_message ( 'PURGE' , "Deleting old netflow data." ) ;
if ( ! defined ( $ conf - > { '_netflow_path' } ) || ! - d $ conf - > { '_netflow_path' } ) {
log_message ( '!' , "Netflow data directory does not exist, skipping." ) ;
}
elsif ( ! - x $ conf - > { '_netflow_nfexpire' } ) {
log_message ( '!' , "Cannot execute " . $ conf - > { '_netflow_nfexpire' } . ", skipping." ) ;
}
else {
`yes 2>/dev/null | $conf->{'_netflow_nfexpire'} -e "$conf->{'_netflow_path'}" -t $conf->{'_netflow_max_lifetime'}d` ;
}
2014-03-18 11:59:53 +01:00
}
else {
2015-01-07 13:47:19 +01:00
log_message ( 'PURGE' , 'netflow_max_lifetime is set to 0. Old netflow data will not be deleted.' ) ;
2013-02-13 16:55:41 +01:00
}
2014-03-18 11:59:53 +01:00
2013-02-13 16:55:41 +01:00
# Delete old log data
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "Deleting old log data." ) ;
2020-03-27 14:36:42 +01:00
if ( defined ( $ conf - > { '_days_purge_old_information' } ) && $ conf - > { '_days_purge_old_information' } > 0 ) {
2017-09-06 17:08:55 +02:00
log_message ( 'PURGE' , 'Deleting log data older than ' . $ conf - > { '_days_purge_old_information' } . ' days.' ) ;
2020-03-27 14:36:42 +01:00
enterprise_hook ( 'pandora_purge_logs' , [ $ dbh , $ conf ] ) ;
2013-02-13 16:55:41 +01:00
}
2015-01-07 13:47:19 +01:00
else {
2017-09-06 17:08:55 +02:00
log_message ( 'PURGE' , 'days_purge_old_data is set to 0. Old log data will not be deleted.' ) ;
2015-01-07 13:47:19 +01:00
}
2016-10-08 09:12:53 +02:00
2021-10-13 22:47:31 +02:00
# Delete old log data
log_message ( 'PURGE' , "Deleting old network configuration manager data." ) ;
2021-12-20 15:50:42 +01:00
if ( defined ( $ conf - > { '_days_purge' } ) && $ conf - > { '_days_purge' } > 0 ) {
log_message ( 'PURGE' , 'Deleting NCM data older than ' . $ conf - > { '_days_purge' } . ' days.' ) ;
enterprise_hook ( 'pandora_purge_ncm' , [ $ dbh , \ & log_message , $ conf - > { '_days_purge' } , $ conf - > { '_history_db_step' } , $ conf - > { '_history_db_delay' } ] ) ;
2021-10-13 22:47:31 +02:00
} else {
2021-12-20 15:50:42 +01:00
log_message ( 'PURGE' , '_days_purge is set to 0. Old network configuration manager data will not be deleted.' ) ;
2021-10-13 22:47:31 +02:00
}
2016-10-08 09:12:53 +02:00
# Delete old special days
log_message ( 'PURGE' , "Deleting old special days." ) ;
if ( $ conf - > { '_num_past_special_days' } > 0 ) {
log_message ( 'PURGE' , 'Deleting special days older than ' . $ conf - > { '_num_past_special_days' } . ' days.' ) ;
if ( $ { RDBMS } eq 'oracle' ) {
db_do ( $ dbh , " DELETE FROM talert_special_days
WHERE \ "date\" < SYSDATE - $conf->{'_num_past_special_days'} AND \"date\" > '0001-01-01'" ) ;
}
elsif ( $ { RDBMS } eq 'mysql' ) {
db_do ( $ dbh , " DELETE FROM talert_special_days
WHERE date < CURDATE ( ) - $ conf - > { '_num_past_special_days' } AND date > '0001-01-01' " ) ;
}
}
2019-01-30 13:03:16 +01:00
2017-09-08 13:04:19 +02:00
# Delete old tgraph_source data
2020-12-14 10:52:12 +01:00
log_message ( 'PURGE' , 'Deleting old tgraph_source data.' ) ;
2017-09-08 13:04:19 +02:00
db_do ( $ dbh , "DELETE FROM tgraph_source WHERE id_graph NOT IN (SELECT id_graph FROM tgraph)" ) ;
2019-01-30 13:03:16 +01:00
2020-12-14 10:52:12 +01:00
2019-03-01 12:39:55 +01:00
# Delete network traffic old data.
2019-01-30 13:03:16 +01:00
log_message ( 'PURGE' , 'Deleting old network matrix data.' ) ;
2019-03-01 12:39:55 +01:00
if ( $ conf - > { '_delete_old_network_matrix' } > 0 ) {
my $ matrix_limit = time ( ) - 86400 * $ conf - > { '_delete_old_network_matrix' } ;
db_do ( $ dbh , "DELETE FROM tnetwork_matrix WHERE utimestamp < ?" , $ matrix_limit ) ;
}
2019-02-28 17:13:48 +01:00
2019-02-13 14:43:55 +01:00
# Delete old messages
log_message ( 'PURGE' , "Deleting old messages." ) ;
if ( $ conf - > { '_delete_old_messages' } > 0 ) {
my $ message_limit = time ( ) - 86400 * $ conf - > { '_delete_old_messages' } ;
db_do ( $ dbh , "DELETE FROM tmensajes WHERE timestamp < ?" , $ message_limit ) ;
}
2019-05-23 10:35:03 +02:00
# Delete old cache data
log_message ( 'PURGE' , "Deleting old cache data." ) ;
db_do ( $ dbh , "DELETE FROM `tvisual_console_elements_cache` WHERE (UNIX_TIMESTAMP(`created_at`) + `expiration`) < UNIX_TIMESTAMP()" ) ;
2013-02-13 16:55:41 +01: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
###############################################################################
2020-11-16 17:34:23 +01:00
sub pandora_compactdb ($$$) {
my ( $ conf , $ dbh , $ dbh_conf ) = @ _ ;
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 ;
2014-03-13 16:29:20 +01:00
my % module_proc_hash ;
2013-05-27 19:58:00 +02:00
2011-06-06 18:33:32 +02:00
return if ( $ conf - > { '_days_compact' } == 0 || $ conf - > { '_step_compact' } < 1 ) ;
2013-05-27 19:58:00 +02:00
# Convert compact interval length from hours to seconds
my $ step = $ conf - > { '_step_compact' } * 3600 ;
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 ;
2013-05-27 19:58:00 +02:00
my $ last_compact = $ start_utime ;
2008-04-24 18:24:55 +02:00
my $ stop_utime ;
2013-05-27 19:58:00 +02:00
# Do not compact the same data twice!
if ( defined ( $ conf - > { '_last_compact' } ) && $ conf - > { '_last_compact' } > $ limit_utime ) {
$ limit_utime = $ conf - > { '_last_compact' } ;
}
2020-06-22 17:33:21 +02:00
if ( $ start_utime <= $ limit_utime || ( defined ( $ conf - > { '_last_compact' } ) && ( ( $ conf - > { '_last_compact' } + 24 * 60 * 60 ) > $ start_utime ) ) ) {
2013-06-12 15:00:48 +02:00
log_message ( 'COMPACT' , "Data already compacted." ) ;
2013-05-27 19:58:00 +02:00
return ;
}
2013-06-12 15:00:48 +02:00
log_message ( 'COMPACT' , "Compacting data from " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( $ limit_utime ) ) . " to " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( $ start_utime ) ) . '.' , '' ) ;
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
2013-05-27 19:58:00 +02:00
last if ( $ start_utime < $ limit_utime ) ;
2007-08-28 17:09:04 +02:00
2013-06-12 15:00:48 +02:00
# Mark the progress
log_message ( '' , "." ) ;
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' } ;
2014-03-13 16:29:20 +01:00
if ( ! defined ( $ module_proc_hash { $ id_module } ) ) {
my $ module_type = get_db_value ( $ dbh , 'SELECT id_tipo_modulo FROM tagente_modulo WHERE id_agente_modulo = ?' , $ id_module ) ;
2014-10-07 11:59:02 +02:00
next unless defined ( $ module_type ) ;
2014-03-13 16:29:20 +01:00
# Mark proc modules.
2020-06-22 17:33:21 +02:00
if ( $ module_type == 2 || $ module_type == 6 || $ module_type == 9 || $ module_type == 18 || $ module_type == 21 || $ module_type == 31 || $ module_type == 35 || $ module_type == 100 ) {
2014-03-13 16:29:20 +01:00
$ module_proc_hash { $ id_module } = 1 ;
}
else {
$ module_proc_hash { $ id_module } = 0 ;
}
}
# Skip proc modules!
next if ( $ module_proc_hash { $ id_module } == 1 ) ;
2008-04-24 18:24:55 +02:00
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
2020-06-22 17:33:21 +02:00
db_do ( $ dbh , ' DELETE ad FROM tagente_datos ad
INNER JOIN tagente_modulo am ON ad . id_agente_modulo = am . id_agente_modulo AND am . id_tipo_modulo NOT IN ( 2 , 6 , 9 , 18 , 21 , 31 , 35 , 100 )
WHERE ad . utimestamp < ? AND ad . 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 } ) ;
}
2013-04-18 20:09:28 +02:00
usleep ( 1000 ) ; # Very small usleep, just to don't burn the DB
2008-04-24 18:24:55 +02:00
# Move to the next interval
$ start_utime = $ stop_utime ;
2007-08-28 17:09:04 +02:00
}
2013-06-12 15:00:48 +02:00
log_message ( '' , "\n" ) ;
2013-05-27 19:58:00 +02:00
# Mark the last compact date
if ( defined ( $ conf - > { '_last_compact' } ) ) {
2020-11-16 17:34:23 +01:00
db_do ( $ dbh_conf , 'UPDATE tconfig SET value=? WHERE token=?' , $ last_compact , 'last_compact' ) ;
2013-05-27 19:58:00 +02:00
} else {
2020-11-16 17:34:23 +01:00
db_do ( $ dbh_conf , 'INSERT INTO tconfig (value, token) VALUES (?, ?)' , $ last_compact , 'last_compact' ) ;
2013-05-27 19:58:00 +02:00
}
2006-03-27 05:37:27 +02:00
}
2014-03-18 11:59:53 +01:00
########################################################################
2009-12-18 12:26:13 +01:00
# Check command line parameters.
2014-03-18 11:59:53 +01:00
########################################################################
2018-05-30 11:08:48 +02:00
sub pandora_init_pdb ($) {
2009-12-18 12:26:13 +01:00
my $ conf = shift ;
2014-03-18 11:59:53 +01:00
2021-05-10 17:58:17 +02:00
log_message ( '' , "Pandora FMS DB Tool v$version\n\n" ) ;
2013-06-12 15:00:48 +02:00
log_message ( '' , "This program is Free Software, licensed under the terms of GPL License v2\n" ) ;
2018-05-25 12:39:30 +02:00
log_message ( '' , "You can download latest versions and documentation at official web\n\n" ) ;
2014-03-18 11:59:53 +01:00
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 ) ;
2013-05-07 17:38:11 +02:00
$ conf - > { '_onlypurge' } = 0 ;
$ conf - > { '_force' } = 0 ;
2012-04-17 13:38:41 +02:00
# 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
}
2013-05-07 17:38:11 +02:00
elsif ( $ param =~ m/-f\z/i ) {
$ conf - > { '_force' } = 1 ;
}
2006-03-27 05:37:27 +02:00
}
2014-03-18 11:59:53 +01:00
2009-12-18 12:26:13 +01:00
help_screen ( ) if ( $ conf - > { '_pandora_path' } eq '' ) ;
2006-03-27 05:37:27 +02:00
}
2020-11-23 20:16:11 +01:00
########################################################################
# Prepares conf read from historical database settings.
########################################################################
sub pandoradb_load_history_conf ($) {
my $ dbh = shift ;
my @ options = get_db_rows ( $ dbh , 'SELECT * FROM `tconfig`' ) ;
my % options = map
{
'_' . $ _ - > { 'token' } = > $ _ - > { 'value' }
} @ options ;
$ options { '_days_autodisable_deletion' } = 0 unless defined ( $ options { '_days_autodisable_deletion' } ) ;
$ options { '_num_past_special_days' } = 0 unless defined ( $ options { '_num_past_special_days' } ) ;
$ options { '_delete_old_network_matrix' } = 0 unless defined ( $ options { '_delete_old_network_matrix' } ) ;
$ options { '_delete_old_messages' } = 0 unless defined ( $ options { '_delete_old_messages' } ) ;
$ options { '_netflow_max_lifetime' } = 0 unless defined ( $ options { '_netflow_max_lifetime' } ) ;
$ options { 'claim_back_snmp_modules' } = 0 unless defined ( $ options { 'claim_back_snmp_modules' } ) ;
return \ % options ;
}
2006-03-27 05:37:27 +02:00
2014-03-18 11:59:53 +01:00
########################################################################
2009-12-18 12:26:13 +01:00
# Read external configuration file.
2014-03-18 11:59:53 +01:00
########################################################################
2018-05-30 11:08:48 +02:00
sub pandora_load_config_pdb ($) {
2009-12-18 12:26:13 +01:00
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
}
2014-07-25 05:47:55 +02:00
$ conf - > { 'dbengine' } = 'mysql' unless defined ( $ conf - > { 'dbengine' } ) ;
2012-05-21 08:37:12 +02:00
$ conf - > { 'dbport' } = '3306' unless defined ( $ conf - > { 'dbport' } ) ;
2014-07-25 05:47:55 +02:00
$ conf - > { 'claim_back_snmp_modules' } = '1' unless defined ( $ conf - > { 'claim_back_snmp_modules' } ) ;
2017-04-11 05:32:58 +02:00
$ conf - > { 'verbosity' } = '3' unless defined ( $ conf - > { 'verbosity' } ) ;
2009-12-18 12:26:13 +01:00
2021-05-17 13:12:42 +02:00
# Configure SSL.
set_ssl_opts ( $ conf ) ;
2016-05-12 13:34:50 +02:00
# Dynamic interval configuration.
$ conf - > { "dynamic_constant" } = 0.10 unless defined ( $ conf - > { "dynamic_constant" } ) ;
$ conf - > { "dynamic_warning" } = 0.10 unless defined ( $ conf - > { "dynamic_warning" } ) ;
$ conf - > { "dynamic_updates" } = 5 unless defined ( $ conf - > { "dynamic_updates" } ) ;
2017-06-01 10:15:22 +02:00
$ conf - > { 'servername' } = $ conf - > { 'servername' } ;
$ conf - > { 'servername' } = `hostname` unless defined ( $ conf - > { 'servername' } ) ;
$ conf - > { "servername" } =~ s/\s//g ;
2015-11-06 04:31:36 +01:00
# workaround for name unconsistency (corresponding entry at pandora_server.conf is 'errorlog_file')
2017-06-01 10:15:22 +02:00
$ conf - > { 'errorlogfile' } = $ conf - > { 'errorlog_file' } ;
$ conf - > { 'errorlogfile' } = "/var/log/pandora_server.error" unless defined ( $ conf - > { 'errorlogfile' } ) ;
2015-11-05 12:17:21 +01:00
2009-12-18 12:26:13 +01:00
# Read additional tokens from the DB
2015-07-22 10:35:09 +02:00
my $ dbh = db_connect ( $ conf - > { 'dbengine' } , $ 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'" ) ;
2013-05-27 19:58:00 +02:00
$ conf - > { '_last_compact' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'last_compact'" ) ;
2009-12-18 12:26:13 +01:00
$ 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'" ) ;
2016-11-11 10:29:47 +01:00
$ conf - > { '_history_event_enabled' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_event_enabled'" ) ;
2009-12-18 12:26:13 +01:00
$ 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'" ) ;
2022-09-08 14:28:36 +02:00
$ conf - > { '_history_db_adv' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_adv'" ) ;
$ conf - > { '_history_db_string_days' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_db_string_days'" ) ;
2016-11-11 10:29:47 +01:00
$ conf - > { '_history_event_days' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'history_event_days'" ) ;
2009-12-18 12:26:13 +01:00
$ 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'" ) ;
2022-08-29 14:05:30 +02:00
$ conf - > { '_days_delete_not_initialized' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'days_delete_not_initialized'" ) ;
2022-08-29 14:13:00 +02:00
$ conf - > { '_delete_notinit' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'delete_notinit'" ) ;
2016-12-14 13:02:10 +01:00
$ conf - > { '_inventory_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'inventory_purge'" ) ;
2019-02-13 14:43:55 +01:00
$ conf - > { '_delete_old_messages' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'delete_old_messages'" ) ;
2019-03-01 13:26:15 +01:00
$ conf - > { '_delete_old_network_matrix' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'delete_old_network_matrix'" ) ;
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'" ) ;
2013-02-13 16:55:41 +01:00
$ conf - > { '_netflow_max_lifetime' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'netflow_max_lifetime'" ) ;
$ conf - > { '_netflow_nfexpire' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'netflow_nfexpire'" ) ;
2021-01-07 10:42:29 +01:00
$ conf - > { '_netflow_path' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'netflow_path'" ) ;
2014-09-17 14:13:51 +02:00
$ conf - > { '_delete_notinit' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'delete_notinit'" ) ;
2019-08-30 08:47:49 +02:00
$ conf - > { '_session_timeout' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'session_timeout'" ) ;
2016-05-27 09:37:58 +02:00
$ conf - > { '_big_operation_step_datos_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'big_operation_step_datos_purge'" ) ;
$ conf - > { '_small_operation_step_datos_purge' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'small_operation_step_datos_purge'" ) ;
2016-06-03 11:58:13 +02:00
$ conf - > { '_days_autodisable_deletion' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'days_autodisable_deletion'" ) ;
2017-09-06 17:08:55 +02:00
$ conf - > { '_days_purge_old_information' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'Days_purge_old_information'" ) ;
$ conf - > { '_elasticsearch_ip' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'elasticsearch_ip'" ) ;
$ conf - > { '_elasticsearch_port' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'elasticsearch_port'" ) ;
2020-02-25 12:10:26 +01:00
$ conf - > { '_server_unique_identifier' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'server_unique_identifier'" ) ;
2016-05-27 09:37:58 +02:00
$ BIG_OPERATION_STEP = $ conf - > { '_big_operation_step_datos_purge' }
if ( $ conf - > { '_big_operation_step_datos_purge' } ) ;
$ SMALL_OPERATION_STEP = $ conf - > { '_small_operation_step_datos_purge' }
if ( $ conf - > { '_small_operation_step_datos_purge' } ) ;
2016-10-08 09:12:53 +02:00
$ conf - > { '_num_past_special_days' } = get_db_value ( $ dbh , "SELECT value FROM tconfig WHERE token = 'num_past_special_days'" ) ;
2013-02-13 16:55:41 +01:00
2009-12-18 12:26:13 +01:00
db_disconnect ( $ dbh ) ;
2018-05-17 18:38:26 +02:00
log_message ( '' , "DB Tool 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 {
2018-07-24 13:12:46 +02:00
my ( $ conf , $ dbh ) = @ _ ;
2010-03-10 17:03:38 +01:00
2013-06-12 15:00:48 +02:00
log_message ( 'INTEGRITY' , "Cleaning up group stats." ) ;
2010-03-10 17:03:38 +01:00
# Delete all records on tgroup_stats
db_do ( $ dbh , 'DELETE FROM tgroup_stat' ) ;
2013-04-24 03:12:50 +02:00
#print "[INTEGRITY] Deleting non-used IP addresses \n";
2013-06-12 15:00:48 +02:00
# DISABLED - Takes too much time and benefits of this are unclear..
2010-03-10 17:03:38 +01:00
# Delete all non-used IP addresses from taddress
2013-04-24 03:12:50 +02:00
#db_do ($dbh, 'DELETE FROM taddress WHERE id_a NOT IN (SELECT id_a FROM taddress_agent)');
2013-04-18 20:09:28 +02:00
2013-06-12 15:00:48 +02:00
log_message ( 'INTEGRITY' , "Deleting orphan alerts." ) ;
2010-03-10 17:03:38 +01:00
# 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)' ) ;
2013-06-12 15:00:48 +02:00
log_message ( 'INTEGRITY' , "Deleting orphan modules." ) ;
2010-03-10 17:03:38 +01:00
# 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
2016-03-16 15:57:45 +01:00
while ( defined ( get_db_value ( $ dbh , 'SELECT id_agente FROM tagente_estado WHERE id_agente NOT IN (SELECT id_agente FROM tagente)' ) ) ) {
db_delete_limit ( $ dbh , 'tagente_estado' , 'id_agente NOT IN (SELECT id_agente FROM tagente)' , $ BIG_OPERATION_STEP ) ;
}
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)' ) ;
2013-04-24 03:12:50 +02:00
2010-05-12 17:33:19 +02:00
# Check enterprise tables
2018-07-24 13:12:46 +02:00
enterprise_hook ( 'pandora_checkdb_integrity_enterprise' , [ $ conf , $ 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 {
2014-09-17 14:13:51 +02:00
my ( $ conf , $ dbh ) = @ _ ;
2014-03-18 11:59:53 +01:00
#-------------------------------------------------------------------
# 1. Check for modules that do not have tagente_estado but have
# tagente_module
#-------------------------------------------------------------------
2021-01-07 10:42:29 +01:00
if ( defined ( $ conf - > { '_delete_notinit' } ) && $ conf - > { '_delete_notinit' } ne "" && $ conf - > { '_delete_notinit' } eq "1" ) {
2014-09-17 14:13:51 +02:00
log_message ( 'CHECKDB' , "Deleting not-init data." ) ;
my @ modules = get_db_rows ( $ dbh ,
' SELECT id_agente_modulo , id_agente
FROM tagente_estado
WHERE estado = 4 ' ) ;
2014-03-18 11:59:53 +01:00
2014-09-17 14:13:51 +02:00
foreach my $ module ( @ modules ) {
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
my $ id_agente = $ module - > { 'id_agente' } ;
# Skip policy modules
my $ is_policy_module = enterprise_hook ( 'is_policy_module' ,
[ $ dbh , $ id_agente_modulo ] ) ;
next if ( defined ( $ is_policy_module ) && $ is_policy_module ) ;
# 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 ) ;
#---------------------------------------------------------------
# Delete the module
#---------------------------------------------------------------
2014-10-17 17:58:04 +02:00
# Mark the agent for module and alert counters update
db_do ( $ dbh ,
' UPDATE tagente
SET update_module_count = 1 , update_alert_count = 1
2014-09-17 14:13:51 +02:00
WHERE id_agente = ? ' , $ id_agente ) ;
2014-10-17 17:58:04 +02:00
# Delete the module
2014-09-17 14:13:51 +02:00
db_do ( $ dbh ,
' DELETE FROM tagente_modulo
WHERE id_agente_modulo = ? ' , $ id_agente_modulo ) ;
# Do a nanosleep here for 0,001 sec
usleep ( 100000 ) ;
# Delete any alerts associated to the module
db_do ( $ dbh ,
' DELETE FROM talert_template_modules
WHERE id_agent_module = ? ' , $ id_agente_modulo ) ;
}
} else {
log_message ( 'CHECKDB' , "Ignoring not-init data." ) ;
2009-12-14 10:37:41 +01:00
}
2014-03-18 11:59:53 +01:00
if ( defined ( $ conf { '_days_delete_unknown' } ) && $ conf { '_days_delete_unknown' } > 0 ) {
2017-04-11 05:35:12 +02:00
log_message ( 'CHECKDB' ,
"Deleting unknown data (More than " . $ conf { '_days_delete_unknown' } . " days)." ) ;
2014-03-18 11:59:53 +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 ) {
2014-10-17 17:58:04 +02:00
my $ id_agente = $ module - > { 'id_agente' } ;
2011-07-08 13:11:36 +02:00
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
2014-03-18 11:59:53 +01:00
2011-07-08 13:11:36 +02:00
# Skip policy modules
2014-03-18 11:59:53 +01:00
my $ is_policy_module = enterprise_hook ( 'is_policy_module' ,
[ $ dbh , $ id_agente_modulo ] ) ;
2011-12-12 11:55:40 +01:00
next if ( defined ( $ is_policy_module ) && $ is_policy_module ) ;
2014-03-18 11:59:53 +01:00
2014-10-17 17:58:04 +02:00
# Mark the agent for module and alert counters update
db_do ( $ dbh ,
' UPDATE tagente
SET update_module_count = 1 , update_alert_count = 1
WHERE id_agente = ? ' , $ id_agente ) ;
2011-07-08 13:11:36 +02:00
# Delete the module
2014-03-18 11:59:53 +01:00
db_do ( $ dbh ,
' DELETE FROM tagente_modulo
WHERE disabled = 0
AND id_agente_modulo = ? ' , $ id_agente_modulo ) ;
2013-02-14 14:11:52 +01:00
2014-10-17 17:58:04 +02:00
# Do a nanosleep here for 0,001 sec
usleep ( 100000 ) ;
2011-07-08 13:11:36 +02:00
# Delete any alerts associated to the module
2014-03-18 11:59:53 +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 ) ;
2013-04-18 20:09:28 +02:00
# Do a nanosleep here for 0,001 sec
usleep ( 100000 ) ;
2011-07-08 13:11:36 +02:00
}
}
2022-08-29 14:05:30 +02:00
# Perform a clean of not initialized modules.
2022-08-29 14:13:00 +02:00
if ( defined ( $ conf { '_days_delete_not_initialized' } ) && $ conf { '_days_delete_not_initialized' } > 0 && $ conf - > { '_delete_notinit' } eq "1" ) {
2022-08-29 14:05:30 +02:00
log_message ( 'CHECKDB' ,
"Deleting not initialized modules (More than " . $ conf { '_days_delete_not_initialized' } . " days)." ) ;
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 = 4 OR estado = 5 )
AND utimestamp < UNIX_TIMESTAMP ( ) - ? ' ,
86400 * $ conf { '_days_delete_not_initialized' } ) ;
foreach my $ module ( @ modules ) {
my $ id_agente = $ module - > { 'id_agente' } ;
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
# Skip policy modules
my $ is_policy_module = enterprise_hook ( 'is_policy_module' ,
[ $ dbh , $ id_agente_modulo ] ) ;
next if ( defined ( $ is_policy_module ) && $ is_policy_module ) ;
# Mark the agent for module and alert counters update
db_do ( $ dbh ,
' UPDATE tagente
SET update_module_count = 1 , update_alert_count = 1
WHERE id_agente = ? ' , $ id_agente ) ;
# Delete the module
db_do ( $ dbh ,
' DELETE FROM tagente_modulo
WHERE disabled = 0
AND id_agente_modulo = ? ' , $ id_agente_modulo ) ;
# Do a nanosleep here for 0,001 sec
usleep ( 100000 ) ;
# Delete any alerts associated to the module
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 ) ;
# Do a nanosleep here for 0,001 sec
usleep ( 100000 ) ;
}
}
2014-03-18 11:59:53 +01:00
log_message ( 'CHECKDB' ,
"Checking database consistency (Missing status)." ) ;
2014-09-17 14:13:51 +02:00
my @ modules = get_db_rows ( $ dbh , 'SELECT * FROM tagente_modulo' ) ;
2009-12-18 12:26:13 +01:00
foreach my $ module ( @ modules ) {
my $ id_agente_modulo = $ module - > { 'id_agente_modulo' } ;
my $ id_agente = $ module - > { 'id_agente' } ;
2014-03-18 11:59:53 +01:00
2009-12-18 12:26:13 +01:00
# check if exist in tagente_estado and create if not
2014-03-18 11:59:53 +01:00
my $ count = get_db_value ( $ dbh ,
' SELECT COUNT ( * )
FROM tagente_estado
WHERE id_agente_modulo = ? ' , $ id_agente_modulo ) ;
2009-12-18 12:26:13 +01:00
next if ( defined ( $ count ) && $ count > 0 ) ;
2014-03-18 11:59:53 +01: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 ) ;
log_message ( 'CHECKDB' ,
"Inserting module $id_agente_modulo in state table." ) ;
2007-08-28 17:09:04 +02:00
}
2014-03-18 11:59:53 +01:00
log_message ( 'CHECKDB' ,
"Checking database consistency (Missing module)." ) ;
#-------------------------------------------------------------------
# 2. Check for modules in tagente_estado that do not have
# tagente_modulo, if there is any, delete it
#-------------------------------------------------------------------
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' } ;
2014-03-18 11:59:53 +01:00
2009-12-18 12:26:13 +01:00
# check if exist in tagente_estado and create if not
2014-03-18 11:59:53 +01:00
my $ count = get_db_value ( $ dbh ,
' SELECT COUNT ( * )
FROM tagente_modulo
WHERE id_agente_modulo = ? ' , $ id_agente_modulo ) ;
2009-12-18 12:26:13 +01:00
next if ( defined ( $ count ) && $ count > 0 ) ;
2014-03-18 11:59:53 +01:00
db_do ( $ dbh , ' DELETE FROM tagente_estado
WHERE id_agente_modulo = ? ' , $ id_agente_modulo ) ;
2013-04-18 20:09:28 +02:00
# Do a nanosleep here for 0,001 sec
usleep ( 100000 ) ;
2014-03-18 11:59:53 +01:00
log_message ( 'CHECKDB' ,
"Deleting non-existing module $id_agente_modulo in state table." ) ;
2009-12-18 12:26:13 +01:00
}
2017-02-21 14:24:06 +01:00
#-------------------------------------------------------------------
# 3. Update empty aliases.
#-------------------------------------------------------------------
log_message ( 'CHECKDB' , "Updating empty aliases." ) ;
db_do ( $ dbh , "UPDATE tagente SET alias=nombre WHERE alias=''" ) ;
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 {
2018-05-25 12:39:30 +02:00
log_message ( '' , "Usage: $0 <path to configuration file> [options]\n\n" ) ;
2013-06-12 15:00:48 +02:00
log_message ( '' , "\t\t-p Only purge and consistency check, skip compact.\n" ) ;
log_message ( '' , "\t\t-f Force execution event if another instance of $0 is running.\n\n" ) ;
2012-12-17 18:11:00 +01:00
exit - 1 ;
2006-03-27 05:37:27 +02:00
}
2013-05-03 20:00:53 +02:00
##############################################################################
# Delete old module data.
##############################################################################
sub pandora_delete_old_module_data {
2013-05-07 17:38:11 +02:00
my ( $ dbh , $ table , $ ulimit_access_timestamp , $ ulimit_timestamp ) = @ _ ;
2013-05-03 20:00:53 +02:00
my $ first_mark ;
my $ total_time ;
my $ purge_steps ;
my $ purge_count ;
my $ mark1 ;
my $ mark2 ;
# 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
# WARNING. This code is EXTREMELLY important. This block (data deletion) could KILL a database if
# you alter code and you don't know exactly what are you doing. Please take in mind this code executes each hour
# and has been patches MANY times. Before altering anything, think twice !
2015-07-22 15:36:46 +02:00
$ first_mark = get_db_value_limit ( $ dbh , "SELECT utimestamp FROM $table ORDER BY utimestamp ASC" , 1 ) ;
2013-05-03 20:00:53 +02:00
if ( defined ( $ first_mark ) ) {
$ total_time = $ ulimit_timestamp - $ first_mark ;
$ purge_steps = int ( $ total_time / $ BIG_OPERATION_STEP ) ;
2013-06-12 15:00:48 +02:00
if ( $ purge_steps > 0 ) {
for ( my $ ax = 1 ; $ ax <= $ BIG_OPERATION_STEP ; $ ax + + ) {
$ mark1 = $ first_mark + ( $ purge_steps * $ ax ) ;
$ mark2 = $ first_mark + ( $ purge_steps * ( $ ax - 1 ) ) ;
# Let's split the intervals in $SMALL_OPERATION_STEP deletes each
$ purge_count = get_db_value ( $ dbh , "SELECT COUNT(id_agente_modulo) FROM $table WHERE utimestamp < $mark1 AND utimestamp >= $mark2" ) ;
while ( $ purge_count > 0 ) {
2015-07-22 15:36:46 +02:00
db_delete_limit ( $ dbh , $ table , 'utimestamp < ? AND utimestamp >= ?' , $ SMALL_OPERATION_STEP , $ mark1 , $ mark2 ) ;
2013-06-12 15:00:48 +02:00
# Do a nanosleep here for 0,001 sec
usleep ( 10000 ) ;
$ purge_count = $ purge_count - $ SMALL_OPERATION_STEP ;
}
log_message ( 'PURGE' , "Deleting old data from $table. $ax%" , "\r" ) ;
2013-05-03 20:00:53 +02:00
}
2013-06-12 15:00:48 +02:00
log_message ( '' , "\n" ) ;
} else {
log_message ( 'PURGE' , "No data to purge in $table." ) ;
2013-05-03 20:00:53 +02:00
}
} else {
2013-06-12 15:00:48 +02:00
log_message ( 'PURGE' , "No data in $table." ) ;
2013-05-03 20:00:53 +02:00
}
}
2014-10-07 14:29:19 +02:00
##############################################################################
# Delete old export data.
##############################################################################
sub pandora_delete_old_export_data {
my ( $ dbh , $ ulimit_timestamp ) = @ _ ;
2020-11-23 20:16:11 +01:00
log_message ( 'PURGE' , "Deleting old export data from tserver_export_data" ) ;
2015-07-22 15:36:46 +02:00
while ( ( my $ rc = db_delete_limit ( $ dbh , 'tserver_export_data' , 'UNIX_TIMESTAMP(timestamp) < ?' , $ SMALL_OPERATION_STEP , $ ulimit_timestamp ) ) ne '0E0' ) {
print "RC:$rc\n" ;
2014-10-07 14:29:19 +02:00
usleep ( 10000 ) ;
} ;
}
2022-09-08 10:14:44 +02:00
##############################################################################
# Delete old data from tplanned_downtime.
##############################################################################
sub pandora_delete_old_tplanned_downtime {
my ( $ conf , $ dbh , $ h_conf ) = @ _ ;
# Use the configuration from the history DB if available, which should be
# less restrictive.
my $ days_purge = $ conf - > { '_days_purge' } ;
if ( defined ( $ h_conf ) &&
defined ( $ h_conf - > { '_days_purge' } ) &&
$ h_conf - > { '_days_purge' } > 0 ) {
$ days_purge = $ h_conf - > { '_days_purge' } ;
}
# _days_purge was not configured.
return unless $ days_purge > 0 ;
my $ ulimit_timestamp = time ( ) - ( 86400 * $ days_purge ) ;
log_message ( 'PURGE' , "Deleting data older than $days_purge days from tplanned_downtime." ) ;
db_do ( $ dbh , " DELETE FROM tplanned_downtime
WHERE type_execution = 'once'
AND date_to < ? " , $ ulimit_timestamp ) ;
}
2015-04-17 14:54:17 +02:00
##############################################################################
# Delete old session data.
##############################################################################
sub pandora_delete_old_session_data {
2020-11-26 18:27:11 +01:00
my ( $ conf , $ dbh , $ ulimit_timestamp ) = @ _ ;
2019-08-30 08:47:49 +02:00
2020-11-26 18:27:11 +01:00
my $ session_timeout = $ conf - > { '_session_timeout' } ;
2019-08-30 08:47:49 +02:00
2020-11-26 18:27:11 +01:00
# DO not erase anything if session_timeout is not set.
return unless ( defined ( $ session_timeout ) && $ session_timeout ne '' ) ;
if ( $ session_timeout == 0 ) {
# As defined in console.
$ session_timeout = 90 ;
}
2019-08-30 08:47:49 +02:00
2020-11-26 18:27:11 +01:00
if ( $ session_timeout == - 1 ) {
# The session expires in 10 years
$ session_timeout = 315576000 ;
} else {
$ session_timeout *= 60 ;
2019-08-30 08:47:49 +02:00
}
2015-04-17 14:54:17 +02:00
2020-11-26 18:27:11 +01:00
$ ulimit_timestamp = time ( ) - $ session_timeout ;
2015-04-17 14:54:17 +02:00
log_message ( 'PURGE' , "Deleting old session data from tsessions_php\n" ) ;
2015-07-22 15:36:46 +02:00
while ( db_delete_limit ( $ dbh , 'tsessions_php' , 'last_active < ?' , $ SMALL_OPERATION_STEP , $ ulimit_timestamp ) ne '0E0' ) {
2015-04-17 14:54:17 +02:00
usleep ( 10000 ) ;
} ;
2019-08-30 08:47:49 +02:00
2020-11-26 18:27:11 +01:00
db_do ( $ dbh , "DELETE FROM tsessions_php WHERE data IS NULL OR id_session REGEXP '^cron-'" ) ;
2015-04-17 14:54:17 +02:00
}
2022-02-02 15:08:13 +01:00
###############################################################################
# Delete old data from the history database.
###############################################################################
sub pandoradb_history ($$) {
my ( $ conf , $ dbh ) = @ _ ;
my $ timestamp = strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) ;
my $ ulimit_access_timestamp = time ( ) - 86400 ;
my $ ulimit_timestamp = time ( ) - ( 86400 * $ conf - > { '_days_purge' } ) ;
log_message ( '' , "Starting at " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) . "\n" ) ;
# Delete old numeric data.
if ( $ conf - > { '_days_purge' } > 0 ) {
pandora_delete_old_module_data ( $ dbh , 'tagente_datos' , $ ulimit_access_timestamp , $ ulimit_timestamp ) ;
} else {
log_message ( 'PURGE' , 'days_purge is set to 0. Old data will not be deleted.' ) ;
}
# Delete old string data.
$ conf - > { '_string_purge' } = 7 unless defined ( $ conf - > { '_string_purge' } ) ;
if ( $ conf - > { '_string_purge' } > 0 ) {
$ ulimit_access_timestamp = time ( ) - 86400 ;
$ ulimit_timestamp = time ( ) - ( 86400 * $ conf - > { '_string_purge' } ) ;
pandora_delete_old_module_data ( $ dbh , 'tagente_datos_string' , $ ulimit_access_timestamp , $ ulimit_timestamp ) ;
} else {
log_message ( 'PURGE' , 'string_purge is set to 0. Old string data will not be deleted.' ) ;
}
# Delete old events.
if ( $ conf - > { '_event_purge' } > 0 ) {
log_message ( 'PURGE' , "Deleting events older than " . $ conf - > { '_event_purge' } . " days from tevento." , '' ) ;
my $ event_limit = time ( ) - 86400 * $ conf - > { '_event_purge' } ;
my $ events_to_delete = get_db_value ( $ dbh , "SELECT COUNT(*) FROM tevento WHERE utimestamp < ?" , $ event_limit ) ;
while ( $ events_to_delete > 0 ) {
db_delete_limit ( $ dbh , 'tevento' , "utimestamp < ?" , $ BIG_OPERATION_STEP , $ event_limit ) ;
$ events_to_delete = $ events_to_delete - $ BIG_OPERATION_STEP ;
# Mark the progress.
log_message ( '' , "." ) ;
# Do not overload the MySQL server.
usleep ( 10000 ) ;
}
log_message ( '' , "\n" ) ;
}
2022-03-24 19:23:35 +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 ( ) . "')" ) ;
2022-02-02 15:08:13 +01:00
log_message ( '' , "Ending at " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) . "\n" ) ;
}
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
###############################################################################
2022-09-08 10:14:44 +02:00
sub pandoradb_main {
my ( $ conf , $ dbh , $ h_conf , $ history_dbh ) = @ _ ;
2009-02-03 14:16:31 +01:00
2013-06-12 15:00:48 +02:00
log_message ( '' , "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
2022-09-08 10:14:44 +02:00
pandora_purgedb ( $ conf , $ dbh , $ h_conf ) ;
2009-12-18 12:26:13 +01:00
# Consistency check
2014-09-17 14:13:51 +02:00
pandora_checkdb_consistency ( $ conf , $ dbh ) ;
2009-12-18 12:26:13 +01:00
2010-03-10 17:03:38 +01:00
# Maintain Referential integrity and other stuff
2018-07-24 13:12:46 +02:00
pandora_checkdb_integrity ( $ conf , $ dbh ) ;
2010-03-10 17:03:38 +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
# Move old data to the history DB
if ( defined ( $ history_dbh ) ) {
2022-09-08 14:28:36 +02:00
undef ( $ history_dbh ) unless defined ( enterprise_hook ( 'pandora_historydb' , [ $ dbh , $ history_dbh , $ conf - > { '_history_db_days' } , $ conf - > { '_history_db_step' } , $ conf - > { '_history_db_delay' } , $ conf - > { '_history_db_string_days' } , $ conf - > { '_history_db_adv' } ] ) ) ;
2020-10-14 11:48:34 +02:00
if ( defined ( $ conf { '_history_event_enabled' } ) && $ conf - > { '_history_event_enabled' } ne "" && $ conf - > { '_history_event_enabled' } == 1 ) {
2016-11-11 10:29:47 +01:00
undef ( $ history_dbh ) unless defined ( enterprise_hook ( 'pandora_history_event' , [ $ dbh , $ history_dbh , $ conf - > { '_history_event_days' } , $ conf - > { '_history_db_step' } , $ conf - > { '_history_db_delay' } ] ) ) ;
}
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
}
2021-01-29 10:25:14 +01:00
# Only active database should be compacted. Disabled for historical database.
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
# Compact on if enable and DaysCompact are below DaysPurge
2022-02-02 15:08:13 +01:00
if ( ( $ conf - > { '_onlypurge' } == 0 )
2021-01-29 10:25:14 +01:00
&& ( $ conf - > { '_days_compact' } < $ conf - > { '_days_purge' } )
) {
2020-11-16 17:34:23 +01:00
pandora_compactdb ( $ conf , defined ( $ history_dbh ) ? $ history_dbh : $ dbh , $ dbh ) ;
2009-12-18 12:26:13 +01:00
}
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 ( ) . "')" ) ;
2014-05-30 14:26:40 +02:00
# Move SNMP modules back to the Enterprise server
enterprise_hook ( "claim_back_snmp_modules" , [ $ dbh , $ conf ] ) ;
2021-01-20 17:56:39 +01:00
# Check if there are discovery tasks with wrong id_recon_server
pandora_check_forgotten_discovery_tasks ( $ conf , $ dbh ) ;
2016-05-12 13:34:50 +02:00
# Recalculating dynamic intervals.
enterprise_hook ( "update_min_max" , [ $ dbh , $ conf ] ) ;
2020-06-16 14:24:40 +02:00
# Metaconsole database cleanup.
enterprise_hook ( "metaconsole_database_cleanup" , [ $ dbh , $ conf ] ) ;
2021-12-07 16:19:50 +01:00
# NCM cleanup.
enterprise_hook ( "ncm_database_cleanup" , [ $ dbh , $ conf ] ) ;
2013-06-12 15:00:48 +02:00
log_message ( '' , "Ending at " . strftime ( "%Y-%m-%d %H:%M:%S" , localtime ( ) ) . "\n" ) ;
2006-03-27 05:37:27 +02:00
}
2013-05-07 17:38:11 +02:00
2021-01-20 17:56:39 +01:00
###############################################################################
# Check for discovery tasks configured with servers down
###############################################################################
sub pandora_check_forgotten_discovery_tasks {
my ( $ conf , $ dbh ) = @ _ ;
log_message ( 'FORGOTTEN DISCOVERY TASKS' , "Check for discovery tasks bound to inactive servers." ) ;
my @ discovery_tasks = get_db_rows ( $ dbh , 'SELECT id_rt, id_recon_server, name FROM trecon_task' ) ;
my $ discovery_tasks_count = @ discovery_tasks ;
# End of the check (this server has not discovery tasks!).
if ( $ discovery_tasks_count eq 0 ) {
log_message ( 'FORGOTTEN DISCOVERY TASKS' , 'There are not defined discovery tasks. Skipping.' ) ;
return ;
}
my $ master_server = get_db_value ( $ dbh , 'SELECT id_server FROM tserver WHERE server_type = ? AND status != -1' , DISCOVERYSERVER ) ;
# Goes through all the tasks to check if any have the server down.
foreach my $ task ( @ discovery_tasks ) {
if ( $ task - > { 'id_recon_server' } ne $ master_server ) {
my $ this_server_status = get_db_value ( $ dbh , 'SELECT status FROM tserver WHERE id_server = ?' , $ task - > { 'id_recon_server' } ) ;
if ( ! defined ( $ this_server_status ) || $ this_server_status eq - 1 ) {
my $ updated_task = db_process_update ( $ dbh , 'trecon_task' , { 'id_recon_server' = > $ master_server } , { 'id_rt' = > $ task - > { 'id_rt' } } ) ;
log_message ( 'FORGOTTEN DISCOVERY TASKS' , 'Updated discovery task ' . $ task - > { 'name' } ) ;
}
}
}
log_message ( 'FORGOTTEN DISCOVERY TASKS' , 'Step ended' ) ;
}
2013-05-07 17:38:11 +02:00
# Init
2018-05-30 11:08:48 +02:00
pandora_init_pdb ( \ % conf ) ;
2013-05-07 17:38:11 +02:00
# Read config file
2018-05-30 11:08:48 +02:00
pandora_load_config_pdb ( \ % conf ) ;
2013-05-07 17:38:11 +02:00
# Load enterprise module
if ( enterprise_load ( \ % conf ) == 0 ) {
2018-05-25 12:39:30 +02:00
log_message ( '' , " [*] " . pandora_get_initial_product_name ( ) . " Enterprise module not available.\n\n" ) ;
2014-03-18 11:59:53 +01:00
}
else {
2018-05-25 12:39:30 +02:00
log_message ( '' , " [*] " . pandora_get_initial_product_name ( ) . " Enterprise module loaded.\n\n" ) ;
2013-05-07 17:38:11 +02:00
}
# Connect to the DB
2015-07-22 12:15:51 +02:00
my $ dbh = db_connect ( $ conf { 'dbengine' } , $ conf { 'dbname' } , $ conf { 'dbhost' } , $ conf { 'dbport' } , $ conf { 'dbuser' } , $ conf { 'dbpass' } ) ;
2018-11-21 12:05:20 +01:00
my $ history_dbh = undef ;
is_metaconsole ( \ % conf ) ;
2020-12-15 10:45:37 +01:00
if ( defined ( $ conf { '_history_db_enabled' } ) && $ conf { '_history_db_enabled' } eq '1' ) {
2018-11-21 12:05:20 +01:00
eval {
2020-03-19 09:56:34 +01:00
$ conf { 'encryption_key' } = enterprise_hook ( 'pandora_get_encryption_key' , [ \ % conf , $ conf { 'encryption_passphrase' } ] ) ;
$ history_dbh = db_connect ( $ conf { 'dbengine' } , $ conf { '_history_db_name' } , $ conf { '_history_db_host' } , $ conf { '_history_db_port' } , $ conf { '_history_db_user' } , pandora_output_password ( \ % conf , $ conf { '_history_db_pass' } ) ) ;
2022-09-08 10:14:44 +02:00
$ h_conf = pandoradb_load_history_conf ( $ history_dbh ) ;
2018-11-21 12:05:20 +01:00
} ;
if ( $@ ) {
if ( is_offline ( \ % conf ) ) {
log_message ( '!' , "Cannot connect to the history database. Skipping." ) ;
} else {
die ( "$@\n" ) ;
}
}
}
2013-05-07 17:38:11 +02:00
2020-09-22 12:17:29 +02:00
# Only run on master servers.
pandora_set_master ( \ % conf , $ dbh ) ;
if ( $ conf { '_force' } == 0 && pandora_is_master ( \ % conf ) == 0 ) {
log_message ( '' , " [*] Not a master server.\n\n" ) ;
exit 1 ;
}
2022-03-28 20:56:05 +02:00
# Set the lock name for pandora_db.
2020-11-23 20:16:11 +01:00
my $ lock_name = $ conf { 'dbname' } ;
2022-03-28 20:56:05 +02:00
# Release the database lock in forced mode.
if ( $ conf { '_force' } == 1 ) {
log_message ( '' , " [*] Releasing database lock.\n\n" ) ;
db_release_pandora_lock ( $ dbh , $ lock_name , $ LOCK_TIMEOUT ) ;
}
2022-08-04 15:11:51 +02:00
# Get a lock merging.
my $ lock_merge = db_get_lock ( $ dbh , 'merge-working' , $ LOCK_TIMEOUT , 1 ) ;
if ( $ lock_merge == 0 ) {
log_message ( '' , " [*] Merge is running.\n\n" ) ;
exit 1 ;
}
# Get a lock on merging events.
my $ lock_merge_events = db_get_lock ( $ dbh , 'merging-events' , $ LOCK_TIMEOUT , 1 ) ;
if ( $ lock_merge_events == 0 ) {
log_message ( '' , " [*] Merge events is running.\n\n" ) ;
exit 1 ;
}
2022-03-28 20:56:05 +02:00
# Get a lock on dbname.
my $ lock = db_get_pandora_lock ( $ dbh , $ lock_name , $ LOCK_TIMEOUT ) ;
if ( $ lock == 0 ) {
2018-05-17 18:38:26 +02:00
log_message ( '' , " [*] Another instance of DB Tool seems to be running.\n\n" ) ;
2013-05-07 17:38:11 +02:00
exit 1 ;
}
# Main
2022-09-08 10:14:44 +02:00
pandoradb_main ( \ % conf , $ dbh , $ h_conf , $ history_dbh ) ;
2013-05-07 17:38:11 +02:00
2020-11-23 20:16:11 +01:00
# history_dbh is unset in pandoradb_main if not in use.
if ( defined ( $ history_dbh ) ) {
log_message ( '' , " [>] DB Tool running on historical database.\n" ) ;
# Keep base settings.
$ h_conf - > { '_onlypurge' } = $ conf { '_onlypurge' } ;
2022-02-02 15:08:13 +01:00
# Launch maintenance process for historical database.
pandoradb_history ( $ h_conf , $ history_dbh ) ;
2020-11-23 20:16:11 +01:00
2021-02-01 15:00:54 +01:00
# Handle partitions.
enterprise_hook ( 'handle_partitions' , [ $ h_conf , $ history_dbh ] ) ;
2020-11-23 20:16:11 +01:00
}
2021-02-25 13:18:04 +01:00
# Keep integrity between PandoraFMS agents and IntegriaIMS inventory objects.
pandora_sync_agents_integria ( $ dbh ) ;
# Get Integria IMS ticket types for alert commands.
my @ types = pandora_get_integria_ticket_types ( $ dbh ) ;
if ( scalar ( @ types ) != 0 ) {
my $ query_string = '' ;
foreach my $ type ( @ types ) {
$ query_string . = $ type - > { 'id' } . ',' . $ type - > { 'name' } . ';' ;
}
2021-03-11 17:54:20 +01:00
$ query_string = substr $ query_string , 0 , - 1 ;
2021-03-10 10:28:27 +01:00
db_do ( $ dbh , "UPDATE talert_commands SET fields_descriptions='[\"Ticket title\",\"Ticket group ID\",\"Ticket priority\",\"Ticket owner\",\"Ticket type\",\"Ticket status\",\"Ticket description\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\"]' WHERE name=\"Integria IMS Ticket\"" ) ;
db_do ( $ dbh , "UPDATE talert_commands SET fields_values='[\"\", \"\", \"\",\"\",\"" . $ query_string . "\",\"\",\"\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\",\"_integria_type_custom_field_\"]' WHERE name=\"Integria IMS Ticket\"" ) ;
2021-02-25 13:18:04 +01:00
}
2013-05-07 17:38:11 +02:00
# Release the lock
2022-03-28 20:56:05 +02:00
db_release_pandora_lock ( $ dbh , $ lock_name ) ;
2013-05-07 17:38:11 +02:00
2018-11-21 12:05:20 +01:00
# Cleanup and exit
db_disconnect ( $ history_dbh ) if defined ( $ history_dbh ) ;
db_disconnect ( $ dbh ) ;
exit 0 ;