Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
import requests
import datetime
args = sys.argv
conf_file = args[1]
with open(conf_file, "r") as file:
conf = file.read()
conf = json.loads(conf)
prism_addr = conf["prism_address"]
prism_user = conf["user_name"]
prism_pass = conf["password"]
def get_simple_timestamp(time_stamp_in_usecs):
simple_timestamp = datetime.datetime.fromtimestamp(int(time_stamp_in_usecs) / 1000000)
return simple_timestamp
def get_ntnx_v2_events():
api_url = 'https://' + prism_addr + ':9440/PrismGateway/services/rest/v2.0/events'
requests.packages.urllib3.disable_warnings()
s = requests.Session()
s.auth = (prism_user, prism_pass)
s.headers.update({'Content-Type': 'application/json; charset=utf-8'})
events = s.get(api_url, verify=False).json()
return events
def main():
events = get_ntnx_v2_events()
# 列名を出力。
print 'event_time_stamp, severity, alert_type, audit_user, acknowledged, message'
# イベント情報を出力。
for event in events['entities']:
created_time_stamp_in_usecs = event['created_time_stamp_in_usecs']
event_time_stamp = get_simple_timestamp(created_time_stamp_in_usecs)
severity = event['severity']
alert_type_uuid = event['alert_type_uuid']
acknowledged = event['acknowledged']
message = event['message']
# context_type / context_value をディクショナリに変換。
context_types = event['context_types']
context_values = event['context_values']
event_contexts = zip(context_types, context_values)
event_context = {}
for (con_type, con_value) in event_contexts:
c_type = str(con_type)
c_value = str(con_value)
event_context[c_type] = c_value
# message を成形。
event_message = message.format(**event_context)
audit_user = event_context['audit_user']
print event_time_stamp, severity, alert_type_uuid, audit_user, acknowledged, event_message
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment