#!/bin/bash

###############################################################
#                                                             #
#   Feature :-  Script for AWS metadata instance Category     #
#   Developer :- Abhishek Sharma                              #
#   Modified Date :- 31st May 2023                            #
#                                                             #
###############################################################

#########################################################################
#This software code is made available "AS IS" without warranties of any #
#kind. You may copy, display, modify and redistribute the software      #
#code either by itself or as incorporated into your code; provided that #
#you do not remove any proprietary notices. Your use of this software   #
#code is at your own risk and you waive any claim against Amazon        #
#Digital Services, Inc. or its affiliates with respect to your use of   #
#this software code. (c) 2006-2007 Amazon Digital Services, Inc. or its #
#affiliates.                                                            #
#########################################################################
CUR_DIR=$( cd "$(dirname "$0")" ; pwd -P )
resolver_path=$CUR_DIR/httpclient

function print_help()
{
echo "ec2-metadata v0.1.1
Use to retrieve EC2 instance metadata from within a running EC2 instance. 
e.g. to retrieve instance id: ec2-metadata -i
		 to retrieve ami id: ec2-metadata -a
		 to get help: ec2-metadata --help
For more information on Amazon EC2 instance meta-data, refer to the documentation at
http://docs.amazonwebservices.com/AWSEC2/2008-05-05/DeveloperGuide/AESDG-chapter-instancedata.html

Usage: ec2-metadata <option>
Options:
--all                     Show all metadata information for this host (also default).
-a/--ami-id               The AMI ID used to launch this instance
-l/--ami-launch-index     The index of this instance in the reservation (per AMI).
-m/--ami-manifest-path    The manifest path of the AMI with which the instance was launched.
-n/--ancestor-ami-ids     The AMI IDs of any instances that were rebundled to create this AMI.
-b/--block-device-mapping Defines native device names to use when exposing virtual devices.
-i/--instance-id          The ID of this instance
-t/--instance-type        The type of instance to launch. For more information, see Instance Types.
-h/--local-hostname       The local hostname of the instance.
-o/--local-ipv4           Public IP address if launched with direct addressing; private IP address if launched with public addressing.
-k/--kernel-id            The ID of the kernel launched with this instance, if applicable.
-z/--availability-zone    The availability zone in which the instance launched. Same as placement
-c/--product-codes        Product codes associated with this instance.
-p/--public-hostname      The public hostname of the instance.
-v/--public-ipv4          NATted public IP Address
-u/--public-keys          Public keys. Only available if supplied at instance launch time
-r/--ramdisk-id           The ID of the RAM disk launched with this instance, if applicable.
-e/--reservation-id       ID of the reservation.
-s/--security-groups      Names of the security groups the instance is launched in. Only available if supplied at instance launch time
-d/--user-data            User-supplied data.Only available if supplied at instance launch time."
}

function chk_config()
{
        #check if run inside an ec2-instance
        x=$($resolver_path http://169.254.169.254/)
        if [ $? -gt 0 ]; then
                echo '[ERROR] Command not valid outside EC2 instance. Please run this command within a running EC2 instance.'
                exit 1
        fi
}

function print_normal_metric() {
        metric_path=$2
	tag_path=$1
        #echo -n $1": "
        RESPONSE=$($resolver_path http://169.254.169.254/latest/meta-data/tags/instance/${tag_path}/)
       if [ -z "$RESPONSE" ]; then
                RESPONSE=$($resolver_path http://169.254.169.254/latest/meta-data/${metric_path}/)
		echo $RESPONSE
        else
                echo $RESPONSE
        fi
}

function print_all()
{
	print_normal_metric ami-id meta-data/ami-id
	print_normal_metric ami-launch-index meta-data/ami-launch-index
	print_normal_metric ami-manifest-path meta-data/ami-manifest-path
	print_normal_metric ancestor-ami-ids meta-data/ancestor-ami-ids
	print_normal_metric instance-id meta-data/instance-id
	print_normal_metric instance-type meta-data/instance-type
	print_normal_metric local-hostname meta-data/local-hostname
	print_normal_metric local-ipv4 meta-data/local-ipv4
	print_normal_metric kernel-id meta-data/kernel-id
	print_normal_metric placement meta-data/placement/availability-zone
	print_normal_metric product-codes meta-data/product-codes
	print_normal_metric public-hostname meta-data/public-hostname
	print_normal_metric public-ipv4 meta-data/public-ipv4
	print_normal_metric ramdisk-id /meta-data/ramdisk-id
	print_normal_metric reservation-id /meta-data/reservation-id
	print_normal_metric security-groups meta-data/security-groups
	print_normal_metric user-data user-data
	print_normal_metric target-lifecycle-state autoscaling/target-lifecycle-state
	print_normal_metric ebsN block-device-mapping/ebsN	
	print_normal_metric ephemeralN block-device-mapping/ephemeralN
	print_normal_metric swap block-device-mapping/swap
	print_normal_metric eia-id elastic-inference/associations/eia-id
	print_normal_metric rebalance events/recommendations/rebalance
	print_normal_metric vpc-ipv4-cidr-block iam/inetwork/interfaces/macs/mac/vpc-ipv4-cidr-block
	print_normal_metric role-name iam/security-credentials/role-name	
	print_normal_metric ipv6 ipv6
	print_normal_metric ma0c ma0c
	print_normal_metric vhostmd metrics/vhostmd 
	print_normal_metric device-number network/interfaces/macs/mac/device-number
	print_normal_metric interface-id network/interfaces/macs/mac/interface-id
	print_normal_metric ipv6s network/interfaces/macs/mac/ipv6s
	print_normal_metric local-hostname  network/interfaces/macs/mac/local-hostname	
	print_normal_metric local-ipv4s network/interfaces/macs/mac/local-ipv4s
	print_normal_metric mac network/interfaces/macs/mac/mac
	print_normal_metric network-card-index network/interfaces/macs/mac/network-card-index
	print_normal_metric owner-id network/interfaces/macs/mac/owner-id
	print_normal_metric public-hostname network/interfaces/macs/mac/public-hostname
	print_normal_metric public-ipv4s network/interfaces/macs/mac/public-ipv4s
	print_normal_metric security-groups network/interfaces/macs/mac/security-groups
	print_normal_metric security-group-ids network/interfaces/macs/mac/security-group-ids
	print_normal_metric subnet-id network/interfaces/macs/mac/subnet-id
	print_normal_metric subnet-ipv4-cidr-block network/interfaces/macs/mac/subnet-ipv4-cidr-block
	print_normal_metric subnet-ipv6-cidr-blocks network/interfaces/macs/mac/subnet-ipv6-cidr-blocks
	print_normal_metric vpc-id network/interfaces/macs/mac/vpc-id
	print_normal_metric vpc-ipv4-cidr-block network/interfaces/macs/mac/vpc-ipv4-cidr-block
	print_normal_metric vpc-ipv4-cidr-blocks network/interfaces/macs/mac/vpc-ipv4-cidr-blocks
	print_normal_metric vpc-ipv6-cidr-blocks network/interfaces/macs/mac/vpc-ipv6-cidr-blocks
	print_normal_metric group-name placement/group-name
	print_normal_metric host-id placement/host-id
	print_normal_metric instance-action spot/instance-action
	print_normal_metric termination-time spot/termination-time
	print_normal_metric instanc tags/instance

}

#check if run inside an EC2 instance
chk_config

#command called in default mode
if [ "$#" -eq 0 ]; then
	print_all
fi

#start processing command line arguments
while [ "$1" != "" ]; do
	case $1 in
	a | ami-id )		                print_normal_metric ami-id ami-id
																 ;;
	b | ami-launch-index )  	        print_normal_metric ami-launch-index ami-launch-index
																 ;;
	c | ami-manifest-path )  	        print_normal_metric ami-manifest-path ami-manifest-path
																 ;;
	d | ancestor-ami-ids )                  print_normal_metric ancestor-ami-ids ancestor-ami-ids
																 ;;
        e | ami )  		                print_normal_metric ami block-device-mapping/ami
                                                                                                                                 ;;
	f | instance-id )                       print_normal_metric instance-id instance-id
																 ;;
	g | instance-type )                     print_normal_metric instance-type instance-type
																 ;;
	h | local-hostname )                    print_normal_metric local-hostname local-hostname
																 ;;
	i | local-ipv4 )                        print_normal_metric local-ipv4 local-ipv4
																 ;;
	j | kernel-id )                         print_normal_metric kernel-id kernel-id
																 ;;
	k | availability-zone )                 print_normal_metric placement placement/availability-zone
																 ;;
	l | product-codes )                     print_normal_metric product-codes product-codes
																 ;;
	m | public-hostname )                   print_normal_metric public-hostname public-hostname
																 ;;
	n | public-ipv4 )           	        print_normal_metric public-ipv4 public-ipv4
																 ;;
	o | ramdisk-id )            		print_normal_metric ramdisk-id ramdisk-id
																 ;;
	p | reservation-id )   	                print_normal_metric reservation-id reservation-id
															 ;;
        q | security-groups )  		        print_normal_metric security-groups security-groups
															 ;;
        r | target-lifecycle-state )       	print_normal_metric target-lifecycle-state autoscaling/target-lifecycle-state
                                                                                                                                 ;;
        s | ebsN )      		        print_normal_metric ebsN block-device-mapping/ebsN
                                                                                                                                 ;;
        t | ephemeralN )  		        print_normal_metric ephemeralN block-device-mapping/ephemeralN
                                                                                                                                 ;;
        u | swap )     			        print_normal_metric swap block-device-mapping/swap
                                                                                                                                 ;;
        v | associations/eia-id )      	        print_normal_metric eia-id elastic-inference/associations/eia-id
                                                                                                                                 ;;
        w | rebalance )                  	print_normal_metric rebalance events/recommendations/rebalance
                                                                                                                                 ;;
        x | vpc-ipv4-cidr-block )       	print_normal_metric vpc-ipv4-cidr-block iam/inetwork/interfaces/macs/mac/vpc-ipv4-cidr-block
                                                                                                                                 ;;
        y | role-name )       			print_normal_metric role-name iam/security-credentials/role-name
                                                                                                                                 ;;
        z | ipv6 )       			print_normal_metric ipv6 ipv6
                                                                                                                                 ;;
       aa | ma0c )       			print_normal_metric ma0c ma0c
                                                                                                                                 ;;
       ab | vhostmd )       			print_normal_metric vhostmd metrics/vhostmd
                                                                                                                                 ;;
       ac | device-number )       		print_normal_metric device-number network/interfaces/macs/mac/device-number
                                                                                                                                 ;;
       ad | interface-id )       		print_normal_metric interface-id network/interfaces/macs/mac/interface-id
                                                                                                                                 ;;
       ae | ipv6s )       			print_normal_metric ipv6s network/interfaces/macs/mac/ipv6s
                                                                                                                                 ;;
       af | local-hostname )       		print_normal_metric local-hostname network/interfaces/macs/mac/local-hostname
                                                                                                                                 ;;
       ag | local-ipv4s )       		print_normal_metric local-ipv4s network/interfaces/macs/mac/local-ipv4s
                                                                                                                                 ;;
       ah | mac )       			print_normal_metric mac network/interfaces/macs/mac/mac
                                                                                                                                 ;;
       ai | network-card-index )       		print_normal_metric network-card-index network/interfaces/macs/mac/network-card-index
                                                                                                                                 ;;
       aj | owner-id )       			print_normal_metric owner-id network/interfaces/macs/mac/owner-id
                                                                                                                                 ;;
       ak | public-hostname )       		print_normal_metric public-hostname network/interfaces/macs/mac/public-hostname
                                                                                                                                 ;;
       al | public-ipv4s )       		print_normal_metric public-ipv4s network/interfaces/macs/mac/public-ipv4s
                                                                                                                                 ;;
       am | security-groups )       		print_normal_metric security-groups network/interfaces/macs/mac/security-groups
                                                                                                                                 ;;
       an | security-group-ids )       		print_normal_metric security-group-ids network/interfaces/macs/mac/security-group-ids
                                                                                                                                 ;;
       ao | subnet-id )       			print_normal_metric subnet-id network/interfaces/macs/mac/subnet-id
                                                                                                                                 ;;
       ap | subnet-ipv4-cidr-block )       	print_normal_metric subnet-ipv4-cidr-block network/interfaces/macs/mac/subnet-ipv4-cidr-block
                                                                                                                                 ;;
       aq | subnet-ipv6-cidr-blocks )       	print_normal_metric subnet-ipv6-cidr-blocks network/interfaces/macs/mac/subnet-ipv6-cidr-blocks
                                                                                                                                 ;;
       ar | vpc-id )       			print_normal_metric vpc-id network/interfaces/macs/mac/vpc-id
                                                                                                                                 ;;
       as | vpc-ipv4-cidr-block )       	print_normal_metric vpc-ipv4-cidr-block network/interfaces/macs/mac/vpc-ipv4-cidr-block
                                                                                                                                 ;;
       at | vpc-ipv4-cidr-blocks )       	print_normal_metric vpc-ipv4-cidr-blocks network/interfaces/macs/mac/vpc-ipv4-cidr-blocks
                                                                                                                                 ;;
       au | vpc-ipv6-cidr-blocks )       	print_normal_metric vpc-ipv6-cidr-blocks network/interfaces/macs/mac/vpc-ipv6-cidr-blocks
                                                                                                                                 ;;
       av | group-name )       			print_normal_metric group-name placement/group-name
                                                                                                                                 ;;
       aw | host-id )      	 		print_normal_metric host-id placement/host-id
                                                                                                                                 ;;
       ax | instance-action )       		print_normal_metric instance-action spot/instance-action
                                                                                                                                 ;;
       ay | termination-time )       		print_normal_metric termination-time spot/termination-time
                                                                                                                                 ;;
#       * )            			        print_normal_metric $1
#                                                                                                                                 ;;
       az | user-data )             		print_normal_metric user-data user-data
																 ;;
	-h | --help )                  		print_help
								 exit
																 ;;
	--all )                        		print_all
								 exit
	esac
	shift
done
