"Fossies" - the Fresh Open Source Software Archive

Member "unix/plugins/grep_snmptrapd" (15 Sep 2021, 6396 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.

    1 #!/usr/bin/perl
    2 ###############################################################################
    3 #
    4 # Copyright (c) 2018  Artica Soluciones Tecnologicas S.L.
    5 #
    6 # grep_log  Perl script to search log files for a matching pattern. The last
    7 #           searched position is saved in an index file so that consecutive
    8 #           runs do not return the same results. The log file inode number is
    9 #           also saved to detect log rotation.
   10 #
   11 # This program is free software; you can redistribute it and/or modify
   12 # it under the terms of the GNU General Public License as published by
   13 # the Free Software Foundation; version 2 of the License.
   14 #
   15 # This program is distributed in the hope that it will be useful,
   16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18 # GNU General Public License for more details.  
   19 #
   20 ###############################################################################
   21 use strict;
   22 use File::Basename;
   23 use IO::Compress::Zip qw(zip $ZipError);
   24 use MIME::Base64;
   25 
   26 # Be verbose
   27 my $Verbose = 0;
   28 
   29 # Index file storage directory, with a trailing '/'
   30 my $Idx_dir=($^O =~ /win/i)?'.\\':'/tmp/';
   31 
   32 # Log file
   33 my $Log_file = '';
   34 
   35 # Index file
   36 my $Idx_file = '';
   37 
   38 # Log file position index
   39 my $Idx_pos = 0;
   40 
   41 # Log file inode number
   42 my $Idx_ino = '';
   43 
   44 # Log file size
   45 my $Idx_size = 0;
   46 
   47 ###############################################################################
   48 # SUB error_msg
   49 # Print an error message and exit.
   50 ###############################################################################
   51 sub error_msg ($) {
   52     my $err_msg = $_[0];
   53 
   54     if (! -z $err_msg) {
   55         print(STDERR "[error] $err_msg.\n");
   56     }
   57 
   58     exit 1;
   59 }
   60 
   61 ###############################################################################
   62 # SUB print_help
   63 # Print a help message.
   64 ###############################################################################
   65 sub print_help () {
   66     print "Usage: $0 <snmptrapd log file>\n";
   67 }
   68 
   69 ###############################################################################
   70 # SUB log_msg
   71 # Print a log message.
   72 ###############################################################################
   73 sub log_msg ($) {
   74     my $log_msg = $_[0];
   75 
   76     if (! -z $log_msg && $Verbose == 1) {
   77         print(STDOUT "[log] $log_msg.\n");
   78     }
   79 }
   80 
   81 ###############################################################################
   82 # SUB load_idx
   83 # Load index file.
   84 ###############################################################################
   85 sub load_idx () {
   86     my $line;
   87     my $current_ino;
   88     my $current_size;
   89 
   90     log_msg("Loading index file $Idx_file");
   91 
   92     open(IDXFILE, $Idx_file) || error_msg("Error opening file $Idx_file: " .
   93                                              $!);
   94 
   95     # Read position and date
   96     $line = <IDXFILE>;
   97     ($Idx_pos, $Idx_ino, $Idx_size) = split(' ', $line);
   98 
   99     close(IDXFILE);
  100 
  101     # Reset the file index if the file has changed
  102     $current_ino = (stat($Log_file))[1];
  103     $current_size = -s "$Log_file";
  104     if ($current_ino != $Idx_ino || $current_size < $Idx_size) {
  105         log_msg("File changed, resetting index");
  106 
  107         $Idx_pos = 0;
  108         $Idx_ino = $current_ino;
  109     }
  110     $Idx_size = $current_size;
  111 
  112     return;
  113 }
  114 
  115 ###############################################################################
  116 # SUB save_idx
  117 # Save index file.
  118 ###############################################################################
  119 sub save_idx () {
  120 
  121     log_msg("Saving index file $Idx_file");
  122 
  123     open(IDXFILE, "> $Idx_file") || error_msg("Error opening file $Idx_file: "
  124                                               . $!);
  125     print (IDXFILE $Idx_pos . " " . $Idx_ino . " " . $Idx_size);
  126     close(IDXFILE);
  127 
  128     return;
  129 }
  130 
  131 ###############################################################################
  132 # SUB create_idx
  133 # Create index file.
  134 ###############################################################################
  135 sub create_idx () {
  136     my $first_line;
  137 
  138     log_msg("Creating index file $Idx_file");
  139 
  140     open(LOGFILE, $Log_file) || error_msg("Error opening file $Log_file: " .
  141                                          $!);
  142 
  143     # Go to EOF and save the position
  144     seek(LOGFILE, 0, 2);
  145     $Idx_pos = tell(LOGFILE);
  146 
  147     close(LOGFILE);
  148 
  149     # Save the file inode number
  150     $Idx_ino = (stat($Log_file))[1];
  151 
  152     # Save the index file
  153     save_idx();
  154 
  155     return;
  156 }
  157 
  158 ###############################################################################
  159 # SUB parse_log
  160 # Parse log file starting from position $Idx_pos.
  161 ###############################################################################
  162 sub parse_log () {
  163     my $line;
  164 
  165     log_msg("Parsing log file $Log_file");
  166 
  167     # Open log file for reading
  168     open(LOGFILE, $Log_file) || error_msg("Error opening file $Log_file: " .
  169                                          $!);
  170 
  171     # Go to starting position.
  172     seek(LOGFILE, $Idx_pos, 0);
  173 
  174     # Parse log file
  175     my $data;
  176 
  177     # Matched line id
  178     my $matched_line = 0;
  179 
  180     $/ = undef;
  181     $data = <LOGFILE>;
  182 
  183     $Idx_pos = tell(LOGFILE);
  184     close(LOGFILE);
  185 
  186     # Save the index file
  187     save_idx();
  188 
  189     return \$data;
  190 }
  191 
  192 ###############################################################################
  193 # SUB parse_log
  194 # Print log data to STDOUT.
  195 ###############################################################################
  196 sub print_log ($) {
  197     my $data = shift;
  198     my $zdata;
  199 
  200     return unless defined($data) and $$data ne '';
  201 
  202     if (!zip($data => \$zdata)) {
  203         error_msg("Compression error: $ZipError");
  204         return;
  205     }
  206 
  207     print STDOUT "<trap_data><![CDATA[" . encode_base64($zdata, '') . "]]></trap_data>\n";
  208 }
  209 
  210 ###############################################################################
  211 ###############################################################################
  212 ## Main
  213 ###############################################################################
  214 ###############################################################################
  215 
  216 # Check command line parameters
  217 if ($#ARGV < 0) {
  218     print_help();
  219     exit 1;
  220 }
  221 
  222 $Log_file     = $ARGV[0];
  223 # Create index file storage directory
  224 if ( ! -d $Idx_dir) {
  225     mkdir($Idx_dir) || error_msg("Error creating directory $Idx_dir: "
  226                                  . $!);
  227 }
  228 
  229 # Check that log file exists
  230 if (! -e $Log_file) {
  231     error_msg("File $Log_file does not exist");
  232 }
  233 
  234 # Create index file if it does not exist
  235 $Idx_file=$Idx_dir . basename($Log_file) . ".idx";
  236 if (! -e $Idx_file) {
  237     create_idx();
  238     exit 0;
  239 }
  240 
  241 # Load index file
  242 load_idx();
  243 
  244 # Parse log file
  245 my $data = parse_log();
  246 
  247 # Print output to STDOUT
  248 print_log ($data);
  249 
  250 exit 0;