Skip to content

Instantly share code, notes, and snippets.

@vikas17a
Created September 2, 2015 16:03
Show Gist options
  • Save vikas17a/c2ba0410570b2d4730d7 to your computer and use it in GitHub Desktop.
Save vikas17a/c2ba0410570b2d4730d7 to your computer and use it in GitHub Desktop.
Nagios plugin for check last updated ami

#Setting up nagios plugin for ami check

  • copy check_ami.py to your nagios plugin directory
  • give executable permission check_ami.py
  • copy config file in you nagios plugin directory add your id and key
  • change command file and add new command
define command{
    command_name    check_ami
    command_line    $USER1$/check_ami.py $ARG1$ $ARG2$ "$ARG3$"
}
  • add service in your host configuration or service configuration
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       mylocalhost
        service_description             check ami
        check_command                   check_ami!mycentos!7!us-east-1
        }
  • Restart nagios
[AUTH]
ID: XXXX--XXXXXX-XXXX
KEY: X--X--X-X_X_X_X_X_X_X_X_X_X_
#!/usr/bin/python
import os
import sys
import json
import boto
import boto.ec2
import datetime
import ConfigParser
config = ConfigParser.ConfigParser()
if os.path.exists('./config'):
config.read('./config')
else:
print '[!] config file not found. Exiting..'
os._exit(1)
expected_ami = sys.argv[1]
expected_days = sys.argv[2]
region = sys.argv[3]
RESPONSE='UNKNOWN'
AWS_ACCESS_KEY_ID = config.get('AUTH', 'ID')
AWS_SECRET_ACCESS_KEY = config.get('AUTH', 'KEY')
date_of_creation = ''
#Get all regions in ec2
try:
con = boto.ec2.connect_to_region(region, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
images_list = con.get_all_images(owners='self')
for image in images_list:
try:
name = image.__dict__['name']
date = image.__dict__['creationDate']
new_format = "%Y-%m-%d"
if expected_ami in name:
date = datetime.datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%fZ")
date_of_creation = date.strftime(new_format)
expected_date = date + datetime.timedelta(days=int(expected_days))
currdate = datetime.datetime.now().strftime(new_format)
if currdate > expected_date.strftime(new_format):
RESPONSE='CRITICAL'
break
else:
RESPONSE='OK'
break
else:
pass
except Exception as e:
pass
except:
pass
print RESPONSE + ' - ' + date_of_creation + ' last date of updated ami'
if RESPONSE == 'OK':
sys.exit(0)
elif RESPONSE == 'CRITICAL':
sys.exit(2)
else:
sys.exit(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment