forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.go
154 lines (132 loc) · 3.41 KB
/
script.go
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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package introspection
import (
"io/ioutil"
"path"
"runtime"
"github.com/juju/errors"
)
var (
// ProfileDir is the directory where the profile script is written.
ProfileDir = "/etc/profile.d"
bashFuncsFilename = "juju-introspection.sh"
)
// WriteProfileFunctions writes the bashFuncs below to a file in the
// /etc/profile.d directory so all bash terminals can easily access the
// introspection worker.
func WriteProfileFunctions(profileDir string) error {
if runtime.GOOS != "linux" {
logger.Debugf("skipping profile funcs install")
return nil
}
filename := profileFilename(profileDir)
if err := ioutil.WriteFile(filename, []byte(bashFuncs), 0644); err != nil {
return errors.Annotate(err, "writing introspection bash funcs")
}
return nil
}
func profileFilename(profileDir string) string {
return path.Join(profileDir, bashFuncsFilename)
}
const bashFuncs = `
juju_agent_call () {
local agent=$1
shift
local path=
for i in "$@"; do
path="$path/$i"
done
juju-introspect --agent=$agent $path
}
juju_machine_agent_name () {
local machine=$(find /var/lib/juju/agents -type d -name 'machine*' -printf %f)
echo $machine
}
juju_controller_agent_name () {
local controller=$(find /var/lib/juju/agents -type d -name 'controller*' -printf %f)
echo $controller
}
juju_application_agent_name () {
local application=$(find /var/lib/juju/agents -type d -name 'application*' -printf %f)
echo $application
}
juju_agent () {
# First arg is the path, second is optional agent name.
if [ "$#" -gt 2 ]; then
echo "expected no args (for machine agent) or one (unit agent)"
return 1
fi
local agent
if [ "$#" -eq 2 ]; then
agent=$2
else
agent=$(juju_machine_agent_name)
if [ -z "$agent" ]; then
agent=$(juju_controller_agent_name)
fi
if [ -z "$agent" ]; then
agent=$(juju_application_agent_name)
fi
fi
juju_agent_call $agent $1
}
juju_goroutines () {
juju_agent debug/pprof/goroutine?debug=1 $@
}
juju_cpu_profile () {
N=30
if test -n "$1"; then
N=$1
shift
fi
echo "Sampling CPU for $N seconds." >&2
juju_agent "debug/pprof/profile?debug=1&seconds=$N" $@
}
juju_heap_profile () {
juju_agent debug/pprof/heap?debug=1 $@
}
juju_engine_report () {
juju_agent depengine $@
}
juju_statepool_report () {
juju_agent statepool $@
}
juju_pubsub_report () {
juju_agent pubsub $@
}
juju_metrics () {
juju_agent metrics/ $@
}
juju_presence_report () {
juju_agent presence/ $@
}
juju_statetracker_report () {
juju_agent debug/pprof/juju/state/tracker?debug=1 $@
}
juju_machine_lock () {
for agent in $(ls /var/lib/juju/agents); do
juju_agent machinelock $agent 2> /dev/null
done
}
# This asks for the command of the current pid.
# Can't use $0 nor $SHELL due to this being wrong in various situations.
shell=$(ps -p "$$" -o comm --no-headers)
if [ "$shell" = "bash" ]; then
export -f juju_agent_call
export -f juju_machine_agent_name
export -f juju_controller_agent_name
export -f juju_application_agent_name
export -f juju_agent
export -f juju_goroutines
export -f juju_cpu_profile
export -f juju_heap_profile
export -f juju_engine_report
export -f juju_metrics
export -f juju_statepool_report
export -f juju_statetracker_report
export -f juju_pubsub_report
export -f juju_presence_report
export -f juju_machine_lock
fi
`