2006-03-27 05:37:27 +02:00
#!/usr/bin/perl
2006-09-29 14:29:36 +02:00
##########################################################################
2007-03-19 21:04:37 +01:00
# Pandora FMS Network Server
2006-09-29 14:29:36 +02:00
##########################################################################
2008-02-20 03:09:36 +01:00
# Copyright (c) 2006-2008 Sancho Lerena, slerena@gmail.com
# (c) 2006-2008 Artica Soluciones Tecnologicas S.L
2006-06-30 00:04:17 +02:00
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
2007-03-12 18:41:58 +01:00
# as published by the Free Software Foundation; version 2
2006-06-30 00:04:17 +02:00
# 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.
2006-09-29 14:29:36 +02:00
##########################################################################
2006-03-27 05:37:27 +02:00
# Includes list
use strict;
use warnings;
2007-03-29 12:42:34 +02:00
use Date::Manip; # Needed to manipulate DateTime formats of input, output and compare
use Time::Local; # DateTime basic manipulation
2006-03-27 05:37:27 +02:00
use Net::Ping; # For ICMP latency
use Time::HiRes; # For high precission timedate functions (Net::Ping)
use IO::Socket; # For TCP/UDP access
2008-02-20 03:09:36 +01:00
use SNMP; # For SNMP access (libsnmp-perl PACKAGE!)
2006-07-02 23:31:08 +02:00
use threads;
2007-07-17 20:34:38 +02:00
use threads::shared;
2006-03-27 05:37:27 +02:00
# Pandora Modules
2007-06-07 12:30:03 +02:00
use PandoraFMS::Config;
use PandoraFMS::Tools;
use PandoraFMS::DB;
2006-03-27 05:37:27 +02:00
# FLUSH in each IO (only for debug, very slooow)
# ENABLED in DEBUGMODE
# DISABLE FOR PRODUCTION
2007-08-21 20:14:40 +02:00
$| = 0;
2006-03-27 05:37:27 +02:00
my %pa_config;
2007-07-17 20:34:38 +02:00
my @pending_task : shared;
my %pending_task_hash : shared;
my %current_task_hash : shared;
2007-07-18 19:53:17 +02:00
my $snmp_lock : shared;
2007-07-19 21:07:30 +02:00
my $icmp_lock : shared;
2008-02-20 03:09:36 +01:00
my $queue_lock : shared;
2006-03-27 05:37:27 +02:00
2007-07-18 19:53:17 +02:00
$ENV{'MIBS'}="ALL"; #Load all available MIBs only once
2008-02-20 03:09:36 +01:00
&SNMP::initMib();
2007-03-23 17:04:53 +01:00
$SIG{'TERM'} = 'pandora_shutdown';
$SIG{'INT'} = 'pandora_shutdown';
2006-03-27 05:37:27 +02:00
# Inicio del bucle principal de programa
2007-03-12 18:41:58 +01:00
pandora_init(\%pa_config, "Pandora FMS Network Server");
2007-05-21 11:12:21 +02:00
2006-03-27 05:37:27 +02:00
# Read config file for Global variables
pandora_loadconfig (\%pa_config,1);
2007-05-21 11:12:21 +02:00
2006-03-27 05:37:27 +02:00
# Audit server starting
2007-03-12 18:41:58 +01:00
pandora_audit (\%pa_config, "Pandora FMS Network Daemon starting", "SYSTEM", "System");
2007-05-21 11:12:21 +02:00
2008-03-13 19:33:44 +01:00
# Thread startup
if ($pa_config{"quiet"} == 0){
print " [*] Starting up network threads\n";
}
2006-03-27 05:37:27 +02:00
2008-03-13 19:33:44 +01:00
# Daemonize and put in background
if ( $pa_config{"daemon"} eq "1" ){
if ($pa_config{"quiet"} eq "0"){
print " [*] Backgrounding Pandora FMS Network Server process.\n\n";
}
&pandora_daemonize ( \%pa_config);
2006-03-27 05:37:27 +02:00
}
2008-03-13 19:33:44 +01:00
2007-07-17 20:34:38 +02:00
# Launch now all network threads
# $ax is local thread id for this server
for (my $ax=0; $ax < $pa_config{'network_threads'}; $ax++){
threads->new( \&pandora_network_consumer, \%pa_config, $ax);
}
# Launch now the network producer thread
threads->new( \&pandora_network_producer, \%pa_config);
2006-07-02 23:31:08 +02:00
2008-03-13 19:33:44 +01:00
if ($pa_config{"quiet"} == 0){
print " [*] All threads loaded and running \n\n";
}
# Start Pandora FMS loggin
pandora_startlog (\%pa_config);
# Last thread is the main process (this process)
2007-03-23 17:04:53 +01:00
my $dbhost = $pa_config{'dbhost'};
2007-05-24 23:14:06 +02:00
my $dbname = $pa_config{'dbname'};
2007-07-17 20:34:38 +02:00
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:3306",
$pa_config{'dbuser'},
$pa_config{'dbpass'},
{ RaiseError => 1, AutoCommit => 1 });
2006-07-02 23:31:08 +02:00
2007-03-23 17:04:53 +01:00
while (1) {
2007-03-29 12:42:34 +02:00
pandora_serverkeepaliver (\%pa_config, 1, $dbh);
2007-03-23 17:04:53 +01:00
threads->yield;
2007-03-29 12:42:34 +02:00
sleep ($pa_config{"server_threshold"});
2006-07-02 23:31:08 +02:00
}
2006-03-27 05:37:27 +02:00
#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
2006-09-29 14:29:36 +02:00
#--------------------- Main Perl Code below this line-----------------------
2006-03-27 05:37:27 +02:00
#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
2007-03-23 17:04:53 +01:00
########################################################################################
# pandora_shutdown ()
# Close system
########################################################################################
sub pandora_shutdown {
logger (\%pa_config,"Pandora FMS Network Server Shutdown by signal ",0);
print " [*] Shutting down Pandora FMS Network Server (received signal)...\n";
exit;
}
2006-09-29 14:29:36 +02:00
##########################################################################
2006-03-27 05:37:27 +02:00
# SUB pandora_network_subsystem
# Subsystem to process network modules
# This module runs each X seconds (server threshold) checking for network modules status
2006-09-29 14:29:36 +02:00
##########################################################################
2007-07-17 20:34:38 +02:00
sub pandora_network_consumer ($$) {
2006-03-27 05:37:27 +02:00
my $pa_config = $_[0];
2007-07-17 20:34:38 +02:00
my $thread_id = $_[1];
2006-03-27 05:37:27 +02:00
2008-03-13 19:33:44 +01:00
if ($pa_config->{"quiet"} == 0){
print " [*] Starting up Network Consumer Thread # $thread_id \n";
}
2007-07-17 20:34:38 +02:00
my $data_id_agent_module;
# Create Database handler
my $dbh = DBI->connect("DBI:mysql:$pa_config->{'dbname'}:$pa_config->{'dbhost'}:3306", $pa_config->{'dbuser'}, $pa_config->{'dbpass'}, { RaiseError => 1, AutoCommit => 1 });
my $counter =0;
2008-04-22 16:35:19 +02:00
LOOP: while (1) {
2007-07-18 19:53:17 +02:00
if ($counter > 10) {
2007-07-17 20:34:38 +02:00
sleep (1);
$counter = 0;
}
# Take the first element on the shared queue
# Insert this element on the current task hash
2008-04-22 16:35:19 +02:00
{
lock $queue_lock;
if (scalar(@pending_task) == 0){
$counter++;
next LOOP;
2007-12-04 02:19:46 +01:00
}
2008-04-22 16:35:19 +02:00
$data_id_agent_module = shift(@pending_task);
delete($pending_task_hash{$data_id_agent_module});
$current_task_hash{$data_id_agent_module}=1;
}
# Executing network task with unmanaged error trapping
eval {
# Call network execution process
exec_network_module ( $pa_config, $data_id_agent_module, $dbh);
};
if ($@){
logger ($pa_config, "[ERROR] Network Task for module $data_id_agent_module causes a system exception", 0);
logger ($pa_config, "ERROR Code: $@", 1);
}
# Remove from queue. If catch an error, probably data is
# not been processed, but has been freed from task queue
{
lock $queue_lock;
delete($current_task_hash{$data_id_agent_module});
2007-07-17 20:34:38 +02:00
}
2008-04-22 16:35:19 +02:00
$counter = 0;
2007-07-17 20:34:38 +02:00
}
}
sub pandora_network_producer ($) {
my $pa_config = $_[0];
2008-03-13 19:33:44 +01:00
if ($pa_config->{"quiet"} == 0){
print " [*] Starting up Network Producer Thread ...\n";
}
2007-07-17 20:34:38 +02:00
my $dbh = DBI->connect("DBI:mysql:$pa_config->{'dbname'}:$pa_config->{'dbhost'}:3306", $pa_config->{'dbuser'}, $pa_config->{'dbpass'}, { RaiseError => 1, AutoCommit => 1 });
my $server_id = $pa_config->{'server_id'};
# Initialize variables for posterior usage
my $query1;
my @sql_data1;
my $data_id_agente_modulo;
my $data_flag;
my $exec_sql1;
while (1) {
2007-07-31 19:26:09 +02:00
if ($pa_config->{"pandora_master"} != 1) {
2007-07-17 20:34:38 +02:00
# Query for normal server, not MASTER server
$query1 = "SELECT
tagente_modulo.id_agente_modulo,
tagente_modulo.flag
FROM
tagente, tagente_modulo, tagente_estado
WHERE
2008-03-13 19:33:44 +01:00
id_network_server = $server_id
2007-07-17 20:34:38 +02:00
AND
tagente_modulo.id_agente = tagente.id_agente
AND
tagente.disabled = 0
AND
2008-03-13 19:33:44 +01:00
tagente_modulo.id_tipo_modulo > 4
AND
tagente_modulo.id_tipo_modulo < 19
AND
tagente_modulo.disabled = 0
2007-07-17 20:34:38 +02:00
AND
tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
AND (
2007-07-18 19:53:17 +02:00
(tagente_estado.last_execution_try + tagente_estado.current_interval) < UNIX_TIMESTAMP()
2007-07-17 20:34:38 +02:00
OR
tagente_modulo.flag = 1
)
ORDER BY
last_execution_try ASC ";
} else {
2007-07-31 19:26:09 +02:00
# Query for MASTER SERVER !
$query1 = "SELECT
DISTINCT(tagente_modulo.id_agente_modulo), tagente_modulo.flag
FROM
tagente, tagente_modulo, tagente_estado, tserver
WHERE
2008-03-13 19:33:44 +01:00
( (tagente.id_network_server = $server_id AND tagente_modulo.id_agente = tagente.id_agente) OR
(tagente.id_network_server != $server_id AND tagente_modulo.id_agente = tagente.id_agente AND tagente.id_network_server = tserver.id_server AND tserver.status = 0)
2007-07-31 19:26:09 +02:00
) AND
tagente.disabled = 0
2008-03-13 19:33:44 +01:00
AND
tagente_modulo.disabled = 0
2007-07-31 19:26:09 +02:00
AND
tagente_modulo.id_tipo_modulo > 4
2008-03-13 19:33:44 +01:00
AND
tagente_modulo.id_tipo_modulo < 19
2007-07-31 19:26:09 +02:00
AND
tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
AND
((tagente_estado.last_execution_try + tagente_estado.current_interval) < UNIX_TIMESTAMP() OR tagente_modulo.flag = 1 )
ORDER BY last_execution_try ASC";
2007-07-17 20:34:38 +02:00
}
$exec_sql1 = $dbh->prepare($query1);
$exec_sql1 ->execute;
2007-07-18 19:53:17 +02:00
while (@sql_data1 = $exec_sql1->fetchrow_array()) {
2007-07-17 20:34:38 +02:00
$data_id_agente_modulo = $sql_data1[0];
$data_flag = $sql_data1[1];
2007-07-18 19:53:17 +02:00
# Skip modules already queued
2007-07-17 20:34:38 +02:00
if ((!defined($pending_task_hash{$data_id_agente_modulo})) &&
(!defined($current_task_hash{$data_id_agente_modulo}))) {
if ($data_flag == 1){
$dbh->do("UPDATE tagente_modulo SET flag = 0 WHERE id_agente_modulo = $data_id_agente_modulo")
2006-03-27 05:37:27 +02:00
}
2007-07-17 20:34:38 +02:00
# Locking scope, do not remove redundant { }
{
2008-02-20 03:09:36 +01:00
lock $queue_lock;
2007-07-17 20:34:38 +02:00
push (@pending_task, $data_id_agente_modulo);
$pending_task_hash {$data_id_agente_modulo}=1;
2006-03-27 05:37:27 +02:00
}
2007-07-17 20:34:38 +02:00
}
}
2007-07-31 19:26:09 +02:00
#logger ($pa_config, "Items in Network Pending Queue: ".scalar(@pending_task), 5);
2007-07-17 20:34:38 +02:00
$exec_sql1->finish();
2006-07-01 03:54:36 +02:00
sleep($pa_config->{"server_threshold"});
2007-07-17 20:34:38 +02:00
} # Main loop
2006-03-27 05:37:27 +02:00
}
2007-03-19 21:04:37 +01:00
##############################################################################
2007-10-04 12:45:46 +02:00
# pandora_ping_icmp (config, destination, timeout)
# Do a ICMP scan, return 1 if alive, 0 if not
2007-03-19 21:04:37 +01:00
##############################################################################
2007-07-17 20:34:38 +02:00
sub pandora_ping_icmp {
2007-10-04 12:45:46 +02:00
my $pa_config = $_[0];
my $dest = $_[1];
my $l_timeout = $_[2];
2007-06-07 19:24:02 +02:00
# temporal vars.
my $result = 0;
my $result2 = 0;
2007-10-04 12:45:46 +02:00
my $temp;
2007-06-07 19:24:02 +02:00
2007-10-04 12:45:46 +02:00
if ($pa_config->{'icmp_checks'} eq ""){
$pa_config->{'icmp_checks'} = 1;
2007-07-19 21:07:30 +02:00
}
2007-10-04 12:45:46 +02:00
# Make more than a single ping (as defined in icmp_checks
for ($temp =0; $temp < $pa_config->{'icmp_checks'}; $temp++){
2007-12-19 19:45:32 +01:00
my $p;
2007-12-04 02:19:46 +01:00
# Some hosts don't accept ICMP with too small payload. Use 16 Bytes min
2007-10-04 12:45:46 +02:00
{
lock $icmp_lock;
$p = Net::Ping->new("icmp", $l_timeout, 32);
$result = $p->ping($dest);
}
2007-07-18 19:53:17 +02:00
2007-10-04 12:45:46 +02:00
if (defined($result)){
$p->close();
if ($result == 1){
$result2 = 1;
$temp = $pa_config->{'icmp_checks'}; # Exit for
}
}
2007-12-19 19:45:32 +01:00
undef ($p);
2007-10-04 12:45:46 +02:00
}
return $result2;
2007-07-19 21:07:30 +02:00
}
##############################################################################
# pandora_ping_latency (destination, timeout, data, result) - Do a ICMP latency check
##############################################################################
sub pandora_ping_latency {
2007-10-04 12:45:46 +02:00
my $dest = $_[0];
my $l_timeout = $_[1];
my $module_data = $_[2];
my $module_result = $_[3];
my $icmp_return;
my $icmp_reply;
my $icmp_ip;
my $nm;
2007-07-20 12:53:19 +02:00
# Locking for use ICMP call safety
2007-07-19 21:07:30 +02:00
{
lock $icmp_lock;
2007-10-04 12:45:46 +02:00
$nm = Net::Ping->new("icmp", $l_timeout, 32);
2007-07-19 21:07:30 +02:00
$nm->hires();
($icmp_return, $icmp_reply, $icmp_ip) = $nm->ping ($dest,$l_timeout);
}
2007-10-04 12:45:46 +02:00
if ($icmp_return) {
$$module_data = $icmp_reply * 1000; # milliseconds
$$module_result = 0; # Successful
} else {
$$module_result = 1; # Error.
$$module_data = 0;
}
$nm->close();
2007-12-19 19:45:32 +01:00
undef($nm);
2007-03-19 21:04:37 +01:00
}
2007-03-29 12:42:34 +02:00
##########################################################################
# SUB pandora_query_tcp (pa_config, tcp_port. ip_target, result, data, tcp_send,
2007-05-21 11:12:21 +02:00
# tcp_rcv, id_tipo_module, dbh)
2007-03-29 12:42:34 +02:00
# Makes a call to TCP modules to get a value.
##########################################################################
2007-07-20 12:53:19 +02:00
sub pandora_query_tcp (%$$$$$$$) {
2007-03-29 12:42:34 +02:00
my $pa_config = $_[0];
my $tcp_port = $_[1];
my $ip_target = $_[2];
my $module_result = $_[3];
my $module_data = $_[4];
my $tcp_send = $_[5];
my $tcp_rcv = $_[6];
my $id_tipo_modulo = $_[7];
2008-02-20 03:09:36 +01:00
my $counter;
for ($counter =0; $counter < $pa_config->{'tcp_checks'}; $counter++){
my $temp; my $temp2;
my $tam;
my $handle=IO::Socket::INET->new(
Proto=>"tcp",
PeerAddr=>$ip_target,
Timeout=>$pa_config->{'tcp_timeout'},
PeerPort=>$tcp_port,
Blocking=>0 ); # Non blocking !!, very important !
if (defined($handle)){
if ($tcp_send ne ""){ # its Expected to sending data ?
# Send data
$handle->autoflush(1);
$tcp_send =~ s/\^M/\r\n/g;
# Replace Carriage rerturn and line feed
$handle->send($tcp_send);
}
# we expect to receive data ? (non proc types)
if (($tcp_rcv ne "") || ($id_tipo_modulo == 10) || ($id_tipo_modulo ==8) || ($id_tipo_modulo == 11)) {
# Receive data, non-blocking !!!! (VERY IMPORTANT!)
$temp2 = "";
for ($tam=0; $tam<($pa_config->{'tcp_timeout'}); $tam++){
$handle->recv($temp,16000,0x40);
$temp2 = $temp2.$temp;
if ($temp ne ""){
$tam++; # If doesnt receive data, increase counter
}
sleep(1);
}
if ($id_tipo_modulo == 9){ # only for TCP Proc
if ($temp2 =~ /$tcp_rcv/i){ # String match !
$$module_data = 1;
$$module_result = 0;
$counter = $pa_config->{'tcp_checks'};
} else {
$$module_data = 0;
$$module_result = 0;
$counter = $pa_config->{'tcp_checks'};
}
} elsif ($id_tipo_modulo == 10 ){ # TCP String (no int conversion)!
$$module_data = $temp2;
$$module_result =0;
} else { # TCP Data numeric (inc or data)
if ($temp2 ne ""){
if ($temp2 =~ /[A-Za-z\.\,\-\/\\\(\)\[\]]/){
$$module_result = 1;
$$module_data = 0; # invalid data
$counter = $pa_config->{'tcp_checks'};
} else {
$$module_data = int($temp2);
$$module_result = 0; # Successful
$counter = $pa_config->{'tcp_checks'};
}
} else {
$$module_result = 1;
$$module_data = 0; # invalid data
$counter = $pa_config->{'tcp_checks'};
}
}
} else { # No expected data to receive, if connected and tcp_proc type successful
if ($id_tipo_modulo == 9){ # TCP Proc
$$module_result = 0;
$$module_data = 1;
$counter = $pa_config->{'tcp_checks'};
}
}
$handle->close();
undef ($handle);
} else { # Cannot connect (open sock failed)
$$module_result = 1; # Fail
if ($id_tipo_modulo == 9){ # TCP Proc
$$module_result = 0;
$$module_data = 0; # Failed, but data exists
$counter = $pa_config->{'tcp_checks'};
}
}
}
2007-03-23 17:04:53 +01:00
}
2006-09-29 14:29:36 +02:00
##########################################################################
2006-03-27 05:37:27 +02:00
# SUB pandora_query_snmp (pa_config, oid, community, target, error, dbh)
# Makes a call to SNMP modules to get a value,
2006-09-29 14:29:36 +02:00
##########################################################################
2007-07-20 12:53:19 +02:00
sub pandora_query_snmp (%$$$$) {
2006-03-27 05:37:27 +02:00
my $pa_config = $_[0];
my $snmp_oid = $_[1];
my $snmp_community =$_[2];
my $snmp_target = $_[3];
# $_[4] contains error var.
2007-07-20 12:53:19 +02:00
2008-02-20 03:09:36 +01:00
my $output ="";
my $snmp_timeout = 1000 * 1000 * $pa_config->{"snmp_timeout"};
my $snmp_retries = $pa_config->{'snmp_checks'};
my $SESSION;
# Locking for SNMP call. SNMP is not thread safe !!
{
lock $snmp_lock;
$SESSION = new SNMP::Session (DestHost => $snmp_target,
2007-11-19 20:43:34 +01:00
Timeout => $snmp_timeout,
2008-02-20 03:09:36 +01:00
Retries => $snmp_retries,
2007-07-18 19:53:17 +02:00
Community => $snmp_community,
Version => 1);
2008-02-20 03:09:36 +01:00
}
if ($SESSION->{ErrorStr}) {
logger($pa_config, "SNMP ERROR SESSION for Target $snmp_target ".$SESSION->{ErrorStr}, 2);
2007-05-21 11:12:21 +02:00
$_[4] = "1";
2007-12-19 19:45:32 +01:00
undef ($SESSION);
2008-02-20 03:09:36 +01:00
return 0;
}
my $oid = SNMP::translateObj($snmp_oid);
# Locking for SNMP call. SNMP is not thread safe !!
{
lock $snmp_lock;
$output = $SESSION->get($oid);
}
if ((!defined($SESSION)) || (!defined($output)) || ($output eq "") || ($SESSION->{ErrorStr})) {
logger($pa_config, "SNMP ERROR SNMPGET for Target $snmp_target ".$SESSION->{ErrorStr}, 2);
$_[4] = "1";
undef ($SESSION);
return 0;
2006-06-28 19:08:22 +02:00
}
2008-02-20 03:09:36 +01:00
$_[4] = "0";
undef ($SESSION);
2006-03-27 05:37:27 +02:00
return $output;
}
2006-09-29 14:29:36 +02:00
##########################################################################
2007-07-17 20:34:38 +02:00
# SUB exec_network_module (paconfig, id_agente_modulo, dbh )
# Execute network module task
2006-09-29 14:29:36 +02:00
##########################################################################
2006-03-27 05:37:27 +02:00
sub exec_network_module {
2007-07-17 20:34:38 +02:00
my $pa_config = $_[0];
my $id_agente_modulo = $_[1];
my $dbh = $_[2];
# Init variables
my $id_agente;
my $id_tipo_modulo;
my $nombre;
my $min;
my $max;
my $module_interval;
my $tcp_port;
my $tcp_send;
my $tcp_rcv;
my $snmp_community;
my $snmp_oid;
my $ip_target;
my $id_module_group;
my $flag;
my @sql_data;
2007-07-31 19:26:09 +02:00
if ((!defined($id_agente_modulo)) || ($id_agente_modulo eq "")){
2008-01-08 19:32:03 +01:00
return 0;
2007-07-31 19:26:09 +02:00
}
2007-07-17 20:34:38 +02:00
my $query_sql = "SELECT * FROM tagente_modulo WHERE id_agente_modulo = $id_agente_modulo";
my $exec_sql = $dbh->prepare($query_sql);
$exec_sql ->execute;
if (@sql_data = $exec_sql->fetchrow_array()){
$id_agente= $sql_data[1];
$id_tipo_modulo = $sql_data[2];
$nombre = $sql_data[4];
$min = $sql_data[6];
$max = $sql_data[5];
$module_interval = $sql_data[7];
$tcp_port = $sql_data[8];
$tcp_send = $sql_data[9];
$tcp_rcv = $sql_data[10];
$snmp_community = $sql_data[11];
$snmp_oid = $sql_data[12];
$ip_target = $sql_data[13];
$id_module_group = $sql_data[14];
$flag = $sql_data[15];
2007-07-18 19:53:17 +02:00
$exec_sql->finish();
} else {
$exec_sql->finish();
2007-12-04 02:19:46 +01:00
logger (\%pa_config,"[ERROR] Processing data for invalid module", 0);
2007-07-18 19:53:17 +02:00
return 0;
2007-07-17 20:34:38 +02:00
}
2007-07-18 19:53:17 +02:00
2007-07-17 20:34:38 +02:00
my $agent_name = dame_agente_nombre ($pa_config, $id_agente, $dbh);
2006-03-27 05:37:27 +02:00
my $error = "1";
my $query_sql2;
my $temp=0; my $tam; my $temp2;
2007-04-21 18:02:16 +02:00
my $module_result = 1; # Fail by default
my $module_data = 0;
2006-03-27 05:37:27 +02:00
2008-01-08 19:32:03 +01:00
if ((defined($ip_target)) && ($ip_target ne "")) {
2007-10-04 12:45:46 +02:00
# ICMP Modules
# ------------
if ($id_tipo_modulo == 6){ # ICMP (Connectivity only: Boolean)
$temp = pandora_ping_icmp ($pa_config, $ip_target, $pa_config->{'networktimeout'});
if ($temp == 1 ){
$module_result = 0; # Successful
$module_data = 1;
} else {
$module_result = 0; # If cannot connect, its down.
$module_data = 0;
}
} elsif ($id_tipo_modulo == 7){ # ICMP (data for latency in ms)
# This module only could be executed if executed as root
if ($> == 0){
pandora_ping_latency ($ip_target, $pa_config->{"networktimeout"}, \$module_data, \$module_result);
} else {
$module_result = 0; # Done but, with zero value
$module_data = 0; # This should don't happen
}
# SNMP Modules (Proc=18, inc, data, string)
# ------------
} elsif (($id_tipo_modulo == 15) || ($id_tipo_modulo == 18) || ($id_tipo_modulo == 16) || ($id_tipo_modulo == 17)) { # SNMP module
if ((defined($snmp_oid)) && ($snmp_oid ne "") && (defined($snmp_community)) && ($snmp_community ne "")) { # Port check
$temp2 = pandora_query_snmp ($pa_config, $snmp_oid, $snmp_community, $ip_target, $error);
} else {
$error = 1
}
if ($error == 0) { # A correct SNMP Query
$module_result = 0;
# SNMP_DATA_PROC
if ($id_tipo_modulo == 18){ #snmp_data_proc
2008-02-20 03:09:36 +01:00
# RFC1213-MIB where it says that: SYNTAX INTEGER { up(1), down(2), testing(3),
# unknown(4), dormant(5), notPresent(6), lowerLayerDown(7) }
2007-10-04 12:45:46 +02:00
if ($temp2 != 1){ # up state is 1, down state in SNMP is 2 ....
$temp2 = 0;
}
$module_data = $temp2;
}
# SNMP_DATA and SNMP_DATA_INC
elsif (($id_tipo_modulo == 15) || ($id_tipo_modulo == 16) ){
2008-02-20 03:09:36 +01:00
if (!is_numeric($temp2)){
$module_result = 1;
2007-10-04 12:45:46 +02:00
} else {
2008-02-20 03:09:36 +01:00
$module_data = $temp2;
2007-10-04 12:45:46 +02:00
}
} else { # String SNMP
$module_data = $temp2;
}
} else { # Failed SNMP-GET
$module_data = 0;
2008-02-20 03:09:36 +01:00
if ($id_tipo_modulo == 18){ # snmp_proc
# Feature from 10Feb08. If snmp_proc_deadresponse = 1 and cannot contact by an error
# this is a fail monitor
if ($pa_config->{"snmp_proc_deadresponse"} eq "1"){
$module_result = 0;
} else {
$module_result = 1;
}
} else {
$module_result = 1; # No data, cannot connect
}
2007-10-04 12:45:46 +02:00
}
# TCP Module
# ----------
} elsif (($id_tipo_modulo == 8) || ($id_tipo_modulo == 9) || ($id_tipo_modulo == 10) || ($id_tipo_modulo == 11)) { # TCP Module
if ((defined($tcp_port)) && ($tcp_port < 65536) && ($tcp_port > 0)) { # Port check
pandora_query_tcp ($pa_config, $tcp_port, $ip_target, \$module_result, \$module_data, $tcp_send, $tcp_rcv, $id_tipo_modulo);
} else {
# Invalid port, get no check
$module_result = 1;
}
}
}
2007-04-21 18:02:16 +02:00
2008-03-13 19:33:44 +01:00
2007-07-18 19:53:17 +02:00
# Write data section
my $timestamp = &UnixDate("today","%Y-%m-%d %H:%M:%S");
my $utimestamp = &UnixDate("today","%s");
2008-03-13 19:33:44 +01:00
# Is everything goes ok
2006-03-27 05:37:27 +02:00
if ($module_result == 0) {
my %part;
$part{'name'}[0]=$nombre;
$part{'description'}[0]="";
2007-05-03 04:15:42 +02:00
$part{'data'}[0] = $module_data;
$part{'max'}[0] = $max;
$part{'min'}[0] = $min;
2007-04-21 18:02:16 +02:00
my $tipo_modulo = dame_nombretipomodulo_idagentemodulo ($pa_config, $id_tipo_modulo, $dbh);
2006-03-27 05:37:27 +02:00
if (($tipo_modulo eq 'remote_snmp') || ($tipo_modulo eq 'remote_icmp') || ($tipo_modulo eq 'remote_tcp') || ($tipo_modulo eq 'remote_udp')) {
2007-04-21 18:02:16 +02:00
module_generic_data ($pa_config, \%part, $timestamp, $agent_name, $tipo_modulo, $dbh);
2006-03-27 05:37:27 +02:00
}
elsif ($tipo_modulo =~ /\_inc/ ) {
2007-04-21 18:02:16 +02:00
module_generic_data_inc ($pa_config, \%part, $timestamp, $agent_name, $tipo_modulo, $dbh);
2006-03-27 05:37:27 +02:00
}
elsif ($tipo_modulo =~ /\_string/) {
2007-04-21 18:02:16 +02:00
module_generic_data_string ($pa_config, \%part, $timestamp, $agent_name, $tipo_modulo, $dbh);
2006-03-27 05:37:27 +02:00
}
elsif ($tipo_modulo =~ /\_proc/){
2007-04-21 18:02:16 +02:00
module_generic_proc ($pa_config, \%part, $timestamp, $agent_name, $tipo_modulo, $dbh);
2006-03-27 05:37:27 +02:00
}
else {
2007-03-29 12:42:34 +02:00
logger ($pa_config, "Problem with unknown module type '$tipo_modulo'", 0);
2008-03-13 19:33:44 +01:00
$module_result = 1;
2006-03-27 05:37:27 +02:00
}
# Update agent last contact
# Insert Pandora version as agent version
2007-07-17 20:34:38 +02:00
pandora_lastagentcontact ($pa_config, $timestamp, $agent_name, $pa_config->{'servername'}.$pa_config->{"servermode"}, $pa_config->{'version'}, -1, $dbh);
2008-03-13 19:33:44 +01:00
}
if ($module_result != 0) {
2007-07-31 19:26:09 +02:00
if ($module_interval == 0){
2008-03-13 19:33:44 +01:00
$module_interval = dame_intervalo ($pa_config, $id_agente, $dbh);
}
2007-07-18 19:53:17 +02:00
# Modules who cannot connect or something go bad, update last_execution_try field
2007-04-21 18:02:16 +02:00
logger ($pa_config, "Cannot obtain exec Network Module $nombre from agent $agent_name", 4);
2007-07-31 19:26:09 +02:00
my $query_act = "UPDATE tagente_estado SET current_interval = $module_interval, last_execution_try = $utimestamp WHERE id_agente_modulo = $id_agente_modulo ";
2006-03-27 05:37:27 +02:00
my $a_idages = $dbh->prepare($query_act);
$a_idages->execute;
$a_idages->finish();
}
}