Fixed the script to create events with especial characters in the parameters

(cherry picked from commit f3e98738ed)
This commit is contained in:
mdtrooper 2014-12-05 12:02:21 +01:00
parent e5fcafa631
commit 875a3735eb
1 changed files with 131 additions and 94 deletions

View File

@ -1,17 +1,18 @@
#!/usr/bin/perl #!/usr/bin/perl
############################################################################### ########################################################################
# Pandora FMS - Remote Event Tool (via WEB API) # Pandora FMS - Remote Event Tool (via WEB API)
############################################################################### ########################################################################
# Copyright (c) 2013 Artica Soluciones Tecnologicas S.L # Copyright (c) 2013 Artica Soluciones Tecnologicas S.L
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2 # modify it under the terms of the GNU General Public License version 2
############################################################################### ########################################################################
# Includes list # Includes list
use strict; use strict;
use LWP::Simple; use LWP::Simple;
use URI::Escape;
# Init # Init
tool_api_init(); tool_api_init();
@ -19,12 +20,13 @@ tool_api_init();
# Main # Main
tool_api_main(); tool_api_main();
############################################################################## ########################################################################
# Print a help screen and exit. # Print a help screen and exit.
############################################################################## ########################################################################
sub help_screen{ sub help_screen () {
print "Options to create event: print "
Options to create event:
\t$0 -p <path_to_consoleAPI> -create event <options> \t$0 -p <path_to_consoleAPI> -create event <options>
@ -54,31 +56,29 @@ Optional parameters:
[-c_instructions <critical_instructions>] [-c_instructions <critical_instructions>]
[-w_instructions <warning_instructions>] [-w_instructions <warning_instructions>]
[-u_instructions <unknown_instructions>] [-u_instructions <unknown_instructions>]
[-owner <owner_user>] : Use the login name, not the descriptive \n\n"; [-owner <owner_user>] : Use the login name, not the descriptive
print "Credential/API syntax: \n\n\t";
print "<credentials>: API credentials separated by comma: <api_pass>,<user>,<pass>\n\n";
print "Example of event generation:\n\n"; Credential/API syntax:
#print "\t$0 -p http://192.168.70.160/pandora_console/include/api.php -u pot12,admin,pandora \ <credentials> : API credentials separated by comma: <api_pass>,<user>,<pass>
#\t-create_event -name \"Sample event executed from commandline\" -group 2 -type \"system\" -agent 2 \
#\t-user \"admin\" -status 0 -am 0 -alert 9 -criticity 3 -comment \"User comments\" -tag \"tags\" \
#\t-source \"Commandline\" -extra 3 -c_instructions \"Critical instructions\" \
#\t-w_instructions \"Warning instructions\" -u_instructions \"Unknown instructions\" -owner \"other\" ";
print "\t$0 -p http://localhost/pandora_console/include/api.php -u 1234,admin,pandora \ Example of event generation:
\t-create_event -name \"SampleEvent\" -group 2 -type \"system\" -agent 189 -status 0 -user \"admin\" \
\t-criticity 3 -am 0 -alert 9 -c_instructions \"Critical instructions\" -w_instructions \"Warning instructions\" \ $0 -p http://localhost/pandora_console/include/api.php -u 1234,admin,pandora
\t-u_instructions \"Unknown instructions\" -source \"Commandline\" -tag \"Tags\" -owner \"other\" "; -create_event -name \"SampleEvent\" -group 2 -type \"system\" -agent 189 -status 0 -user \"admin\"
-criticity 3 -am 0 -alert 9 -c_instructions \"Critical instructions\" -w_instructions \"Warning instructions\"
-u_instructions \"Unknown instructions\" -source \"Commandline\" -tag \"Tags\" -owner \"other\"
";
print "\n\n\n";
exit; exit;
} }
############################################################################## ########################################################################
# Init screen # Init screen
############################################################################## ########################################################################
sub tool_api_init () { sub tool_api_init () {
print "\nPandora FMS Remote Event Tool Copyright (c) 2013 Artica ST\n"; print "\nPandora FMS Remote Event Tool Copyright (c) 2013 Artica ST\n";
@ -95,11 +95,14 @@ sub tool_api_init () {
} }
###############################################################################
###############################################################################
########################################################################
########################################################################
# MAIN # MAIN
############################################################################### ########################################################################
############################################################################### ########################################################################
sub tool_api_main () { sub tool_api_main () {
@ -137,10 +140,12 @@ sub tool_api_main () {
if ($ARGV[0] eq '-h') { if ($ARGV[0] eq '-h') {
print "HELP!\n"; print "HELP!\n";
help_screen(); help_screen();
} elsif ($ARGV[0] ne '-p') { }
elsif ($ARGV[0] ne '-p') {
print "[ERROR] Missing API path! Read help info:\n\n"; print "[ERROR] Missing API path! Read help info:\n\n";
help_screen (); help_screen ();
} else { }
else {
$api_path = $ARGV[1]; $api_path = $ARGV[1];
} }
@ -152,12 +157,14 @@ sub tool_api_main () {
if ($#db_info < 2) { if ($#db_info < 2) {
print "[ERROR] Invalid database credentials! Read help info:\n\n"; print "[ERROR] Invalid database credentials! Read help info:\n\n";
help_screen(); help_screen();
} else { }
else {
$api_pass = $db_info[0]; $api_pass = $db_info[0];
$db_user = $db_info[1]; $db_user = $db_info[1];
$db_pass = $db_info[2]; $db_pass = $db_info[2];
} }
} else { }
else {
print "[ERROR] Missing database credentials! Read help info:\n\n"; print "[ERROR] Missing database credentials! Read help info:\n\n";
help_screen (); help_screen ();
} }
@ -167,15 +174,17 @@ sub tool_api_main () {
if ($ARGV[5] ne '-name') { if ($ARGV[5] ne '-name') {
print "[ERROR] Missing event name! Read help info:\n\n"; print "[ERROR] Missing event name! Read help info:\n\n";
help_screen (); help_screen ();
} else { }
$event_name = $ARGV[6]; else {
$event_name = uri_escape($ARGV[6]);
} }
#~ id group (required) #~ id group (required)
if ($ARGV[7] ne '-group') { if ($ARGV[7] ne '-group') {
print "[ERROR] Missing event group! Read help info:\n\n"; print "[ERROR] Missing event group! Read help info:\n\n";
help_screen (); help_screen ();
} else { }
else {
$id_group = $ARGV[8]; $id_group = $ARGV[8];
} }
@ -183,7 +192,8 @@ sub tool_api_main () {
if ($ARGV[9] ne '-type') { if ($ARGV[9] ne '-type') {
print "[ERROR] Missing event type! Read help info:\n\n"; print "[ERROR] Missing event type! Read help info:\n\n";
help_screen (); help_screen ();
} else { }
else {
$event_type = $ARGV[10]; $event_type = $ARGV[10];
} }
@ -215,16 +225,16 @@ sub tool_api_main () {
$tags = $ARGV[$i+1]; $tags = $ARGV[$i+1];
} }
if ($line eq '-source') { if ($line eq '-source') {
$source = $ARGV[$i+1]; $source = uri_escape($ARGV[$i + 1]);
} }
if ($line eq '-c_instructions') { if ($line eq '-c_instructions') {
$critical_instructions = $ARGV[$i+1]; $critical_instructions = uri_escape($ARGV[$i + 1]);
} }
if ($line eq '-w_instructions') { if ($line eq '-w_instructions') {
$warning_instructions = $ARGV[$i+1]; $warning_instructions = uri_escape($ARGV[$i + 1]);
} }
if ($line eq '-u_instructions') { if ($line eq '-u_instructions') {
$unknown_instructions = $ARGV[$i+1]; $unknown_instructions = uri_escape($ARGV[$i + 1]);
} }
if ($line eq '-owner') { if ($line eq '-owner') {
$owner_user = $ARGV[$i+1]; $owner_user = $ARGV[$i+1];
@ -232,9 +242,32 @@ sub tool_api_main () {
$i++; $i++;
} }
$data_event = $event_name."|".$id_group."|".$id_agent."|".$status."|".$id_user."|".$event_type."|".$criticity."|".$id_agent_module."|".$id_alert_am."|".$critical_instructions."|".$warning_instructions."|".$unknown_instructions."|".$user_comment."|".$owner_user."|".$source."|".$tags; $data_event = $event_name . "|" .
$id_group . "|" .
$id_agent . "|" .
$status . "|" .
$id_user . "|" .
$event_type . "|" .
$criticity . "|" .
$id_agent_module . "|" .
$id_alert_am . "|" .
$critical_instructions . "|" .
$warning_instructions . "|" .
$unknown_instructions . "|" .
$user_comment . "|" .
$owner_user . "|" .
$source . "|" .
$tags;
$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; $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;
#DEBUG TRACE# #DEBUG TRACE#
@ -252,12 +285,16 @@ sub tool_api_main () {
} }
else { else {
my $content = get($call_api); my $content = get($call_api);
print("\n\n");
print($call_api);
print("\n\n");
if ($option eq '-create_event') { if ($option eq '-create_event') {
if ($content eq undef) { if ($content eq undef) {
print "[ERROR] Not respond or bad syntax. Read help info:\n\n"; print "[ERROR] Not respond or bad syntax. Read help info:\n\n";
help_screen(); help_screen();
} else { }
else {
print "Event ID: $content"; print "Event ID: $content";
} }
} }