Repository navigation
Expand file tree
/
Copy path.bashrc
More file actions
executable file
·3353 lines (2728 loc) · 95.6 KB
/
Copy path.bashrc
File metadata and controls
executable file
·3353 lines (2728 loc) · 95.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################################################################################
# BASHRC CONFIGURATION
################################################################################
#
# A comprehensive collection of bash configurations, aliases, functions, and
# utilities for command-line productivity. This file includes:
#
# - Shell options and behavior configuration
# - Environment variables and PATH management
# - Generic aliases and typo corrections
# - Git/GitHub workflow helpers
# - Docker and Kubernetes utilities
# - Network, ssh, MAC, and video utilities
# - Shortcuts for many programming languages
#
################################################################################
################################################################################
# SHELL OPTIONS
################################################################################
# Enable vi-mode for command line editing. Allows using vi/vim keybindings
# for navigating and editing commands (press ESC to enter command mode).
set -o vi
# Enable extended pattern matching features (like !(pattern), ?(pattern), etc.)
# Useful for advanced glob patterns in pathname expansion.
shopt -s extglob
# Append to history file rather than overwriting it. Prevents loss of history
# when running multiple terminal sessions simultaneously.
shopt -s histappend
# Set default file creation permissions. Files created will have permissions
# 644 (rw-r--r--) and directories 755 (rwxr-xr-x).
umask 0022
################################################################################
# ENVIRONMENT VARIABLES
################################################################################
# Suppress macOS deprecation warning about bash (macOS prefers zsh)
# Reference: https://www.addictivetips.com/mac-os/hide-default-interactive-shell-is-now-zsh-in-terminal-on-macos/
export BASH_SILENCE_DEPRECATION_WARNING=1
# Enable colored output for ls and other commands on macOS
export CLICOLOR=1
# Set vi as the default text editor
export EDITOR=vi
# Configure pager behavior:
# -X: Don't clear screen on exit
# -F: Quit if entire file fits on one screen
# -R: Allow ANSI color escape sequences
export LESS="-XFR"
# Define standard location for source code repositories
export SRC_HOME="${HOME}/src"
# MySQL client library path
# export DYLD_LIBRARY_PATH=/usr/local/opt/mysql-client/lib/
################################################################################
# UNFORTUNATELY COMMON TYPOS. UGH.
################################################################################
alias bi='vi'
alias del='rm'
alias vo='vi'
alias vu='vi'
alias dir='la'
alias dor='la'
alias dri='la'
alias dur='la'
alias mroe='more'
alias copy='cp'
alias move='mv'
alias ,,='..'
alias ,,,='...'
alias ,,,,='....'
alias .ale='make'
################################################################################
# PERFORMANCE TESTING
################################################################################
alias t='TIMEFORMAT="That took %1R seconds" && time'
alias curly='curl -w "@${HOME}/.curl_format" -o /dev/null -s -v'
alias ip='curl -s http://ipecho.net/plain; echo'
alias loadspeed='phantomjs $HOME/.loadspeed.js'
################################################################################
# SHORTCUT ALIASES
################################################################################
alias m='make'
alias la='ls -la'
alias dirw='la | grep drw'
alias j='jobs'
alias 1='fg %1'
alias 2='fg %2'
alias 3='fg %3'
alias 4='fg %4'
alias bas='vi ~/.bashrc; sleep 0.1; . ~/.bashrc'
alias bass='vi ~/.bashrc_private; sleep 0.1; . ~/.bashrc'
alias sudo='sudo ' # from http://www.shellperson.net/using-sudo-with-an-alias/
alias du1='du -h -d 1 | sort -h'
alias du2='du -h -d 2 | sort -h'
alias dfk='df -h -k'
alias tl='tail -f'
alias beep='for i in {1..3} ; do tput bel; sleep 0.5; done' # TODO: no worky
alias grepl='grep --line-buffered'
alias noempty='egrep --line-buffered -v "^[[:space:]]*$"'
alias nojello='grep --line-buffered -v jello'
alias ports='netstat -tulan'
alias utc='date -u'
alias ut='utc'
alias today='note-exact today'
alias x=exit
alias hosts='sudo vi /etc/hosts'
alias teep='pmset sleepnow'
# Directories
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .3='cd ../../..'
alias .4='cd ../../../..'
alias .5='cd ../../../../..'
alias dot='cd ${HOME}/.dotfiles'
alias leet='cd ${SRC_HOME}/leet'
alias src='cd $SRC_HOME'
# Runs the last non-'yolo' command with sudo, preventing recursive calls
function yolo {
local cmd="$(history | grep -v -E '^\s*[0-9]+\s+yolo' | tail -1 | head -1 | sed -E 's/^[ ]*[0-9]+[ ]+//')"
if [[ "$cmd" == "yolo" || "$cmd" == "please" ]]; then
echo "yolo: Refusing to run 'yolo' recursively"
return 1
fi
echo "yolo: ${cmd}"
eval "sudo $cmd"
}
alias please='yolo'
# Sources the given file if it exists, printing status messages
function include {
local file="$1"
if [ -f "$file" ] ; then
echo " Including ${file}"
source "$file"
else
echo " Not including ${file}"
fi
}
# OMG so handy
function mcd {
mkdir -p "$1"
cd "$1"
}
# Also OMG so handy. Shows processes matching a case-insensitive search string, excluding grep itself
function pz {
ps -aef | grep -i $1 | grep -v grep
}
# Fetches and pretty-prints geographic and network info for a given IP or current IP using ipinfo.io
function geo {
local ip="$1"
# if no IP specified, use current
local url="https://ipinfo.io"
if [ -n "$ip" ]; then
url+="/${ip}/json"
fi
# get it and output it
local info=$(curl -s ${url})
echo ${info} | jq .
}
# Generates a wildcard SSL certificate signing request and private key for a given domain
function wildcard_csr {
local domain=$1
openssl req -nodes -newkey rsa:2048 -nodes -keyout $domain.key -out $domain.csr -subj "/C=US/ST=${STATE}/L=${CITY}/O=${ORG}/CN=*.$domain"
}
# Runs a command with nohup and times its execution, redirecting output to nohup.out
function nohuptime {
nohup bash -c 'time $* > nohup.out 2>&1'
}
################################################################################
# IDEMPOTENT PATHS
################################################################################
function append_to_path {
for d; do
case ":$PATH:" in
*":$d:"*) :;;
*) PATH=$PATH:$d;;
esac
done
}
function prepend_to_path {
for d; do
case ":$PATH:" in
*":$d:"*) :;;
*) PATH=$d:$PATH;;
esac
done
}
append_to_path /usr/local/opt/coreutils/libexec/gnubin
append_to_path /usr/local/bin
append_to_path /usr/local/sbin
append_to_path ${HOME}/bin
################################################################################
# GIT/GITHUB
################################################################################
alias b='git co --track' # b <branch_name>
alias bfg='/usr/local/opt/openjdk/bin/java -jar /usr/local/Cellar/bfg/1.14.0/libexec/bfg-1.14.0.jar'
alias bis='git bisect'
alias bs='git branch' # list all branches
alias bs-dates="git for-each-ref --sort=committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'"
alias branch='co -b'
alias copilot-update='( cd ~/.vim/pack/github/start/copilot.vim && gpl && cd - )'
alias gi='git'
alias god='git'
alias got='git'
alias gut='git'
alias gd='git diff'
alias gds='gd --staged'
alias ga='git add'
alias gr='git restore'
alias st='git status'
alias co='git checkout'
alias gpl='git pull origin `git rev-parse --abbrev-ref HEAD`'
alias gps='git push origin `git rev-parse --abbrev-ref HEAD`'
alias stash='git stash'
alias stahs='stash'
alias sta='stash'
alias master='co master'
alias main='co main'
alias dev='co dev'
export GIT_PS1_SHOWSTASHSTATE=true
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUPSTREAM="auto"
include ~/.git-prompt.sh
include ~/.git-completion.bash
function wip {
local COMMENT="$*"
git commit -m "WIP: $COMMENT"
gps
}
function git-show-largest-files {
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| awk '/^blob/ {print substr($0,6)}' \
| sort --numeric-sort --key=2 --reverse \
| head -20 \
| cut --complement --characters=13-40 \
| numfmt --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest
}
function branchd {
branch=$1
if [ "$branch" = "" ]; then
echo "No branch specified. Exiting."
return
fi
git branch -D $1
git push origin --delete $1
}
function branchm {
branch=$1
if [ "$branch" = "" ]; then
echo "No branch specified. Exiting."
return
fi
git checkout -b $1
git push origin $1
cd -
}
function tag-del {
git tag --delete $1
git push --delete origin $1
}
function tag-list {
for tag in `git tag`
do
echo "`git rev-list -n 1 $tag`: $tag"
done
}
function tag-update-local {
git fetch origin --tags
}
function gc {
git commit -m '$@'
}
function pr {
local title="$1"
# if [ -z "$title" ]; then
# echo
# echo " Please set the title of the PR"
# echo
# return
# fi
local output=`gh pr create --base main --fill 2>&1`
if [ $? -ne 0 ]; then
echo
echo "ERROR"
echo "ERROR"
echo
echo "$output"
echo
echo "ERROR"
echo "ERROR"
echo
return
fi
echo $output
local url=`echo $output | grep https`
echo "$url is our URL"
open "$url"
}
function beeper {
runs=`gh run list --limit 30 --repo ${REPO}`
in_progress_ids=`echo "$runs" | grep in_progress | tr -d ' ' | cut -d$'\t' -f7`
}
################################################################################
# GITHUB ACTIONS
################################################################################
# alias w='g && cd workflows'
# alias sc='g && cd scripts'
################################################################################
# DOCKER
################################################################################
alias d='docker'
alias dc='d container'
alias di='d image'
alias dps='d ps -a'
alias drun='docker container run'
function ubuntu-R {
run-in-container flex "/usr/bin/R --no-save -q"
}
alias dr=ubuntu-R
function dbash {
# run-in-container $1 /bin/bash
local image="$1"
# local image_full="gcr.io/${GCLOUD_PROJECT}/$image"
if [[ "$image" == *"/"* ]]; then
local image_full="$image"
else
local image_full="gcr.io/${GCLOUD_PROJECT}/$image"
fi
echo
echo " Entering $image_full"
echo
docker run -it --entrypoint /bin/bash "$image_full"
}
alias dbash-flex='dbash flex'
alias dbash-composer='dbash composer'
alias dbash-composer-r='dbash composer-r'
alias dbash-r='dbash rocker/tidyverse:4.0.4'
function run-in-container {
local image="$1"
local cmd="$2"
local image_full="gcr.io/${GCLOUD_PROJECT}/$image"
if [[ "$image" == *"/"* ]]; then
image_full="$image"
fi
echo "image: $image_full"
# start it, if its not running
local container_id=`docker ps | grep $image_full | cut -d' ' -f1`
if [ -z "$container_id" ]; then
# echo "Starting container $image_full"
started_id=`docker container run -p 8000:80 -d --hostname $image --detach $image_full`
container_id=$started_id
fi
container_id=`echo $container_id | cut -c-12` # make it a short id
echo
echo " Entering $image_full container $container_id"
echo
docker container exec -it $container_id $cmd
# stop it, if it wasnt running
if [ -n "$started_id" ]; then
docker container stop -t 0 $started_id > /dev/null
fi
}
################################################################################
# KUBERNETES
################################################################################
export USE_GKE_GCLOUD_AUTH_PLUGIN=True
alias k=kubectl
alias desc='k describe'
alias kns=kubens
alias kaf='k apply -f'
alias kcf='k create --save-config -f'
alias kall='k get all -A --show-labels'
alias ke='k exec'
alias kc='k config'
alias kl='k logs --timestamps --prefix -f'
function kcs {
local substring="$1"
if [ -z "$substring" ]; then
echo
echo "Please enter a partial context name:"
echo
kubectl config get-contexts | tr -s " " | sed -e "s/ /,/g" | cut -d',' -f1-2 | tr -s "," "\t" | grep -v CURRENT
echo
return
fi
local new_context=`kc get-contexts -o name | grep "$substring" | head -1`
# echo "Setting context to: $new_context"
echo
kc use-context $new_context
echo
kc get-contexts | tr -s " " | sed -e "s/ /,/g" | cut -d',' -f1-2 | tr -s "," "\t" | grep -v CURRENT
echo
}
function kg {
kubectl get $*
}
function kj {
kg $* -o json | jq .
}
function ky {
kg $* -o yaml | yq .
}
function pod-bash {
local pod_pattern="$1"
local pods=`podsa | grep "$pod_pattern" | tr -s ' '`
local namespace=`echo $pods | cut -d' ' -f1`
local pod_name=`echo $pods | cut -d' ' -f2`
echo "Bashing into: $pod_name"
local shell=/bin/bash
if [ "$namespace" != "default" ]; then
echo "Using /bin/sh instead..."
shell=/bin/sh
fi
if [[ "$pod_name" == *"rocky"* ]]; then
echo "Using /bin/sh instead..."
shell=/bin/sh
fi
if [[ "$pod_name" == *"haproxy"* ]]; then
echo "Using /bin/sh instead..."
shell=/bin/sh
fi
kubectl exec --namespace "$namespace" -it "$pod_name" -- "$shell"
# bp "$pod"
}
function pod-log {
local pod_pattern="$1"
local logfile="$2"
local pod=`podsa | grep -v NAME | sort | grep "$pod_pattern" | grep Running | head -1 | tr -s ' ' | cut -d' ' -f2`
# echo "pod is $pod"
# echo "logfile is $logfile"
if [ -n "$logfile" ]; then
echo "Tailing pod: $pod logfile: "$logfile""
filename="/var/log/uwsgi-$logfile.log"
k exec "$pod" -- ls -al "$filename"
echo
sleep 1
k exec "$pod" -- tail -f "$filename"
else
echo "Tailing pod: $pod"
kl "$pod"
fi
}
function pod-log-api-http {
pod-log api api-http
}
function pod-log-api-ws {
pod-log api api-ws
}
function pod-log-hap {
pod-log hap | grep -v "STATS\|GET / \|NOSRV\|SSL handshake failure"
}
function pod-log-all {
kl -l org=${KUBE_ORG}
}
function clients {
curl -s https://ws.${DNS_SUFFIX}/clients | jq .
}
function clients-local {
curl -s http://localhost:81/clients | jq .
}
function node-pods {
local node="$1"
if [ -z "$node" ]; then
echo
echo " I need a node name. Here are our nodes:"
echo
kg nodes
echo
return
fi
kg node "$node" -o json | jq .status.images[].names | grep -v "gcr.io\|\]\|\[" | sort
}
function ingress-annotations {
local ingress_name=$1
if [ -z $ingress_name ]; then
ingress_name=ingress-${KUBE_ORG}
fi
# echo "Ingress: $ingress_name"
# ret=`kubectl get ingress $ingress_name -o json`
ret=`kubectl get ingress $ingress_name -o json | jq .metadata.annotations`
echo "$ret" | jq -s .
}
function forwarding-rules {
annotations=`ingress-annotations | grep forwarding-rule`
echo "$annotations" | tr -s ' ' | cut -d' ' -f3 | tr -d '"' | tr -d ','
}
alias configmaps='kg configmaps -A'
alias daemonsets='kg daemonsets -A -o wide'
alias dms=daemonsets
alias deps='kg deployments'
alias depsa='deps -A'
alias events='kg events -w --sort-by=.metadata.creationTimestamp'
alias eventsa='events -A'
alias nodes='kg nodes -o wide'
alias nodesa='nodes -A'
alias pods='kg pods -o wide | grep -v Evicted'
alias podsa='kg pods -o wide -A | grep -v Evicted'
alias podsc='pods | grep "Running\|Creat" | grep -v "^airflow\|composer-fluentd" | grep -v Error'
alias pod-nodes=node-pods
alias ingress='kg ingress -A'
alias ingresses=ingress
alias services='kg services -o wide'
alias servicesa='services -A'
alias secrets='kg secrets'
alias secretsa='secrets -A'
alias replicasets='kg replicasets -o wide -A'
alias replicasetsa='replicasets -A'
alias cluster-dump='kubectl cluster-info dump'
alias clusters='gcloud container clusters list'
alias gcompute='gcloud compute'
function cluster {
gcloud container clusters describe --format json $1 | jq .
}
function kall-default {
kall-iterate default
}
function kall-iterate {
local namespace="$1"
for i in $(kubectl api-resources --verbs=list --namespaced -o name | grep -v "events" | sort | uniq)
do
echo "Resource:" $i; kubectl -n ${namespace} get --ignore-not-found ${i}
done
}
function kswitch {
local new_context=$1
cd ${SRC_HOME}/etc/docker
make needs-credentials APP=$new_context
cd -
}
function __kube_ps1()
{
# Get current context
local CONTEXT=$(grep "current-context:" ~/.kube/config | sed "s/current-context: gke_${GCLOUD_PROJECT}_us-east1-b_//")
if [ -n "$CONTEXT" ]; then
echo "[kube:${CONTEXT}]"
fi
}
if [ -f $HOME/.bashrc_kubectl ]; then
source $HOME/.bashrc_kubectl
complete -F __start_kubectl k # from https://kubernetes.io/docs/reference/kubectl/cheatsheet/
fi
################################################################################
# GOOGLE CLOUD CONTAINER METADATA
################################################################################
function metadata-query {
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/$1" -H "Metadata-Flavor: Google"
}
function metadata-tags {
metadata-query tags
}
function metadata-disks {
metadata-query "disks/?recursive=true"
}
function metadata-env {
metadata-query attributes/kube-env
}
function metadata-name {
metadata-query name
}
################################################################################
# GOOGLE CLOUD
################################################################################
alias gcluster='gcloud container clusters'
alias gcl='gcluster'
alias gql='gcloud beta sql'
alias gdb='gcloud sql instances'
alias gdbs='gdb list'
alias gl='gcloud beta logging'
alias gpub='gcloud pubsub'
alias gtopic='gpub topics'
alias gsub='gpub subscriptions'
alias sshg='gcloud compute ssh'
export CLOUDSDK_PYTHON_SITEPACKAGES=1
export CLOUDSDK_PYTHON=/usr/local/bin/python3
function lsg {
gsutil ls -l gs://$1
}
function images {
local image="$1"
local REPOSITORY="gcr.io/${GCLOUD_PROJECT}"
if [ -z "$image" ]; then
gcloud container images list --repository=$REPOSITORY
return
fi
if [[ "$image" != "$REPOSITORY"* ]]; then
image="$REPOSITORY/$image"
fi
gcloud container images list-tags $image
}
# Queries the loadbalancer logs
function glog {
# BACKEND SERVICES
# gcloud compute backend-services list
# gcloud compute backend-services describe --global --format=json k8s1-18cca515-default-service-api-80-9719b3d2
# FORWARDING RULES
# gcloud compute forwarding-rules list
# gcloud compute forwarding-rules describe --global --format=json k8s2-fs-dh9u9nk2-default-ingress-9ye9weet | jq .
# HELPFUL DOCS
# https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest
# https://cloud.google.com/logging/docs/logs-views
# https://cloud.google.com/logging/docs/buckets
# EXAMPLE LOG ENTRY FROM THE LOAD BALANCER
# {
# "http_request": {
# "cache_fill_bytes": "0",
# "cache_hit": false,
# "cache_lookup": false,
# "cache_validated_with_origin_server": false,
# "latency": "0.025376s",
# "referer": "",
# "remote_ip": "35.196.21.140",
# "request_method": "GET",
# "request_size": "198",
# "request_url": "https://papi.${DNS_SUFFIX}/schedule/near/143",
# "response_size": "1355",
# "server_ip": "10.88.1.139",
# "status": 200,
# "user_agent": "python-requests/2.25.1"
# },
# "insert_id": "1oi2nmwf4wj376",
# "json_payload": {
# "@type": "type.googleapis.com/google.cloud.loadbalancing.type.LoadBalancerLogEntry",
# "statusDetails": "response_sent_by_backend"
# },
# "labels": {},
# "log_name": "projects/${GCLOUD_PROJECT}/logs/requests",
# "receive_timestamp": "2021-09-10T22:28:33.356191462Z",
# "resource": {
# "labels": {
# "backend_service_name": "k8s1-18cca515-default-service-api-80-9719b3d2",
# "forwarding_rule_name": "k8s2-fs-dh9u9nk2-default-ingress-9ye9weet",
# "project_id": "${GCLOUD_PROJECT}",
# "target_proxy_name": "k8s2-ts-dh9u9nk2-default-ingress-9ye9weet",
# "url_map_name": "k8s2-um-dh9u9nk2-default-ingress-9ye9weet",
# "zone": "global"
# },
# "type": "http_load_balancer"
# },
# "severity": 200,
# "span_id": "ad5ce504c108a19d",
# "timestamp": "2021-09-10T22:28:32.701888Z",
# "trace": "projects/${GCLOUD_PROJECT}/traces/0474a1e41cfb053be0fd9c422bc292ff",
# "trace_sampled": false
# }
local clause="$1"
if [ -z "$clause" ]; then
echo
echo " Please supply a clause:"
echo ' glog tail # live tail'
echo ' glog "http_request.status = 200" # http successes'
echo ' glog "http_request.status >= 400" # http errors'
echo ' glog "http_request.request_method=GET" # GET requests'
echo ' glog "http_request.user_agent:Mozilla" # hits by Mozilla'
echo ' glog "http_request.remote_ip=35.196.21.140" # hits from megacron'
echo ' glog "http_request.request_url:schedule/near" # hits to the schedule/near endpoint'
echo ' glog "NOT http_request.request_url:schedule/near" # hits NOT to the schedule/near endpoint'
echo ' glog "timestamp <= \"2021-09-14T11:01:00.0Z\" AND timestamp >= \"2021-09-14T11:00:00.0Z\"" ' # hits during a timeperiod
echo
return
fi
local bucket=${BUCKET_LOADBALANCER_LOG}
local location=us-east1
local resource=resource.type=http_load_balancer
local ts_begin=receive_timestamp:label=ts-begin
local ts_end=timestamp:label=ts-end
local status=http_request.status
local method=http_request.request_method:label=method
local ip=http_request.remote_ip:label=ip
local url=http_request.request_url:label=url
local user_agent=http_request.user_agent:label=user-agent
local duration=duration\(start=timestamp,end=receive_timestamp,precision=3\)
local backend=resource.labels.backend_service_name:label=backend-service
local forwarding=resource.labels.forwarding_rule_name:label=forwarding-rule
local items=$ts_begin,$ts_end,$duration,$status,$method,$backend,$forwarding,$ip,$url,$user_agent
local sep=','
# sep="\\t"
local format="csv[separator='$sep']"
local format_arg="$format($items)"
if [ "$clause" = "tail" ]; then
# Tails the live loadbalancer log from stackdriver
gcloud alpha logging tail \
$resource \
--format="$format_arg"
# --buffer-window=1s \
return
fi
# Reads the logging bucket
gcloud logging read \
--bucket=$bucket --location=$location \
--format="$format_arg" \
--view=lb-all \
"$resource AND $clause"
}
function glog-errors {
glog "http_request.status >= 400"
}
function glog-net {
# glog-net 10.142.0.55
# Tails the log, looking for australia connections to prod-lb-2
# NOTE: Not all log entries are identical - they vary in their json_payload!
# A couple examples are below:
#
# EXAMPLE LOG ENTRY FROM THE VPC FLOW LOG:
#
# insert_id: lnfc9wfsnlqnr
# json_payload:
# bytes_sent: '0'
# connection:
# dest_ip: 10.142.0.55
# dest_port: 59748.0
# protocol: 6.0
# src_ip: 10.142.0.14
# src_port: 81.0
# dest_instance:
# project_id: gcloud-project-name
# region: us-east1
# vm_name: prod-lb-2
# zone: us-east1-b
# dest_vpc:
# project_id: gcloud-project-name
# subnetwork_name: default
# vpc_name: default
# end_time: '2022-02-26T00:38:56.801628410Z'
# packets_sent: '32'
# reporter: SRC
# src_instance:
# project_id: gcloud-project-name
# region: us-east1
# vm_name: prod-box-1
# zone: us-east1-b
# src_vpc:
# project_id: gcloud-project-name
# subnetwork_name: default
# vpc_name: default
# start_time: '2022-02-26T00:38:56.801628410Z'
# labels: {}
# log_name: projects/gcloud-project-name/logs/compute.googleapis.com%2Fvpc_flows
# receive_timestamp: '2022-02-26T00:39:06.260415192Z'
# resource:
# labels:
# location: us-east1-b
# project_id: gcloud-project-name
# subnetwork_id: '2966778331651493890'
# subnetwork_name: default
# type: gce_subnetwork
# severity: 0
# span_id: ''
# timestamp: '2022-02-26T00:39:06.260415192Z'
# trace: ''
# trace_sampled: false
#
# THIS EXAMPLE HAS DEST_GKE_DETAILS:
#
# insert_id: v32pezfspm26f
# json_payload:
# bytes_sent: '12672'
# connection:
# dest_ip: 10.48.10.7
# dest_port: 53.0
# protocol: 17.0
# src_ip: 10.48.14.4
# src_port: 37597.0
# dest_gke_details:
# cluster:
# cluster_location: us-east1-b
# cluster_name: us-east1-cluster-d40432e2-gke
# pod:
# pod_name: kube-dns-7f4d6f474d-xzfnt
# pod_namespace: kube-system
# service:
# - service_name: kube-dns
# service_namespace: kube-system
# dest_instance:
# project_id: gcloud-project-name
# region: us-east1
# vm_name: gke-us-east1-cluster-d40-nodepool-airflow-15e689e0-00sg
# zone: us-east1-b
# dest_vpc:
# project_id: gcloud-project-name
# subnetwork_name: default
# vpc_name: default
# end_time: '2022-02-26T00:39:02.801958676Z'
# packets_sent: '128'
# reporter: DEST
# src_gke_details:
# cluster:
# cluster_location: us-east1-b
# cluster_name: us-east1-cluster-d40432e2-gke
# pod:
# pod_name: airflow-scheduler-6d9f76664f-gppx2
# pod_namespace: composer-1-16-15-airflow-1-10-15-d40432e2
# src_instance:
# project_id: gcloud-project-name
# region: us-east1
# vm_name: gke-us-east1-cluster-d40432e-default-pool-8e49d417-yx6c
# zone: us-east1-b
# src_vpc:
# project_id: gcloud-project-name
# subnetwork_name: default
# vpc_name: default
# start_time: '2022-02-26T00:39:02.801958676Z'
# labels: {}
# log_name: projects/gcloud-project-name/logs/compute.googleapis.com%2Fvpc_flows
# receive_timestamp: '2022-02-26T00:39:06.591510802Z'
# resource:
# labels:
# location: us-east1-b
# project_id: gcloud-project-name
# subnetwork_id: '2966778331651493890'
# subnetwork_name: default
# type: gce_subnetwork
# severity: 0
# span_id: ''
# timestamp: '2022-02-26T00:39:06.591510802Z'
# trace: ''
# trace_sampled: false`
# THIS EXAMPLE HAS DEST_LOCATION:
# insert_id: tosr7afjhafxr
# json_payload:
# bytes_sent: '1300'
# connection:
# dest_ip: 34.73.91.160
# dest_port: 443.0
# protocol: 6.0
# src_ip: 10.142.0.8
# src_port: 51748.0
# dest_location:
# asn: 15169.0
# continent: America
# country: usa
# end_time: '2022-02-26T00:39:04.334656719Z'
# packets_sent: '8'
# reporter: SRC
# rtt_msec: '11'
# src_gke_details:
# cluster:
# cluster_location: us-east1-b
# cluster_name: blockhead-cluster
# src_instance:
# project_id: gcloud-project-name
# region: us-east1
# vm_name: gke-blockhead-cluster-default-pool-09d3a8c5-ifnp
# zone: us-east1-b
# src_vpc:
# project_id: gcloud-project-name
# subnetwork_name: default
# vpc_name: default
# start_time: '2022-02-26T00:39:02.476456466Z'
# labels: {}
# log_name: projects/gcloud-project-name/logs/compute.googleapis.com%2Fvpc_flows
# receive_timestamp: '2022-02-26T00:39:14.250142254Z'
# resource:
# labels:
# location: us-east1-b
# project_id: gcloud-project-name
# subnetwork_id: '2966778331651493890'
# subnetwork_name: default
# type: gce_subnetwork
# severity: 0
# span_id: ''
# timestamp: '2022-02-26T00:39:14.250142254Z'
# trace: ''