Skip to content

Commit

Permalink
flattening resources in the aws.health event sent by coralogix-notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kolov committed Jul 5, 2022
1 parent 9edb3c5 commit b3ceeb7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 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 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 del event[\"detail\"][\"affectedEntities\"]\n del event[\"resources\"]\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"
"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
4 changes: 2 additions & 2 deletions coralogix-notifier/cfn-templates/coralogix-notifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ Resources:
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) ):
del event["detail"]["affectedEntities"]
del event["resources"]
event["detail"].pop("affectedEntities", None)
event.pop("resources", None)
for entity in entities:
event["detail"]["affectedEntity"] = entity
event["resource"] = entity.get("entityValue")
Expand Down

0 comments on commit b3ceeb7

Please sign in to comment.