check_file_age

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
fsodah
Posts: 295
Joined: Thu Sep 12, 2019 1:19 am

check_file_age

Post by fsodah »

how can i use it, and can i add check_file_age as command and run it into my server to check multiple linux servers?
fsodah
Posts: 295
Joined: Thu Sep 12, 2019 1:19 am

Re: check_file_age

Post by fsodah »

You do not have the required permissions to view the files attached to this post.
fsodah
Posts: 295
Joined: Thu Sep 12, 2019 1:19 am

Re: check_file_age

Post by fsodah »

see from command line it works
You do not have the required permissions to view the files attached to this post.
fsodah
Posts: 295
Joined: Thu Sep 12, 2019 1:19 am

Re: check_file_age

Post by fsodah »

Code: Select all

#!/bin/perl -w

# check_file_age.pl Copyright (C) 2003 Steven Grimm <[email protected]>
#
# Checks a file's size and modification time to make sure it's not empty
# and that it's sufficiently recent.
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
# Boston, MA 02110-1301, USA

use strict;
use English;
######
use File::Glob ':glob';
######
use Getopt::Long;
use File::stat;
use File::Basename;
use vars qw($PROGNAME);
use FindBin;
use lib "$FindBin::Bin";
use lib '/usr/local/nagios/libexec';
use utils qw (%ERRORS &print_revision &support);

sub print_help ();
sub print_usage ();

my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
my ($result, $message, $age, $size, $st, $perfdata, $output, @filelist, $filename, $safe_filename, $counter, $summary, $high_water_mark, $this_level, $this_result);

$PROGNAME="check_file_age";

$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';

$opt_w = 240;
$opt_c = 600;
$opt_W = 0;
$opt_C = 0;
$opt_f = "";

Getopt::Long::Configure('bundling');
GetOptions(
        "V"   => \$opt_V, "version"     => \$opt_V,
        "h"   => \$opt_h, "help"        => \$opt_h,
        "i"   => \$opt_i, "ignore-missing"      => \$opt_i,
        "f=s" => \$opt_f, "file"        => \$opt_f,
        "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
        "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
        "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
        "C=f" => \$opt_C, "critical-size=f" => \$opt_C);

if ($opt_V) {
        print_revision($PROGNAME, '2.3.3');
        exit $ERRORS{'OK'};
}

if ($opt_h) {
        print_help();
        exit $ERRORS{'OK'};
}

$opt_f = shift unless ($opt_f);

if (! $opt_f) {
        print "FILE_AGE UNKNOWN: No file specified\n";
        exit $ERRORS{'UNKNOWN'};
}

####
$opt_f = '"' . $opt_f . '"' if $opt_f =~ / /;
####
#$opt_f = '' . $opt_f . '' if $opt_f =~ / /;
#$opt_f = '' . $opt_f . '';

# Check that file(s) exists (can be directory or link)
$perfdata = "";
$output = "";
####################
#@filelist = glob($opt_f);
################
@filelist = bsd_glob($opt_f);
$counter = 0;
$high_water_mark = 0;

$result = "OK";
foreach $filename (@filelist) {
        unless (-e $filename) {
                if ($opt_i) {
                        if ($output) {
                                $output = $output . "\n";
                        }
                        $output = $output . "FILE_AGE OK: $filename doesn't exist, but ignore-missing was set\n";
                        $this_result = "OK";
                        $this_level = 0;

                } else {
                        if ($output) {
                                $output = $output . "\n";
                        }
                        $output = $output . "FILE_AGE CRITICAL: File not found - $filename\n";
                        $this_result = "CRITICAL";
                        $this_level = 2;
                }

                if ($high_water_mark < $this_level) {
                        $high_water_mark = $this_level;
                        $counter = 1;
                        $result = $this_result;
                }
                elsif($high_water_mark = $this_level) {
                        $counter = $counter + 1;
                }
                next;
        }

        $st = File::stat::stat($filename);
        $age = time - $st->mtime;
        $size = $st->size;
        if (scalar @filelist == 1) {
                $perfdata = $perfdata . "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0 ";
        }
        else {
                $safe_filename = basename($filename);
                $safe_filename =~ s/[='"]/_/g;
                $perfdata = $perfdata . "${safe_filename}_age=${age}s;${opt_w};${opt_c} ${safe_filename}_size=${size}B;${opt_W};${opt_C};0 ";
        }

        $this_result = 'OK';
        $this_level = 0;

        if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
                $this_result = 'CRITICAL';
                $this_level = 2;
        }
        elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
                $this_result = 'WARNING';
                $this_level = 1;
        }

        if ($high_water_mark < $this_level) {
                $high_water_mark = $this_level;
                $counter = 1;
                $result = $this_result;
        }
        elsif ($high_water_mark == $this_level) {
                $counter = $counter + 1;
        }

        if ($output) {
                $output = $output . "\n";
        }
        $output = $output . "FILE_AGE $this_result: $filename is $age seconds old and $size bytes ";
}

