Code review

This commit is contained in:
José González 2021-05-18 10:25:45 +02:00
parent 0633ac24b9
commit be6269fc89
2 changed files with 14 additions and 26 deletions

View File

@ -3,7 +3,7 @@
######################################################################## ########################################################################
# 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) 2021 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
@ -12,6 +12,7 @@
# Includes list # Includes list
use strict; use strict;
use LWP::Simple; use LWP::Simple;
use LWP::UserAgent;
use MIME::Base64; use MIME::Base64;
use lib '/usr/lib/perl5'; use lib '/usr/lib/perl5';
use PandoraFMS::Tools; use PandoraFMS::Tools;
@ -88,7 +89,7 @@ Optional parameters:
############################################################################## ##############################################################################
sub tool_api_init () { sub tool_api_init () {
print "\nPandora FMS Remote Event Tool Copyright (c) 2013-2015 Artica ST\n"; print "\nPandora FMS Remote Event Tool Copyright (c) 2013-2021 Artica ST\n";
print "This program is Free Software, licensed under the terms of GPL License v2\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"; print "You can download latest versions and documentation at http://www.pandorafms.org\n\n";
@ -190,7 +191,7 @@ sub tool_api_main () {
$id_group = $ARGV[$i + 1]; $id_group = $ARGV[$i + 1];
} }
if ($line eq '-name') { if ($line eq '-name') {
$event_name = $ARGV[$i + 1]; $event_name = join('%23', split("#", $ARGV[$i + 1]));
} }
if ($line eq '-type') { if ($line eq '-type') {
$event_type = $ARGV[$i + 1]; $event_type = $ARGV[$i + 1];
@ -255,17 +256,13 @@ sub tool_api_main () {
} }
if ($event_name eq "") { if ($event_name eq "") {
print "[ERROR] Missing id agent! Read help info:\n\n"; print "[ERROR] Missing event name! Read help info:\n\n";
help_screen (); help_screen ();
} }
if ($id_group eq "") { if ($id_group eq "") {
print "[ERROR] Missing event group! Read help info:\n\n"; print "[ERROR] Missing event group! Read help info:\n\n";
help_screen (); help_screen ();
} }
if ($id_agent eq "" && $agent_name eq "") {
print "[ERROR] Missing id agent! and agent_name Read help info:\n\n";
help_screen ();
}
$data_event = $event_name . $data_event = $event_name .
"|" . $id_group . "|" . $id_group .
@ -321,7 +318,8 @@ sub tool_api_main () {
exit; exit;
} }
else { else {
my $content = get($call_api); my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
my $content = $ua->get($call_api);
if ($option eq '-create_event') { if ($option eq '-create_event') {
if ($content eq undef) { if ($content eq undef) {
@ -329,7 +327,7 @@ sub tool_api_main () {
help_screen(); help_screen();
} }
else { else {
print "Event ID: $content"; print "Event ID: $content->{'_content'}";
} }
} }
elsif ($option eq '-validate_event') { elsif ($option eq '-validate_event') {

View File

@ -3,7 +3,7 @@
######################################################################## ########################################################################
# Pandora FMS - Remote Event Tool (via WEB API) # Pandora FMS - Remote Event Tool (via WEB API)
######################################################################## ########################################################################
# Copyright (c) 2013-2021 Artica Soluciones Tecnologicas S.L # Copyright (c) 2021 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
@ -62,7 +62,7 @@ Optional parameters:
[-owner_user <owner event>] : Use the login name, not the descriptive [-owner_user <owner event>] : Use the login name, not the descriptive
[-source <source>] : (By default 'Pandora') [-source <source>] : (By default 'Pandora')
[-tag <tags>] : Tag (must exist in the system to be imported) [-tag <tags>] : Tag (must exist in the system to be imported)
[-custom_data <custom_data>]: Custom data should be a base 64 encoded JSON document example -custom_data \'{\"test1\" : 1, \"test2\": 2}\' [-custom_data <custom_data>]: Custom data has to be in JSON format. Example: -custom_data \'{\"test1\" : \"t1\", \"test2\": \"2\"}\'
[-id_extra <id extra>] : Id extra [-id_extra <id extra>] : Id extra
[-agent_name <Agent name>] : Agent name, Not to be confused with the alias. [-agent_name <Agent name>] : Agent name, Not to be confused with the alias.
[-force_create_agent<0 o 1>]: Force the creation of agent through an event this will create when it is 1. [-force_create_agent<0 o 1>]: Force the creation of agent through an event this will create when it is 1.
@ -191,7 +191,7 @@ sub tool_api_main () {
$id_group = $ARGV[$i + 1]; $id_group = $ARGV[$i + 1];
} }
if ($line eq '-name') { if ($line eq '-name') {
$event_name = $ARGV[$i + 1]; $event_name = join('%23', split("#", $ARGV[$i + 1]));
} }
if ($line eq '-type') { if ($line eq '-type') {
$event_type = $ARGV[$i + 1]; $event_type = $ARGV[$i + 1];
@ -263,10 +263,6 @@ sub tool_api_main () {
print "[ERROR] Missing event group! Read help info:\n\n"; print "[ERROR] Missing event group! Read help info:\n\n";
help_screen (); help_screen ();
} }
if ($id_agent eq "") {
print "[ERROR] Missing id agent! Read help info:\n\n";
help_screen ();
}
$data_event = $event_name . $data_event = $event_name .
"|" . $id_group . "|" . $id_group .
@ -322,26 +318,20 @@ sub tool_api_main () {
exit; exit;
} }
else { else {
#-----------DEBUG----------------------------
#print($call_api . "\n\n\n");
# Support HTTPS without hostname verification.
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 }); my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
my $content = $ua->get($call_api); my $content = $ua->get($call_api);
#-----------DEBUG----------------------------
#print($content . "\n\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->{'_content'}; print "Event ID: $content->{'_content'}";
} }
} }
elsif ($option eq '-validate_event') { elsif ($option eq '-validate_event') {
print "[RESULT] ".$content->{'_content'}; print "[RESULT] $content";
} }
} }