From 488297d544a081e4f71b3d632c55a229c5843679 Mon Sep 17 00:00:00 2001 From: vgilc Date: Tue, 25 Sep 2012 14:18:22 +0000 Subject: [PATCH] 2012-09-25 Vanessa Gil * util/tool_api.pl: Added file to create events using API. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6998 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 4 + pandora_server/util/tool_api.pl | 160 ++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 pandora_server/util/tool_api.pl diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 3d9f9087ea..0238210bae 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,7 @@ +2012-09-25 Vanessa Gil + + * util/tool_api.pl: Added file to create events using API. + 2012-09-17 Ramon Novoa * lib/PandoraFMS/Core.pm: Added support for inverse intervals when calculating diff --git a/pandora_server/util/tool_api.pl b/pandora_server/util/tool_api.pl new file mode 100644 index 0000000000..2b53ce96a1 --- /dev/null +++ b/pandora_server/util/tool_api.pl @@ -0,0 +1,160 @@ +#!/usr/bin/perl + +############################################################################### +# Pandora FMS General Management Tool +############################################################################### +# Copyright (c) 2010 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 version 2 +############################################################################### + +# Includes list +use strict; +use LWP::Simple; + +# Init +tool_api_init(); + +# Main +tool_api_main(); + +############################################################################## +# Print a help screen and exit. +############################################################################## +sub help_screen{ + print "Usage: perl $0 -p -n [-o ] -u \n\n"; + print "Call syntax create_event: \n\t"; + print ": Name of event (required)\n\t"; + print ": Serialized parameters separated by comma (optional). In this order: \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t\t"; + print " \n\t"; + print ": Credentials of API and database separated by comma (required). In this order\n\t\t"; + print ",,\n\t"; + + print "Example: \n\t"; + print "perl ./api.pl -p http://127.0.0.1/pandora_console/include/api.php -n name_event_create_api -o 1,admin,0,0 -u 1234,admin,pandora\n"; + + print "\n"; + exit; +} + +############################################################################## +# Init screen +############################################################################## +sub tool_api_init () { + + print "\nPandora FMS API tool Copyright (c) 2010 Artica ST\n"; + print "This program is Free Software, licensed under the terms of GPL License v2\n"; + print "You can download latest versions and documentation at http://www.pandorafms.org\n\n"; + + # Load config file from command line + help_screen () if ($#ARGV < 0); + + help_screen () if (($ARGV[0] eq '-h') || ($ARGV[0] eq '-help')); + +} + +############################################################################### +############################################################################### +# MAIN +############################################################################### +############################################################################### + +sub tool_api_main () { + + my $api_path; + my $event_name; + my $data_event; + my $credentials; + my $api_pass; + my $db_user; + my $db_pass; + my @db_info; + + #~ help or api path (required) + if ($ARGV[0] eq '-h') { + print "HELP!\n"; + help_screen (); + } elsif ($ARGV[0] ne '-p') { + print "Missing API path! See help info...\n"; + help_screen (); + } else { + $api_path = $ARGV[1]; + } + + #~ event name (required) + if ($ARGV[2] ne '-n') { + print "Missing event name! See help info...\n"; + help_screen (); + } else { + $event_name = $ARGV[3]; + } + + #~ other event fields (optional) + if ($ARGV[4] eq '-o') { + $data_event = $ARGV[5]; + + #~ credentials of database + if ($ARGV[6] eq '-u') { + $credentials = $ARGV[7]; + @db_info = split(',', $credentials); + + if ($#db_info < 2) { + print "Invalid database credentials! See help info...\n"; + help_screen (); + } else { + $api_pass = $db_info[0]; + $db_user = $db_info[1]; + $db_pass = $db_info[2]; + + } + } else { + print "Missing database credentials! See help info...\n"; + help_screen (); + } + } elsif ($ARGV[4] eq '-u') { #~ credentials of database + $credentials = $ARGV[5]; + } else { + print "Missing database credentials! See help info...\n"; + help_screen (); + } + + my @args = @ARGV; + my $ltotal=$#args; + + if ($ltotal < 0) { + print "[ERROR] No valid arguments\n\n"; + help_screen (); + exit; + } + else { + my $call_api = $api_path.'?op=set&op2=create_event&id='.$event_name.'&other='.$data_event.'&other_mode=url_encode_separator_,&apipass='.$api_pass.'&user='.$db_user.'&pass='.$db_pass; + my $content = get($call_api); + + if ($content == undef) { + print "[ERROR] Not respond or bad syntax.\n\n"; + help_screen(); + } else { + print "Event ID: $content"; + } + } + + print "\nExiting!\n\n"; + + exit; +}