$summary = "$result: $counter files are $result";

if (scalar @filelist == 1) {
        print "$output | $perfdata \n";
}
else {
        print "$summary \n$output | $perfdata \n";
}


exit $ERRORS{$result};

sub print_usage () {
        print "Usage:\n";
        print "  $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
        print "  $PROGNAME [-h | --help]\n";
        print "  $PROGNAME [-V | --version]\n";
}

sub print_help () {
        print_revision($PROGNAME, '2.3.3');
        print "Copyright (c) 2003 Steven Grimm\n\n";
        print_usage();
        print "\n";
        print "  -i | --ignore-missing :  return OK if the file does not exist\n";
        print "  <secs>  File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
        print "  <size>  File must be at least this many bytes long (default: crit 0 bytes)\n";
        print "\n";
        support();
}

User avatar
jbrunkow
Posts: 441
Joined: Fri Mar 13, 2020 10:45 am

Re: check_file_age

Post by jbrunkow »

Can you try configuring the commands without using arguments? That way we can at least tell if that's part of the issue or not.

What happens if you try to submit the command as the nagios user? It is possible that the application does not have enough permissions to that file.

Code: Select all

su nagios
/usr/local/nagios/libexec/check_file_age -w 500 -c 6000 -f /home/pons/file.txt
ls -l /usr/local/nagios/libexec/check_file_age /home/pons/file.txt
What is the result when you try to use the Run Check Command from the Service Management page? Do you get any specific error messages?

It never hurts to send ( PM ) us a profile either! We can find lots of information about your system in there. You can download the profile by navigating to System Profile under System Config on the left pane > and clicking the Download Profile button on that page.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
fsodah
Posts: 295
Joined: Thu Sep 12, 2019 1:19 am

Re: check_file_age

Post by fsodah »

As i said before from the server command line it works ... when i call the same from the GUI it says file does not exist.
I tried also to adjust the script ... but it didnt work by searching google found many links talking about the perl script adjustment, but still not working.

https://github.com/pmkane/nagios-plugin ... d347d0fcb0
User avatar
jbrunkow
Posts: 441
Joined: Fri Mar 13, 2020 10:45 am

Re: check_file_age

Post by jbrunkow »

Yes, but when you ran it from the command line you did so as root. The GUI uses the nagios user. Therefore, it would be a more accurate test to run it as nagios instead of root. This could help us determine if you are encountering a permissions issue or not.

I would also like to reiterate that it would help us if you could send us the output from those commands or a profile.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
fsodah
Posts: 295
Joined: Thu Sep 12, 2019 1:19 am

Re: check_file_age

Post by fsodah »

You are right from nagios is not working

>>>I would also like to reiterate that it would help us if you could send us the output from those commands or a profile.
What else do you want me to send?
You do not have the required permissions to view the files attached to this post.
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: check_file_age

Post by scottwilkerson »

fsodah wrote:What else do you want me to send?

Code: Select all

ls -l /usr/local/nagios/libexec/check_file_age /home/pons/file.txt
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
fsodah
Posts: 295
Joined: Thu Sep 12, 2019 1:19 am

Re: check_file_age

Post by fsodah »

Code: Select all

[nagios@nagios ~]$  /usr/local/nagios/libexec/check_file_age -w 5000 -c 6000  -f /home/pons/file.txt
FILE_AGE CRITICAL: File not found - /home/pons/file.txt
 |  
[nagios@nagios ~]$ ls -l /usr/local/nagios/libexec/check_file_age /home/pons/file.txt
ls: cannot access /home/pons/file.txt: Permission denied
-rwxrwxr-x 1 apache nagios 5255 Aug 24 14:14 /usr/local/nagios/libexec/check_file_age
[nagios@nagios ~]$ 
Locked