Skip to content

Commit ae04776

Browse files
committed
Expansion of Grafana documentation page.
Inspired by discussion at gcgarner#185 and gcgarner#131 and similar. Covers setting the time-zone for the container. Recommends NOT attempting to change either the admin username or password via environment variables and explains why. Also includes how to recover from a lost admin password. Discusses the how-to of overriding Grafana settings using the example of enabling anonymous access. Explains how to establish a "clean slate" state (a bit quicker than completely deleting and re-installing IOTstack).
1 parent bbb5766 commit ae04776

File tree

1 file changed

+216
-15
lines changed

1 file changed

+216
-15
lines changed

docs/Grafana.md

Lines changed: 216 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,228 @@
11
# Grafana
2+
23
## References
4+
35
- [Docker](https://hub.docker.com/r/grafana/grafana)
46
- [Website](https://grafana.com/)
57

6-
## Security
7-
Grafana's default credentials are username "admin" password "admin" it will ask you to choose a new password on boot. Go to `<yourIP>:3000` in your web browser.
8+
## Setting your time-zone
9+
10+
The default *~/IOTstack/services/grafana/grafana.env* contains this line:
11+
12+
```
13+
#TZ=Africa/Johannesburg
14+
```
15+
16+
Uncomment that line and change the right hand side to [your own timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
17+
18+
19+
## Security
20+
21+
If Grafana has just been installed but has **never** been launched then the following will be true:
22+
23+
* The folder *~/IOTstack/volumes/grafana* will not exist; and
24+
* The file *~/IOTstack/services/grafana/grafana.env* will contain these lines:
25+
26+
```
27+
#GF_SECURITY_ADMIN_USER=admin
28+
#GF_SECURITY_ADMIN_PASSWORD=admin
29+
```
30+
31+
You should see those lines as **documentation** rather than something you are being invited to edit. It is telling you that the default administative user for Grafana is "admin" and that the default password for that user is "admin".
32+
33+
If you do not change anything then, when you bring up the stack and use a browser to connect to your Raspberry Pi on port 3000, Grafana will:
34+
35+
* Expect you to login as user "admin" with password "admin"; and then
36+
* Force you to change the default password to something else.
37+
38+
Thereafter, you will login as "admin" with whatever password you chose. You can change the administrator's password as often as you like via the web UI (*profile* button, *change password* tab).
39+
40+
This method (of **not** touching these two keys in *grafana.env*) is the recommended approach. *Please* try to resist the temptation to fiddle!
41+
42+
### I want a different admin username (not recommended)
43+
44+
If, before you bring up the stack for the first time, you do this:
45+
46+
```
47+
GF_SECURITY_ADMIN_USER=jack
48+
#GF_SECURITY_ADMIN_PASSWORD=admin
49+
```
50+
51+
then, when you bring up the stack and connect on port 3000, Grafana will:
52+
53+
* Expect you to login as user "jack" with password "admin"; and then
54+
* Force you to change the default password to something else.
55+
56+
Thereafter, "jack" will be the Grafana administrator and you will login with the password you chose, until you decide to change the password to something else via the web UI.
57+
58+
Don't think you can come back later and tweak the Grafana administrator name in the environment variables. It doesn't work that way. It's a one-shot.
59+
60+
### I want a different default admin password (not recommended)
61+
62+
Well, first off, the two methods above both make you set a different password on first login so there probably isn't much point to this.
63+
64+
But, if you *really* insist…
65+
66+
If, before you bring up the stack for the first time, you do this:
67+
68+
```
69+
#GF_SECURITY_ADMIN_USER=admin
70+
GF_SECURITY_ADMIN_PASSWORD=jack
71+
```
72+
73+
then, when you bring up the stack and use a browser to connect to your Raspberry Pi on port 3000, Grafana will expect you to login as user "admin" with password "jack".
74+
75+
Grafana will not force you to change the password on first login but you will still be able to change it via the web UI.
76+
77+
But don't think you can come back later and change the password in the environment variables. It doesn't work that way. It's a one-shot.
78+
79+
### I want to change everything (not recommended)
80+
81+
If, before you bring up the stack for the first time, you do this:
82+
83+
```
84+
GF_SECURITY_ADMIN_USER=bill
85+
GF_SECURITY_ADMIN_PASSWORD=jack
86+
```
87+
88+
then, when you bring up the stack and use a browser to connect to your Raspberry Pi on port 3000, Grafana will expect you to login as user "bill" with password "jack".
89+
90+
Grafana will not force you to change the password on first login but you will still be able to change it via the web UI.
91+
92+
But don't think you can come back later and tweak either the username or password in the environment variables. It doesn't work that way. It's a one-shot.
93+
94+
### Distilling it down
95+
96+
**Before** Grafana is launched for the first time:
97+
98+
* *GF\_SECURITY\_ADMIN\_USER* has a default value of "admin". You *can* explicitly set it to "admin" or some other value. Whatever option you choose then that's the account name of Grafana's administrative user. But choosing any value other than "admin" is probably a bad idea.
99+
* *GF\_SECURITY\_ADMIN\_PASSWORD* has a default value of "admin". You can explicitly set it to "admin" or some other value. If its value is "admin" then you will be forced to change it the first time you login to Grafana. If its value is something other than "admin" then that will be the password until you change it via the web UI.
100+
101+
These two environment keys **only** work for the **first** launch of Grafana. Once Grafana has been launched, you can **never** change either the username or the password by editing *grafana.env*.
102+
103+
For this reason, it is better to leave *grafana.env* in its shrink-wrapped state. Your first login is as "admin/admin" and then you set the password you actually want when Grafana prompts you to change it.
104+
105+
### HELP – I forgot my Grafana admin password!
106+
107+
Assuming your IOTstack is up, the magic incantation is:
108+
109+
```
110+
$ docker exec grafana grafana-cli --homepath "/usr/share/grafana" admin reset-admin-password "admin"
111+
```
112+
113+
Then, use a browser to connect to your Raspberry Pi on port 3000. Grafana will:
114+
115+
* Expect you login as user "admin" with password "admin"; and then
116+
* Force you to change the default password to something else.
117+
118+
This magic incantation assumes that your administrative username is "admin". If you ignored the advice above and changed the administator username to something else then all bets are off. It might work anyway but we haven't tested it. Sorry. But that's why we said changing the username was not recommended.
119+
120+
## Overriding Grafana settings
121+
122+
Grafana documentation contains [a list of settings](https://grafana.com/docs/installation/configuration/). Settings are described in terms of how they appear in ".ini" files.
123+
124+
An example of the sort of thing you might want to do is to enable anonymous access to your Grafana dashboards. The [Grafana documentation](https://grafana.com/docs/grafana/latest/auth/overview/#anonymous-authentication) describes this in ".ini" format as:
125+
126+
```
127+
[auth.anonymous]
128+
enabled = true
129+
130+
# Organization name that should be used for unauthenticated users
131+
org_name = Main Org.
132+
133+
# Role for unauthenticated users, other valid values are `Editor` and `Admin`
134+
org_role = Viewer
135+
```
136+
137+
".ini" format is not really appropriate in a Docker context. Instead, you use environment variables to override Docker's settings. Environment variables are placed in *~/IOTstack/services/grafana/grafana.env*.
138+
139+
You need to convert ".ini" format to environment variable syntax. The rules are:
140+
141+
* Start with "GF_", then
142+
* Append the \[section name\], replacing any periods with underscores, then
143+
* Append the section key "as is", then
144+
* Append an "=", then
145+
* Append the right hand side in quotes.
146+
147+
Applying those rules gets you:
148+
149+
```
150+
GF_AUTH_ANONYMOUS_ENABLED="true"
151+
GF_AUTH_ANONYMOUS_ORG_NAME="Main Org."
152+
GF_AUTH_ANONYMOUS_ORG_ROLE="Viewer"
153+
```
154+
155+
> It is not strictly necessary to encapsulate every right hand side value in quotes. In the above, both "true" and "Viewer" would work without quotes, whereas "Main Org." needs quotes because of the embedded space.
156+
157+
After you have changed *~/IOTstack/services/grafana/grafana.env*, you need to propagate the changes into the Grafana container:
158+
159+
```
160+
$ cd ~/IOTstack
161+
$ docker-compose stop grafana
162+
$ docker-compose up -d
163+
```
164+
165+
> In theory, the second command could be omitted, or both the second and third commands could be replaced with "docker-compose restart grafana" but experience suggests stopping the container is more reliable.
166+
167+
A slightly more real-world example would involve choosing a different default organisation name for anonymous access. This example uses "ChezMoi".
168+
169+
First, the environment key needs to be set to that value:
170+
171+
```
172+
GF_AUTH_ANONYMOUS_ORG_NAME=ChezMoi
173+
```
174+
175+
Then that change needs to be propagated into the Grafana container as explained above.
176+
177+
Next, Grafana needs to be told that "ChezMoi" is the default organisation:
178+
179+
1. Use your browser to login to Grafana as an administrator.
180+
2. From the "Server Admin" slide-out menu on the left hand side, choose "Orgs".
181+
3. In the list that appears, click on "Main Org". This opens an editing panel.
182+
4. Change the "Name" field to "ChezMoi" and click "Update".
183+
5. Sign-out of Grafana. You will be taken back to the login screen. Your URL bar will look something like this:
184+
185+
```
186+
http://myhost.mydomain.com:3000/login
187+
```
188+
6. Edit the URL to remove the "login" suffix and press return. If all your changes were applied successfully, you will have anonymous access and the URL will look something like this:
189+
190+
```
191+
http://myhost.mydomain.com:3000/?orgId=1
192+
```
193+
194+
## HELP – I made a mess!
195+
196+
"I made a bit of a mess with Grafana. First time user. Steep learning curve. False starts, many. Mistakes, unavoidable. Been there, done that. But now I **really** need to start from a clean slate. And, yes, I understand there is no *undo* for this."
197+
198+
Begin by stopping Grafana:
199+
200+
```
201+
$ cd ~/IOTstack
202+
$ docker-compose stop grafana
203+
```
204+
205+
You have two options:
206+
207+
1. Destroy your settings and dashboards but retain any plugins you may have installed:
208+
209+
```
210+
$ sudo rm ~/IOTstack/volumes/grafana/data/grafana.db
211+
```
212+
213+
2. Nuke everything (triple-check this command **before** you hit return):
8214

9-
## Overwriting grafana.ini settings
215+
```
216+
$ sudo rm -rf ~/IOTstack/volumes/grafana/data
217+
```
10218

11-
A list of the settings available in grafana.ini are listed [here](https://grafana.com/docs/installation/configuration/)
219+
This is where you should edit *~/IOTstack/services/grafana/grafana.env* to correct any problems (such as choosing an administrative username other than "admin").
12220

13-
To overwrite a setting edit the IOTstack/services/grafana/grafana.env file. The format is `GF_<SectionName>_<KeyName>`
221+
When you are ready, bring Grafana back up again:
14222

15-
An example would be:
16223
```
17-
GF_PATHS_DATA=/var/lib/grafana
18-
GF_PATHS_LOGS=/var/log/grafana
19-
# [SERVER]
20-
GF_SERVER_ROOT_URL=http://localhost:3000/grafana
21-
GF_SERVER_SERVE_FROM_SUB_PATH=true
22-
# [SECURITY]
23-
GF_SECURITY_ADMIN_USER=admin
24-
GF_SECURITY_ADMIN_PASSWORD=admin
224+
$ cd ~/IOTstack
225+
$ docker-compose up -d
25226
```
26227

27-
After the alterations run `docker-compose up -d` to pull them in
228+
Grafana will automatically recreate everything it needs. You will be able to login as "admin/admin".

0 commit comments

Comments
 (0)