196 lines
7.4 KiB
Perl
Executable File
196 lines
7.4 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
##########################################################################
|
|
# Pandora Server. SNMP Console
|
|
##########################################################################
|
|
# Copyright (c) 2004-2008 Sancho Lerena, slerena@gmail.com
|
|
# Copyright (c) 2005-2008 Artica Soluciones Tecnologicas S.L
|
|
#
|
|
#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; either version 2
|
|
#of the License, or (at your option) any later version.
|
|
#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
|
|
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
##########################################################################
|
|
|
|
# Includes list
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Date::Manip; # Needed to manipulate DateTime formats of input, output and compare
|
|
use Time::Local; # DateTime basic manipulation
|
|
use Time::HiRes; # For high precission timedate functions (Net::Ping)
|
|
|
|
# Pandora Modules
|
|
use PandoraFMS::Config;
|
|
use PandoraFMS::Tools;
|
|
use PandoraFMS::DB;
|
|
enterprise_load ();
|
|
|
|
# FLUSH in each IO (only for debug, very slooow)
|
|
# ENABLED in DEBUGMODE
|
|
# DISABLE FOR PRODUCTION
|
|
$| = 0;
|
|
|
|
my %pa_config;
|
|
|
|
$SIG{'TERM'} = 'pandora_shutdown';
|
|
$SIG{'INT'} = 'pandora_shutdown';
|
|
|
|
# Inicio del bucle principal de programa
|
|
pandora_init(\%pa_config,"Pandora SNMP Console");
|
|
|
|
# Read config file for Global variables
|
|
pandora_loadconfig (\%pa_config, 2);
|
|
|
|
# Audit server starting
|
|
pandora_audit (\%pa_config, "Pandora Server SNMP Console Daemon starting", "SYSTEM", "System");
|
|
|
|
# Daemonize and put in background
|
|
if ( $pa_config{"daemon"} eq "1" ){
|
|
if ($pa_config{"quiet"} eq "0"){
|
|
print " [*] Backgrounding Pandora FMS SNMP Console process.\n\n";
|
|
}
|
|
&pandora_daemonize ( \%pa_config);
|
|
}
|
|
|
|
pandora_snmptrapd (\%pa_config);
|
|
|
|
##########################################################################
|
|
## SUB pandora_snmptrapd
|
|
## Pandora SNMP Trap console/daemon subsystem
|
|
##########################################################################
|
|
|
|
sub pandora_snmptrapd {
|
|
my $pa_config = $_[0];
|
|
my $snmp_logfile = $pa_config->{'snmp_logfile'};
|
|
my $logfile_size; # Size of logfile, use for calculating index file
|
|
my @array;
|
|
my $datos;
|
|
my $timestamp;
|
|
my $source;
|
|
my $oid;
|
|
my $type;
|
|
my $type_desc;
|
|
my $value;
|
|
my $custom_oid = '';
|
|
my $custom_type = '';
|
|
my $custom_value = '';
|
|
my $sql_insert;
|
|
my @index_data;
|
|
|
|
while ( ! -e $snmp_logfile) { # Wait until a snmplogfile exists
|
|
sleep 5;
|
|
}
|
|
open (SNMPLOGFILE, $snmp_logfile);
|
|
print " [*] SNMP Console enabled \n";
|
|
$index_data[0]=0;
|
|
$index_data[1]=0;
|
|
# Check for index file
|
|
if ( -e $snmp_logfile.".index" ){
|
|
open SNMPLOGFILE_INDEX, $snmp_logfile.".index";
|
|
$datos = <SNMPLOGFILE_INDEX>;
|
|
close SNMPLOGFILE_INDEX;
|
|
@index_data = split(/\s+/,$datos);
|
|
# $index_data[0] is the last line readed
|
|
# $index_data[1] is the size of file (use for calculate new files or reset logfiles
|
|
}
|
|
$logfile_size = (stat($snmp_logfile))[7];
|
|
|
|
if ($logfile_size < $index_data[1]){ # Log size smaller last time we read it -> new one
|
|
unlink ($snmp_logfile.".index");
|
|
$index_data[0]=0;
|
|
$index_data[1]=0;
|
|
logger ($pa_config,"New SNMP logfile detected, resetting index",1);
|
|
}
|
|
|
|
if (($index_data[1] <= $logfile_size) && ($index_data[0] > 0)){
|
|
# Skip already processed records
|
|
for ($value=0;$value < $index_data[0];$value++){
|
|
$datos = readline SNMPLOGFILE;
|
|
}
|
|
}
|
|
# open database, only ONCE. We pass reference to DBI handler ($dbh) to all subprocess
|
|
my $dbh = DBI->connect("DBI:mysql:$pa_config->{'dbname'}:$pa_config->{'dbhost'}:3306",$pa_config->{'dbuser'}, $pa_config->{'dbpass'}, { RaiseError => 1, AutoCommit => 1 });
|
|
|
|
my $trap2agent = enterprise_hook('snmp_get_trap2agent', [$dbh]);
|
|
|
|
# Main loop for reading file
|
|
while ( 1 ){
|
|
while ($datos = <SNMPLOGFILE>) {
|
|
$index_data[0]++;
|
|
$index_data[1]=(stat($snmp_logfile))[7];
|
|
open SNMPLOGFILE_INDEX, ">".$snmp_logfile.".index";
|
|
print SNMPLOGFILE_INDEX $index_data[0]," ",$index_data[1];
|
|
close SNMPLOGFILE_INDEX;
|
|
|
|
#print "DEBUG $datos \n";
|
|
if (($datos !~ m/NET-SNMP/) && ($datos =~ m/\[\*\*\]/)) { # SKIP Headers
|
|
@array = split(/\[\*\*\]/, $datos);
|
|
$timestamp = $array[0]." ".$array[1];
|
|
$source = $array[2];
|
|
$oid = $array[3];
|
|
$type = $array[4];
|
|
$type_desc = $array[5];
|
|
$value = limpia_cadena($array[6]);
|
|
if ($type == 6){ # Custom OID type
|
|
$datos = $array[7];
|
|
if ($datos !~ m/STRING/) { # No string datatype, marked with " chars
|
|
$datos =~ m/([0-9\.]*)\s\=\s([A-Za-z0-9]*)\:\s(.+)/;
|
|
$custom_oid = $1;
|
|
$custom_type = $2;
|
|
$custom_value = limpia_cadena($3);
|
|
} else { # String type
|
|
if ($datos =~ m/([0-9\.]*)\s\=\s([A-Za-z0-9]*)\:\s\"(.+)\"/){
|
|
$custom_oid = $1;
|
|
$custom_type = $2;
|
|
$custom_value = limpia_cadena($3);
|
|
}
|
|
}
|
|
} else { # not custom OID type, deleting old values in these vars
|
|
$custom_oid="";
|
|
$custom_type="";
|
|
$custom_value=$type_desc; # Bug fixed, 080108 by anonymous
|
|
}
|
|
|
|
if (! defined(enterprise_hook ('snmp_insert_trap', [$pa_config, $source, $oid, $type, $value, $custom_oid, $custom_value, $custom_type, $timestamp, $dbh]))) {
|
|
$sql_insert = "INSERT INTO ttrap (timestamp, source, oid, type, value, oid_custom, value_custom, type_custom) VALUES ('$timestamp', '$source', '$oid', $type, '$value', '$custom_oid', '$custom_value', '$custom_type')";
|
|
logger ($pa_config,"Received SNMP Trap from $source", 4);
|
|
eval {
|
|
$dbh->do($sql_insert) || logger ($pa_config, "Cannot write to database while updating SNMP Trap data (error in INSERT)",0);
|
|
# Evaluate TRAP Alerts for this trap
|
|
calcula_alerta_snmp ($pa_config, $source, $oid, $oid, $custom_value, $timestamp, $dbh);
|
|
};
|
|
if ($@) {
|
|
logger ($pa_config, "[ERROR] Cannot access to database while updating SNMP Trap data",0);
|
|
logger ($pa_config, "[ERROR] SQL Errorcode: $@", 2);
|
|
}
|
|
}
|
|
enterprise_hook ('snmp_trap2agent', [defined($trap2agent) ? $trap2agent : 0, $pa_config, $source, $oid, $value, $custom_oid, $custom_value, $timestamp, $dbh]);
|
|
}
|
|
}
|
|
sleep ($pa_config{'server_threshold'});
|
|
pandora_serverkeepaliver($pa_config,2,$dbh);
|
|
}
|
|
$dbh->disconnect();
|
|
}
|
|
|
|
########################################################################################
|
|
# pandora_shutdown ()
|
|
# Close system
|
|
########################################################################################
|
|
sub pandora_shutdown {
|
|
logger (\%pa_config, "Pandora FMS Server '".$pa_config{'servername'}.$pa_config{"servermode"}."' Shutdown by signal ",0);
|
|
print " [*] Shutting down ".$pa_config{'servername'}.$pa_config{"servermode"} ."(received signal)...\n";
|
|
my $dbh = DBI->connect("DBI:mysql:$pa_config{'dbname'}:$pa_config{'dbhost'}:3306",$pa_config{'dbuser'}, $pa_config{'dbpass'}, { RaiseError => 1, AutoCommit => 1 });
|
|
pandora_event (\%pa_config, $pa_config{'servername'}.$pa_config{"servermode"}." going Down", 0,
|
|
0, 4, 0, 0, "system", $dbh);
|
|
pandora_updateserver (\%pa_config, $pa_config{'servername'}, 0, 2, $dbh);
|
|
exit;
|
|
}
|