Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.88 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. #Script zur Anzeige der angemeldeten Agenten und KeyUser in OTRS
  4. #Copyright (c) 2008 by Michael Glaess
  5.  
  6.  
  7. use strict;
  8. use Getopt::Long;
  9. use vars qw($opt_t $opt_q $opt_c $opt_w $opt_v $opt_h);
  10. my(%ERRORS) = ( OK=>0, WARNING=>1, CRITICAL=>2, UNKNOWN=>3, WARN=>1, CRIT=>2 );
  11. use DBI;
  12. use DBD::mysql;
  13. use Net::SMTP;
  14. sub print_help();
  15.  
  16. my $VERSION ="1.1";
  17. my $DBuser  ="nagios";
  18. my $DBpass  ="nagios";
  19. my $opt_v;
  20. my $opt_h;
  21. my $opt_w;
  22. my $opt_c;
  23. my $opt_H;
  24. my $opt_U;
  25. my $opt_P;
  26. my $status  ="0";  
  27. my $anzahl;
  28. my $anzahla;
  29. my $anzahlu;
  30. my $sql;
  31. my $result;
  32. my $message;
  33.  
  34. sub check_otrs {
  35. #Check DatabaseConnection
  36. my $dbhm=DBI->connect("dbi:mysql:otrs:$opt_H","$opt_U","$opt_P",
  37.     {
  38.     PrintError=>1,
  39.     }
  40. );
  41. unless ( $dbhm ) {
  42.     die("No Connection to Database");
  43.     }
  44. my $dbhm2=DBI->connect("dbi:mysql:otrs:$opt_H","$opt_U","$opt_P",
  45.     {
  46.     PrintError=>1,
  47.     }
  48. );
  49.  
  50.  
  51. # Anzahl Agenten
  52.     $sql = "Select count(*) as Anzahl from sessions where left(session_value,4)='User'";
  53.    
  54. my $sqlp=$dbhm->prepare($sql);
  55. if (!$sqlp->execute()){
  56.     print "CRITICAL - Unable to Execute SQL-Query";
  57.     $status = $ERRORS{'CRITICAL'};
  58.     }    
  59.     $result=$sqlp->fetchrow_hashref();
  60.     $anzahla=$result->{Anzahl};
  61.  
  62. # Anzahl KeyUser
  63.     $sql = "Select count(*) as Anzahl from sessions where left(session_value,4)<>'User'";
  64.  
  65. $sqlp=$dbhm->prepare($sql);
  66. if (!$sqlp->execute()){
  67.     print "CRITICAL - Unable to Execute SQL-Query";
  68.     $status = $ERRORS{'CRITICAL'};
  69.     }    
  70.     $result=$sqlp->fetchrow_hashref();
  71.     $anzahlu=$result->{Anzahl};
  72.  
  73.     $anzahl=$anzahla + $anzahlu;
  74. #Set Optimize Text for Nagios
  75.     $message = "Anzahl Agenten $anzahla<br>";
  76.     $message .= "Anzahl KeyUser $anzahlu<br>";
  77.     $message .= "Gesamt $anzahl";    
  78.     $message = $message." |Agenten=$anzahla;KeyUser=$anzahlu;Gesamt=$anzahl";
  79.    
  80.    
  81. if    ($anzahl >= $opt_c)
  82.     {$status = $ERRORS{'CRITICAL'};}
  83. elsif ($anzahl >= $opt_w)
  84.     {$status = $ERRORS{'WARNING'};}
  85. else { $status = $ERRORS{'OK'};}
  86.  
  87.  
  88. $sqlp->finish();
  89.  
  90. }
  91. ####################################
  92.  
  93.  
  94. sub print_help () {
  95.     printf "$0 plugin for Nagios check for count of Agents/KeyUsers loggedin in OTRS\n";
  96.     printf "Copyright (c) 2008 Michael Glaess\n";
  97.     printf "Usage:\n";
  98.     printf "   -w (--warn)   Warning-Level, Default: 0\n";
  99.     printf "   -c (--crit)   Criticle-Level, Default: 2\n";
  100.     printf "   -v            Version\n";
  101.     printf "   -h (--help)   Help\n";
  102.     printf "\n";
  103.     print_usage();    
  104. }
  105. ##############################################
  106. sub print_usage () {
  107.     print "Usage: $0 \n";
  108.     print "       $0 -w 2 -c 3\n";
  109.     print "       $0 -w 2 -c 4\n";
  110. }
  111. ###############################################
  112. $ENV{'BASH_ENV'}='';
  113. $ENV{'ENV'}='';
  114.  
  115. Getopt::Long::Configure('bundling');
  116. GetOptions
  117.     ("v" => \$opt_v, "version"      => \$opt_v,
  118.      "h" => \$opt_h, "help"         => \$opt_h,
  119.      "w:i" => \$opt_w, "warn"   => \$opt_w,
  120.      "c:i" => \$opt_c, "crit"   => \$opt_c,
  121.      "H:s" => \$opt_H, "U:s"    => \$opt_U,
  122.      "P:s" => \$opt_P );
  123.  
  124. #Set default Values
  125. if ( !$opt_w){$opt_w=1;} #Warning at just one ticket
  126. if ( !$opt_c){$opt_c=2;} #Set Critical even on two tickets
  127. if ( !$opt_H){$opt_H="localhost";}
  128. if ( !$opt_U){$opt_U=$DBuser;}
  129. if ( !$opt_P){$opt_P=$DBpass;}
  130.  
  131. #printf "OPT_t = $opt_t,OPT_w= $opt_w, OPT_c = $opt_c, OPT_q = $opt_q\n";
  132. if ($opt_v) {
  133.     print "$0: $VERSION\n" ;
  134.     exit $ERRORS{'OK'};
  135. }
  136. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  137.  
  138. $status = $ERRORS{OK}; $message = '';
  139.  
  140. #Call CheckUp Routine
  141. check_otrs;
  142.  
  143. #Give System a feedback what have we done
  144. if( $message ) {
  145.     if( $status == $ERRORS{OK} ) {
  146.         print "OK: ";
  147.     } elsif( $status == $ERRORS{WARNING} ) {
  148.         print "WARNING: ";
  149.     } elsif( $status == $ERRORS{CRITICAL} ) {
  150.         print "CRITICAL: ";
  151.     }
  152.     print "$message\n";
  153. } else {
  154.     $status = $ERRORS{UNKNOWN};
  155.     print "No Data yet\n";
  156. }
  157. exit $status;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement