From 3bf7f1594a79ea5816f7d321bb4da60ddbb3a2cf Mon Sep 17 00:00:00 2001 From: slerena Date: Thu, 4 Mar 2010 19:55:19 +0000 Subject: [PATCH] 2010-03-04 Sancho Lerena * conf/pandora_server.conf: Several typos and better formating. * util/change_remoteconfig.pl: Tool to massive edit the server IP in remote configuration files. Could be modified to alter any other fixed field. * util/gpx2pandora_agent_data.pl: Transform a standard GPX GIS data file in several Pandora FMS XML data server compatible files with GIS data. * util/gis.README: Small "documentation" on how to start to using GIS in the Server. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2474 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 16 ++ pandora_server/conf/pandora_server.conf | 35 ++-- pandora_server/util/change_remoteconfig.pl | 180 ++++++++++++++++++ pandora_server/util/gis.README | 24 +++ pandora_server/util/gpx2pandora_agent_data.pl | 116 +++++++++++ 5 files changed, 355 insertions(+), 16 deletions(-) create mode 100644 pandora_server/util/change_remoteconfig.pl create mode 100644 pandora_server/util/gis.README create mode 100755 pandora_server/util/gpx2pandora_agent_data.pl diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 7262db8d96..532d72fed1 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,19 @@ +2010-03-04 Sancho Lerena + + * conf/pandora_server.conf: Several typos and better formating. + + * util/change_remoteconfig.pl: Tool to massive edit the server IP + in remote configuration files. Could be modified to alter any other + fixed field. + + * util/gpx2pandora_agent_data.pl: Transform a standard GPX GIS data + file in several Pandora FMS XML data server compatible files with GIS + data. + + * util/gis.README: Small "documentation" on how to start to using GIS + in the Server. + + 2010-03-04 Pablo de la Concepción * conf/pandora_server.conf: Corrected typo diff --git a/pandora_server/conf/pandora_server.conf b/pandora_server/conf/pandora_server.conf index f9b811a4ce..8b22417e9d 100755 --- a/pandora_server/conf/pandora_server.conf +++ b/pandora_server/conf/pandora_server.conf @@ -233,32 +233,35 @@ max_queue_files 250 # restart 0 # restart_delay 60 +# More information about GIS Setup in /usr/share/pandora_server/util/gis.README +# Flag to activate GIS (positional information for agents and maps) +# by default it is desactivated +#activate_gis 0 -# Flag to activate GIS (positional information for agents and maps) by default it is desactivated -# activate_gis 0 - -# Radius of the Error in meters to consider two gis locations as the same location. -# location_error 50 +# Radius of error in meters to consider two gis locations as the same location. +#location_error 50 # Recon reverse geolocation mode [disabled, sql, file] -# * disabled: The recon task doesn't try to geolocate the ip discovered. -# * sql: The recon task trys to query the SQL database to geolocate the ip discovered -# * file: The recon task trys to find the geolocation information of the ip discovered in -# the file indicated in the recon_reverse_geolocation_file parameter +# disabled The recon task doesn't try to geolocate the ip discovered. +# sql The recon task trys to query the SQL database to geolocate the +# ip discovered +# file The recon task trys to find the geolocation information of the +# ip discovered in the file indicated in the +# recon_reverse_geolocation_file parameter + # recon_reverse_geolocation_mode disabled -# Recon reverse geolocation file (databases with the reverse geolocation information using -# MaxMind GPL GeoLiteCity.dat format). - +# Recon reverse geolocation file. This is the database with the reverse +# geolocation information using MaxMind GPL GeoLiteCity.dat format). #recon_reverse_geolocation_file /usr/local/share/GeoIP/GeoIPCity.dat -# Radius (in meters) of the circle in where the agents will be place randomly when finded by a recon task -# The center of the cicle is guessed by geolocating the IP. +# Radius (in meters) of the circle in where the agents will be place randomly +# when finded by a recon task. Center of the circle is guessed +# by geolocating the IP. #recon_location_scatter_radius 1000 - # Pandora Server self-monitoring (embedded agent) (by default disabled) -# self_monitoring 1 +#self_monitoring 1 # Update parent from the agent xml #update_parent 1 diff --git a/pandora_server/util/change_remoteconfig.pl b/pandora_server/util/change_remoteconfig.pl new file mode 100644 index 0000000000..70e17ead7a --- /dev/null +++ b/pandora_server/util/change_remoteconfig.pl @@ -0,0 +1,180 @@ +#!/usr/bin/perl +# (c) Artica Soluciones Tecnologicas 2010 +# This script is licensed under GPL v2 licence. + +use strict; +use POSIX qw(floor); + + +# TODO: Let more massive changes (fields) to be changed. + +# Used to calculate the MD5 checksum of a string +use constant MOD232 => 2**32; +if ($#ARGV != 1) { + print "This tool is used to do a massive change in all remote configuration\n"; + print "files for the remote agents, and change a list of files, given it's \n"; + print "agent name (case sensisitive)\n\n"; + print "Usage: change_remoteconfig.pl \n\n"; + exit; +} + +my $fichero_nombres = $ARGV[0]; +my $servidor_destino = $ARGV[1]; + +# Ruta al directorio data_in +my $data_in = "/var/spool/pandora/data_in"; +print "Massive changes are set. Ready to modify files at $data_in/conf and the MD5 hashes in $data_in/md5\n"; + +md5_init(); +open (NOMBRES, $fichero_nombres) or die ("File $fichero_nombres not readable : $!"); +my @servidores = ; +close (NOMBRES); +print "Server IP address '$servidor_destino' is about to be changed in these agents:\n"; +print "Total agents: ". scalar(@servidores)."\n"; +print @servidores; + +print "Waiting 10 seconds. Press ^C to cancel.n\n"; +sleep (10); + +foreach (@servidores) { + my $servidor = $_; + chomp ($servidor); + print "Procesing: $servidor " ; + my $nombre_md5 = md5($servidor); + my $fichero_conf = "$data_in/conf/$nombre_md5.conf"; + # Se lee el fichero y se cambia la linea correspondiente + open (CONF_FILE, $fichero_conf)or print ("Could not open file '$fichero_conf': $!."); + open (NEW_CONF_FILE, '>', "$fichero_conf.new")or print ("Could not open file '$fichero_conf.new': $!."); + while (my $linea = ) { + if ($linea =~ m/^\s*server_ip.*/) { + $linea = "server_ip\t$servidor_destino\n"; + } + print NEW_CONF_FILE $linea; + } + close (CONF_FILE); + close (NEW_CONF_FILE); + `mv $fichero_conf.new $fichero_conf`; + + # Calculate the new configuration file MD5 digest + open (CONF_FILE, $fichero_conf)or print ("Could not open file '$fichero_conf': $!."); + binmode(CONF_FILE); + my $conf_md5 = md5 (join ('', )); + close (CONF_FILE); + print "Nuevo MD5 : $conf_md5\t"; + my $fichero_md5 = "$data_in/md5/$nombre_md5.md5"; + `echo -n "$conf_md5" > $fichero_md5`; +} + +############################################################################### +# MD5 leftrotate function. See http://en.wikipedia.org/wiki/MD5#Pseudocode. +############################################################################### +sub leftrotate ($$) { + my ($x, $c) = @_; + + return (0xFFFFFFFF & ($x << $c)) | ($x >> (32 - $c)); +} + +############################################################################### +# 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)); +} + + diff --git a/pandora_server/util/gis.README b/pandora_server/util/gis.README new file mode 100644 index 0000000000..500e768524 --- /dev/null +++ b/pandora_server/util/gis.README @@ -0,0 +1,24 @@ +Pandora FMS uses the MaxMind GeoIP API to map IP address to it's GPS coordinates. This is a GPL library included in Pandora FMS libraries. + +In order to be able to use GeoIP/GIS features of the Pandora FMS reconserver, you need to download and install a GeoLiteCite maps. You can download from maxmind site or from Pandora FMS download site. For more information about MaxMind and GeoIP database, check this URLs: + + http://www.maxmind.com/app/geoip_resources + http://www.maxmind.com/app/installation + http://www.maxmind.com/app/geolitecity + +Install manually Geo-IP-PurePerl-1.24.tar.gz + +This database will be used with Pandora FMS recon server for positioning detected host Systems. Check Pandora FMS documentacion for more information. + +Prior to use this database you need to decompress it with gzip -d and edit your /etc/pandora/pandora_server.conf file and point the GeoLiteCity GeoLiteCity.dat file with Pandora FMS configuration token 'recon_reverse_geolocation_file', like: + + activate_gis 1 + recon_reverse_geolocation_file /usr/share/pandora_server/util/GeoLiteCity.dat + location_error 50 + recon_reverse_geolocation_mode file + recon_location_scatter_radius 1000 + +Get a new version at: + + http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz + diff --git a/pandora_server/util/gpx2pandora_agent_data.pl b/pandora_server/util/gpx2pandora_agent_data.pl new file mode 100755 index 0000000000..a02f6e7695 --- /dev/null +++ b/pandora_server/util/gpx2pandora_agent_data.pl @@ -0,0 +1,116 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use XML::Simple; +use PandoraFMS::Tools; +use Data::Dumper; +use Sys::Hostname; +use POSIX qw(strftime); + +use constant AGENT_VERSION => '3.1'; + + +# Check parameters + +if ($#ARGV != 1) { + print "Pandora FMS GIS tool to produce XML files from a standard GPX file\n"; + print "This will put all the XML files in /var/spool/pandora/data_in directory \n"; + print "\n"; + print "Usage: $0 \n\n"; + exit 1; +} + +# Configuration tokens +my %Conf = ( + 'server_path' => '/var/spool/pandora/data_in', + 'interval' => 300, + 'agent_name' => hostname (), + 'description' => 'Data from GPX', + 'group' => '', + 'encoding' => 'ISO-8859-1', +); + + +my $file_name = shift; +my $agent_name = shift; +if (defined($agent_name)) { + print "agent_name: $agent_name\n"; + $Conf{'agent_name'} = $agent_name; +} + +my $xml_data = XMLin ($file_name, forcearray => 1 ); + +# Invalid XML +if ($@) { + print "Invalid XML"; + rename($file_name, $file_name . '_BADXML'); + exit -1; +} + +# Debug, code commented +print "Printing XML DATA\n"; +#print Dumper ($xml_data); +#print "Finish Printing XML DATA\n"; +# 'rte' => [ +# { +# 'rtept' => [ +# { +# 'ele' => [ +# '728' +# ], +# 'speed' => [ +# '0' +# ], +# 'time' => [ +# '2010-02-19T10:45:08Z' +# ], +# 'lat' => '40.4327545166', +# 'lon' => '-3.7009150982' +# }, +# +my $posiciones = $xml_data->{'rte'}[0]; + +# Process positions +foreach my $position (@{$posiciones->{'rtept'}}) { + my $longitude= $position->{'lon'}; + my $latitude= $position->{'lat'}; + my $altitude= $position->{'ele'}[0]; + my $timestamp= $position->{'time'}[0]; + + $timestamp =~ s/Z$//; + $timestamp =~ s/T/ /; + $timestamp =~ s/02/31/; + # Use the current time + $timestamp= strftime ('%Y/%m/%d %H:%M:%S', localtime ()); + + print "Longitude: $longitude, Latitude: $latitude, Altitude: $altitude, Timestamp: $timestamp\n"; + + my $OS = $^O; + + my $xml = "\n" . + "\n"; + $xml .= ""; + $xml .= " "; + $xml .= " "; + $xml .= " generic_proc"; + $xml .= " "; + $xml .= ""; + $xml .= ""; + +# print $xml; + + # Save XML data file + + my $temp_file = $Conf{'server_path'} . '/' . $Conf{'agent_name'} . '.' . time () . '.data'; + open (TEMP_FILE, "> $temp_file") ||print ("Could not write XML data file: $!"); + print TEMP_FILE $xml; + close (TEMP_FILE); + sleep(1); +} +