Feb-20-2018, 12:18 PM
Hi Thanks for your reply,
sorry, i am replying bit late and trying to put out various methods and failing :(
The actual context is i am trying to get some linux server status and posting to slack channel
In original post I have given the sample which is base code which i am trying to build correct code for my context
This is original code
Hope I am giving right info.. can you please help..where i am doing it wrong...
The above code says json is not serializable error
Regargs
ASP
sorry, i am replying bit late and trying to put out various methods and failing :(
The actual context is i am trying to get some linux server status and posting to slack channel
In original post I have given the sample which is base code which i am trying to build correct code for my context
This is original code
from __future__ import print_function import boto3 import json import logging import os logging.basicConfig() from urllib2 import Request, urlopen, URLError, HTTPError # configuration # The Slack channel to send a message to stored in the slackChannel environment variable SLACK_CHANNEL = "#XXXXXXXXXX" # Add the webhook URL from Slack below HOOK_URL = "https://hooks.slack.com/services/xxxxx/xxxx/xxxxx" response = conn.get_all_instances() MESSAGE = response # Setting up logging logger = logging.getLogger() logger.setLevel(logging.INFO) slack_message = { 'channel': SLACK_CHANNEL, 'text': MESSAGE} req = Request(HOOK_URL, json.dumps(slack_message)) try: response = urlopen(req) response.read() logger.info("Message posted to %s", slack_message['channel']) except HTTPError as e: logger.error("Request failed: %d %s", e.code, e.reason) except URLError as e: logger.error("Server connection failed: %s", e.reason)So in above code line number 18 the variable "response" actually returns in below format
{ "Reservations": [ { "Instances": [ { "Monitoring": { "State": "disabled" }, "PublicDnsName": "", "StateReason": { "Message": "Client.UserInitiatedShutdown: User initiated shutdown", "Code": "Client.UserInitiatedShutdown" }, "State": { "Code": 80, "Name": "stopped" }, "EbsOptimized": false, "LaunchTime": "2018-02-08T10:37:36.000Z", "PrivateIpAddress": "xx.xx.xx.xxx", "ProductCodes": [], "VpcId": "xxxxxx", "StateTransitionReason": "User initiated (2018-02-08 11:13:34 GMT)", "InstanceId": "xxxxxxx", "EnaSupport": true, "ImageId": "xxxxx", "PrivateDnsName": "xxxxxxxx", "KeyName": "xxxxxx", "SecurityGroups": [ { "GroupName": "xxxxxxxx", "GroupId": "xx-xxxx" } ], "ClientToken": "", "SubnetId": "xxxx-xxxxx", "InstanceType": "xx.xxx", "NetworkInterfaces": [so i want to post this information to slack channel in under stable way for readers
Hope I am giving right info.. can you please help..where i am doing it wrong...
The above code says json is not serializable error
Regargs
ASP