check_eventdb.pl version 2.0 ------------------------------------------------------------------------------------------------------------- Table of Contents 01. Copyright 02. Change History 03. Installation and Configuration 04. A first look - --help 05. Some parameters in more detail 06. Interaction with Nagios 07. Acknowledging events from Nagios 08. Acknowledging events via events 09. Stylesheet enhancement for Netways EventDB index.php ------------------------------------------------------------------------------------------------------------- 01. Copyright ============= Copyright (C) 2009 Martin Fuerstenau Based on the script of Gerd Mueller Copyright (C) 2006 Gerd Mueller / Netways GmbH 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; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ------------------------------------------------------------------------------------------------------------- 02. Changes History =================== 2009-06-11: Martin Fuerstenau - Added the ability to check program filed from the database. It can be be delivered by all agents but wasn't used yet - Added $opt_dbuser and $opt_dbpasswd in "default values". So there is no need to add a lot of users/password in the Nagios command definition file 2009-09-25: Martin Fuerstenau Version 2 Corrections: - Code reformatting for better readability. Mainly it means putting corresponding braces and brackets in the same column - use subs qw(add_part myexit print_usage); removed from code. It was obsolete here due to the fact that all subroutines a called in a distinct manner i.e. print_usage() and NOT print_usage. print_usage would be interprated as a bareword due to use of strict. - Removed declaration of global variables because declaration of local variables (my...) works in the same way when placed at the beginning of the program and you have the benefit do place declaration, default preset ans comment in one line - $update removed because it was unused - @sql and @params replaced by $sql for some reasons: - easier and more effective to store statement in a variable than in a array and join all elements together to get a string. - easier to use a string directly in a variable than to use a sprintf and variable values seperatly - Reformatted print_usage to have help output better to read - in add_part my ($field, $value, $op, $agg) = @_; was shortened to my ($field, $value) = @_; due to the fact that $op and $agg were never passed via function call - $op and $agg now defined "presets and declarations" instead of add_part - use Config::IniFiles; removed from code because it was not used. - Replaced the myexit call in print_usage with a simple exit in the if statement at the beginning of the code because the output of myexit could be a little bit confusing to the user. - The only used SQL statement "SELECT COUNT(uid) as count from $opt_dbtable where 1" was replaced. - "as count" was absolutely not necessary because "count" as column name was used nowhere - the statement would not work in the case it should give back ID (database uid) or messagestrings. Instead of this $rowcnt (Row counter) was used to determine the number of rows in an array. - Replaced in myexit() $out .= chr(10) with $out .= "\n" for better readability. Who knows today which ASCII is what. And as a second reason \n (new line) is safer. On a Unix/Linux system it means chr(10) which is a linefeed, on a Windows system it means chr(10)/Linefeed and chr(13)/Carriage return. - $agg stored only the scalar AND for the select statement and was only used in one place. Therefore the variable was removed and the scalar was used directly in the statement. This make the statement easier to read and to maintain. - A return code of -1 in myexit() for UNKNOWN was not correct (It results in a 255 on the shell). According to the Nagios manual UNKNOWN should cause in a 3. Corrected. New functions and options: - -r|--regexp will treat messagestrings as regular expressions - -s|--show shows the found messages in Nagios. Lenght of string can be set in $message_lenght and multilineoutput can be HTML style or Nagios 3 style. See $ml_show_msg. - --acknowledged will include acknowledged entries in the select statement - --version added to print program version - Values for priority can be added as single scalar (for example emerg) or as a comma separated list (for example emerg,alert,crit) - variable §matches removed. It was replaced by $rowcnt because we let not count the database. We count the rows received from the database. - --maxage Maximum age for records use for alerts - Acting as eventhandler: In case of using check_eventdb as a event handler all three variables (--servicestate, --servicestatetype and --serviceoutput) nust be used together. Records found will be acknowledged. --servicestate Contains $SERVICESTATE$ from Nagios if started as eventhandler for acknowledging or deleting records/entries --servicestatetype Contains $SERVICESTATETYPE$ from Nagios if started as eventhandler for acknowledging records/entries --serviceoutput Contains $SERVICEOUTPUT$ from Nagios if started as eventhandler for acknowledging or deleting records/entries. $SERVICESTATE$ MUST be OK or UP, $SERVICESTATETYPE$ MUST be hard and $SERVICEOUTPUT$ should contain the output of your "submit passive check result" together with the name of the operator (for example "problem fixed - D.Duck") - --recover The messagestring following this will cause a recovery. Wildcards are allowed. If -r is set the string will be interpreted as a regular expression. -H must been set. And the message containing the recovery string must be the latest. If there are alerts newer than the recovery string only this alerts are used. --recover_regexp Interpret recoverstring as regular expression (awk/sed style). Works only with --recover set. --recover_prio The messagestring following this will contain the priority for recovery -H must been set. --recover_prog This will contain a name of a program causing the recovery in case the recovery message is not sent by the program causing the error. -H must been set. ------------------------------------------------------------------------------------------------------------- 03. Installation and Configuration Installation of check_eventdb.pl version 2 is easy. As in version 1 you must have the perl modules Getopt::Long; File::Basename; DBI; DBD-mysql must be installed. Config::IniFiles is no longer needed because it was declared in version but never used (See change history). Place the plugin in your plugin directory and ensure that it can be executed. Eventually you have to correct in line 1 the name of the perl interpreter. Second you have to adjust some variable in the code to your needs: my $opt_db = "eventdb"; # Database name my $opt_dbtable = "events"; # Table for query my $opt_dbhost = "localhost"; # Database host my $opt_dbuser = "eventdb"; # Connecting user my $opt_dbpasswd = "mypasswd"; # As it says .... If you do not set these variables you have to handle over the values from the command line. You can always overwirte these values from the commandline. my $message_lenght = 20; # As mentioned above cut messages to number # of characters If you will display the messages from the events within the nagios web interface it could be wise to cut the message strings to a predefined lenght. This has to be done here. my $ml_show_msg = '0'; # Multilinestyle of displayed messages # 0 = Nagios 3 mulitlinestype # 1 = HTML style (adds
to output This is a switch for multiline output. If you prefer to see the messages in the overview or you use a Nagios version prior to version 3.x it must be set to 1. ------------------------------------------------------------------------------------------------------------- 04. A first look - --help Usage: check_eventdb.pl [ -H|--host ] [-p|--priority ] [-m|--msg ] [--maxage ] [-r|--regexp] [-s|--show] [-t|--type ] [-a|--acknowledged] [-P|--program ] [-l|--label