"Fossies" - the Fresh Open Source Software Archive

Member "unix/pandora_revent" (15 Sep 2021, 10171 Bytes) of package /linux/misc/pandorafms_agent_unix-7.0NG.757.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "pandora_revent": 7.0NG.754_vs_7.0NG.755.

    1 #!/usr/bin/perl
    2 
    3 ########################################################################
    4 # Pandora FMS - Remote Event Tool (via WEB API) 
    5 ########################################################################
    6 # Copyright (c) 2021 Artica Soluciones Tecnologicas S.L
    7 #
    8 # This program is free software; you can redistribute it and/or
    9 # modify it under the terms of the GNU General Public License version 2
   10 ########################################################################
   11 
   12 # Includes list
   13 use strict;
   14 use LWP::Simple;
   15 use LWP::UserAgent;
   16 use MIME::Base64;
   17 use lib '/usr/lib/perl5';
   18 use PandoraFMS::Tools;
   19 
   20 # Init
   21 tool_api_init();
   22 
   23 # Main
   24 tool_api_main();
   25 
   26 ########################################################################
   27 # Print a help screen and exit.
   28 ########################################################################
   29 sub help_screen{
   30 
   31     print "Options to create event: 
   32 
   33 \t$0 -p <path_to_consoleAPI> -u <credentials> -create_event <options> 
   34 
   35 Where options:\n
   36     -u <credentials>   : API credentials separated by comma: <api_pass>,<user>,<pass>
   37     -name <event_name> : Free text (surrounded by single-quotes, for security reasons)
   38     -group <id_group>  : Group ID (use 0 for 'all') 
   39     
   40 Optional parameters:
   41 
   42     [-agent <id_agent] : Agent ID
   43     [-status <status>] : 0 New, 1 Validated, 2 In process
   44     [-user <id_user>] : User comment (use in combination with -comment option)
   45     [-type <event_type>] : unknown, alert_fired, alert_recovered, alert_ceased
   46                                   alert_manual_validation, system, error, new_agent
   47                                   configuration_change, going_unknown, going_down_critical,
   48                                   going_down_warning, going_up_normal
   49     [-severity <severity>] : 0 Maintance,
   50                                   1 Informative,
   51                                   2 Normal,
   52                                   3 Warning,
   53                                   4 Crit,
   54                                   5 Minor,
   55                                   6 Major
   56     [-am <id_agent_module>] : ID Agent Module linked to event
   57     [-alert <id_alert_am>] : ID Alert Module linked to event
   58     [-c_instructions <critical_instructions>]
   59     [-w_instructions <warning_instructions>]
   60     [-u_instructions <unknown_instructions>]
   61     [-user_comment <comment>] : Free text  (surrounded by single-quotes, for security reasons) 
   62     [-owner_user <owner event>] : Use the login name, not the descriptive
   63     [-source <source>] : (By default 'Pandora')
   64     [-tag <tags>] : Tag (must exist in the system to be imported)
   65     [-custom_data <custom_data>]: Custom data has to be in JSON format. Example: -custom_data \'{\"test1\" : \"t1\", \"test2\": \"2\"}\'
   66     [-id_extra <id extra>] : Id extra
   67     [-agent_name <Agent name>] : Agent name, Not to be confused with the alias.
   68     [-force_create_agent<0 o 1>] : Force the creation of agent through an event this will create when it is 1.
   69     [-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
   70     [-server_id <server_id>] : The pandora node server_id\n\n";
   71     
   72     print "Example of event generation:\n\n";
   73     
   74     print "\t./pandora_revent.pl -p http://localhost/pandora_console/include/api.php -u 1234,admin,pandora \
   75     \t-create_event -name \"SampleEvent\" -group 2 -agent 189 -status 0 -user \"admin\" -type \"system\" \
   76     \t-severity 3 -am 0 -alert 9 -c_instructions \"Critical instructions\" -w_instructions \"Warning instructions\" \
   77     \t-u_instructions \"Unknown instructions\" -id_extra \"id extra\" -agent_name \"agent name\" -source \"Commandline\" -tag \"Tags\"";
   78     
   79     print "\n\n\nOptions to validate event: \n\n\t";
   80     print "$0 -p <path_to_consoleAPI> -u <credentials> -validate_event <options> -id <id_event>\n\n";
   81     print "Sample of event validation: \n\n\t";
   82     
   83     print "$0 -p http://localhost/pandora/include/api.php -u pot12,admin,pandora -validate_event -id 234";
   84     print "\n\n\n";
   85     exit;
   86 }
   87 
   88 ##############################################################################
   89 # Init screen
   90 ##############################################################################
   91 sub tool_api_init () {
   92     
   93     print "\nPandora FMS Remote Event Tool Copyright (c) 2013-2021 Artica ST\n";
   94     print "This program is Free Software, licensed under the terms of GPL License v2\n";
   95     print "You can download latest versions and documentation at http://www.pandorafms.org\n\n";
   96     
   97     if ($#ARGV < 0) {
   98         help_screen();
   99     }
  100     
  101     if (($ARGV[0] eq '-h') || ($ARGV[0] eq '-help')) {
  102         help_screen();
  103     }
  104     
  105 }
  106 
  107 ########################################################################
  108 ########################################################################
  109 # MAIN
  110 ########################################################################
  111 ########################################################################
  112 
  113 sub tool_api_main () {
  114     
  115     my $api_path;
  116     my $event_name;
  117     my $id_group;
  118     my $event_type;
  119     my $data_event;
  120     my $credentials;
  121     my $api_pass;
  122     my $db_user;
  123     my $db_pass;
  124     my @db_info;
  125     my $id_agent = '0';
  126     my $id_user = '';
  127     my $status = '';
  128     my $id_agent_module = '';
  129     my $id_alert_am = '';
  130     my $severity = '';
  131     my $user_comment = '';
  132     my $tags = '';
  133     my $source = '';
  134     my $critical_instructions = '';
  135     my $warning_instructions = '';
  136     my $unknown_instructions = '';
  137     my $id_extra = '';
  138     my $agent_name = '';
  139     my $force_create_agent = 0;
  140     my $owner_user = '';
  141     my $id_event;
  142     my $option = $ARGV[4];
  143     my $call_api;
  144     my $custom_data = "";
  145     my $server_id = 0;
  146     my $separator = '|';
  147     
  148     #~ help or api path (required)
  149     if ($ARGV[0] eq '-h') {
  150         print "HELP!\n";
  151         help_screen();
  152     }
  153     elsif ($ARGV[0] ne '-p') {
  154         print "[ERROR] Missing API path! Read help info:\n\n";
  155         help_screen ();
  156     }
  157     else {
  158         $api_path = $ARGV[1];
  159     }
  160     
  161     #~ credentials of database
  162     if ($ARGV[2] eq '-u') {
  163         $credentials = $ARGV[3];
  164         @db_info = split(',', $credentials);
  165         
  166         if ($#db_info < 2) {
  167             print "[ERROR] Invalid database credentials! Read help info:\n\n";
  168             help_screen();
  169         }
  170         else {
  171             $api_pass = $db_info[0];
  172             $db_user = $db_info[1];
  173             $db_pass = $db_info[2];
  174         }
  175     }
  176     else {
  177         print "[ERROR] Missing database credentials! Read help info:\n\n";
  178         help_screen ();
  179     }
  180     
  181     if ($ARGV[4] eq '-create_event') {
  182         my $i = 0;
  183         foreach (@ARGV) {
  184             my $line = $_;
  185             
  186             #-----------DEBUG----------------------------
  187             #print("i " . $i .  " line " . $line . "\n");
  188             
  189             if ($line eq '-agent') {
  190                 $id_agent = $ARGV[$i + 1];
  191                 # If not defined, send 0 for API.
  192                 if ($id_agent eq undef) {
  193                     $id_agent = '0';
  194                 }
  195 
  196             }
  197             if ($line eq '-group') {
  198                 $id_group = $ARGV[$i + 1];
  199             }
  200             if ($line eq '-name') {
  201                 $event_name = $ARGV[$i + 1];
  202                 $event_name =~ s/#/%23/g;
  203             }
  204             if ($line eq '-type') {
  205                 $event_type = $ARGV[$i + 1];
  206             }
  207             if ($line eq '-user') {
  208                 $id_user = $ARGV[$i + 1];
  209             }
  210             if ($line eq '-status') {
  211                 $status = $ARGV[$i + 1];
  212             }
  213             if ($line eq '-am') {
  214                 $id_agent_module = $ARGV[$i + 1];
  215             }
  216             if ($line eq '-alert') {
  217                 $id_alert_am = $ARGV[$i + 1];
  218             }
  219             if ($line eq '-severity') {
  220                 $severity = $ARGV[$i + 1];
  221             }
  222             if ($line eq '-tag') {
  223                 $tags = $ARGV[$i + 1];
  224             }
  225             if ($line eq '-source') {
  226                 $source = $ARGV[$i + 1];
  227             }
  228             if ($line eq '-c_instructions') {
  229                 $critical_instructions = $ARGV[$i + 1];
  230                 $critical_instructions = uri_encode($critical_instructions);
  231             }
  232             if ($line eq '-w_instructions') {
  233                 $warning_instructions = $ARGV[$i + 1];
  234                 $warning_instructions = uri_encode($warning_instructions);  
  235             }
  236             if ($line eq '-u_instructions') {
  237                 $unknown_instructions = $ARGV[$i + 1];
  238                 $unknown_instructions = uri_encode($unknown_instructions);
  239             }
  240             if ($line eq '-id_extra') {
  241                 $id_extra = $ARGV[$i + 1];
  242             }
  243             if ($line eq '-agent_name') {
  244                 $agent_name = $ARGV[$i + 1];
  245             }
  246             if ($line eq '-force_create_agent') {
  247                 $force_create_agent = $ARGV[$i + 1];
  248             }
  249             if ($line eq '-user_comment') {
  250                 $user_comment = $ARGV[$i + 1];
  251             }
  252             if ($line eq '-owner_user') {
  253                 $owner_user = $ARGV[$i + 1];
  254             }
  255             if ($line eq '-custom_data') {
  256                 $custom_data = $ARGV[$i + 1];
  257                 $custom_data = encode_base64($custom_data, ''); 
  258             }
  259             if ($line eq '-server_id') {
  260                 $server_id = $ARGV[$i + 1];
  261             }
  262             if ($line eq '-separator') {
  263                 $separator = $ARGV[$i + 1];
  264             }
  265             
  266             $i++;
  267         }
  268         
  269         if ($event_name eq "") {
  270             print "[ERROR] Missing event name! Read help info:\n\n";
  271             help_screen ();
  272         }
  273         if ($id_group eq "") {
  274             print "[ERROR] Missing event group! Read help info:\n\n";
  275             help_screen ();
  276         }
  277         
  278         $data_event = $event_name .
  279             $separator . $id_group .
  280             $separator . $id_agent .
  281             $separator . $status .
  282             $separator . $id_user .
  283             $separator . $event_type .
  284             $separator . $severity .
  285             $separator . $id_agent_module .
  286             $separator . $id_alert_am . 
  287             $separator . $critical_instructions .
  288             $separator . $warning_instructions .
  289             $separator . $unknown_instructions .
  290             $separator . $user_comment .
  291             $separator . $owner_user .
  292             $separator . $source .
  293             $separator . $tags .
  294             $separator . $custom_data .
  295             $separator . $server_id .
  296             $separator . $id_extra .
  297             $separator . $agent_name .
  298             $separator . $force_create_agent;
  299 
  300         $call_api = $api_path . '?' .
  301             'op=set&' .
  302             'op2=create_event&' .
  303             'other=' . $data_event .'&' .
  304             'other_mode=url_encode_separator_'.$separator.'&' .
  305             'apipass=' . $api_pass . '&' .
  306             'user=' . $db_user . '&' .
  307             'pass=' . $db_pass;
  308         
  309     }
  310     elsif ($ARGV[4] eq '-validate_event') {
  311         #~ id event(required)
  312         if ($ARGV[5] ne '-id') {
  313             print "[ERROR] Missing id event! Read help info:\n\n";
  314             help_screen ();
  315         }
  316         else {
  317             $id_event = $ARGV[6];
  318         }
  319         
  320         $call_api = $api_path.'?op=set&op2=validate_event_by_id&id='.$id_event.'&apipass='.$api_pass.'&user='.$db_user.'&pass='.$db_pass;
  321     } 
  322     
  323     my @args = @ARGV;
  324     my $ltotal=$#args; 
  325     
  326     if ($ltotal < 0) {
  327         print "[ERROR] No valid arguments. Read help info:\n\n";
  328         help_screen ();
  329         exit;
  330     }
  331     else {
  332         my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
  333         my $content = $ua->get($call_api);
  334         
  335         if ($option eq '-create_event') {
  336             if ($content eq undef) {
  337                 print "[ERROR] Not respond or bad syntax. Read help info:\n\n";
  338                 help_screen();
  339             }
  340             else {
  341                 print "Event ID: $content->{'_content'}";
  342             }
  343         }
  344         elsif ($option eq '-validate_event') {
  345             print "[RESULT] $content";
  346         }
  347     }
  348     
  349     print "\nExiting!\n\n";
  350     
  351     exit;
  352 }