Skip to content

Commit

Permalink
Merge pull request aws#85 from kolov/flatten-resource-health-event
Browse files Browse the repository at this point in the history
flattening resources in the aws.health event sent by coralogix-notifier
  • Loading branch information
tipuq authored Jul 6, 2022
2 parents 70bc107 + b3ceeb7 commit e1a74ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion coralogix-notifier/cfn-templates/coralogix-notifier.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
},
"Code": {
"ZipFile": {
"Fn::Sub": "# Sample Lambda Function to post notifications to a slack channel when an AWS Health event happens\nimport os\nimport time\nimport logging\nimport json\nimport urllib.error\nfrom urllib.request import Request, urlopen\n\nCORALOGIX_LOG_URL = os.getenv('CORALOGIX_LOG_URL')\nPRIVATE_KEY = os.getenv('PRIVATE_KEY')\nAPP_NAME = os.getenv('APP_NAME')\nSUB_SYSTEM = os.getenv('SUB_SYSTEM')\n\nWARN = 4\nTIMEOUT = os.getenv('CORALOGIX_TIMEOUT_HTTP', 30)\nRETRIES = os.getenv('CORALOGIX_RETRIES_HTTP', 2)\n\nlogger = logging.getLogger()\nlogger.setLevel(logging.INFO)\n\ndef lambda_handler(event, context):\n message = {\n \"privateKey\": str(PRIVATE_KEY),\n \"applicationName\": str(APP_NAME),\n \"subsystemName\": str(SUB_SYSTEM),\n \"logEntries\": [{\"timestamp\": (time.time() * 1000), \"severity\": WARN, \"text\": event}]\n }\n jsondata = json.dumps(message).encode('utf-8')\n for attempt in range(int(RETRIES)):\n try:\n req = Request(CORALOGIX_LOG_URL)\n req.add_header('Content-Type', 'application/json; charset=utf-8')\n req.add_header('Content-Length', len(jsondata))\n response = urlopen(req, data=jsondata,timeout=TIMEOUT)\n if response.getcode() == 200:\n logger.info(\"Health log published to Coralogix successfully 200 OK\")\n return True\n else:\n logger.error(\"health log publish failed, status code %d, %b\", response.getcode(), response.read)\n except urllib.error.URLError as e:\n logger.error(\"URL Error %s\", e)\n except urllib.error.HTTPError as e:\n logger.error(\"HTTP Error %s\", e)\n logger.info(\"attempt number %d\", attempt + 1)\n time.sleep(5)\n"
"Fn::Sub": "# Sample Lambda Function to post notifications to a slack channel when an AWS Health event happens\nimport os\nimport time\nimport logging\nimport json\nimport urllib.error\nfrom urllib.request import Request, urlopen\n\nCORALOGIX_LOG_URL = os.getenv('CORALOGIX_LOG_URL')\nPRIVATE_KEY = os.getenv('PRIVATE_KEY')\nAPP_NAME = os.getenv('APP_NAME')\nSUB_SYSTEM = os.getenv('SUB_SYSTEM')\n\nWARN = 4\nTIMEOUT = os.getenv('CORALOGIX_TIMEOUT_HTTP', 30)\nRETRIES = os.getenv('CORALOGIX_RETRIES_HTTP', 2)\n\nlogger = logging.getLogger()\nlogger.setLevel(logging.INFO)\n\ndef lambda_handler(event, context):\n def send(e):\n message = {\n \"privateKey\": str(PRIVATE_KEY),\n \"applicationName\": str(APP_NAME),\n \"subsystemName\": str(SUB_SYSTEM),\n \"logEntries\": [{\"timestamp\": (time.time() * 1000), \"severity\": WARN, \"text\": event}]\n }\n jsondata = json.dumps(message).encode('utf-8')\n for attempt in range(int(RETRIES)):\n try:\n req = Request(CORALOGIX_LOG_URL)\n req.add_header('Content-Type', 'application/json; charset=utf-8')\n req.add_header('Content-Length', len(jsondata))\n response = urlopen(req, data=jsondata,timeout=TIMEOUT)\n if response.getcode() == 200:\n logger.info(\"Health log published to Coralogix successfully 200 OK\")\n return True\n else:\n logger.error(\"health log publish failed, status code %d, %b\", response.getcode(), response.read)\n except urllib.error.URLError as e:\n logger.error(\"URL Error %s\", e)\n except urllib.error.HTTPError as e:\n logger.error(\"HTTP Error %s\", e)\n logger.info(\"attempt number %d\", attempt + 1)\n time.sleep(5)\n\n entities = event.get(\"detail\", {}).get(\"affectedEntities\")\n resources = event.get(\"resources\")\n if(type(entities) == list and type(resources) == list and sorted(list(map(lambda x: x[\"entityValue\"], entities))) == sorted(resources) ):\n event[\"detail\"].pop(\"affectedEntities\", None)\n event.pop(\"resources\", None)\n for entity in entities:\n event[\"detail\"][\"affectedEntity\"] = entity\n event[\"resource\"] = entity.get(\"entityValue\")\n send(event)\n else:\n send(event)\n"
}
},
"Environment": {
Expand Down
69 changes: 41 additions & 28 deletions coralogix-notifier/cfn-templates/coralogix-notifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,44 +65,57 @@ Resources:
import json
import urllib.error
from urllib.request import Request, urlopen

CORALOGIX_LOG_URL = os.getenv('CORALOGIX_LOG_URL')
PRIVATE_KEY = os.getenv('PRIVATE_KEY')
APP_NAME = os.getenv('APP_NAME')
SUB_SYSTEM = os.getenv('SUB_SYSTEM')

WARN = 4
TIMEOUT = os.getenv('CORALOGIX_TIMEOUT_HTTP', 30)
RETRIES = os.getenv('CORALOGIX_RETRIES_HTTP', 2)

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
message = {
"privateKey": str(PRIVATE_KEY),
"applicationName": str(APP_NAME),
"subsystemName": str(SUB_SYSTEM),
"logEntries": [{"timestamp": (time.time() * 1000), "severity": WARN, "text": event}]
}
jsondata = json.dumps(message).encode('utf-8')
for attempt in range(int(RETRIES)):
try:
req = Request(CORALOGIX_LOG_URL)
req.add_header('Content-Type', 'application/json; charset=utf-8')
req.add_header('Content-Length', len(jsondata))
response = urlopen(req, data=jsondata,timeout=TIMEOUT)
if response.getcode() == 200:
logger.info("Health log published to Coralogix successfully 200 OK")
return True
else:
logger.error("health log publish failed, status code %d, %b", response.getcode(), response.read)
except urllib.error.URLError as e:
logger.error("URL Error %s", e)
except urllib.error.HTTPError as e:
logger.error("HTTP Error %s", e)
logger.info("attempt number %d", attempt + 1)
time.sleep(5)
def send(e):
message = {
"privateKey": str(PRIVATE_KEY),
"applicationName": str(APP_NAME),
"subsystemName": str(SUB_SYSTEM),
"logEntries": [{"timestamp": (time.time() * 1000), "severity": WARN, "text": event}]
}
jsondata = json.dumps(message).encode('utf-8')
for attempt in range(int(RETRIES)):
try:
req = Request(CORALOGIX_LOG_URL)
req.add_header('Content-Type', 'application/json; charset=utf-8')
req.add_header('Content-Length', len(jsondata))
response = urlopen(req, data=jsondata,timeout=TIMEOUT)
if response.getcode() == 200:
logger.info("Health log published to Coralogix successfully 200 OK")
return True
else:
logger.error("health log publish failed, status code %d, %b", response.getcode(), response.read)
except urllib.error.URLError as e:
logger.error("URL Error %s", e)
except urllib.error.HTTPError as e:
logger.error("HTTP Error %s", e)
logger.info("attempt number %d", attempt + 1)
time.sleep(5)

entities = event.get("detail", {}).get("affectedEntities")
resources = event.get("resources")
if(type(entities) == list and type(resources) == list and sorted(list(map(lambda x: x["entityValue"], entities))) == sorted(resources) ):
event["detail"].pop("affectedEntities", None)
event.pop("resources", None)
for entity in entities:
event["detail"]["affectedEntity"] = entity
event["resource"] = entity.get("entityValue")
send(event)
else:
send(event)
Environment:
Variables:
CORALOGIX_LOG_URL: !Ref CoralogixLogURL
Expand Down

0 comments on commit e1a74ab

Please sign in to comment.