Skip to content

Instantly share code, notes, and snippets.

@kyle-hailey
Last active April 21, 2023 20:32
Show Gist options
  • Save kyle-hailey/b7527e38128660ad7c35f06fe820d1be to your computer and use it in GitHub Desktop.
Save kyle-hailey/b7527e38128660ad7c35f06fe820d1be to your computer and use it in GitHub Desktop.
using Google GCP API, get a list of Cloud SQL instances and create a yaml for datadog
USER=
PW=
DB=
DEBUG=1
# Debug function to print a message
debug() {
if [[ $DEBUG == 1 ]]; then
echo "# $1 (debug) "
# echo -e "$1 "
fi
}
# dummy entry for testing
entry=$( cat << EOF
- host: DB_PRIVATE_IP
reported_hostname: DB_NAME_DB_PUBLIC_IP
EOF
)
entry=$( cat << EOF
- host: DB_PRIVATE_IP
username: $USER
password: $PW
reported_hostname: DB_NAME-DB_PUBLIC_IP
collect_activity_metrics: true
collect_bloat_metrics: false
dbm: true
dbname: $DB
relations:
- relation_regex: .*
batch_max_concurrent_send: 100
samples:
batch_max_concurrent_send: 100
metrics:
batch_max_concurrent_send: 100
database_monitoring:
metrics:
batch_max_concurrent_send: 100
EOF
)
cmd=$( cat << EOF
cat db.json | jq '.items[] |
{name: .name, ipAddresses:
[.ipAddresses[] |
select(.type == "PRIVATE" or .type == "PRIMARY") |
.ipAddress] |
select(length > 0)} |
[.name] + (if .ipAddresses |
length == 1 then [.ipAddresses[0], .ipAddresses[0]]
else .ipAddresses end) | @csv' |\
sed -e 's;\\\\;;g' | sed -e 's/""/"/g'
EOF
)
for i in 1; do
cat << EOF
init_config:
instances:
EOF
done > /tmp/conf.yaml
for mystring in `eval $cmd`; do
debug "----"
debug $mystring
debug " "
export DB_NAME=$(echo $mystring | cut -d',' -f1 | sed -e 's/"//g' )
export DB_PUBLIC_IP=$(echo $mystring | cut -d',' -f2 |sed -e 's/"//g' )
export DB_PRIVATE_IP=$(echo $mystring | cut -d',' -f3| sed -e 's/"//g' )
debug "DB_NAME $DB_NAME"
echo -e "$entry" | \
sed -e "s/DB_NAME/$DB_NAME/" | \
sed -e "s/DB_PUBLIC_IP/$DB_PUBLIC_IP/" | \
sed -e "s/DB_PRIVATE_IP/$DB_PRIVATE_IP/"
debug "----"
debug " "
done >> /tmp/conf.yaml
sudo cp /etc/datadog-agent/conf.d/postgres.d/conf.yaml /etc/datadog-agent/conf.d/postgres.d/conf.yaml.$$
sudo cp /tmp/conf.yaml /etc/datadog-agent/conf.d/postgres.d/conf.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment