2007-08-02 19:53:07 +02:00
|
|
|
package PandoraFMS::Tools;
|
|
|
|
##########################################################################
|
2008-07-22 17:52:34 +02:00
|
|
|
# Tools Package
|
|
|
|
# Pandora FMS. the Flexible Monitoring System. http://www.pandorafms.org
|
2007-08-02 19:53:07 +02:00
|
|
|
##########################################################################
|
2009-01-24 16:41:39 +01:00
|
|
|
# Copyright (c) 2005-2009 Artica Soluciones Tecnologicas S.L
|
2007-08-02 19:53:07 +02:00
|
|
|
#
|
2009-01-24 16:41:39 +01:00
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser 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
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2007-08-02 19:53:07 +02:00
|
|
|
##########################################################################
|
2010-02-15 13:25:44 +01:00
|
|
|
|
2007-08-02 19:53:07 +02:00
|
|
|
use warnings;
|
|
|
|
use Time::Local;
|
2009-04-14 Ramon Novoa <rnovoa@artica.es>
* pandora_ctl, pandora_network, pandora_wmi, pandora_plugin,
pandora_prediction, bin/pandora_wmi, bin/pandora_plugin,
bin/pandora_prediction, bin/pandora_snmpconsole, bin/pandora_recon,
bin/pandora_network, pandora_recon, pandora_snmpconsole: Deleted
from repository. Old server code and startup scripts.
* Makefile.PL, pandora_server_installer, lib/PandoraFMS/Config.pm
lib/PandoraFMS/DB.pm, lib/PandoraFMS/Tools.pm, bin/pandora_server,
pandora_package_installer, pandora_server: Updated to work with the
new code, removed unneeded dependencies, fixed some bugs etc.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/PluginServer.pm,
lib/PandoraFMS/ProducerConsumerServer.pm, lib/PandoraFMS/Server.pm,
lib/PandoraFMS/PredictionServer.pm, lib/PandoraFMS/Core.pm,
lib/PandoraFMS/ReconServer.pm, lib/PandoraFMS/DataServer.pm,
lib/PandoraFMS/NetworkServer.pm, lib/PandoraFMS/WMIServer.pm: Added
to repository. New server code.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1620 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-04-14 21:13:16 +02:00
|
|
|
use POSIX qw(setsid strftime);
|
2010-09-24 13:09:44 +02:00
|
|
|
use POSIX;
|
2010-10-07 17:09:04 +02:00
|
|
|
use PandoraFMS::Sendmail; # New in 2.0. Used to sendmail internally, without external scripts
|
2009-11-06 16:57:06 +01:00
|
|
|
#use Module::Loaded;
|
2007-08-02 19:53:07 +02:00
|
|
|
|
2010-09-24 13:09:44 +02:00
|
|
|
# Used to calculate the MD5 checksum of a string
|
|
|
|
use constant MOD232 => 2**32;
|
|
|
|
|
2007-08-02 19:53:07 +02:00
|
|
|
require Exporter;
|
|
|
|
|
|
|
|
our @ISA = ("Exporter");
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
2008-03-13 19:33:44 +01:00
|
|
|
our @EXPORT = qw(
|
2009-01-24 16:41:39 +01:00
|
|
|
pandora_daemonize
|
|
|
|
logger
|
|
|
|
limpia_cadena
|
|
|
|
md5check
|
|
|
|
float_equal
|
|
|
|
sqlWrap
|
|
|
|
is_numeric
|
|
|
|
clean_blank
|
|
|
|
pandora_sendmail
|
|
|
|
pandora_get_os
|
|
|
|
pandora_trash_ascii
|
|
|
|
enterprise_hook
|
|
|
|
enterprise_load
|
2009-04-14 Ramon Novoa <rnovoa@artica.es>
* pandora_ctl, pandora_network, pandora_wmi, pandora_plugin,
pandora_prediction, bin/pandora_wmi, bin/pandora_plugin,
bin/pandora_prediction, bin/pandora_snmpconsole, bin/pandora_recon,
bin/pandora_network, pandora_recon, pandora_snmpconsole: Deleted
from repository. Old server code and startup scripts.
* Makefile.PL, pandora_server_installer, lib/PandoraFMS/Config.pm
lib/PandoraFMS/DB.pm, lib/PandoraFMS/Tools.pm, bin/pandora_server,
pandora_package_installer, pandora_server: Updated to work with the
new code, removed unneeded dependencies, fixed some bugs etc.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/PluginServer.pm,
lib/PandoraFMS/ProducerConsumerServer.pm, lib/PandoraFMS/Server.pm,
lib/PandoraFMS/PredictionServer.pm, lib/PandoraFMS/Core.pm,
lib/PandoraFMS/ReconServer.pm, lib/PandoraFMS/DataServer.pm,
lib/PandoraFMS/NetworkServer.pm, lib/PandoraFMS/WMIServer.pm: Added
to repository. New server code.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1620 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-04-14 21:13:16 +02:00
|
|
|
print_message
|
2009-08-03 17:12:07 +02:00
|
|
|
get_tag_value
|
2010-02-18 18:21:40 +01:00
|
|
|
disk_free
|
|
|
|
load_average
|
|
|
|
free_mem
|
2010-09-24 13:09:44 +02:00
|
|
|
md5
|
|
|
|
md5_init
|
2009-01-24 16:41:39 +01:00
|
|
|
);
|
2007-08-02 19:53:07 +02:00
|
|
|
|
2008-06-19 02:14:17 +02:00
|
|
|
##########################################################################
|
|
|
|
## SUB pandora_trash_ascii
|
|
|
|
# Generate random ascii strings with variable lenght
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
sub pandora_trash_ascii {
|
2009-01-24 16:41:39 +01:00
|
|
|
my $config_depth = $_[0];
|
|
|
|
my $a;
|
|
|
|
my $output;
|
|
|
|
|
|
|
|
for ($a=0;$a<$config_depth;$a++){
|
|
|
|
$output = $output.chr(int(rand(25)+97));
|
|
|
|
}
|
|
|
|
return $output
|
2008-06-19 02:14:17 +02:00
|
|
|
}
|
|
|
|
|
2008-06-17 Sancho Lerena <slerena@artica.es>
* config.pm: Added options for xprobe2, and autocreate.
* tools.pm: Added functions pandora_create_agent(), pandora_get_os(),
and pandora_event() (this has been moved from DB.pm).
* pandora_server.conf: Added options for xprobe2, autocreate and
autocreate_group.
* pandora_network: Added support for TCP scanning (not implemented in
console yet), parent detection using traceroute, OS fingerprinting with
xprobe2, event creation using central functions, and some optimizations.
* pandora_server: Added support for agent autocreation, OS detection from
the XML header, and event notification "new_agent" type.
* pandora_recon: First code to implement traceroute functionality
using Pureperl module. Added code for remote OS fingerprinting.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@874 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2008-06-17 19:26:03 +02:00
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# SUB pandora_get_os (string)
|
|
|
|
# Detect OS using a string, and return id_os
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
sub pandora_get_os ($) {
|
2010-09-24 13:09:44 +02:00
|
|
|
my $command = $_[0];
|
2008-07-22 17:52:34 +02:00
|
|
|
if (defined($command) && $command ne ""){
|
|
|
|
if ($command =~ m/Windows/i){
|
|
|
|
return 9;
|
|
|
|
}
|
|
|
|
elsif ($command =~ m/Cisco/i){
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
elsif ($command =~ m/SunOS/i){
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
elsif ($command =~ m/Solaris/i){
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
elsif ($command =~ m/AIX/i){
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
elsif ($command =~ m/HP-UX/i){
|
|
|
|
return 5;
|
|
|
|
}
|
2009-03-26 17:33:28 +01:00
|
|
|
elsif ($command =~ m/Apple/i){
|
|
|
|
return 8;
|
|
|
|
}
|
2009-08-19 21:49:55 +02:00
|
|
|
elsif ($command =~ m/Linux/i){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ($command =~ m/Enterasys/i){
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
elsif ($command =~ m/3com/i){
|
|
|
|
return 11;
|
|
|
|
}
|
2010-06-10 11:25:26 +02:00
|
|
|
elsif ($command =~ m/Octopods/i){
|
2009-08-19 04:20:54 +02:00
|
|
|
return 13;
|
|
|
|
}
|
2009-08-19 21:49:55 +02:00
|
|
|
elsif ($command =~ m/BSD/i){
|
|
|
|
return 4;
|
|
|
|
}
|
2008-07-22 17:52:34 +02:00
|
|
|
else {
|
|
|
|
return 10; # Unknown / Other
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 10;
|
2008-06-17 Sancho Lerena <slerena@artica.es>
* config.pm: Added options for xprobe2, and autocreate.
* tools.pm: Added functions pandora_create_agent(), pandora_get_os(),
and pandora_event() (this has been moved from DB.pm).
* pandora_server.conf: Added options for xprobe2, autocreate and
autocreate_group.
* pandora_network: Added support for TCP scanning (not implemented in
console yet), parent detection using traceroute, OS fingerprinting with
xprobe2, event creation using central functions, and some optimizations.
* pandora_server: Added support for agent autocreation, OS detection from
the XML header, and event notification "new_agent" type.
* pandora_recon: First code to implement traceroute functionality
using Pureperl module. Added code for remote OS fingerprinting.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@874 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2008-06-17 19:26:03 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-02 19:53:07 +02:00
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Sub daemonize ()
|
|
|
|
# Put program in background (for daemon mode)
|
|
|
|
##########################################################################
|
|
|
|
|
2008-03-13 19:33:44 +01:00
|
|
|
sub pandora_daemonize {
|
2009-01-24 16:41:39 +01:00
|
|
|
my $pa_config = $_[0];
|
|
|
|
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
|
|
|
|
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
|
|
|
|
open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
|
|
|
|
chdir '/tmp' or die "Can't chdir to /tmp: $!";
|
|
|
|
defined(my $pid = fork) or die "Can't fork: $!";
|
|
|
|
exit if $pid;
|
|
|
|
setsid or die "Can't start a new session: $!";
|
|
|
|
umask 0;
|
|
|
|
|
|
|
|
# Store PID of this process in file presented by config token
|
|
|
|
if ($pa_config->{'PID'} ne ""){
|
|
|
|
open (FILE, "> ".$pa_config->{'PID'}) or die "[FATAL] Cannot open PIDfile at ".$pa_config->{'PID'};
|
|
|
|
print FILE "$$";
|
|
|
|
close (FILE);
|
|
|
|
}
|
2007-08-02 19:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------+
|
|
|
|
# Pandora other General functions |
|
|
|
|
# -------------------------------------------+
|
|
|
|
|
2008-06-17 Sancho Lerena <slerena@artica.es>
* config.pm: Added options for xprobe2, and autocreate.
* tools.pm: Added functions pandora_create_agent(), pandora_get_os(),
and pandora_event() (this has been moved from DB.pm).
* pandora_server.conf: Added options for xprobe2, autocreate and
autocreate_group.
* pandora_network: Added support for TCP scanning (not implemented in
console yet), parent detection using traceroute, OS fingerprinting with
xprobe2, event creation using central functions, and some optimizations.
* pandora_server: Added support for agent autocreation, OS detection from
the XML header, and event notification "new_agent" type.
* pandora_recon: First code to implement traceroute functionality
using Pureperl module. Added code for remote OS fingerprinting.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@874 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2008-06-17 19:26:03 +02:00
|
|
|
|
2008-06-13 18:42:35 +02:00
|
|
|
##########################################################################
|
|
|
|
# SUB pandora_sendmail
|
|
|
|
# Send a mail, connecting directly to MTA
|
|
|
|
# param1 - config hash
|
|
|
|
# param2 - Destination email addres
|
|
|
|
# param3 - Email subject
|
|
|
|
# param4 - Email Message body
|
|
|
|
##########################################################################
|
|
|
|
|
2009-11-08 02:38:04 +01:00
|
|
|
sub pandora_sendmail {
|
|
|
|
|
|
|
|
#WARNING: To use MTA Auth is needed v0.79_16 or higer of Mail:Sendmail
|
|
|
|
#http://cpansearch.perl.org/src/MIVKOVIC/Mail-Sendmail-0.79_16/Sendmail.pm
|
|
|
|
|
2009-01-24 16:41:39 +01:00
|
|
|
my $pa_config = $_[0];
|
|
|
|
my $to_address = $_[1];
|
|
|
|
my $subject = $_[2];
|
|
|
|
my $message = $_[3];
|
|
|
|
|
|
|
|
my %mail = ( To => $to_address,
|
|
|
|
Message => $message,
|
|
|
|
Subject => $subject,
|
2009-11-08 02:38:04 +01:00
|
|
|
'X-Mailer' => "Pandora FMS",
|
2009-01-24 16:41:39 +01:00
|
|
|
Smtp => $pa_config->{"mta_address"},
|
|
|
|
Port => $pa_config->{"mta_port"},
|
|
|
|
From => $pa_config->{"mta_from"},
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($pa_config->{"mta_user"} ne ""){
|
2009-11-08 02:38:04 +01:00
|
|
|
$mail{auth} = {user=>$pa_config->{"mta_user"}, password=>$pa_config->{"mta_pass"}, method=>$pa_config->{"mta_auth"}, required=>1 };
|
2009-01-24 16:41:39 +01:00
|
|
|
}
|
2009-11-08 02:38:04 +01:00
|
|
|
|
|
|
|
if (sendmail %mail) {
|
|
|
|
return;
|
|
|
|
} else {
|
2009-01-24 16:41:39 +01:00
|
|
|
logger ($pa_config, "[ERROR] Sending email to $to_address with subject $subject", 1);
|
2009-11-08 02:38:04 +01:00
|
|
|
logger ($pa_config, "ERROR Code: $Mail::Sendmail::error", 5);
|
2009-01-24 16:41:39 +01:00
|
|
|
}
|
2009-11-08 02:38:04 +01:00
|
|
|
|
2008-06-13 18:42:35 +02:00
|
|
|
}
|
|
|
|
|
2007-08-02 19:53:07 +02:00
|
|
|
##########################################################################
|
|
|
|
# SUB is_numeric
|
|
|
|
# Return TRUE if given argument is numeric
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
sub is_numeric {
|
2009-03-04 18:15:39 +01:00
|
|
|
my $val = $_[0];
|
|
|
|
|
|
|
|
if (!defined($val)){
|
2007-08-02 19:53:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-10-19 12:42:42 +02:00
|
|
|
# Replace "," for "."
|
|
|
|
$val =~ s/\,/\./;
|
2009-03-04 18:15:39 +01:00
|
|
|
|
2009-10-19 12:42:42 +02:00
|
|
|
my $DIGITS = qr{ \d+ (?: [.] \d*)? | [.] \d+ }xms;
|
|
|
|
my $SIGN = qr{ [+-] }xms;
|
|
|
|
my $NUMBER = qr{ ($SIGN?) ($DIGITS) }xms;
|
|
|
|
if ( $val !~ /^${NUMBER}$/ ) {
|
|
|
|
return 0; #Non-numeric
|
|
|
|
} else {
|
|
|
|
return 1; #Numeric
|
|
|
|
}
|
2007-08-02 19:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# SUB md5check (param_1, param_2)
|
|
|
|
# Verify MD5 file .checksum
|
|
|
|
##########################################################################
|
|
|
|
# param_1 : Name of data file
|
|
|
|
# param_2 : Name of md5 file
|
|
|
|
|
|
|
|
sub md5check {
|
|
|
|
my $buf;
|
|
|
|
my $buf2;
|
|
|
|
my $file = $_[0];
|
|
|
|
my $md5file = $_[1];
|
|
|
|
open(FILE, $file) or return 0;
|
|
|
|
binmode(FILE);
|
|
|
|
my $md5 = Digest::MD5->new;
|
|
|
|
while (<FILE>) {
|
|
|
|
$md5->add($_);
|
|
|
|
}
|
|
|
|
close(FILE);
|
|
|
|
$buf2 = $md5->hexdigest;
|
|
|
|
open(FILE,$md5file) or return 0;
|
|
|
|
while (<FILE>) {
|
|
|
|
$buf = $_;
|
|
|
|
}
|
|
|
|
close (FILE);
|
|
|
|
$buf=uc($buf);
|
|
|
|
$buf2=uc($buf2);
|
|
|
|
if ($buf =~ /$buf2/ ) {
|
|
|
|
#print "MD5 Correct";
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
#print "MD5 Incorrect";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
# SUB logger (pa_config, message, level)
|
2007-08-02 19:53:07 +02:00
|
|
|
# Log to file
|
|
|
|
##########################################################################
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
sub logger ($$;$) {
|
|
|
|
my ($pa_config, $message, $level) = @_;
|
2010-05-25 15:58:38 +02:00
|
|
|
|
|
|
|
$level = 1 unless defined ($level);
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
return if ($level > $pa_config->{'verbosity'});
|
2007-08-02 19:53:07 +02:00
|
|
|
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
my $file = $pa_config->{'logfile'};
|
2007-08-02 19:53:07 +02:00
|
|
|
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
# Log rotation
|
|
|
|
if (-e $file && (stat($file))[7] > $pa_config->{'max_log_size'}) {
|
|
|
|
rename ($file, $file.'.old');
|
2007-08-02 19:53:07 +02:00
|
|
|
}
|
2010-05-10 14:22:17 +02:00
|
|
|
|
|
|
|
open (FILE, ">> $file") or die "[FATAL] Could not open logfile '$file'";
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
print FILE strftime ("%Y-%m-%d %H:%M:%S", localtime()) . " " . $pa_config->{'servername'} . $pa_config->{'servermode'} . " [V". $level ."] " . $message . "\n";
|
|
|
|
close (FILE);
|
2007-08-02 19:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# limpia_cadena (string) - Purge a string for any forbidden characters (esc, etc)
|
|
|
|
##########################################################################
|
|
|
|
sub limpia_cadena {
|
|
|
|
my $micadena;
|
|
|
|
$micadena = $_[0];
|
2008-08-22 20:02:23 +02:00
|
|
|
if (defined($micadena)){
|
|
|
|
$micadena =~ s/[^\-\:\;\.\,\_\s\a\*\=\(\)a-zA-Z0-9]/ /g;
|
|
|
|
$micadena =~ s/[\n\l\f]/ /g;
|
|
|
|
return $micadena;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2007-08-02 19:53:07 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 19:00:09 +01:00
|
|
|
##########################################################################
|
|
|
|
# clean_blank (string) - Purge a string for any blank spaces in it
|
|
|
|
##########################################################################
|
|
|
|
sub clean_blank {
|
2009-01-24 16:41:39 +01:00
|
|
|
my $input = $_[0];
|
|
|
|
$input =~ s/\s//g;
|
|
|
|
return $input;
|
2008-02-25 19:00:09 +01:00
|
|
|
}
|
|
|
|
|
2007-08-02 19:53:07 +02:00
|
|
|
########################################################################################
|
|
|
|
# sub sqlWrap(texto)
|
|
|
|
# Elimina comillas y caracteres problematicos y los sustituye por equivalentes
|
|
|
|
########################################################################################
|
|
|
|
|
|
|
|
sub sqlWrap {
|
|
|
|
my $toBeWrapped = shift(@_);
|
|
|
|
if (defined $toBeWrapped){
|
2009-01-24 16:41:39 +01:00
|
|
|
$toBeWrapped =~ s/\'/\\\'/g;
|
2009-03-26 17:33:28 +01:00
|
|
|
$toBeWrapped =~ s/\"/\\\'/g; # " This is for highlighters that don't understand escaped quotes
|
2009-01-24 16:41:39 +01:00
|
|
|
return "'".$toBeWrapped."'";
|
2007-08-02 19:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# sub float_equal (num1, num2, decimals)
|
|
|
|
# This function make possible to compare two float numbers, using only x decimals
|
|
|
|
# in comparation.
|
|
|
|
# Taken from Perl Cookbook, O'Reilly. Thanks, guys.
|
|
|
|
##########################################################################
|
|
|
|
sub float_equal {
|
2009-01-24 16:41:39 +01:00
|
|
|
my ($A, $B, $dp) = @_;
|
|
|
|
return sprintf("%.${dp}g", $A) eq sprintf("%.${dp}g", $B);
|
2007-08-02 19:53:07 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 12:33:07 +01:00
|
|
|
##########################################################################
|
|
|
|
# Tries to load the PandoraEnterprise module. Must be called once before
|
|
|
|
# enterprise_hook ().
|
|
|
|
##########################################################################
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
sub enterprise_load ($) {
|
|
|
|
my $pa_config = shift;
|
|
|
|
|
|
|
|
# Check dependencies
|
2009-10-07 17:10:14 +02:00
|
|
|
|
|
|
|
# Already loaded
|
2009-11-06 16:57:06 +01:00
|
|
|
#return 1 if (is_loaded ('PandoraFMS::Enterprise'));
|
2009-10-07 17:10:14 +02:00
|
|
|
|
|
|
|
# Try to load the module
|
2009-12-18 19:22:33 +01:00
|
|
|
# eval 'local $SIG{__DIE__}; require PandoraFMS::Enterprise;';
|
|
|
|
eval 'require PandoraFMS::Enterprise;';
|
2009-10-07 17:10:14 +02:00
|
|
|
|
|
|
|
# Ops
|
2009-04-14 Ramon Novoa <rnovoa@artica.es>
* pandora_ctl, pandora_network, pandora_wmi, pandora_plugin,
pandora_prediction, bin/pandora_wmi, bin/pandora_plugin,
bin/pandora_prediction, bin/pandora_snmpconsole, bin/pandora_recon,
bin/pandora_network, pandora_recon, pandora_snmpconsole: Deleted
from repository. Old server code and startup scripts.
* Makefile.PL, pandora_server_installer, lib/PandoraFMS/Config.pm
lib/PandoraFMS/DB.pm, lib/PandoraFMS/Tools.pm, bin/pandora_server,
pandora_package_installer, pandora_server: Updated to work with the
new code, removed unneeded dependencies, fixed some bugs etc.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/PluginServer.pm,
lib/PandoraFMS/ProducerConsumerServer.pm, lib/PandoraFMS/Server.pm,
lib/PandoraFMS/PredictionServer.pm, lib/PandoraFMS/Core.pm,
lib/PandoraFMS/ReconServer.pm, lib/PandoraFMS/DataServer.pm,
lib/PandoraFMS/NetworkServer.pm, lib/PandoraFMS/WMIServer.pm: Added
to repository. New server code.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1620 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-04-14 21:13:16 +02:00
|
|
|
return 0 if ($@);
|
|
|
|
return 1;
|
2008-11-04 12:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Tries to call a PandoraEnterprise function. Returns undef if unsuccessful.
|
|
|
|
##########################################################################
|
|
|
|
sub enterprise_hook ($$) {
|
2009-04-14 Ramon Novoa <rnovoa@artica.es>
* pandora_ctl, pandora_network, pandora_wmi, pandora_plugin,
pandora_prediction, bin/pandora_wmi, bin/pandora_plugin,
bin/pandora_prediction, bin/pandora_snmpconsole, bin/pandora_recon,
bin/pandora_network, pandora_recon, pandora_snmpconsole: Deleted
from repository. Old server code and startup scripts.
* Makefile.PL, pandora_server_installer, lib/PandoraFMS/Config.pm
lib/PandoraFMS/DB.pm, lib/PandoraFMS/Tools.pm, bin/pandora_server,
pandora_package_installer, pandora_server: Updated to work with the
new code, removed unneeded dependencies, fixed some bugs etc.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/PluginServer.pm,
lib/PandoraFMS/ProducerConsumerServer.pm, lib/PandoraFMS/Server.pm,
lib/PandoraFMS/PredictionServer.pm, lib/PandoraFMS/Core.pm,
lib/PandoraFMS/ReconServer.pm, lib/PandoraFMS/DataServer.pm,
lib/PandoraFMS/NetworkServer.pm, lib/PandoraFMS/WMIServer.pm: Added
to repository. New server code.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1620 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-04-14 21:13:16 +02:00
|
|
|
my $func = shift;
|
|
|
|
my @args = @{shift ()};
|
2008-11-04 12:33:07 +01:00
|
|
|
|
|
|
|
# Temporarily disable strict refs
|
|
|
|
no strict 'refs';
|
|
|
|
|
|
|
|
# Prepend the package name
|
|
|
|
$func = 'PandoraFMS::Enterprise::' . $func;
|
2009-10-07 17:10:14 +02:00
|
|
|
return undef unless (defined (&$func));
|
2008-11-04 12:33:07 +01:00
|
|
|
|
2009-08-20 17:48:42 +02:00
|
|
|
# Try to call the function
|
|
|
|
my $output = eval { &$func (@args); };
|
|
|
|
|
|
|
|
# Check for errors
|
2009-10-15 13:23:40 +02:00
|
|
|
#return undef if ($@);
|
|
|
|
|
|
|
|
# undef is returned only if the enterprise function was not found
|
|
|
|
return '' unless defined ($output);
|
2009-08-20 17:48:42 +02:00
|
|
|
|
2009-04-14 Ramon Novoa <rnovoa@artica.es>
* pandora_ctl, pandora_network, pandora_wmi, pandora_plugin,
pandora_prediction, bin/pandora_wmi, bin/pandora_plugin,
bin/pandora_prediction, bin/pandora_snmpconsole, bin/pandora_recon,
bin/pandora_network, pandora_recon, pandora_snmpconsole: Deleted
from repository. Old server code and startup scripts.
* Makefile.PL, pandora_server_installer, lib/PandoraFMS/Config.pm
lib/PandoraFMS/DB.pm, lib/PandoraFMS/Tools.pm, bin/pandora_server,
pandora_package_installer, pandora_server: Updated to work with the
new code, removed unneeded dependencies, fixed some bugs etc.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/PluginServer.pm,
lib/PandoraFMS/ProducerConsumerServer.pm, lib/PandoraFMS/Server.pm,
lib/PandoraFMS/PredictionServer.pm, lib/PandoraFMS/Core.pm,
lib/PandoraFMS/ReconServer.pm, lib/PandoraFMS/DataServer.pm,
lib/PandoraFMS/NetworkServer.pm, lib/PandoraFMS/WMIServer.pm: Added
to repository. New server code.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1620 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-04-14 21:13:16 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Prints a message to STDOUT at the given log level.
|
|
|
|
##########################################################################
|
|
|
|
sub print_message ($$$) {
|
|
|
|
my ($pa_config, $message, $log_level) = @_;
|
|
|
|
|
2009-11-13 Ramon Novoa <rnovoa@artica.es>
* conf/pandora_server.conf: Updated configuration tokens.
* lib/PandoraFMS/SNMPServer.pm, lib/PandoraFMS/Config.pm,
lib/PandoraFMS/PluginServer.pm, lib/PandoraFMS/PredictionServer.pm,
lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
lib/PandoraFMS/DataServer.pm, lib/PandoraFMS/NetworkServer.pm,
lib/PandoraFMS/Tools.pm, lib/PandoraFMS/WMIServer.pm,
bin/pandora_server: Standardized and improved logging.
git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2108 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
2009-11-13 14:52:48 +01:00
|
|
|
print STDOUT $message . "\n" if ($pa_config->{'verbosity'} >= $log_level);
|
2008-11-04 12:33:07 +01:00
|
|
|
}
|
|
|
|
|
2009-08-03 17:12:07 +02:00
|
|
|
##########################################################################
|
|
|
|
# Returns the value of an XML tag from a hash returned by XMLin (one level
|
|
|
|
# depth).
|
|
|
|
##########################################################################
|
|
|
|
sub get_tag_value ($$$) {
|
|
|
|
my ($hash_ref, $tag, $def_value) = @_;
|
|
|
|
|
|
|
|
return $def_value unless defined ($hash_ref->{$tag}) and ref ($hash_ref->{$tag});
|
|
|
|
|
|
|
|
# Return the first found value
|
|
|
|
foreach my $value (@{$hash_ref->{$tag}}) {
|
|
|
|
|
|
|
|
# If the tag is defined but has no value a ref to an empty hash is returned by XML::Simple
|
|
|
|
return $value unless ref ($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $def_value;
|
|
|
|
}
|
|
|
|
|
2010-09-24 13:09:44 +02:00
|
|
|
###############################################################################
|
|
|
|
# Initialize some variables needed by the MD5 algorithm.
|
|
|
|
# See http://en.wikipedia.org/wiki/MD5#Pseudocode.
|
|
|
|
###############################################################################
|
|
|
|
my (@R, @K);
|
|
|
|
sub md5_init () {
|
|
|
|
|
|
|
|
# R specifies the per-round shift amounts
|
|
|
|
@R = (7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
|
|
|
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
|
|
|
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
|
|
|
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21);
|
|
|
|
|
|
|
|
# Use binary integer part of the sines of integers (radians) as constants
|
|
|
|
for (my $i = 0; $i < 64; $i++) {
|
|
|
|
$K[$i] = floor(abs(sin($i + 1)) * MOD232);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Return the MD5 checksum of the given string.
|
|
|
|
# Pseudocode from http://en.wikipedia.org/wiki/MD5#Pseudocode.
|
|
|
|
###############################################################################
|
|
|
|
sub md5 ($) {
|
|
|
|
my $str = shift;
|
|
|
|
|
|
|
|
# Note: All variables are unsigned 32 bits and wrap modulo 2^32 when calculating
|
|
|
|
|
|
|
|
# Initialize variables
|
|
|
|
my $h0 = 0x67452301;
|
|
|
|
my $h1 = 0xEFCDAB89;
|
|
|
|
my $h2 = 0x98BADCFE;
|
|
|
|
my $h3 = 0x10325476;
|
|
|
|
|
|
|
|
# Pre-processing
|
|
|
|
my $msg = unpack ("B*", pack ("A*", $str));
|
|
|
|
my $bit_len = length ($msg);
|
|
|
|
|
|
|
|
# Append "1" bit to message
|
|
|
|
$msg .= '1';
|
|
|
|
|
|
|
|
# Append "0" bits until message length in bits ≡ 448 (mod 512)
|
|
|
|
$msg .= '0' while ((length ($msg) % 512) != 448);
|
|
|
|
|
|
|
|
# Append bit /* bit, not byte */ length of unpadded message as 64-bit little-endian integer to message
|
|
|
|
$msg .= unpack ("B64", pack ("VV", $bit_len));
|
|
|
|
|
|
|
|
# Process the message in successive 512-bit chunks
|
|
|
|
for (my $i = 0; $i < length ($msg); $i += 512) {
|
|
|
|
|
|
|
|
my @w;
|
|
|
|
my $chunk = substr ($msg, $i, 512);
|
|
|
|
|
|
|
|
# Break chunk into sixteen 32-bit little-endian words w[i], 0 <= i <= 15
|
|
|
|
for (my $j = 0; $j < length ($chunk); $j += 32) {
|
|
|
|
push (@w, unpack ("V", pack ("B32", substr ($chunk, $j, 32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialize hash value for this chunk
|
|
|
|
my $a = $h0;
|
|
|
|
my $b = $h1;
|
|
|
|
my $c = $h2;
|
|
|
|
my $d = $h3;
|
|
|
|
my $f;
|
|
|
|
my $g;
|
|
|
|
|
|
|
|
# Main loop
|
|
|
|
for (my $y = 0; $y < 64; $y++) {
|
|
|
|
if ($y <= 15) {
|
|
|
|
$f = $d ^ ($b & ($c ^ $d));
|
|
|
|
$g = $y;
|
|
|
|
}
|
|
|
|
elsif ($y <= 31) {
|
|
|
|
$f = $c ^ ($d & ($b ^ $c));
|
|
|
|
$g = (5 * $y + 1) % 16;
|
|
|
|
}
|
|
|
|
elsif ($y <= 47) {
|
|
|
|
$f = $b ^ $c ^ $d;
|
|
|
|
$g = (3 * $y + 5) % 16;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$f = $c ^ ($b | (0xFFFFFFFF & (~ $d)));
|
|
|
|
$g = (7 * $y) % 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $temp = $d;
|
|
|
|
$d = $c;
|
|
|
|
$c = $b;
|
|
|
|
$b = ($b + leftrotate (($a + $f + $K[$y] + $w[$g]) % MOD232, $R[$y])) % MOD232;
|
|
|
|
$a = $temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add this chunk's hash to result so far
|
|
|
|
$h0 = ($h0 + $a) % MOD232;
|
|
|
|
$h1 = ($h1 + $b) % MOD232;
|
|
|
|
$h2 = ($h2 + $c) % MOD232;
|
|
|
|
$h3 = ($h3 + $d) % MOD232;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Digest := h0 append h1 append h2 append h3 #(expressed as little-endian)
|
|
|
|
return unpack ("H*", pack ("V", $h0)) . unpack ("H*", pack ("V", $h1)) . unpack ("H*", pack ("V", $h2)) . unpack ("H*", pack ("V", $h3));
|
|
|
|
}
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# MD5 leftrotate function. See http://en.wikipedia.org/wiki/MD5#Pseudocode.
|
|
|
|
###############################################################################
|
|
|
|
sub leftrotate ($$) {
|
|
|
|
my ($x, $c) = @_;
|
|
|
|
|
|
|
|
return (0xFFFFFFFF & ($x << $c)) | ($x >> (32 - $c));
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
## Convert a date (yyy-mm-ddThh:ii:ss) to Timestamp.
|
|
|
|
##########################################################################
|
|
|
|
sub dateTimeToTimestamp {
|
|
|
|
$_[0] =~ /(\d{4})-(\d{2})-(\d{2})([ |T])(\d{2}):(\d{2}):(\d{2})/;
|
|
|
|
my($year, $mon, $day, $GMT, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6, $7);
|
|
|
|
#UTC
|
|
|
|
return timegm($sec, $min, $hour, $day, $mon - 1, $year - 1900);
|
|
|
|
#BST
|
|
|
|
#print "BST\t" . mktime($sec, $min, $hour, $day, $mon - 1, $year - 1900, 0, 0) . "\n";
|
|
|
|
}
|
|
|
|
|
2010-02-18 18:21:40 +01:00
|
|
|
##############################################################################
|
|
|
|
# Below some "internal" functions for automonitoring feature
|
|
|
|
# TODO: Implement the same for other systems like Solaris or BSD
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
sub disk_free ($) {
|
|
|
|
my $target = $_[0];
|
|
|
|
|
|
|
|
# Try to use df command with Posix parameters...
|
|
|
|
my $command = "df -k -P ".$target." | tail -1 | awk '{ print \$4/1024}'";
|
|
|
|
my $output = `$command`;
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub load_average {
|
|
|
|
my $load_average = `cat /proc/loadavg | awk '{ print \$1 }'`;
|
|
|
|
return $load_average;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub free_mem {
|
|
|
|
my $free_mem = `free | grep Mem | awk '{ print \$4 }'`;
|
|
|
|
return $free_mem;
|
|
|
|
}
|
|
|
|
|
2007-08-02 19:53:07 +02:00
|
|
|
# End of function declaration
|
|
|
|
# End of defined Code
|
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|