Debian Codegraph @

docs/core_concepts.md

raw
1 # Core concepts
2
3 ## Logstreams
4
5 As the name suggests, a logstream (or shortened to `lstream`) is a consecutive stream of log messages; in Nerdlog implementation, two kinds of logstreams are supported:
6
7 * Provided by either one or more log files (actually, as of now the limitation is to have at most 2 files in a logstream, but this limitation will hopefully be removed at some point). For example, `/var/log/syslog.1` and `/var/log/syslog` constitute a single logstream;
8 * Provided by `journalctl`.
9
10 By default, nerdlog checks available logstreams in the following order:
11
12 * If available, use the `/var/log/messages` file (and the older `/var/log/messages.1`)
13 * If available, use the `/var/log/syslog` file (and the older `/var/log/syslog.1`)
14 * As the last resort, use `journalctl` if available.
15
16 Why preferring the logfiles instead of `journalctl`: because [log files work much faster and are more reliable](https://github.com/dimonomid/nerdlog/issues/7#issuecomment-2820521885). For some benchmarks, [also see this comment](https://github.com/dimonomid/nerdlog/issues/7#issuecomment-2823303380). However, `journalctl` is more universally available these days, and it often also has longer log history, so nerdlog has full support for it.
17
18 In order to collect data from a logstream, Nerdlog needs to know a few things: first the ssh connection details (hostname, user and port), filename of the last log file, and filename of the previous log file (in the future there might be support for more older files). In the most explicit form, here's how a single logstream specification would look like:
19
20 ```
21 [email protected]:22:/var/log/syslog:/var/log/syslog.1
22 ```
23
24 It's a valid syntax and can be used on the query edit form. Multiple logstreams can be provided too, comma-separated.
25
26 If you want to select `journalctl` explicitly, specify `journalctl` as the "file":
27
28 ```
29 [email protected]:22:journalctl
30 ```
31
32 However, having many hosts to connect to, it would be tedious having to specify them all like that; so, here's how it can be simplified:
33
34 ### Default values
35
36 Everything except hostname is optional here: just like you'd expect, user defaults to the current OS user, and port defaults to 22. Then, as mentioned above, latest logfile defaults to either `/var/log/messages` or `/var/log/syslog` or `journalctl` (whatever is present on the host), and the previous log file defaults to the same as latest one but with the appended `.1` to it, so e.g. `/var/log/syslog.1`, just like log rotation tools normally do (irrelevant for `journalctl`, obviously).
37
38 Putting it all together, if the defaults work for us, all we have to do is to specify `myhost.com`. Or again, multiple hosts like `foo.com,bar.com`.
39
40 ### SSH config
41
42 Nerdlog reads ssh config file (`~/.ssh/config`) as well. So for example if our ssh config contains this:
43
44 ```
45 Host myhost-01
46 User myuser
47 HostName actualhost1.com
48 Port 1234
49
50 Host myhost-02
51 User myuser
52 HostName actualhost2.com
53 Port 7890
54 ```
55
56 Then we can specify just `myhost-01`, and it'll be an equivalent of:
57
58 ```
59 [email protected]:1234
60 ```
61
62 Globs are supported too, so if we want to get logs from both hosts in this ssh config, we can specify `myhost-*`, and it'll be an equivalent of:
63
64 ```
65 [email protected]:1234,[email protected]:7890
66 ```
67
68 Keep in mind though that as of today, ssh config parsing unfortunately has some major limitations; check out [this issue on Github](https://github.com/dimonomid/nerdlog/issues/12) for more details.
69
70 ### Nerdlog logstreams config
71
72 One obvious problem though is that SSH config doesn't let us specify the log files to read. If we need to configure non-default log files, we can use the `~/.config/nerdlog/logstreams.yaml` file, which looks like that:
73
74 ```
75 log_streams:
76 myhost-01:
77 hostname: actualhost1.com
78 port: 1234
79 user: myuser
80 log_files:
81 - /some/custom/logfile
82 myhost-02:
83 hostname: actualhost2.com
84 port: 7890
85 user: myuser
86 log_files:
87 - /some/custom/logfile
88 ```
89
90 Having that, we can specify `myhost-01`, and it'll be an equivalent of:
91
92 ```
93 [email protected]:1234:/some/custom/logfile:/some/custom/logfile.1
94 ```
95
96 ### Combining multiple configs
97
98 In fact, Nerdlog checks all of these configs in the following order, where every next step can fill missing things in, using hostname as a key:
99
100 * Specified logstream
101 * Nerdlog config
102 * SSH config
103 * Defaults
104
105 Therefore, having the SSH config as shown above, we can simplify the aforementioned `logstreams.yaml` as follows:
106
107 ```
108 log_streams:
109 myhost-01:
110 log_files:
111 * /some/custom/logfile
112 myhost-02:
113 log_files:
114 * /some/custom/logfile
115 ```
116
117 And get the same result, because hostname, user and port will come from the SSH config.
118
119 ### Reading log files with sudo
120
121 Before we begin: it is obviously a security risk, so think twice. If your OS allows reading logs without `sudo`, e.g. by adding the user to the `adm` or `systemd-journal` groups, it might be a better option.
122
123 On some systems like OpenSUSE, `/var/log/messages` is owned by `root:root`, so if we want to use log files and not `journalctl`, one has to either login as root, or use `sudo`.
124
125 If you want to use `sudo`, then first of all, make sure that the password is not required for your user, and then add `options: {"sudo": true}` to `/etc/logstreams.yaml` for the corresponding logstream, like this:
126
127 ```
128 log_streams:
129 myhost-01:
130 # ... Potentially any other configuration for the logstream
131 options: {"sudo": true}
132 ```
133
134 Another note on security: allowing sudo without a password is of course a massive security issue.
135
136 To make it more secure, it's technically possible to provision the host(s) by uploading that agent script manually under e.g. `/usr/local/bin`, owned by root, and then make it possible in Nerdlog to use that script instead of uploading a new one every time. It's not yet supported in Nerdlog, since manual provisioning like that means some maintenance burden every time nerdlog is updated, or every time we need to read logs from a new host, so I'm not sure if it's worth. Let me know if you actually need it for your use case, and I can hopefully make it happen.
137
138 ### Setting extra env vars or executing arbitrary init commands
139
140 One more extra option for a logstream is `shell_init`, which is an array of arbitrary shell commands. Can be used for setting extra env vars like `export TZ=UTC`, or whatever else.
141
142 Usage is as follows:
143
144 ```
145 log_streams:
146 myhost-01:
147 # ... Potentially any other configuration for the logstream
148 options:
149 shell_init:
150 - 'export TZ=UTC'
151 - 'export FOO=bar'
152 - 'some other command'
153 ```
154
155 ## Query
156
157 A Nerdlog query consists of 3 primary components and 1 extra:
158
159 * Logstreams to connect to: where to get the logs from;
160 * Time range to read;
161 * Optional awk pattern: to filter the logs in the selected time range.
162
163 On the query edit form, you'll see one more field: "Select field expression", it looks like this:
164
165 ```
166 time STICKY, lstream, message, *
167 ```
168
169 But it only affects the presentation of the logs in the UI. It somewhat resembles the SQL `SELECT` syntax, although a lot more limited.
170
171 The `STICKY` here just means that when the table is scrolled to the right, these sticky columns will remain visible at the left side.
172
173 Another supported keyword here is `AS`, so e.g. `message AS msg` is a valid syntax.