Slide 21
Slide 21 text
21
Copyright © 2022 BTC Corporation All Rights Reserved.
カスタムインベントリの導入 カスタムインベントリ取得用のスクリプトの作成
if [ "$(curl -sL -w '%{http_code}' 169.254.169.254/latest/meta-data/instance-id -o /dev/null)" =
"200" ]; then
instanceId=$(curl 169.254.169.254/latest/meta-data/instance-id)
inventoryPath=(/var/lib/amazon/ssm/$instanceId/inventory/custom)
else
hybridDirectory=$(find /var/lib/amazon/ssm -name "mi-*")
inventoryPath=($hybridDirectory/inventory/custom)
fi
printf '{"SchemaVersion":"1.0","TypeName":"Custom:Log4J","Content":[' >
$inventoryPath/CustomLog4J.json
for jarPath in $(grep -r --include *.[wj]ar "JndiLookup.class" / 2>&1 | grep matches | sed -e
's/Binary file //' -e 's/ matches//'); do
printf '%s' $SPLITTER >> $inventoryPath/CustomLog4J.json
SPLITTER=","
printf '{"Filename":"%s","Path":"%s"}' $(basename $jarPath) $jarPath >>
$inventoryPath/CustomLog4J.json
done
printf ']}¥n' >> $inventoryPath/CustomLog4J.json
log4jを利用しているかどうかをインベントリ項目として収集するためのShell スクリプト
1. カスタムインベントリとして収集したい構成情報を取得
2. 形式通りにJSONファイルを作成
3. 所定のフォルダ/ディレクトリ配下に配置
◼ スクリプトの内容
$data = get-wmiobject win32_logicaldisk | Select-Object @{n="DeviceId";e={$_."DeviceID"}},
@{n="VolumeName";e={$_."VolumeName"}}, @{n="Use%";e={"{0}" -f
[math]::Round(($_."Size" - $_."FreeSpace") * 100 / $_."Size",0)}}, @{n="Size(GB)";e={"{0}" -f
[math]::Round($_."Size" / 1GB ,0)}} | ConvertTo-Json
$content = "{`"SchemaVersion`" : `"1.0`", `"TypeName`": `"Custom:DiskUtilization`", `"Content`":
$data}"
$instanceId = Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/instance-id
$filepath = "C:¥ProgramData¥Amazon¥SSM¥InstanceData¥" + $instanceId +
"¥inventory¥custom¥CustomDiskUsage.json"
if (-NOT (Test-Path $filepath)) {
New-Item $filepath -ItemType file
}
Set-Content -Path $filepath -Value $content
ディスク使用率をインベントリ項目として収集するためのPowerShell スクリプト