Skip to content

Instantly share code, notes, and snippets.

@shashyajoshi
shashyajoshi / get_instance_data.sh
Last active December 1, 2023 06:02
Script to collect instance labels and metadata
#!/bin/bash
# Header row for the CSV output
echo "project_id,name,labels-app-name,labels-biz-unit,labels-env-name,metadata-server-role,metadata-server-type,metadata-os-image"
# for every project in the list get the required details. Remove CSV header from the gcloud output. Add the project id to the beginning of each row
for project in $(gcloud projects list --format="value(projectId)")
do
gcloud compute instances list --format="csv[no-heading](name, labels.app-name, labels.biz-unit, labels.env-name, metadata.items.server-role, metadata.items.server-type, metadata.items.os-image)" --project $project | sed "s/^/$project,/"
done
@shashyajoshi
shashyajoshi / get_instance_data.output
Created July 2, 2018 21:19
Output of get_instance_data.sh
$ bash get_instance_data.sh
project_id,name,labels-app-name,labels-biz-unit,labels-env-name,metadata-server-role,metadata-server-type,metadata-os-image
sj-test-070418,kube-master,micro-svc-app,it,qa,kubernetes,master,centos
sj-test-070218,db-server-1,myapp,engineering,non-prod,db,mysql,centos
sj-test-062218,app-server-1,misc,ecom,prod,app,tomcat,centos
sj-test-062218,web-server-1,misc,ecom,prod,web,nginx,centos
sj-test-062218,web-server-2,misc,ecom,prod,web,nginx,centos
@shashyajoshi
shashyajoshi / gcloud_projects_list.output
Last active April 15, 2019 12:01
Output of the gcloud projects list command in json format
$ gcloud projects list --format json
[
{
"createTime": "2018-07-02T14:13:29.262Z",
"labels": {
"app-name": "micro-svc-app",
"biz-unit": "it",
"env-name": "qa",
"owner": "john-smith"
},
@shashyajoshi
shashyajoshi / gcloud_projects_list_csv.output
Last active July 2, 2018 21:33
Output of gcloud project list command in CSV format
1)
$ gcloud projects list --format="csv(projectId, name, labels)"
project_id,name,labels
sj-test-070418,sj-test-070418,app-name=micro-svc-app;biz-unit=it;env-name=qa;owner=john-smith
sj-test-070218,sj-test-070218,app-name=myapp;biz-unit=engineering;env-name=non-prod;owner=shashank
sj-test-062218,sj-test-062218,app-name=misc;biz-unit=ecom;env-name=prod;owner=jane-doe
secure-moment-207103,My First Project,
2)
@shashyajoshi
shashyajoshi / gcloud_compute_instances_list.output
Last active July 2, 2018 22:55
Output of the gcloud instances list command in json format
$ gcloud compute instances list --format json
(~~ part of the response ~~)
"id": "3091147347390362603",
"kind": "compute#instance",
"labels": {
"app-name": "misc",
"biz-unit": "ecom",
"env-name": "prod",
},
"metadata": {
@shashyajoshi
shashyajoshi / gcloud_compute_instances_list_csv.output
Created July 2, 2018 21:34
Output of the gcloud instances list command in CSV format
$ gcloud compute instances list - format="csv(name,labels.app-name,labels.biz-unit,labels.env-name,metadata.items.server-role,metadata.items.server-type, metadata.items.os-image)"
name,app-name,biz-unit,env-name,server-role,server-type,os-image
app-server-1,misc,ecom,prod,app,tomcat,centos
web-server-1,misc,ecom,prod,web,nginx,centos
web-server-2,misc,ecom,prod,web,nginx,centos
@shashyajoshi
shashyajoshi / get_license_data.sh
Last active July 2, 2018 23:36
Script to collect OS license data from the instances
#!/bin/bash
# Header row for the CSV output
echo "project_id,instance_name,license"
# for every project in the list get the required details. Remove CSV header from the gcloud output. Add the project id to the beginning of each row
for project in $(gcloud projects list --format="value(projectId)")
do
# Because of the nested structure you get ']] at the end of the output. Use sed to remove it
gcloud compute instances list --format="csv[no-heading](name,disks.licenses.basename())" --project $project | sed s/\']]//g | sed "s/^/$project,/"
@shashyajoshi
shashyajoshi / get_license_data.output
Created July 2, 2018 21:50
Output of the get_license_data.sh script
$ bash get_license_data.sh
project_id,instance_name,license
sj-test-070418,kube-master,centos-7
sj-test-070218,db-server-1,centos-7
sj-test-062218,app-server-1,centos-7
sj-test-062218,web-server-1,centos-7
sj-test-062218,web-server-2,centos-7
sj-test-062218,test-redhat,rhel-7-server
@shashyajoshi
shashyajoshi / get_disks_data.sh
Last active July 2, 2018 23:36
Script to collect disk user data
#!/bin/bash
# Header row for the CSV output
echo "project_id,disk_name,user"
# for every project in the list get the required details. Remove CSV header from the gcloud output. Add the project id to the beginning of each row
for project in $(gcloud projects list --format="value(projectId)")
do
# Because of the nested structure you get '] at the end of the output. Use sed to remove it
gcloud compute disks list --format="csv[no-heading](name,users.basename())" --project $project | sed s/\']//g | sed "s/^/$project,/"
@shashyajoshi
shashyajoshi / get_disks_data.output
Created July 2, 2018 22:14
Output of the get_disks_data.sh script
$ bash get_disk_data.sh
project_id,disk_name,user
sj-test-070418,kube-master,kube-master
sj-test-070218,orphan-disk-2,
sj-test-070218,db-server-1,db-server-1
sj-test-062218,orphan-disk-1,
sj-test-062218,app-server-1,app-server-1
sj-test-062218,web-server-1,web-server-1
sj-test-062218,web-server-2,web-server-2