Feb-23-2018, 11:17 AM
Hi buran,
at last i am able to get what i need...but there is small issue i am getting formatted text and it posted to slack channel. But the problem now is its posting for one item only..actually i have two server running. However from command line its printing both instance details but onto slack its posting only one of it
State Tag name
stopped abcustomvpcsubnet1
running KNginx
but when i checked slack channel its only showing..
running KNginx
how ever other instance named abcustomvpcsubnet1 not posted to slack channel
do you see placement of POST not proper ? any guidance, why its showing on screen all the records, but not on to slack...
Regards
ASP
at last i am able to get what i need...but there is small issue i am getting formatted text and it posted to slack channel. But the problem now is its posting for one item only..actually i have two server running. However from command line its printing both instance details but onto slack its posting only one of it
#!/usr/bin/env python # import json import subprocess import sys import requests # Function def awsdescribe(): p = subprocess.Popen(["aws", "ec2", "describe-instances"], stdout=subprocess.PIPE) (output, err) = p.communicate() return output # MAIN desc = {} try: desc = json.loads(awsdescribe()) except ValueError as ve: print "Oops! Something went wrong: {}".format(ve) sys.exit(1) for reservations in desc['Reservations']: for instances in reservations['Instances']: public_ip_address = instances.get('PublicIpAddress', "-") or "-" private_ip_address = instances.get('PrivateIpAddress', "-") or "-" public_dns_name = instances.get('PublicDnsName', "-") or "-" private_dns_name = instances.get('PrivateDnsName', "-") or "-" for tags in instances['Tags']: key = tags['Key'] value = tags['Value'] if key == 'Name': tag_name = "{}\t".format(value) SLACK_CHANNEL = "#xxxxxxx" HOOK_URL = "https://hooks.slack.com/services/xxxxxx/xxxxxx/xxxxxxxxxx" MESSAGE = {'text': tag_name + " " "is " + instances['State']['Name']} response = requests.post( HOOK_URL, data=json.dumps(MESSAGE), headers={'Content-Type': 'application/json'}) if response.status_code != 200: raise ValueError( 'Request to slack returned an error %s, the response is:\n%s' % (response.status_code, response.text)When I execute from command line, its showing all the instances
State Tag name
stopped abcustomvpcsubnet1
running KNginx
but when i checked slack channel its only showing..
running KNginx
how ever other instance named abcustomvpcsubnet1 not posted to slack channel
do you see placement of POST not proper ? any guidance, why its showing on screen all the records, but not on to slack...
Regards
ASP