200 lines
6.2 KiB
Bash
Executable File
200 lines
6.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
#------------------------------------------------------------------------------------------------------
|
|
__init (){
|
|
# pwd
|
|
# cd ${git_path}
|
|
# pwd
|
|
|
|
git pull
|
|
|
|
datetime=`date "+%Y.%m.%d %H:%M:%S"`
|
|
echo -e "*기준 시간 : ${datetime}\n" > ${file}
|
|
cat ${origin} >> ${file}
|
|
}
|
|
#------------------------------------------------------------------------------------------------------
|
|
__git_push (){
|
|
git add ${file}
|
|
git commit -m 'steampipe schedule'
|
|
git push
|
|
}
|
|
#------------------------------------------------------------------------------------------------------
|
|
__append (){
|
|
line_count=`cat ${exec_log} | grep -v -- -- | grep -v name | wc -l`
|
|
echo -e "\n${title} [${line_count}]\n" >> ${file}
|
|
cat ${exec_log} >> ${file}
|
|
}
|
|
#------------------------------------------------------------------------------------------------------
|
|
__query_exec (){
|
|
steampipe query "${1}" > ${exec_log}
|
|
__log_sed
|
|
}
|
|
#------------------------------------------------------------------------------------------------------
|
|
__log_sed (){
|
|
sed -i 's/+/|/g' ${exec_log}
|
|
sed -i "s/node-role.kubernetes.io\///g" ${exec_log}
|
|
sed -i "s/31/32/g" ${exec_log}
|
|
sed -i "s/62/64/g" ${exec_log}
|
|
sed -i '1d;$d' ${exec_log}
|
|
}
|
|
#------------------------------------------------------------------------------------------------------
|
|
node_query="""
|
|
SELECT
|
|
name,
|
|
annotations ->> 'projectcalico.org/IPv4Address' AS IP,
|
|
COALESCE(taints -> 0 ->> 'key', '-') AS Taints_key,
|
|
COALESCE(tags ->> 'kops.k8s.io/instancegroup', '-') AS Instance_group,
|
|
capacity ->> 'cpu' AS CPU,
|
|
CEIL((CAST(regexp_replace(capacity ->> 'memory', 'Ki', '') AS DECIMAL) / POWER(2, 20))) AS Memory,
|
|
tags ->> 'topology.kubernetes.io/zone' AS Zone,
|
|
tags ->> 'beta.kubernetes.io/instance-type' AS Instance_type,
|
|
node_info ->> 'osImage' AS OS,
|
|
node_info ->> 'kubeletVersion' AS K8S_ver,
|
|
node_info ->> 'containerRuntimeVersion' AS Runtime_ver
|
|
FROM
|
|
kubernetes_node
|
|
ORDER BY
|
|
Taints_key
|
|
"""
|
|
|
|
resources_query="""
|
|
(SELECT
|
|
name,
|
|
'Stateful_Set' as kind,
|
|
available_replicas as count,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'cpu') AS reqeust_cpu,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'memory') AS reqeust_mem,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'cpu') AS limit_cpu,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'memory') AS limit_mem
|
|
|
|
FROM
|
|
kubernetes_stateful_set
|
|
WHERE
|
|
name not like 'rel-%')
|
|
union
|
|
(SELECT
|
|
name,
|
|
'Deployment' as kind,
|
|
available_replicas as count,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'cpu') AS reqeust_cpu,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'memory') AS reqeust_mem,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'cpu') AS limit_cpu,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'memory') AS limit_mem
|
|
FROM
|
|
kubernetes_deployment
|
|
WHERE
|
|
name not like 'rel-%')
|
|
union
|
|
(SELECT
|
|
name,
|
|
'DaemonSet' as kind,
|
|
number_available as count,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'cpu') AS reqeust_cpu,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'requests' ->> 'memory') AS reqeust_mem,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'cpu') AS limit_cpu,
|
|
(template -> 'spec' -> 'containers' -> 0 -> 'resources' -> 'limits' ->> 'memory') AS limit_mem
|
|
FROM
|
|
kubernetes_daemonset
|
|
WHERE
|
|
name not like 'rel-%')
|
|
order by kind
|
|
"""
|
|
|
|
service_query="""
|
|
select
|
|
name,
|
|
namespace,
|
|
type,
|
|
lower(p ->> 'nodePort') as Node_Port,
|
|
age(current_timestamp, creation_timestamp)
|
|
from
|
|
kubernetes_service,
|
|
jsonb_array_elements(ports) as p
|
|
where type='NodePort' and name not like '%rel-%'
|
|
order by
|
|
Node_Port
|
|
"""
|
|
|
|
aws_ri_query="""
|
|
SELECT
|
|
COALESCE(a.availability_zone, b.availability_zone, '-') AS availability_zone,
|
|
COALESCE(a.instance_type, b.instance_type, c.instance_type, '-') AS instance_type,
|
|
COALESCE(c.cpu, 0) AS cpu,
|
|
COALESCE(c.memory, 0) AS memory,
|
|
COALESCE(a.ri_count, 0) AS ri_count,
|
|
COALESCE(b.ec2_count, 0) AS ec2_count,
|
|
COALESCE(b.ec2_count, 0) - COALESCE(a.ri_count, 0) AS result
|
|
FROM
|
|
(SELECT
|
|
availability_zone,
|
|
instance_type,
|
|
SUM(instance_count) AS ri_count
|
|
FROM
|
|
aws_ec2_reserved_instance
|
|
WHERE
|
|
instance_state='active'
|
|
GROUP BY
|
|
availability_zone,
|
|
instance_type
|
|
) a
|
|
FULL OUTER JOIN
|
|
(SELECT
|
|
placement_availability_zone AS availability_zone,
|
|
instance_type,
|
|
COUNT(*) AS ec2_count
|
|
FROM
|
|
aws_ec2_instance
|
|
WHERE
|
|
instance_state='running' AND
|
|
instance_lifecycle!='spot'
|
|
GROUP BY
|
|
availability_zone,
|
|
instance_type
|
|
) b
|
|
ON
|
|
a.availability_zone = b.availability_zone AND
|
|
a.instance_type = b.instance_type
|
|
INNER JOIN
|
|
(SELECT
|
|
instance_type,
|
|
(CAST(memory_info ->> 'SizeInMiB' AS FLOAT) / 1024) AS memory,
|
|
(CAST(v_cpu_info ->> 'DefaultCores' AS FLOAT) * 2) AS cpu
|
|
FROM
|
|
aws_ec2_instance_type
|
|
WHERE
|
|
instance_type in (SELECT instance_type FROM aws_ec2_instance WHERE instance_state='running' AND instance_lifecycle!='spot')
|
|
GROUP BY
|
|
instance_type, memory, cpu
|
|
) c
|
|
ON
|
|
COALESCE(a.instance_type, b.instance_type, '-') = c.instance_type
|
|
ORDER BY availability_zone
|
|
"""
|
|
#------------------------------------------------------------------------------------------------------
|
|
#git_path="/home/jhjung/git/dsk-iac"
|
|
origin="$(pwd)/org_README.md"
|
|
exec_log="query.log"
|
|
file="../../README.md"
|
|
#------------------------------------------------------------------------------------------------------
|
|
__init
|
|
|
|
title="## 노드 목록"
|
|
__query_exec "${node_query}"
|
|
__append
|
|
|
|
title="## 리소스 목록"
|
|
__query_exec "${resources_query}"
|
|
__append
|
|
|
|
title="## 서비스 목록 (NodePort)"
|
|
__query_exec "${service_query}"
|
|
__append
|
|
|
|
title="## 예약 인스턴스 사용 내역"
|
|
__query_exec "${aws_ri_query}"
|
|
__append
|
|
#------------------------------------------------------------------------------------------------------
|
|
rm ${exec_log}
|
|
|
|
__git_push
|