Skip to content

Commit 6b11a5c

Browse files
committed
update docs and examples
1 parent 665bf7f commit 6b11a5c

File tree

4 files changed

+181
-70
lines changed

4 files changed

+181
-70
lines changed

docs/templates/agent-metadata.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ See the [Terraform reference](https://registry.terraform.io/providers/coder/code
1616
All of these examples use [heredoc strings](https://developer.hashicorp.com/terraform/language/expressions/strings#heredoc-strings) for the script declaration. With heredoc strings, you
1717
can script without messy escape codes, just as if you were working in your terminal.
1818

19+
Some of the below examples use the [`coder stat`](../cli/stat.md) command.
20+
This is useful for determining CPU/memory usage inside a container, which
21+
can be tricky otherwise.
22+
1923
Here's a standard set of metadata snippets for Linux agents:
2024

2125
```hcl
@@ -25,33 +29,51 @@ resource "coder_agent" "main" {
2529
metadata {
2630
display_name = "CPU Usage"
2731
key = "cpu"
28-
# calculates CPU usage by summing the "us", "sy" and "id" columns of
29-
# vmstat.
30-
script = <<EOT
31-
top -bn1 | awk 'FNR==3 {printf "%2.0f%%", $2+$3+$4}'
32-
EOT
32+
# Uses the coder stat command to get container CPU usage.
33+
script = "coder stat cpu"
3334
interval = 1
3435
timeout = 1
3536
}
3637
3738
metadata {
38-
display_name = "Disk Usage"
39-
key = "disk"
40-
script = "df -h | awk '$6 ~ /^\\/$/ { print $5 }'"
39+
display_name = "Memory Usage"
40+
key = "mem"
41+
# Uses the coder stat command to get container memory usage in GiB.
42+
script = "coder stat mem --prefix Gi"
4143
interval = 1
4244
timeout = 1
4345
}
4446
4547
metadata {
46-
display_name = "Memory Usage"
47-
key = "mem"
48+
display_name = "CPU Usage (Host)"
49+
key = "cpu_host"
50+
# calculates CPU usage by summing the "us", "sy" and "id" columns of
51+
# top.
52+
script = <<EOT
53+
top -bn1 | awk 'FNR==3 {printf "%2.0f%%", $2+$3+$4}'
54+
EOT
55+
interval = 1
56+
timeout = 1
57+
}
58+
59+
metadata {
60+
display_name = "Memory Usage (Host)"
61+
key = "mem_host"
4862
script = <<EOT
4963
free | awk '/^Mem/ { printf("%.0f%%", $4/$2 * 100.0) }'
5064
EOT
5165
interval = 1
5266
timeout = 1
5367
}
5468
69+
metadata {
70+
display_name = "Disk Usage"
71+
key = "disk"
72+
script = "df -h | awk '$6 ~ /^\\/$/ { print $5 }'"
73+
interval = 1
74+
timeout = 1
75+
}
76+
5577
metadata {
5678
display_name = "Load Average"
5779
key = "load"

dogfood/main.tf

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -93,106 +93,73 @@ resource "coder_agent" "dev" {
9393
}
9494
startup_script_behavior = "blocking"
9595

96+
# The following metadata blocks are optional. They are used to display
97+
# information about your workspace in the dashboard. You can remove them
98+
# if you don't want to display any information.
9699
metadata {
97100
display_name = "CPU Usage"
101+
key = "0_cpu_usage"
102+
script = "coder stat cpu"
98103
interval = 10
99104
timeout = 1
100-
key = "0_cpu_usage"
101-
script = <<EOT
102-
#!/bin/bash
103-
# check if we are in cgroup v2 or v1
104-
if [ -e /sys/fs/cgroup/cpu.stat ]; then
105-
# cgroup v2
106-
cusage=$(cat /sys/fs/cgroup/cpu.stat | head -n 1 | awk '{ print $2 }')
107-
else
108-
# cgroup v1
109-
# cusage=$(cat /sys/fs/cgroup/cpu,cpuacct/cpuacct.usage)
110-
echo "Coming Soon!"
111-
exit 0
112-
fi
113-
114-
# get previous usage
115-
if [ -e /tmp/cusage ]; then
116-
cusage_p=$(cat /tmp/cusage)
117-
else
118-
echo $cusage > /tmp/cusage
119-
echo "Unknown"
120-
exit 0
121-
fi
122-
123-
# interval in microseconds should be metadata.interval * 1000000
124-
interval=10000000
125-
ncores=$(nproc)
126-
echo "$cusage $cusage_p $interval $ncores" | awk '{ printf "%2.0f%%\n", (($1 - $2)/$3/$4)*100 }'
127-
128-
EOT
129105
}
130106

131107
metadata {
132108
display_name = "RAM Usage"
109+
key = "1_ram_usage"
110+
script = "coder stat mem"
133111
interval = 10
134112
timeout = 1
135-
key = "1_ram_usage"
136-
script = <<EOT
137-
#!/bin/bash
138-
# first check if we are in cgroup v2 or v1
139-
if [ -e /sys/fs/cgroup/memory.stat ]; then
140-
# cgroup v2
141-
echo "`cat /sys/fs/cgroup/memory.current` `cat /sys/fs/cgroup/memory.max`" | awk '{ used=$1/1024/1024/1024; total=$2/1024/1024/1024; printf "%0.2f / %0.2f GB\n", used, total }'
142-
else
143-
# cgroup v1
144-
echo "`cat /sys/fs/cgroup/memory/memory.usage_in_bytes` `cat /sys/fs/cgroup/memory/memory.limit_in_bytes`" | awk '{ used=$1/1024/1024/1024; total=$2/1024/1024/1024; printf "%0.2f / %0.2f GB\n", used, total }'
145-
fi
146-
EOT
147113
}
148114

149-
150115
metadata {
151116
display_name = "CPU Usage (Host)"
152-
key = "2_cpu_host"
153-
script = <<EOT
154-
vmstat | awk 'FNR==3 {printf "%2.0f%%", $13+$14+$16}'
155-
EOT
117+
key = "2_cpu_usage_host"
118+
script = "coder stat cpu --host"
156119
interval = 10
157120
timeout = 1
158121
}
159122

160123
metadata {
161-
display_name = "Memory and Swap Usage (Host)"
162-
key = "3_mem_swap_host"
124+
display_name = "RAM Usage (Host)"
125+
key = "3_ram_usage_host"
126+
script = "coder stat mem --host"
127+
interval = 10
128+
timeout = 1
129+
}
130+
131+
metadata {
132+
display_name = "Swap Usage (Host)"
133+
key = "4_swap_usage_host"
163134
script = <<EOT
164-
mem_usage=`free | awk '/^Mem/ { printf("%.0f%%", $3/$2 * 100.0) }'`
165-
swp_usage=`free | awk '/^Swap/ { printf("%.0f%%", $3/$2 * 100.0) }'`
166-
echo "Memory: $mem_usage, Swap: $swp_usage"
167-
EOT
135+
echo "$(free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }') GiB"
136+
EOT
168137
interval = 10
169138
timeout = 1
170139
}
171140

172141
metadata {
173142
display_name = "Load Average (Host)"
174-
key = "4_load_host"
143+
key = "5_load_host"
175144
# get load avg scaled by number of cores
176145
script = <<EOT
177146
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
178147
EOT
179-
interval = 10
148+
interval = 60
180149
timeout = 1
181150
}
182151

183152
metadata {
184153
display_name = "Disk Usage (Host)"
185-
key = "5_disk_host"
186-
script = <<EOT
187-
df --output=pcent / | sed -n "2p"
188-
EOT
154+
key = "6_disk_host"
155+
script = "coder stat disk --path /"
189156
interval = 600
190-
timeout = 10 #getting disk usage can take a while
157+
timeout = 10
191158
}
192159

193160
metadata {
194161
display_name = "Word of the Day"
195-
key = "6_word"
162+
key = "7_word"
196163
script = <<EOT
197164
curl -o - --silent https://www.merriam-webster.com/word-of-the-day 2>&1 | awk ' $0 ~ "Word of the Day: [A-z]+" { print $5; exit }'
198165
EOT

examples/templates/docker/main.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,72 @@ resource "coder_agent" "main" {
4646
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
4747
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
4848
}
49+
50+
# The following metadata blocks are optional. They are used to display
51+
# information about your workspace in the dashboard. You can remove them
52+
# if you don't want to display any information.
53+
# For basic resources, you can use the `coder stat` command.
54+
# If you need more control, you can write your own script.
55+
metadata {
56+
display_name = "CPU Usage"
57+
key = "0_cpu_usage"
58+
script = "coder stat cpu"
59+
interval = 10
60+
timeout = 1
61+
}
62+
63+
metadata {
64+
display_name = "RAM Usage"
65+
key = "1_ram_usage"
66+
script = "coder stat mem"
67+
interval = 10
68+
timeout = 1
69+
}
70+
71+
metadata {
72+
display_name = "Home Disk"
73+
key = "3_home_disk"
74+
script = "coder stat disk --path $${HOME}"
75+
interval = 60
76+
timeout = 1
77+
}
78+
79+
metadata {
80+
display_name = "CPU Usage (Host)"
81+
key = "4_cpu_usage_host"
82+
script = "coder stat cpu --host"
83+
interval = 10
84+
timeout = 1
85+
}
86+
87+
metadata {
88+
display_name = "Memory Usage (Host)"
89+
key = "5_mem_usage_host"
90+
script = "coder stat mem --host"
91+
interval = 10
92+
timeout = 1
93+
}
94+
95+
metadata {
96+
display_name = "Load Average (Host)"
97+
key = "6_load_host"
98+
# get load avg scaled by number of cores
99+
script = <<EOT
100+
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
101+
EOT
102+
interval = 60
103+
timeout = 1
104+
}
105+
106+
metadata {
107+
display_name = "Swap Usage (Host)"
108+
key = "7_swap_host"
109+
script = <<EOT
110+
free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }'
111+
EOT
112+
interval = 10
113+
timeout = 1
114+
}
49115
}
50116

51117
resource "coder_app" "code-server" {

examples/templates/kubernetes/main.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,62 @@ resource "coder_agent" "main" {
116116
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
117117
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
118118
EOT
119+
120+
# The following metadata blocks are optional. They are used to display
121+
# information about your workspace in the dashboard. You can remove them
122+
# if you don't want to display any information.
123+
# For basic resources, you can use the `coder stat` command.
124+
# If you need more control, you can write your own script.
125+
metadata {
126+
display_name = "CPU Usage"
127+
key = "0_cpu_usage"
128+
script = "coder stat cpu"
129+
interval = 10
130+
timeout = 1
131+
}
132+
133+
metadata {
134+
display_name = "RAM Usage"
135+
key = "1_ram_usage"
136+
script = "coder stat mem"
137+
interval = 10
138+
timeout = 1
139+
}
140+
141+
metadata {
142+
display_name = "Home Disk"
143+
key = "3_home_disk"
144+
script = "coder stat disk --path $${HOME}"
145+
interval = 60
146+
timeout = 1
147+
}
148+
149+
metadata {
150+
display_name = "CPU Usage (Host)"
151+
key = "4_cpu_usage_host"
152+
script = "coder stat cpu --host"
153+
interval = 10
154+
timeout = 1
155+
}
156+
157+
metadata {
158+
display_name = "Memory Usage (Host)"
159+
key = "5_mem_usage_host"
160+
script = "coder stat mem --host"
161+
interval = 10
162+
timeout = 1
163+
}
164+
165+
metadata {
166+
display_name = "Load Average (Host)"
167+
key = "6_load_host"
168+
# get load avg scaled by number of cores
169+
script = <<EOT
170+
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
171+
EOT
172+
interval = 60
173+
timeout = 1
174+
}
119175
}
120176

121177
# code-server

0 commit comments

Comments
 (0)