#!/usr/bin/perl

#
# By Cyril Menigoz
# cyril.menigoz@gmail.com
#







#date --date "1 hour ago"



# imports
use strict;
use Nagios::Plugin;
use List::Util qw(first);
use DateTime;
# variables
my $shinken;
my $warn ;
my $crit;
my $statut;
my $message;
my $value;
my $w;
my $user;


# plugin setup
$shinken = Nagios::Plugin->new(
        plugin                   => 'check_access_graph',
        shortname        => 'Access activity ',
        version                => '1.0',
        usage                   => 'Usage: %s  -C <type> -w <warning>  -c <critical>   -u <user> ' ,
        blurb                    => 'This plugin checks the simultaned connection to your host ',
        license                 => 'This nagios plugin is free software, and comes with ABSOLUTELY no WARRANTY!'
);
#        usage                   => 'Usage: %s -C <check type>  [-w <warning %>] [-c <critical %>]   [ -W <website(s)>] [-I < IP filter for the web access > ] [-af <access.log file]   [-u <user>] ',


 $shinken->add_arg(spec => 'user|u=s',
                          help => " user to check",
                          required => 1);                     
                            
$shinken->add_arg(spec => 'warning|w=s',
                          help => "Warning threshold  ",
                          required => 1);
$shinken->add_arg(spec => 'critical|c=s',
                          help => "Critical threshold ",
                          required => 1);                          
               

# main
$shinken->getopts;

$warn = $shinken->opts->warning;
$crit = $shinken->opts->critical;
$user = $shinken->opts->user;
$w = `which w 2> /dev/null`;
chop $w;
if ( ! -e $w ) {
        $shinken->nagios_die("could not find ps binary!!");
        }

$value=`$w $user -h|wc -l`;

=for comment
	  print  "\naccessValues[0]=>". @accessValues[0];
	  print  "\naccessValues[1]=>". @accessValues[1];
	  print  "\naccessValues[2]=>". @accessValues[2];
	  print  "\naccessValues[3]=>". @accessValues[3];
	  print  "\naccessValues[4]=>". @accessValues[4];

=cut
		    $statut = $shinken->check_threshold
		    (
			      check => $value,
			      warning =>$warn,
			      critical => $crit,
		    );
		    $message = $value."simultaned access of ".$user ;



#Web
$shinken->add_perfdata(
	  label =>  "simultaned access of ".$user ,
	  value => $value,
	  );


$shinken->nagios_exit($statut, $message);




