2013-03-12 18:53:25 +01:00
#!/usr/bin/perl
2017-06-21 14:34:06 +02:00
########################################################################
2013-03-12 18:53:25 +01:00
# Pandora FMS - Remote Event Tool (via WEB API)
2017-06-21 14:34:06 +02:00
########################################################################
2021-05-18 10:25:45 +02:00
# Copyright (c) 2021 Artica Soluciones Tecnologicas S.L
2013-03-12 18:53:25 +01:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
2017-06-21 14:34:06 +02:00
########################################################################
2013-03-12 18:53:25 +01:00
# Includes list
use strict;
use LWP::Simple;
2021-05-18 10:25:45 +02:00
use LWP::UserAgent;
2017-06-21 14:34:06 +02:00
use MIME::Base64;
use lib '/usr/lib/perl5';
use PandoraFMS::Tools;
2013-03-12 18:53:25 +01:00
# Init
tool_api_init();
# Main
tool_api_main();
2017-06-21 14:34:06 +02:00
########################################################################
2013-03-12 18:53:25 +01:00
# Print a help screen and exit.
2017-06-21 14:34:06 +02:00
########################################################################
2013-03-12 18:53:25 +01:00
sub help_screen{
print "Options to create event:
2017-06-21 14:34:06 +02:00
\t$0 -p <path_to_consoleAPI> -u <credentials> -create_event <options>
2013-03-12 18:53:25 +01:00
Where options:\n
2021-05-19 12:40:06 +02:00
-u <credentials> : API credentials separated by comma: <api_pass>,<user>,<pass>
-name <event_name> : Free text (surrounded by single-quotes, for security reasons)
-group <id_group> : Group ID (use 0 for 'all')
2013-03-12 18:53:25 +01:00
Optional parameters:
2021-05-18 10:52:39 +02:00
2021-05-19 12:40:06 +02:00
[-agent <id_agent] : Agent ID
[-status <status>] : 0 New, 1 Validated, 2 In process
[-user <id_user>] : User comment (use in combination with -comment option)
[-type <event_type>] : unknown, alert_fired, alert_recovered, alert_ceased
2017-06-21 14:34:06 +02:00
alert_manual_validation, system, error, new_agent
configuration_change, going_unknown, going_down_critical,
going_down_warning, going_up_normal
2021-05-19 12:40:06 +02:00
[-severity <severity>] : 0 Maintance,
2017-06-21 14:34:06 +02:00
1 Informative,
2 Normal,
3 Warning,
4 Crit,
5 Minor,
6 Major
2021-05-19 12:40:06 +02:00
[-am <id_agent_module>] : ID Agent Module linked to event
[-alert <id_alert_am>] : ID Alert Module linked to event
2017-06-21 14:34:06 +02:00
[-c_instructions <critical_instructions>]
[-w_instructions <warning_instructions>]
[-u_instructions <unknown_instructions>]
2021-05-19 12:40:06 +02:00
[-user_comment <comment>] : Free text (surrounded by single-quotes, for security reasons)
[-owner_user <owner event>] : Use the login name, not the descriptive
[-source <source>] : (By default 'Pandora')
[-tag <tags>] : Tag (must exist in the system to be imported)
2017-06-21 14:34:06 +02:00
[-custom_data <custom_data>]: Custom data has to be in JSON format. Example: -custom_data \'{\"test1\" : \"t1\", \"test2\": \"2\"}\'
2021-05-19 12:40:06 +02:00
[-id_extra <id extra>] : Id extra
[-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.
[-separator '<char/s>'] : If you use the vertical bar `|` in the event name, you must set other character for send the info. This must be surrounded by single-quotes
[-server_id <server_id>] : The pandora node server_id\n\n";
2013-03-12 18:53:25 +01:00
print "Example of event generation:\n\n";
2017-06-21 14:34:06 +02:00
print "\t./pandora_revent.pl -p http://localhost/pandora_console/include/api.php -u 1234,admin,pandora \
\t-create_event -name \"SampleEvent\" -group 2 -agent 189 -status 0 -user \"admin\" -type \"system\" \
\t-severity 3 -am 0 -alert 9 -c_instructions \"Critical instructions\" -w_instructions \"Warning instructions\" \
\t-u_instructions \"Unknown instructions\" -id_extra \"id extra\" -agent_name \"agent name\" -source \"Commandline\" -tag \"Tags\"";
print "\n\n\nOptions to validate event: \n\n\t";
print "$0 -p <path_to_consoleAPI> -u <credentials> -validate_event <options> -id <id_event>\n\n";
print "Sample of event validation: \n\n\t";
print "$0 -p http://localhost/pandora/include/api.php -u pot12,admin,pandora -validate_event -id 234";
print "\n\n\n";
exit;
2013-03-12 18:53:25 +01:00
}
##############################################################################
# Init screen
##############################################################################
sub tool_api_init () {
2017-06-21 14:34:06 +02:00
2021-05-18 10:25:45 +02:00
print "\nPandora FMS Remote Event Tool Copyright (c) 2013-2021 Artica ST\n";
2013-03-12 18:53:25 +01:00
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";
2017-06-21 14:34:06 +02:00
2013-03-12 18:53:25 +01:00
if ($#ARGV < 0) {
help_screen();
}
if (($ARGV[0] eq '-h') || ($ARGV[0] eq '-help')) {
help_screen();
}
2017-06-21 14:34:06 +02:00
2013-03-12 18:53:25 +01:00
}
2017-06-21 14:34:06 +02:00
########################################################################
########################################################################
2013-03-12 18:53:25 +01:00
# MAIN
2017-06-21 14:34:06 +02:00
########################################################################
########################################################################
2013-03-12 18:53:25 +01:00
sub tool_api_main () {
my $api_path;
my $event_name;
my $id_group;
my $event_type;
my $data_event;
my $credentials;
my $api_pass;
my $db_user;
my $db_pass;
my @db_info;
2021-05-18 15:27:45 +02:00
my $id_agent = '0';
2013-03-12 18:53:25 +01:00
my $id_user = '';
my $status = '';
my $id_agent_module = '';
my $id_alert_am = '';
2017-06-21 14:34:06 +02:00
my $severity = '';
2013-03-12 18:53:25 +01:00
my $user_comment = '';
my $tags = '';
my $source = '';
my $critical_instructions = '';
my $warning_instructions = '';
my $unknown_instructions = '';
2017-06-21 14:34:06 +02:00
my $id_extra = '';
my $agent_name = '';
my $force_create_agent = 0;
2013-03-12 18:53:25 +01:00
my $owner_user = '';
my $id_event;
my $option = $ARGV[4];
my $call_api;
2017-06-21 14:34:06 +02:00
my $custom_data = "";
my $server_id = 0;
2021-05-19 12:40:06 +02:00
my $separator = '|';
2017-06-21 14:34:06 +02:00
2013-03-12 18:53:25 +01:00
#~ help or api path (required)
if ($ARGV[0] eq '-h') {
print "HELP!\n";
help_screen();
2017-06-21 14:34:06 +02:00
}
elsif ($ARGV[0] ne '-p') {
2013-03-12 18:53:25 +01:00
print "[ERROR] Missing API path! Read help info:\n\n";
help_screen ();
2017-06-21 14:34:06 +02:00
}
else {
2013-03-12 18:53:25 +01:00
$api_path = $ARGV[1];
}
#~ credentials of database
if ($ARGV[2] eq '-u') {
$credentials = $ARGV[3];
@db_info = split(',', $credentials);
if ($#db_info < 2) {
print "[ERROR] Invalid database credentials! Read help info:\n\n";
help_screen();
2017-06-21 14:34:06 +02:00
}
else {
2013-03-12 18:53:25 +01:00
$api_pass = $db_info[0];
$db_user = $db_info[1];
$db_pass = $db_info[2];
}
2017-06-21 14:34:06 +02:00
}
else {
2013-03-12 18:53:25 +01:00
print "[ERROR] Missing database credentials! Read help info:\n\n";
help_screen ();
}
if ($ARGV[4] eq '-create_event') {
my $i = 0;
foreach (@ARGV) {
my $line = $_;
2017-06-21 14:34:06 +02:00
#-----------DEBUG----------------------------
#print("i " . $i . " line " . $line . "\n");
2013-03-12 18:53:25 +01:00
if ($line eq '-agent') {
2017-06-21 14:34:06 +02:00
$id_agent = $ARGV[$i + 1];
2021-05-18 15:27:45 +02:00
# If not defined, send 0 for API.
if ($id_agent eq undef) {
$id_agent = '0';
}
2013-03-12 18:53:25 +01:00
}
2017-06-21 14:34:06 +02:00
if ($line eq '-group') {
$id_group = $ARGV[$i + 1];
}
if ($line eq '-name') {
2021-05-18 10:25:45 +02:00
$event_name = join('%23', split("#", $ARGV[$i + 1]));
2017-06-21 14:34:06 +02:00
}
if ($line eq '-type') {
$event_type = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
if ($line eq '-user') {
2017-06-21 14:34:06 +02:00
$id_user = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
if ($line eq '-status') {
2017-06-21 14:34:06 +02:00
$status = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
if ($line eq '-am') {
2017-06-21 14:34:06 +02:00
$id_agent_module = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
if ($line eq '-alert') {
2017-06-21 14:34:06 +02:00
$id_alert_am = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
2017-06-21 14:34:06 +02:00
if ($line eq '-severity') {
$severity = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
if ($line eq '-tag') {
2017-06-21 14:34:06 +02:00
$tags = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
if ($line eq '-source') {
2017-06-21 14:34:06 +02:00
$source = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
if ($line eq '-c_instructions') {
2017-06-21 14:34:06 +02:00
$critical_instructions = $ARGV[$i + 1];
$critical_instructions = uri_encode($critical_instructions);
2013-03-12 18:53:25 +01:00
}
if ($line eq '-w_instructions') {
2017-06-21 14:34:06 +02:00
$warning_instructions = $ARGV[$i + 1];
$warning_instructions = uri_encode($warning_instructions);
2013-03-12 18:53:25 +01:00
}
if ($line eq '-u_instructions') {
2017-06-21 14:34:06 +02:00
$unknown_instructions = $ARGV[$i + 1];
$unknown_instructions = uri_encode($unknown_instructions);
}
if ($line eq '-id_extra') {
$id_extra = $ARGV[$i + 1];
}
if ($line eq '-agent_name') {
$agent_name = $ARGV[$i + 1];
}
if ($line eq '-force_create_agent') {
$force_create_agent = $ARGV[$i + 1];
}
if ($line eq '-user_comment') {
$user_comment = $ARGV[$i + 1];
}
if ($line eq '-owner_user') {
$owner_user = $ARGV[$i + 1];
2013-03-12 18:53:25 +01:00
}
2017-06-21 14:34:06 +02:00
if ($line eq '-custom_data') {
$custom_data = $ARGV[$i + 1];
$custom_data = encode_base64($custom_data, '');
2013-03-12 18:53:25 +01:00
}
2017-06-21 14:34:06 +02:00
if ($line eq '-server_id') {
$server_id = $ARGV[$i + 1];
}
2021-05-19 12:40:06 +02:00
if ($line eq '-separator') {
$separator = $ARGV[$i + 1];
}
2017-06-21 14:34:06 +02:00
2013-03-12 18:53:25 +01:00
$i++;
}
2014-01-15 11:23:01 +01:00
2017-06-21 14:34:06 +02:00
if ($event_name eq "") {
2021-05-18 10:25:45 +02:00
print "[ERROR] Missing event name! Read help info:\n\n";
2017-06-21 14:34:06 +02:00
help_screen ();
}
if ($id_group eq "") {
print "[ERROR] Missing event group! Read help info:\n\n";
help_screen ();
}
$data_event = $event_name .
2021-05-19 12:40:06 +02:00
$separator . $id_group .
$separator . $id_agent .
$separator . $status .
$separator . $id_user .
$separator . $event_type .
$separator . $severity .
$separator . $id_agent_module .
$separator . $id_alert_am .
$separator . $critical_instructions .
$separator . $warning_instructions .
$separator . $unknown_instructions .
$separator . $user_comment .
$separator . $owner_user .
$separator . $source .
$separator . $tags .
$separator . $custom_data .
$separator . $server_id .
$separator . $id_extra .
$separator . $agent_name .
$separator . $force_create_agent;
2013-03-12 18:53:25 +01:00
2017-06-21 14:34:06 +02:00
$call_api = $api_path . '?' .
'op=set&' .
'op2=create_event&' .
'other=' . $data_event .'&' .
2021-05-19 12:40:06 +02:00
'other_mode=url_encode_separator_'.$separator.'&' .
2017-06-21 14:34:06 +02:00
'apipass=' . $api_pass . '&' .
'user=' . $db_user . '&' .
'pass=' . $db_pass;
}
elsif ($ARGV[4] eq '-validate_event') {
#~ id event(required)
2013-03-12 18:53:25 +01:00
if ($ARGV[5] ne '-id') {
print "[ERROR] Missing id event! Read help info:\n\n";
help_screen ();
2017-06-21 14:34:06 +02:00
}
else {
2013-03-12 18:53:25 +01:00
$id_event = $ARGV[6];
}
$call_api = $api_path.'?op=set&op2=validate_event_by_id&id='.$id_event.'&apipass='.$api_pass.'&user='.$db_user.'&pass='.$db_pass;
}
my @args = @ARGV;
2017-06-21 14:34:06 +02:00
my $ltotal=$#args;
2013-03-12 18:53:25 +01:00
if ($ltotal < 0) {
print "[ERROR] No valid arguments. Read help info:\n\n";
help_screen ();
exit;
}
else {
2021-05-18 10:25:45 +02:00
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
my $content = $ua->get($call_api);
2013-03-12 18:53:25 +01:00
if ($option eq '-create_event') {
if ($content eq undef) {
print "[ERROR] Not respond or bad syntax. Read help info:\n\n";
help_screen();
2017-06-21 14:34:06 +02:00
}
else {
2021-05-18 10:25:45 +02:00
print "Event ID: $content->{'_content'}";
2013-03-12 18:53:25 +01:00
}
2017-06-21 14:34:06 +02:00
}
elsif ($option eq '-validate_event') {
2013-03-12 18:53:25 +01:00
print "[RESULT] $content";
}
}
2017-06-21 14:34:06 +02:00
print "\nExiting!\n\n";
exit;
2013-03-12 18:53:25 +01:00
}