github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/introspection/script.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package introspection 5 6 import ( 7 "io/ioutil" 8 "path" 9 "runtime" 10 11 "github.com/juju/errors" 12 ) 13 14 var ( 15 profileDir = "/etc/profile.d" 16 bashFuncsFilename = "juju-introspection.sh" 17 ) 18 19 // WriteProfileFunctions writes the bashFuncs below to a file in the 20 // /etc/profile.d directory so all bash terminals can easily access the 21 // introspection worker. 22 func WriteProfileFunctions() error { 23 if runtime.GOOS != "linux" { 24 logger.Debugf("skipping profile funcs install") 25 return nil 26 } 27 filename := profileFilename() 28 if err := ioutil.WriteFile(filename, []byte(bashFuncs), 0644); err != nil { 29 return errors.Annotate(err, "writing introspection bash funcs") 30 } 31 return nil 32 } 33 34 func profileFilename() string { 35 return path.Join(profileDir, bashFuncsFilename) 36 } 37 38 const bashFuncs = ` 39 juju_agent_call () { 40 local agent=$1 41 shift 42 local path= 43 for i in "$@"; do 44 path="$path/$i" 45 done 46 juju-introspect --agent=$agent $path 47 } 48 49 juju_machine_agent_name () { 50 local machine=$(ls -d /var/lib/juju/agents/machine*) 51 machine=$(basename $machine) 52 echo $machine 53 } 54 55 juju_machine_or_unit () { 56 # First arg is the path, second is optional agent name. 57 if [ "$#" -gt 2 ]; then 58 echo "expected no args (for machine agent) or one (unit agent)" 59 return 1 60 fi 61 local agent=$(juju_machine_agent_name) 62 if [ "$#" -eq 2 ]; then 63 agent=$2 64 fi 65 juju_agent_call $agent $1 66 } 67 68 juju_goroutines () { 69 juju_machine_or_unit debug/pprof/goroutine?debug=1 $@ 70 } 71 72 juju_cpu_profile () { 73 N=30 74 if test -n "$1"; then 75 N=$1 76 shift 77 fi 78 echo "Sampling CPU for $N seconds." >&2 79 juju_machine_or_unit "debug/pprof/profile?debug=1&seconds=$N" $@ 80 } 81 82 juju_heap_profile () { 83 juju_machine_or_unit debug/pprof/heap?debug=1 $@ 84 } 85 86 juju_engine_report () { 87 juju_machine_or_unit depengine $@ 88 } 89 90 juju_statepool_report () { 91 juju_machine_or_unit statepool $@ 92 } 93 94 juju_pubsub_report () { 95 juju_machine_or_unit pubsub $@ 96 } 97 98 juju_metrics () { 99 juju_machine_or_unit metrics/ $@ 100 } 101 102 juju_presence_report () { 103 juju_machine_or_unit presence/ $@ 104 } 105 106 juju_statetracker_report () { 107 juju_machine_or_unit debug/pprof/juju/state/tracker?debug=1 $@ 108 } 109 110 juju_machine_lock () { 111 for agent in $(ls /var/lib/juju/agents); do 112 juju_machine_or_unit machinelock $agent 2> /dev/null 113 done 114 } 115 116 # This asks for the command of the current pid. 117 # Can't use $0 nor $SHELL due to this being wrong in various situations. 118 shell=$(ps -p "$$" -o comm --no-headers) 119 if [ "$shell" = "bash" ]; then 120 export -f juju_agent_call 121 export -f juju_machine_agent_name 122 export -f juju_machine_or_unit 123 export -f juju_goroutines 124 export -f juju_cpu_profile 125 export -f juju_heap_profile 126 export -f juju_engine_report 127 export -f juju_metrics 128 export -f juju_statepool_report 129 export -f juju_statetracker_report 130 export -f juju_pubsub_report 131 export -f juju_presence_report 132 export -f juju_machine_lock 133 fi 134 `