Nagios Downtime using a ServiceGroup

This is not a complete script. I was only interested in scheduling a SERVICEGROUP through the Nagios command file (NAGIOSCMD). This is for obvious reasons if you ever used Nagios in large environments. It is painful to schedule and cancel downtimes.

Since I wanted to be able to delete multiple downtime entries and I used a feature added somewhere in 3.x called DEL_DOWNTIME_BY_START_TIME_COMMENT. The cancellation of downtime is done by providing a Start date and a comment.

import sys, argparse, time
from datetime import datetime, timedelta

##  EXAMPLES
## # python nagios_downtime.py --action add --Servicegroup PRDVIPS --begin "2016-12-10 8:36" --duration 10 --author rrosso --comment "Test Scheduling Downtime in Nagios"
## [1481387741.0] SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;PRDVIPs;1481387760.0;1481388360.0;1;0;600;rrosso;Test Scheduling Downtime in Nagios
## # python nagios_downtime.py --action cancel --begin "2016-12-10 8:36" --comment="Test Scheduling Downtime in Nagios"
## [1481387769.0] DEL_DOWNTIME_BY_START_TIME_COMMENT;1481387760.0;Test Scheduling Downtime in Nagios
##

VERSION = "0.4"
VERDATE = "2016-12-10"

NAGIOSCMD =  "/usr/local/nagios/var/rw/nagios.cmd"
now = datetime.now()
cmd = '[' + str(time.mktime(now.timetuple())) + '] '
execline = ''

parser = argparse.ArgumentParser(description='Nagios Downtime Scheduler.')
parser.add_argument('--action', dest='action', help='use add or cancel as action for downtime entries', required=True)
parser.add_argument('--Servicegroup', dest='servicegroup', help ='Schedule Downtime a specific ServiceGroup')
parser.add_argument('--duration', dest='duration', help='Duration of downtime, in minutes.)
parser.add_argument('--begin', dest='begin', help='Beginning of Downtime. ex: 2016-12-10 18:10', required=True)
parser.add_argument('--author', dest='author', default='admin', help='Author: Who is scheduling the downtime?')
parser.add_argument('--comment', dest='comment', help='Comment: Reason for scheduling the downtime.', required=True)
parser.add_argument('--dryrun', action='store_true', help='Dry run.  Do not do anything but show action.')

args = parser.parse_args()

## need some argument checking here.  what is required what conflicts etc..
if (args.action not in ['add','cancel']):
  sys.exit(1)

if (args.begin != None):
  #check for proper format here...
  #beginTime = datetime.datetime(2016,12,8,13,0).strftime('%s')
  beginTime = datetime.strptime(args.begin,'%Y-%m-%d %H:%M')

if (args.action == 'add'):
  if (args.servicegroup):
    cmd = cmd + 'SCHEDULE_SERVICEGROUP_SVC_DOWNTIME;'
    endTime = beginTime + timedelta(minutes=int(args.duration))
    execline=cmd + 'PRDVIPs;' + str(time.mktime(beginTime.timetuple())) + ';' + str(time.mktime(endTime.timetuple())) + ';1;0;' + '600' +';' + args.author + ';' + args.comment + '\n'

if (args.action == 'cancel'):
  cmd = cmd + 'DEL_DOWNTIME_BY_START_TIME_COMMENT;'
  execline=cmd + str(time.mktime(beginTime.timetuple())) + ';' + args.comment + '\n'

print 'Nagios CMD interaction will be: ' + execline

if (args.dryrun):
  print "Note that this is a dry run ie so not committing transaction"
else:
  print "Note that this is not a dry run ie --dryrun was not used so committing transaction"
  f = open(NAGIOSCMD,'w')
  f.write(execline)

Nagios on Linux for SPARC

I recently experimented a little with Linux for SPARC(more here https://oss.oracle.com/projects/linux-sparc/) and found it to be surprisingly stable. One of the environments I support is a pure OVM for SPARC environment and no luxury of Linux. So I am running some open source tools like Nagios, HAproxy etc on Solaris. Nagios has worked ok but is painful to compile. There are also some bugs that cause high utilization.

I tried a Linux for SPARC instance and since they are pretty much like RedHat/Oracle/CentOS it means a fair bit of packages already exist. Nagios does not exist so I compiled it. Suffice to say installing dependencies from YUM and compiling was a breeze compared to Solaris.

You can pretty much follow this doc to the letter:
https://assets.nagios.com/downloads/nagioscore/docs/Installing_Nagios_Core_From_Source.pdf

Things to note.
1. By default the firewall does not allow inbound http.

2. If you have permission issues in the web frontend or something like Internal server error you can disable(quick test) and then configure selinux for nagios scripts.

# setenforce 0
# chcon -R -t httpd_sys_content_t /usr/local/nagios

3. Redo plugins with openssl for https checks. I wanted to do https checks.

# yum install openssl-devel
# pwd
/usr/src/nagios/nagios-plugins-2.1.1

# ./configure --with-openssl --with-nagios-user=nagios --with-nagios-group=nagios
[..]
                    --with-openssl: yes
# make
# make install

# /usr/local/nagios/libexec/check_http -H 10.2.10.33 -S -p 215 
HTTP OK: HTTP/1.1 200 OK - 2113 bytes in 0.017 second response time |time=0.016925s;;;0.000000 size=2113B;;;0

I made a https command as follow.

command.cfg
# 'check_https' command definition
define command{
        command_name    check_https
        command_line    $USER1$/check_http -H $HOSTADDRESS$ -S -p $ARG1$
        }

And referenced as follow.

storage.cfg
define service{
        use                             remote-service         ; Name of service template to use
        host_name                       zfssa1
        service_description             HTTPS
        check_command                   check_https!215
        notifications_enabled           0
        }

Nagios Email Notifications with Comments

If you prefer more detail in the email notification of Nagios here is how to add those comments. For example someone acknowledging a service and you want to see the comment they added during acknowledgement.

# pwd
/usr/local/nagios/etc/objects
# more commands.cfg
[..]
define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n\nACK Comment: $SERVICEACKCOMMENT$\n\nComment: $NOTIFICATIONCOMMENT$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$

Note: I added $SERVICEACKCOMMENT$ and $NOTIFICATIONCOMMENT$ but I think in newer versions of Nagios $SERVICEACKCOMMENT$ is deprecated and you only need $NOTIFICATIONCOMMENT$.

Link: http://nagios.sourceforge.net/docs/3_0/macrolist.html