Skip to content

Instantly share code, notes, and snippets.

@ryancurrah
Last active September 25, 2015 02:54
Show Gist options
  • Save ryancurrah/aca29f03437925c207de to your computer and use it in GitHub Desktop.
Save ryancurrah/aca29f03437925c207de to your computer and use it in GitHub Desktop.
sensu-event-report
__author__ = 'ryan currah'
import requests
from argparse import ArgumentParser
import csv
import logging
logging.basicConfig(filename=__file__.replace('.py', '.log'),
format='%(asctime)s %(levelname)s: %(message)s',
level=logging.DEBUG)
def main():
parser = ArgumentParser()
parser.add_argument('--api-address', help='Sensu API Hostname or Address '
'for example "http://sensuapi.acme.com:4567"',
required=True)
parser.add_argument('--check-name', help='Name of the Sensu check', required=True)
parser.add_argument('--check-status', help='Integer status of the check to collect', type=int,
required=True)
parser.add_argument('--out-filename', help='Name of the CSV file output', required=True)
args = parser.parse_args()
sensu_events = get_sensu_events(args.api_address, args.check_name, args.check_status)
save_csv_file(sensu_events, args.out_filename)
return
def get_sensu_events(api_address, check_name, check_status):
logging.info('Getting Sensu events...')
events = get_events(api_address)
event_header = []
event_list = []
for event in events:
create_header = True if not event_header else False
temp_list = []
try:
event_status = int(event['check']['status'])
except ValueError:
event_status = -1
if event['check']['name'] == check_name and event_status == check_status:
for key, value in event.iteritems():
if key == 'client' or key == 'check':
for k, v in value.iteritems():
if create_header:
event_header.append(k)
temp_list.append(v)
else:
if create_header:
event_header.append(key)
temp_list.append(value)
if create_header:
event_list.append(event_header)
event_list.append(temp_list)
return event_list
def get_events(api_address):
return requests.get('{0}/events'.format(api_address)).json()
def save_csv_file(csv_data, filename):
with open(filename, 'wb') as f:
writer = csv.writer(f)
writer.writerows(csv_data)
return
if __name__ == '__main__':
main()
@ryancurrah
Copy link
Author

Usage:

python sensu-event-report.py --api-address http://sensuapi.acme.com:4567 --check-name check_linux_process_cvd --check-status 2 --out-filename report.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment