简体   繁体   中英

AWS SSM Custom Inventory

I am trying to setup custom inventory type to show Linux instances disk usage as it is showing for Windows instances in this article: https://aws.amazon.com/blogs/mt/get-disk-utilization-of-your-fleet-using-ec2-systems-manager-custom-inventory-types/

I have been trying to create the same logic as it is given in the PowerShell script in the link above but I am not able to find a way to convert my Linux disk usage command output in JSON format. I cannot install jq or any other software.

Here's my script :

#!/bin/sh
output=$(df -hTP | grep -vE '^tmpfs|cdrom' | awk '{print $1,$3,$6}')
content= echo "{\"SchemaVersion\":\"1.0\",\"TypeName\":\"Custom:LinuxDiskUsage\",\"Content\":{$output}}"
instanceid=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)

How can I change this script so that I can get the same results as it is showing for Windows instances?

The current output is:

{
  "SchemaVersion" : "1.0",
  "TypeName" : "Custom:LinuxDiskUsage",
  "Content" : {
                 Filesystem Size Use% devtmpfs 474M 0% /dev/xvda1 8.0G 17%
              }
}

But I hope to get the following:

{
  "SchemaVersion" : "1.0",
  "TypeName" : "Custom:LinuxDiskUsage",
  "Content" : {
                "Filesystem" : "/dev/xvda1",
                "Size" : "8.0G",
                "Use%": "17%"
              }
}
output=$(df -hTP | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print "{ \"Filesystem\": \""$1"\", \"Size\": \""$3"\", \"Use\%\": \""$6"\"},"}')
content= echo "{\"SchemaVersion\":\"1.0\",\"TypeName\":\"Custom:LinuxDiskUsage\",\"Content\":[$output]}"

will do it. Basically, since you don't use anything to generate JSON you have to generate your own. There might be comma too much in your output. That could be fixed with a join or cutting the last character out.

You can do this completely with awk:

df -h --output=source,size,pcent | awk '
         BEGIN { 
                 printf "{\n\t\"SchemaVersion\" : \"1.0\",\n\t\"TypeName\" : \"Customer:LinuxDiskUsage\",\n\t\"Content\" : "
               } 
         NR > 1 && $0 !~ /tmpfs/ && $0 !~ /cdrom/ { 
                printf "\n\t\t{\n\t\t\t\"Filesystem\" : \"%s\"\n\t\t\t\"Size\" : \"%s\",\n\t\t\t\"Use%\" : \"%s\"\n\t\t}\n",$1,$2,$3 
               } 
         END { 
                printf "\t}\n" 
             }'

One liner:

df -h --output=source,size,pcent | awk 'BEGIN { printf "{\n\t\"SchemaVersion\" : \"1.0\",\n\t\"TypeName\" : \"Customer:LinuxDiskUsage\",\n\t\"Content\" : "} NR > 1 && $0 !~ /tmpfs/ && $0 !~ /cdrom/ { printf "\n\t\t{\n\t\t\t\"Filesystem\" : \"%s\"\n\t\t\t\"Size\" : \"%s\",\n\t\t\t\"Use%\" : \"%s\"\n\t\t}\n",$1,$2,$3 } END { printf "\t}\n" }'

Firstly, remove any "noise" from the output using the output flag of df. There is also no need to pipe through grep and then awk. Use the BEGIN block of awk to print the SchemaVersion, TypeName and Content tags. In the main block, print the output of the df command for the Filesystem, Use and Size tags when tmpfs and cdrom are not encountered along with the headers (NR>1). In the END block, print the closing tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM