diff --git a/pandora_server/util/pandora_revent_create.pl b/pandora_server/util/pandora_revent_create.pl index e6a9f13ed2..3c4e68c79a 100644 --- a/pandora_server/util/pandora_revent_create.pl +++ b/pandora_server/util/pandora_revent_create.pl @@ -1,17 +1,18 @@ #!/usr/bin/perl -############################################################################### +######################################################################## # Pandora FMS - Remote Event Tool (via WEB API) -############################################################################### +######################################################################## # Copyright (c) 2013 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; +use URI::Escape; # Init tool_api_init(); @@ -19,72 +20,71 @@ tool_api_init(); # Main tool_api_main(); -############################################################################## +######################################################################## # Print a help screen and exit. -############################################################################## -sub help_screen{ - - print "Options to create event: +######################################################################## +sub help_screen () { + + print " +Options to create event: \t$0 -p -create event Where options:\n - -u + -u -create_event - -name : Free text - -group : Group ID (use 0 for 'all') - -type : unknown, alert_fired, alert_recovered, alert_ceased - alert_manual_validation, system, error, new_agent - configuration_change, going_unknown, going_down_critical, - going_down_warning, going_up_normal + -name : Free text + -group : Group ID (use 0 for 'all') + -type : unknown, alert_fired, alert_recovered, alert_ceased + alert_manual_validation, system, error, new_agent + configuration_change, going_unknown, going_down_critical, + going_down_warning, going_up_normal Optional parameters: - [-agent ] : Agent ID - [-user ] : User comment (use in combination with -comment option) - [-status ] : 0 New, 1 Validated, 2 In process - [-am ] : ID Agent Module linked to event - [-alert ] : ID Alert Module linked to event - [-criticity ] : 0 Maintance, 1 Informative, 2 Normal, - 3 Warning, 4 Crit, 5 Minor, 6 Major + [-agent ] : Agent ID + [-user ] : User comment (use in combination with -comment option) + [-status ] : 0 New, 1 Validated, 2 In process + [-am ] : ID Agent Module linked to event + [-alert ] : ID Alert Module linked to event + [-criticity ] : 0 Maintance, 1 Informative, 2 Normal, + 3 Warning, 4 Crit, 5 Minor, 6 Major - [-comment ] : Free text for comment - [-tag ] : Tag (must exist in the system to be imported) - [-source ] : (By default 'Pandora') - [-c_instructions ] - [-w_instructions ] - [-u_instructions ] - [-owner ] : Use the login name, not the descriptive \n\n"; + [-comment ] : Free text for comment + [-tag ] : Tag (must exist in the system to be imported) + [-source ] : (By default 'Pandora') + [-c_instructions ] + [-w_instructions ] + [-u_instructions ] + [-owner ] : Use the login name, not the descriptive + + +Credential/API syntax: + + : API credentials separated by comma: ,, - print "Credential/API syntax: \n\n\t"; - print ": API credentials separated by comma: ,,\n\n"; - - print "Example of event generation:\n\n"; - - #print "\t$0 -p http://192.168.70.160/pandora_console/include/api.php -u pot12,admin,pandora \ -#\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 \ - \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\" \ - \t-u_instructions \"Unknown instructions\" -source \"Commandline\" -tag \"Tags\" -owner \"other\" "; - - print "\n\n\n"; - exit; +Example of event generation: + + $0 -p http://localhost/pandora_console/include/api.php -u 1234,admin,pandora + -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\" + + +"; + + exit; } -############################################################################## +######################################################################## # Init screen -############################################################################## +######################################################################## sub tool_api_init () { - + print "\nPandora FMS Remote Event Tool Copyright (c) 2013 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"; - + if ($#ARGV < 0) { help_screen(); } @@ -92,14 +92,17 @@ sub tool_api_init () { if (($ARGV[0] eq '-h') || ($ARGV[0] eq '-help')) { help_screen(); } - + } -############################################################################### -############################################################################### + + + +######################################################################## +######################################################################## # MAIN -############################################################################### -############################################################################### +######################################################################## +######################################################################## sub tool_api_main () { @@ -132,15 +135,17 @@ sub tool_api_main () { my $id_event; my $option = $ARGV[4]; my $call_api; - + #~ help or api path (required) if ($ARGV[0] eq '-h') { print "HELP!\n"; help_screen(); - } elsif ($ARGV[0] ne '-p') { + } + elsif ($ARGV[0] ne '-p') { print "[ERROR] Missing API path! Read help info:\n\n"; help_screen (); - } else { + } + else { $api_path = $ARGV[1]; } @@ -152,30 +157,34 @@ sub tool_api_main () { if ($#db_info < 2) { print "[ERROR] Invalid database credentials! Read help info:\n\n"; help_screen(); - } else { + } + else { $api_pass = $db_info[0]; $db_user = $db_info[1]; $db_pass = $db_info[2]; } - } else { + } + else { print "[ERROR] Missing database credentials! Read help info:\n\n"; help_screen (); } if ($ARGV[4] eq '-create_event') { - #~ event name (required) + #~ event name (required) if ($ARGV[5] ne '-name') { print "[ERROR] Missing event name! Read help info:\n\n"; 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') { print "[ERROR] Missing event group! Read help info:\n\n"; help_screen (); - } else { + } + else { $id_group = $ARGV[8]; } @@ -183,87 +192,115 @@ sub tool_api_main () { if ($ARGV[9] ne '-type') { print "[ERROR] Missing event type! Read help info:\n\n"; help_screen (); - } else { + } + else { $event_type = $ARGV[10]; } - + my $i = 0; foreach (@ARGV) { my $line = $_; if ($line eq '-agent') { - $id_agent = $ARGV[$i+1]; + $id_agent = $ARGV[$i + 1]; } if ($line eq '-user') { - $id_user = $ARGV[$i+1]; + $id_user = $ARGV[$i + 1]; } if ($line eq '-status') { - $status = $ARGV[$i+1]; + $status = $ARGV[$i + 1]; } if ($line eq '-am') { - $id_agent_module = $ARGV[$i+1]; + $id_agent_module = $ARGV[$i + 1]; } if ($line eq '-alert') { - $id_alert_am = $ARGV[$i+1]; + $id_alert_am = $ARGV[$i + 1]; } if ($line eq '-criticity') { - $criticity = $ARGV[$i+1]; + $criticity = $ARGV[$i + 1]; } if ($line eq '-comment') { - $user_comment = $ARGV[$i+1]; + $user_comment = $ARGV[$i + 1]; } if ($line eq '-tag') { $tags = $ARGV[$i+1]; } if ($line eq '-source') { - $source = $ARGV[$i+1]; + $source = uri_escape($ARGV[$i + 1]); } if ($line eq '-c_instructions') { - $critical_instructions = $ARGV[$i+1]; + $critical_instructions = uri_escape($ARGV[$i + 1]); } if ($line eq '-w_instructions') { - $warning_instructions = $ARGV[$i+1]; + $warning_instructions = uri_escape($ARGV[$i + 1]); } if ($line eq '-u_instructions') { - $unknown_instructions = $ARGV[$i+1]; + $unknown_instructions = uri_escape($ARGV[$i + 1]); } if ($line eq '-owner') { $owner_user = $ARGV[$i+1]; } $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; - - $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; - + + $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; + #DEBUG TRACE# - + #print "$call_api\n"; - + } my @args = @ARGV; - my $ltotal=$#args; - + my $ltotal = $#args; + if ($ltotal < 0) { print "[ERROR] No valid arguments. Read help info:\n\n"; help_screen (); exit; - } + } else { my $content = get($call_api); + print("\n\n"); + print($call_api); + print("\n\n"); if ($option eq '-create_event') { if ($content eq undef) { print "[ERROR] Not respond or bad syntax. Read help info:\n\n"; help_screen(); - } else { + } + else { print "Event ID: $content"; } } } - - print "\nExiting!\n\n"; - - exit; + + print "\nExiting!\n\n"; + + exit; }