简体   繁体   English

AWS SSM 自定义清单

[英]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/我正在尝试设置自定义清单类型以显示 Linux 实例磁盘使用情况,如本文中为 Windows 实例显示的那样: 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.我一直在尝试创建与上面链接中的 PowerShell 脚本中给出的逻辑相同的逻辑,但我无法找到将 Linux 磁盘使用情况命令输出转换为 JSON 格式的方法。 I cannot install jq or any other software.我无法安装jq或任何其他软件。

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?如何更改此脚本,以便获得与 Windows 实例显示的结果相同的结果?

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.基本上,由于您不使用任何东西来生成 JSON,因此您必须自己生成。 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:你可以用 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.首先,使用 df 的输出标志从输出中去除任何“噪音”。 There is also no need to pipe through grep and then awk.也不需要通过 grep 然后通过管道传输 awk。 Use the BEGIN block of awk to print the SchemaVersion, TypeName and Content tags.使用 awk 的 BEGIN 块打印 SchemaVersion、TypeName 和 Content 标签。 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).在主块中,当没有遇到 tmpfs 和 cdrom 以及标头 (NR>1) 时,打印 Filesystem、Use 和 Size 标签的 df 命令的输出。 In the END block, print the closing tags.在 END 块中,打印结束标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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