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