Changes between Version 7 and Version 8 of OMDeventhandlers

Show
Ignore:
Timestamp:
11/14/11 14:42:33 (8 years ago)
Author:
jonmills (IP: 152.54.3.30)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OMDeventhandlers

    v7 v8  
    1010  * [http://nagios.sourceforge.net/docs/3_0/macrolist.html Nagios has lots of built-in Macros] you can use inside your Nagios configuration. 
    1111 
    12 {{{ 
    13 extra_nagios_conf += r""" 
     12 * Check out our example config file from code.renci.org SVN: 
     13  * [https://code.renci.org/gf/project/networkedclouds/scmsvn/?action=browse&path=%2Fnagios%2Ftrunk%2Feventhandlers%2Fconfig%2Feventhandlers.mk&view=markup eventhandlers.mk] 
     14  * Install this at $OMD_ROOT/etc/check_mk/conf.d/ 
    1415 
    15 # Defines an eventhandler (a Nagios command) that will run when service_description "Check_MK inventory" discovers  
    16 # new things on a host that it can monitor.  The purpose is to automatically reconfigure Check_MK 
    17 # to monitor those newly-discovered services. 
    18 define command { 
    19     command_name    cmk_reinventory 
    20     command_line    $USER4$/local/bin/cmk_reinventory.sh $HOSTNAME$ $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ 
    21 } 
    22 """ 
    23  
    24 # Map the eventhandler command to the service definition in Nagios 
    25 extra_service_conf["event_handler"] = [ 
    26         ( "cmk_reinventory", ALL_HOSTS, ["Check_MK inventory"]), 
    27 ] 
    28 # Enable eventhandlers in Nagios for the service definition 
    29 extra_service_conf["event_handler_enabled"] = [ 
    30         ( "1", ALL_HOSTS, ["Check_MK inventory"]), 
    31 ] 
    32 }}} 
    3316 
    3417=== cmk_reinventory Eventhandler script === 
    35  * This script re-writes Check_MK's configuration files, then reload Check_MK, which in turn re-compiles Nagios configuration, and reloads the Nagios daemon. 
     18 * A script that re-writes Check_MK's configuration files, then reloads Check_MK, which in turn re-compiles Nagios configuration, and reloads the Nagios daemon. 
     19 * SVN source: 
     20  * [https://code.renci.org/gf/project/networkedclouds/scmsvn/?action=browse&path=%2Fnagios%2Ftrunk%2Feventhandlers%2Fscripts%2Fcmk_reinventory.sh&view=markup cmk_reinventory.sh] 
    3621 
    37 {{{ 
    38 #!/bin/bash 
    39 # 
    40 # Event handler script for re-inventorying a host when the 
    41 # "Check_MK Inventory" check comes back telling you that there 
    42 # are unchecked services on a host. 
    4322 
    44 export PATH="/omd/sites/nagios/lib/perl5/bin:/omd/sites/nagios/local/bin:/omd/sites/nagios/bin:/omd/sites/nagios/local/lib/perl5/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/opt/local/bin:/opt/local/sbin" 
    45  
    46 touch /tmp/cmk_reinventory 
    47  
    48 # Note: The "Check_MK Inventory" check is enabled by having 
    49 # something like the following in your main.mk file:  
    50 #        
    51 #       inventory_max_cachefile_age = 120 # seconds (Default: 120 seconds) 
    52 #       inventory_check_interval = 120 # minutes 
    53 #       inventory_check_severity = 1 
    54  
    55 # What state is the "Check_MK Inventory" service in? 
    56  
    57 # Command executed by Nagios was  
    58 #       /opt/omd/site/`whoami`/local/bin/cmk_reinventory.sh $HOSTNAME$ $SERVICESTATE$ $SERVICESTATETYPE$ \ 
    59 #               $SERVICEATTEMPT$ 
    60 # 
    61 # These are bash args brought in from the command line 
    62 LOG=/tmp/cmk_reinventory 
    63 HOSTNAME=$1 
    64 SERVICESTATE=$2 
    65 SERVICESTATETYPE=$3 
    66 SERVICEATTEMPT=$4 
    67  
    68 echo "$0" > $LOG 
    69 echo `date` >> $LOG 
    70  
    71 case "$SERVICESTATE" in 
    72  
    73 OK) 
    74  
    75         # All services are checked, so don't do anything... 
    76         echo "SERVICESTATE is $SERVICESTATE" >> $LOG 
    77         echo "Run the add_vm.sh script against $HOSTNAME" >> $LOG 
    78         echo "${OMD_ROOT}/local/bin/add_vm.sh $HOSTNAME" >>$LOG 
    79         ${OMD_ROOT}/local/bin/add_vm.sh $HOSTNAME >>$LOG 
    80         ;; 
    81  
    82 WARNING) 
    83  
    84         echo "SERVICESTATE is $SERVICESTATE" >> $LOG 
    85  
    86         # Because we defined 'inventory_check_severity = 1' in main.mk, 
    87         # unchecked services will result in a warning. Fix it! 
    88  
    89         case "$SERVICESTATETYPE" in 
    90         # We're in a "soft" state, meaning that Nagios is in the middle of retrying the 
    91         # check before it turns into a "hard" state and contacts get notified... 
    92  
    93         SOFT) 
    94  
    95                 echo "SERVICESTATETYPE is $SERVICESTATETYPE" >> $LOG 
    96                 # What check attempt are we on?  We don't want to restart the web server on the first 
    97                 # check, because it may just be a fluke! 
    98  
    99                 case "$SERVICEATTEMPT" in 
    100  
    101                 # Wait until the check has been tried 3 times before executing the command. 
    102                 3) 
    103                         echo "SERVICEATTEMPT is $SERVICEATTEMPT" >> $LOG 
    104  
    105                         #Do the work 
    106                         echo "Re-inventorying the host $HOSTNAME" >> $LOG 
    107                         cmk -Iu ${HOSTNAME} >> $LOG 
    108                         echo "Reloading nagios..." >> $LOG 
    109                         cmk -O >> $LOG 
    110                         echo "Run the add_vm.sh script against $HOSTNAME" >> $LOG 
    111                         echo "${OMD_ROOT}/local/bin/add_vm.sh $HOSTNAME" >>$LOG 
    112                         ${OMD_ROOT}/local/bin/add_vm.sh $HOSTNAME >>$LOG 
    113                         ;; 
    114                         esac 
    115                 ;; 
    116  
    117         # If somehow we missed the 3rd try of the SOFT state, then we'll try again HARD 
    118         HARD) 
    119                 echo "SERVICESTATETYPE is $SERVICESTATETYPE" >> $LOG 
    120                 #Do the work 
    121                 echo "Re-inventorying the host $HOSTNAME" >> $LOG 
    122                 cmk -Iu ${HOSTNAME} >> $LOG 
    123                 echo "Reloading nagios..." >> $LOG 
    124                 cmk -O >> $LOG 
    125                 echo "Run the add_vm.sh script against $HOSTNAME" >> $LOG 
    126                 echo "${OMD_ROOT}/local/bin/add_vm.sh $HOSTNAME" >>$LOG 
    127                 ${OMD_ROOT}/local/bin/add_vm.sh $HOSTNAME >>$LOG 
    128                 ;; 
    129          
    130         esac 
    131         ;; 
    132  
    133 UNKNOWN) 
    134         # Don't do anything; we handle it in the WARNING section... 
    135         ;; 
    136  
    137 CRITICAL) 
    138  
    139         # Don't do anything; we handle it in the WARNING section... 
    140         ;; 
    141 esac 
    142  
    143 exit 0 
    144 }}} 
    145  
    146 === add_vm.sh script === 
    147  
    148  * The purpose of the add_vm.sh script is to figure out if all the KVM Virtual Machines running on a Node Controller are being monitored, and if not, to add the VM and its IP address into Check_MK's configuration. 
    149  * The script is called directly by cmk_reinventory.sh, rather than triggered by a Nagios Event.  The reason is because no such event exists to trigger this script!  When "Check_MK inventory" discovers a new type of KVM check, it brings that up as a new service in Nagios.  But because it's initial state is OK, it never triggers an event.  (Events are only triggered by a state change, and going from non-existent to OK is not a state change in Nagios' view.) 
    150  
    151 {{{ 
    152 #!/bin/bash 
    153 # 
    154 # Event handler script for re-inventorying a host when the 
    155 # "Check_MK inventory" check comes back telling you that there 
    156 # are unchecked services on a host. 
    157  
    158 export PATH="/omd/sites/nagios/lib/perl5/bin:/omd/sites/nagios/local/bin:/omd/sites/nagios/bin:/omd/sites/nagios/local/lib/perl5/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/opt/local/bin:/opt/local/sbin" 
    159  
    160 # Command executed by Nagios was  
    161 #       /opt/omd/site/`whoami`/local/bin/cmk_addhost.sh $HOSTNAME$ 
    162 # 
    163 # ${OMD_ROOT} should be an env var set by OMD itself... 
    164  
    165 HOSTNAME=$1 
    166 NODE=$(echo $HOSTNAME | awk -F. '{print $1}') 
    167 LOG=/tmp/add_vm.sh 
    168 touch $LOG 
    169  
    170 # This yields the hostnames of VMs running on $HOSTNAME 
    171 VMLIST=$(grep 'qemu' ${OMD_ROOT}/var/check_mk/autochecks/${HOSTNAME}.mk | \ 
    172         awk '{print $3}' | sed 's/'\','//' | sed 's/'\''//') 
    173 echo "VMs running on $HOSTNAME are:" > $LOG 
    174 echo $VMLIST >> $LOG 
    175  
    176 for INSTANCE in $VMLIST 
    177 do 
    178         #First, see if the INSTANCE exists in the hosts.mk file 
    179         if [ $(grep $INSTANCE ${OMD_ROOT}/etc/check_mk/conf.d/hosts.mk >/dev/null; echo $?) = 1 ]; then 
    180  
    181                 # We need to add it to the ipaddresses.mk file for fakedns 
    182                 IP=$(euca-describe-addresses | grep $INSTANCE | awk '{print $2}') 
    183                 echo "Instance $INSTANCE on $HOSTNAME has IP $IP" >> $LOG 
    184                 [ $IP ] && sed -i '/ipaddresses/a'\"''$INSTANCE'" : "'$IP'",\' ${OMD_ROOT}/etc/check_mk/conf.d/ipaddresses.mk >> $LOG 
    185  
    186                 # Add this hostname to the hosts.mk file, sorting it right below its KVM nodes's name 
    187                 [ $IP ] && sed -i '/'$HOSTNAME'/a'\'''$INSTANCE'|ping|vm|'$NODE\','\''' ${OMD_ROOT}/etc/check_mk/conf.d/hosts.mk >> $LOG         
    188  
    189                 # Now inventory the new host 
    190                 echo "cmk -Iu $INSTANCE" >> $LOG 
    191                 ${OMD_ROOT}/bin/cmk -Iu ${INSTANCE} >> $LOG 
    192         fi 
    193 done; 
    194  
    195 # Now reload Nagios 
    196 [ $INSTANCE ] && ${OMD_ROOT}/bin/cmk -O >> $LOG 
    197  
    198 exit 0 
    199 }}} 
    20023 
    20124== Removing VMs: Host Check Eventhandler ==