This patch allows for multiple RRD writer threads to service the queue.
[rrdtool.git] / doc / rrdcached.pod
1 =pod
2
3 =head1 NAME
4
5 rrdcached - Data caching daemon for rrdtool
6
7 =head1 SYNOPSIS
8
9 B<rrdcached> [B<-l/-L> I<address>] [B<-w> I<timeout>] [B<-z> I<delay>] [B<-f> I<timeout>] [B<-t> I<write_threads>] [B<-j> I<dir>] [-F] [B<-b> I<dir> [B<-B>]]
10
11 =head1 DESCRIPTION
12
13 B<rrdcached> is a daemon that receives updates to existing RRD files,
14 accumulates them and, if enough have been received or a defined time has
15 passed, writes the updates to the RRD file. A I<flush> command may be used to
16 force writing of values to disk, so that graphing facilities and similar can
17 work with up-to-date data.
18
19 The daemon was written with big setups in mind. Those setups usually run into
20 IOE<nbsp>related problems sooner or later for reasons that are beyond the scope
21 of this document. Check the wiki at the RRDTool homepage for details. Also
22 check L<SECURITY CONSIDERATIONS> below before using this daemon! A detailed
23 description of how the daemon operates can be found in the L<HOW IT WORKS>
24 section below.
25
26 =head1 OPTIONS
27
28 =over 4
29
30 =item B<-l> I<address>
31
32 Tells the daemon to bind to I<address> and accept incoming connections on that
33 socket. If I<address> begins with C<unix:>, everything following that prefix is
34 interpreted as the path to a UNIX domain socket. Otherwise the address or node
35 name are resolved using L<getaddrinfo>.
36
37 For network sockets, a port may be specified by using the form
38 C<B<[>I<address>B<]:>I<port>>. If the address is an IPv4 address or a fully
39 qualified domain name (i.E<nbsp>e. the address contains at least one dot
40 (C<.>)), the square brackets can be omitted, resulting in the (simpler)
41 C<I<address>B<:>I<port>> pattern.. The default port is B<42217/udp>.
42
43 The following formats are accepted. Please note that the address of the UNIX
44 domain socket B<must> start with a slash in the second case!
45
46    unix:</path/to/unix.sock>
47    /<path/to/unix.sock>
48    <hostname-or-ip>
49    [<hostname-or-ip>]:<port>
50    <hostname-or-ipv4>:<port>
51
52 If the B<-l> option is not specified the default address,
53 C<unix:/tmp/rrdcached.sock>, will be used.
54
55 =item B<-L> I<address>
56
57 Same as B<-l>, except creates a low-privilege socket.  See B<SECURITY
58 CONSIDERATIONS> for more information.
59
60 =item B<-w> I<timeout>
61
62 Data is written to disk every I<timeout> seconds. If this option is not
63 specified the default interval of 300E<nbsp>seconds will be used.
64
65 =item B<-z> I<delay>
66
67 If specified, rrdcached will delay writing of each RRD for a random number
68 of seconds in the rangeE<nbsp>[0,I<delay>).  This will avoid too many
69 writes being queued simultaneously.  This value should be no greater than
70 the value specified in B<-w>.  By default, there is no delay.
71
72 =item B<-f> I<timeout>
73
74 Every I<timeout> seconds the entire cache is searched for old values which are
75 written to disk. This only concerns files to which updates have stopped, so
76 setting this to a high value, such as 3600E<nbsp>seconds, is acceptable in most
77 cases. This timeout defaults to 3600E<nbsp>seconds.
78
79 =item B<-p> I<file>
80
81 Sets the name and location of the PID-file. If not specified, the default,
82 C<I<$localststedir>/run/rrdcached.pid> will be used.
83
84 =item B<-t> I<write_threads>
85
86 Specifies the number of threads used for writing RRD files.  The default
87 isE<nbsp>4.  Increasing this number will allow rrdcached to have more
88 simultaneous I/O requests into the kernel.  This may allow the kernel to
89 re-order disk writes, resulting in better disk throughput.
90
91 =item B<-j> I<dir>
92
93 Write updates to a journal in I<dir>.  In the event of a program or system
94 crash, this will allow the daemon to write any updates that were pending
95 at the time of the crash.
96
97 On startup, the daemon will check for journal files in this directory.  If
98 found, all updates therein will be read into memory before the daemon
99 starts accepting new connections.
100
101 The journal will be rotated with the same frequency as the flush timer
102 given by B<-f>.
103
104 When journaling is enabled, the daemon will use a fast shutdown procedure.
105 Rather than flushing all files to disk, it will make sure the journal is
106 properly written and exit immediately.  Although the RRD data files are
107 not fully up-to-date, no information is lost; all pending updates will be
108 replayed from the journal next time the daemon starts up.
109
110 To disable fast shutdown, use the B<-F> option.
111
112 =item B<-F>
113
114 ALWAYS flush all updates to the RRD data files when the daemon is shut
115 down, regardless of journal setting.
116
117 =item B<-b> I<dir>
118
119 The daemon will change into a specific directory at startup. All files passed
120 to the daemon, that are specified by a B<relative> path, will be interpreted
121 to be relative to this directory. If not given the default, C</tmp>, will be
122 used.
123
124   +------------------------+------------------------+
125   ! Command line           ! File updated           !
126   +------------------------+------------------------+
127   ! foo.rrd                ! /tmp/foo.rrd           !
128   ! foo/bar.rrd            ! /tmp/foo/bar.rrd       !
129   ! /var/lib/rrd/foo.rrd   ! /var/lib/rrd/foo.rrd   !
130   +------------------------+------------------------+
131   Paths given on the command  line and paths actually
132   updated by the daemon,  assuming the base directory
133   "/tmp".
134
135 B<WARNING:> The paths up to and including the base directory B<MUST NOT BE>
136 symbolic links.  In other words, if the base directory is
137 specified as:
138
139     -b /base/dir/somewhere
140
141 ... then B<NONE> of the following should be symbolic links:
142
143     /base
144     /base/dir
145     /base/dir/somewhere
146
147 =item B<-B>
148
149 Only permit writes into the base directory specified in B<-b> (and any
150 sub-directories).  This does B<NOT> detect symbolic links.  Paths
151 containing C<../> will also be blocked.
152
153 =back
154
155 =head1 AFFECTED RRDTOOL COMMANDS
156
157 The following commands may be made aware of the B<rrdcached> using the command
158 line argument B<--daemon> or the environment variable B<RRDCACHED_ADDRESS>:
159
160 =over 4
161
162 =item B<dump>
163
164 =item B<fetch>
165
166 =item B<flush>
167
168 =item B<graph>
169
170 =item B<graphv>
171
172 =item B<info>
173
174 =item B<last>
175
176 =item B<lastupdate>
177
178 =item B<update>
179
180 =item B<xport>
181
182 =back
183
184 The B<update> command can send values to the daemon instead of writing them to
185 the disk itself. All other commands can send a B<FLUSH> command (see below) to
186 the daemon before accessing the files, so they work with up-to-date data even
187 if the cache timeout is large.
188
189 =head1 ERROR REPORTING
190
191 The daemon reports errors in one of two ways: During startup, error messages
192 are printed to C<STDERR>. One of the steps when starting up is to fork to the
193 background and closing C<STDERR> - after this writing directly to the user is
194 no longer possible. Once this has happened, the daemon will send log messages
195 to the system logging daemon using L<syslog(3)>. The facility used is
196 C<LOG_DAEMON>.
197
198 =head1 HOW IT WORKS
199
200 When receiving an update, B<rrdcached> does not write to disk but looks for an
201 entry for that file in its internal tree. If not found, an entry is created
202 including the current time (called "First" in the diagram below). This time is
203 B<not> the time specified on the command line but the time the operating system
204 considers to be "now". The value and time of the value (called "Time" in the
205 diagram below) are appended to the tree node.
206
207 When appending a value to a tree node, it is checked whether it's time to write
208 the values to disk. Values are written to disk if
209 S<C<now() - First E<gt>= timeout>>, where C<timeout> is the timeout specified
210 using the B<-w> option, see L<OPTIONS>. If the values are "old enough" they
211 will be enqueued in the "update queue", i.E<nbsp>e. they will be appended to
212 the linked list shown below.  Because the tree nodes and the elements of the
213 linked list are the same data structures in memory, any update to a file that
214 has already been enqueued will be written with the next write to the RRD file,
215 too.
216
217 A separate "update thread" constantly dequeues the first element in the update
218 queue and writes all its values to the appropriate file. So as long as the
219 update queue is not empty files are written at the highest possible rate.
220
221 Since the timeout of files is checked only when new values are added to the
222 file, "dead" files, i.E<nbsp>e. files that are not updated anymore, would never
223 be written to disk. Therefore, every now and then, controlled by the B<-f>
224 option, the entire tree is walked and all "old" values are enqueued. Since this
225 only affects "dead" files and walking the tree is relatively expensive, you
226 should set the "flush interval" to a reasonably high value. The default is
227 3600E<nbsp>seconds (one hour).
228
229 The downside of caching values is that they won't show up in graphs generated
230 from the RRDE<nbsp>files. To get around this, the daemon provides the "flush
231 command" to flush specific files. This means that the file is inserted at the
232 B<head> of the update queue or moved there if it is already enqueued. The flush
233 command will return only after the file's pending updates have been written
234 to disk.
235
236  +------+   +------+                               +------+
237  ! head !   ! root !                               ! tail !
238  +---+--+   +---+--+                               +---+--+
239      !         /\                                      !
240      !        /  \                                     !
241      !       /\  /\                                    !
242      !      /\/\ \ `----------------- ... --------,    !
243      V     /      `-------,                       !    V
244  +---+----+---+    +------+-----+             +---+----+---+
245  ! File:  foo !    ! File:  bar !             ! File:  qux !
246  ! First: 101 !    ! First: 119 !             ! First: 180 !
247  ! Next:&bar -+--->! Next:&... -+---> ... --->! Next:NULL  !
248  | Prev:NULL  !<---+-Prev:&foo  !<--- ... ----+-Prev: &... !
249  +============+    +============+             +============+
250  ! Time:  100 !    ! Time:  120 !             ! Time:  180 !
251  ! Value:  10 !    ! Value: 0.1 !             ! Value: 2,2 !
252  +------------+    +------------+             +------------+
253  ! Time:  110 !    ! Time:  130 !             ! Time:  190 !
254  ! Value:  26 !    ! Value: 0.1 !             ! Value: 7,3 !
255  +------------+    +------------+             +------------+
256  :            :    :            :             :            :
257  +------------+    +------------+             +------------+
258  ! Time:  230 !    ! Time:  250 !             ! Time:  310 !
259  ! Value:  42 !    ! Value: 0.2 !             ! Value: 1,2 !
260  +------------+    +------------+             +------------+
261
262 The above diagram demonstrates:
263
264 =over
265
266 =item *
267
268 Files/values are stored in a (balanced) tree.
269
270 =item *
271
272 Tree nodes and entries in the update queue are the same data structure.
273
274 =item *
275
276 The local time ("First") and the time specified in updates ("Time") may differ.  
277
278 =item *
279
280 Timed out values are inserted at the "tail".
281
282 =item *
283
284 Explicitly flushed values are inserted at the "head".
285
286 =item *
287
288 ASCII art rocks.
289
290 =back
291
292 =head1 SECURITY CONSIDERATIONS
293
294 The client/server protocol does not have any authentication or
295 authorization mechanism.  Therefore, take care to restrict which users can
296 connect to the daemon.
297
298 Control sockets are divided into high-privilege (B<-l>) and low-privilege
299 (B<-L>) sockets.  High-privilege sockets accept all commands, whereas
300 low-privilege sockets accept only B<FLUSH>, B<STATS>, and B<HELP>.
301
302 For a multi-user environment where only certain users require read/write
303 access, the recommended configuration uses two sockets as follows:
304
305 =over
306
307 =item B<-l> I</protected/dir/rrd.sock>
308
309 Create a high-privilege unix-domain socket.  This should be protected with
310 the same Unix permissions that are used to protect the RRD files.  Updates
311 should be directed to this socket.
312
313 =item B<-L> I<127.0.0.1>
314
315 Create a low-privilege TCP socket listening on localhost.  All users on
316 the local system may use this to trigger FLUSH of individual files.  Users
317 with read-only access should be directed to this socket.
318
319 =back
320
321 If you (want to) use the network capability, i.E<nbsp>e. let the daemon bind to
322 an IPv4 or IPv6 socket, it is B<your> job to install a packet filter or similar
323 mechanism to prevent unauthorized connections. Unless you have a dedicated VLAN
324 or VPN for this, using the network option is probably a bad idea!
325
326 The daemon will blindly write to any file it gets told, so you really should
327 create a separate user just for this daemon. Also it does not do any sanity
328 checks, so if it gets told to write values for a time far in the future, your
329 files will be messed up good!
330
331 You have been warned.
332
333 =head1 PROTOCOL
334
335 The daemon communicates with clients using a line based ASCII protocol which is
336 easy to read and easy to type. This makes it easy for scripts to implement the
337 protocol and possible for users to use L<telnet> to connect to the daemon
338 and test stuff "by hand".
339
340 The protocol is line based, this means that each record consists of one or more
341 lines. A line is terminated by the line feed character C<0x0A>, commonly
342 written as C<\n>. In the examples below, this character will be written as
343 C<E<lt>LFE<gt>> ("line feed").
344
345 After the connection has been established, the client is expected to send a
346 "command". A command consists of the command keyword, possibly some arguments,
347 and a terminating newline character. For a list of commands, see
348 L<Valid Commands> below.
349
350 Example:
351
352   FLUSH /tmp/foo.rrd<LF>
353
354 The daemon answers with a line consisting of a status code and a short status
355 message, separated by one or more space characters. A negative status code
356 signals an error, a positive status code or zero signal success. If the status
357 code is greater than zero, it indicates the number of lines that follow the
358 status line.
359
360 Examples:
361
362  0 Success<LF>
363
364  2 Two lines follow<LF>
365  This is the first line<LF>
366  And this is the second line<LF>
367
368 =head2 Valid Commands
369
370 The following commands are understood by the daemon:
371
372 =over 4
373
374 =item B<FLUSH> I<filename>
375
376 Causes the daemon to put I<filename> to the B<head> of the update queue
377 (possibly moving it there if the node is already enqueued). The answer will be
378 sent B<after> the node has been dequeued.
379
380 =item B<FLUSHALL>
381
382 Causes the daemon to start flushing ALL pending values to disk.  This
383 returns immediately, even though the writes may take a long time.
384
385 =item B<PENDING> I<filename>
386
387 Shows any "pending" updates for a file, in order.  The updates shown have
388 not yet been written to the underlying RRD file.
389
390 =item B<FORGET> I<filename>
391
392 Removes I<filename> from the cache.  Any pending updates B<WILL BE LOST>.
393
394 =item B<QUEUE>
395
396 Shows the files that are on the output queue.  Returns zero or more lines
397 in the following format, where E<lt>num_valsE<gt> is the number of values
398 to be written for the E<lt>fileE<gt>:
399
400     <num_vals> <file>
401
402 =item B<HELP> [I<command>]
403
404 Returns a short usage message. If no command is given, or I<command> is
405 B<HELP>, a list of commands supported by the daemon is returned. Otherwise a
406 short description, possibly containing a pointer to a manual page, is returned.
407 Obviously, this is meant for interactive usage and the format in which the
408 commands and usage summaries are returned is not well defined.
409
410 =item B<STATS>
411
412 Returns a list of metrics which can be used to measure the daemons performance
413 and check its status. For a description of the values returned, see
414 L<Performance Values> below.
415
416 The format in which the values are returned is similar to many other line based
417 protocols: Each value is printed on a separate line, each consisting of the
418 name of the value, a colon, one or more spaces and the actual value.
419
420 Example:
421
422  9 Statistics follow
423  QueueLength: 0
424  UpdatesReceived: 30
425  FlushesReceived: 2
426  UpdatesWritten: 13
427  DataSetsWritten: 390
428  TreeNodesNumber: 13
429  TreeDepth: 4
430  JournalBytes: 190
431  JournalRotate: 0
432
433 =item B<UPDATE> I<filename> I<values> [I<values> ...]
434
435 Adds more data to a filename. This is B<the> operation the daemon was designed
436 for, so describing the mechanism again is unnecessary. Read L<HOW IT WORKS>
437 above for a detailed explanation.
438
439 Note that rrdcached only accepts absolute timestamps in the update values.
440 Updates strings like "N:1:2:3" are automatically converted to absolute
441 time by the RRD client library before sending to rrdcached.
442
443 =item B<WROTE> I<filename>
444
445 This command is written to the journal after a file is successfully
446 written out to disk.  It is used during journal replay to determine which
447 updates have already been applied.  It is I<only> valid in the journal; it
448 is not accepted from the other command channels.
449
450 =item B<BATCH>
451
452 This command initiates the bulk load of multiple commands.  This is
453 designed for installations with extremely high update rates, since it
454 permits more than one command to be issued per read() and write().
455
456 All commands are executed just as they would be if given individually,
457 except for output to the user.  Messages indicating success are
458 suppressed, and error messages are delayed until the client is finished.
459
460 Command processing is finished when the client sends a dot (".") on its
461 own line.  After the client has finished, the server responds with an
462 error count and the list of error messages (if any).  Each error messages
463 indicates the number of the command to which it corresponds, and the error
464 message itself.  The first user command after B<BATCH> is command number one.
465
466     client:  BATCH
467     server:  0 Go ahead.  End with dot '.' on its own line.
468     client:  UPDATE x.rrd 1223661439:1:2:3            <--- command #1
469     client:  UPDATE y.rrd 1223661440:3:4:5            <--- command #2
470     client:  and so on...
471     client:  .
472     server:  2 Errors
473     server:  1 message for command 1
474     server:  12 message for command 12
475
476 =item B<QUIT>
477
478 Disconnect from rrdcached.
479
480 =back
481
482 =head2 Performance Values
483
484 The following counters are returned by the B<STATS> command:
485
486 =over 4
487
488 =item B<QueueLength> I<(unsigned 64bit integer)>
489
490 Number of nodes currently enqueued in the update queue.
491
492 =item B<UpdatesReceived> I<(unsigned 64bit integer)>
493
494 Number of UPDATE commands received.
495
496 =item B<FlushesReceived> I<(unsigned 64bit integer)>
497
498 Number of FLUSH commands received.
499
500 =item B<UpdatesWritten> I<(unsigned 64bit integer)>
501
502 Total number of updates, i.E<nbsp>e. calls to C<rrd_update_r>, since the
503 daemon was started.
504
505 =item B<DataSetsWritten> I<(unsigned 64bit integer)>
506
507 Total number of "data sets" written to disk since the daemon was
508 started. A data set is one or more values passed to the B<UPDATE>
509 command. For example: C<1223661439:123:456> is one data set with two
510 values. The term "data set" is used to prevent confusion whether
511 individual values or groups of values are counted.
512
513 =item B<TreeNodesNumber> I<(unsigned 64bit integer)>
514
515 Number of nodes in the cache.
516
517 =item B<TreeDepth> I<(unsigned 64bit integer)>
518
519 Depth of the tree used for fast key lookup.
520
521 =item B<JournalBytes> I<(unsigned 64bit integer)>
522
523 Total number of bytes written to the journal since startup.
524
525 =item B<JournalRotate> I<(unsigned 64bit integer)>
526
527 Number of times the journal has been rotated since startup.
528
529 =back
530
531 =head1 SIGNALS
532
533 =over 4
534
535 =item SIGINT and SIGTERM
536
537 The daemon exits normally on receipt of either of these signals.  Pending
538 updates are handled in accordance with the B<-j> and B<-F> options.
539
540 =item SIGUSR1
541
542 The daemon exits AFTER flushing all updates out to disk.  This may take a
543 while.
544
545 =item SIGUSR2
546
547 The daemon exits immediately, without flushing updates out to disk.
548 Pending updates will be replayed from the journal when the daemon starts
549 up again.  B<WARNING: if journaling (-j) is NOT enabled, any pending
550 updates WILL BE LOST>.
551
552 =back
553
554 =head1 BUGS
555
556 No known bugs at the moment.
557
558 =head1 SEE ALSO
559
560 L<rrdtool>, L<rrdgraph>
561
562 =head1 AUTHOR
563
564 B<rrdcached> and this manual page have been written by Florian Forster
565 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt>.
566
567 =head1 CONTRIBUTORS
568
569 kevin brintnall E<lt>kbrint@rufus.netE<gt>
570
571 =cut
572