librrd(3)
okay bestie, welcome to librrd(3) from the rrdtool manual β the RRD library functions page. πβ¨ This is part 1, so keep your wig on.
πΈ NAME πΈ
librrd β RRD library functions. That's the whole name vibe. π
π DESCRIPTION π
okay so librrd contains most of the functionality in RRDtool. The command line utilities and language bindings are often just wrappers around the code contained in librrd. It's giving "the library is the main character and the CLIs are wearing its clothes." π
This manual page documents the librrd API.
β οΈ NOTE: This document is a work in progress, and should be considered incomplete as long as this warning persists. For more information about the librrd functions, always consult the source code. No cap β the source is the final boss. π«‘
π CORE FUNCTIONS π
β¨ rrd_dump_cb_r β¨
rrd_dump_cb_r(char *filename, int opt_header, rrd_output_callback_t cb,
void *user)
okay so sometimes you need the output of rrd_dump but you do not want to write it to a file or the standard output. In those situations, an application can ask rrd_dump_cb_r to call a user-defined function every time there is output to be stored somewhere. This can be used, to e.g. directly feed an XML parser with the dumped output or transfer the resulting string in memory. Very "give me the data in my hands" energy. π«΄
The arguments for rrd_dump_cb_r are the same as for rrd_dump_opt_r except that the output filename parameter is replaced by the user-defined callback function and an additional parameter for the callback function that is passed untouched, i.e. to store information about the callback state needed for the user-defined callback to function properly.
Recent versions of rrd_dump_opt_r internally use this callback mechanism to write their output to the file provided by the user.
size_t rrd_dump_opt_cb_fileout(
const void *data,
size_t len,
void *user)
{
return fwrite(data, 1, len, (FILE *)user);
}
The associated call for rrd_dump_cb_r looks like
res = rrd_dump_cb_r(filename, opt_header,
rrd_dump_opt_cb_fileout, (void *)out_file);
where the last parameter specifies the file handle rrd_dump_opt_cb_fileout should write to. There's no specific condition for the callback to detect when it is called for the first time, nor for the last time. If you require this for initialization and cleanup you should do those tasks before and after calling rrd_dump_cb_r respectively. Lowkey important. β οΈ
π« rrd_fetch_cb_register π«
rrd_fetch_cb_register(rrd_fetch_cb_t c)
If your data does not reside in rrd files, but you would like to draw charts using the rrd_graph functionality, you can supply your own rrd_fetch function and register it using the rrd_fetch_cb_register function.
The argument signature and api must be the same of the callback function and must be equivalent to the one of rrd_fetch_fn in rrd_fetch.c.
To activate the callback function you can use the pseudo filename cb//free_form_text.
Note that rrdtool graph will not ask the same rrd for data twice. It determines this by building a key out of the values supplied to the fetch function. If the values are the same, the previous answer will be used. Caching, but make it mysterious. π΅οΈβοΈ
π UTILITY FUNCTIONS π
π² rrd_random() π²
Generates random numbers just like random(). This further ensures that the random number generator is seeded exactly once per process. Slay, deterministic seeding. π±
π’ rrd_strtodbl π’
an rrd aware string to double converter which sets rrd_error in if there is a problem and uses the return code exclusively for conversion status reporting. It's giving "the error goes in rrd_error, the return code is just the status reporter." π
π rrd_strtod π
works like normal strtod, but it is locale independent (and thread safe). No cap, locale independent and thread safe. π
π§΅ rrd_snprintf π§΅
works like normal snprintf but it is locale independent (and thread safe).
π rrd_add_ptr π
rrd_add_ptr(void ***dest, size_t *dest_size, void *src)
Dynamically resize the array pointed to by dest. dest_size is a pointer to the current size of dest. Upon successful realloc(), the dest_size is incremented by 1 and the src pointer is stored at the end of the new dest. Returns 1 on success, 0 on failure. Boom, array goes brrr. π
type **arr = NULL;
type *elem = "whatever";
size_t arr_size = 0;
if (!rrd_add_ptr(&arr, &arr_size, elem))
handle_failure();
π§± rrd_add_ptr_chunk π§±
rrd_add_ptr_chunk(void ***dest, size_t *dest_size, void *src, size_t *alloc,
size_t chunk)
Like rrd_add_ptr, except the destination is allocated in chunks of chunk. alloc points to the number of entries allocated, whereas dest_size points to the number of valid pointers. If more pointers are needed, chunk pointers are allocated and alloc is increased accordingly. alloc must be >= dest_size.
This method improves performance on hosts with expensive realloc(). Your malloc is high maintenance? This is for you. πΈ
πͺ’ rrd_add_strdup πͺ’
rrd_add_strdup(char ***dest, size_t *dest_size, char *src)
Like rrd_add_ptr, except adds a strdup of the source string.
char **arr = NULL;
size_t arr_size = NULL;
char *str = "example text";
if (!rrd_add_strdup(&arr, &arr_size, str))
handle_failure();
π§Ί rrd_add_strdup_chunk π§Ί
rrd_add_strdup_chunk(char ***dest, size_t *dest_size, char *src, size_t *alloc,
size_t chunk)
Like rrd_add_strdup, except the destination is allocated in chunks of chunk. alloc points to the number of entries allocated, whereas dest_size points to the number of valid pointers. If more pointers are needed, chunk pointers are allocated and alloc is increased accordingly. alloc must be >= dest_size.
π§Ή rrd_free_ptrs π§Ή
rrd_free_ptrs(void ***src, size_t *cnt)
Free an array of pointers allocated by rrd_add_ptr or rrd_add_strdup. Also frees the array pointer itself. On return, the source pointer will be NULL and the count will be zero. Cleanup queen. π§Ή
/* created as above */
rrd_free_ptrs(&arr, &arr_size);
/* here, arr == NULL && arr_size == 0 */
π rrd_mkdir_p π
rrd_mkdir_p(const char *pathname, mode_t mode)
Create the directory named pathname including all of its parent directories (similar to mkdir -p on the command line - see mkdir(1) for more information). The argument mode specifies the permissions to use. It is modified by the process's umask. See mkdir(2) for more details.
The function returns 0 on success, a negative value else. In case of an error, errno is set accordingly. Aside from the errors documented in mkdir(2), the function may fail with the following errors:
EINVALβpathnameisNULLor the empty string.ENOMEMβ Insufficient memory was available.- any error returned by
stat(2)
In contrast to mkdir(2), the function does not fail if pathname already exists and is a directory. Iconic behavior. π
β³ rrd_scaled_duration β³
rrd_scaled_duration (const char * token, unsigned long divisor, unsigned long * valuep)
Parse a token in a context where it contains a count (of seconds or PDP instances), or a duration that can be converted to a count by representing the duration in seconds and dividing by some scaling factor. For example, if a user would natively express a 3 day archive of samples collected every 2 minutes, the sample interval can be represented by 2m instead of 120, and the archive duration by 3d (to be divided by 120) instead of 2160 (3*24*60*60 / 120). See more examples in "STEP, HEARTBEAT, and Rows As Durations" in rrdcreate.
token must be a number with an optional single-character suffix encoding the scaling factor:
sindicates secondsmindicates minutes. The value is multiplied by 60.hindicates hours. The value is multiplied by 3600 (or60m).dindicates days. The value is multiplied by 86400 (or24h).windicates weeks. The value is multiplied by 604800 (or7d).Mindicates months. The value is multiplied by 2678400 (or31d). (Note this factor accommodates the maximum number of days in a month.)yindicates years. The value is multiplied by 31622400 (or366d). (Note this factor accommodates leap years.)
divisor is a positive value representing the duration in seconds of an interval that the desired result counts.
valuep is a pointer to where the decoded value will be stored if the conversion is successful.
The initial characters of token must be the base-10 representation of a positive integer, or the conversion fails.
If the remainder token is empty (no suffix), it is a count and no scaling is performed.
If token has one of the suffixes above, the count is multiplied to convert it to a duration in seconds. The resulting number of seconds is divided by divisor to produce a count of intervals each of duration divisor seconds. If division would produce a remainder (e.g., 5m (300 seconds) divided by 90s), the conversion is invalid.
If token has unrecognized trailing characters the conversion fails.
The function returns a null pointer if the conversion was successful and valuep has been updated to the scaled value. On failure, it returns a text diagnostic suitable for use in user error messages.
π CLIENT FUNCTIONS π
Okay so this whole section is about connecting to an rrdcached instance β either via a unix address or an inet address β and then creating, updating, or gathering statistics about a specified RRD database file. Very demure, very powerful. π
There are two different interfaces, bestie: the rrd_client_ family of functions operate on a user-provided client object (rrd_client_t) and support multiple concurrent connections to rrdcache instances. The simpler rrdc_ family handles connections transparently but can only be used for one connection at a time. Pick your fighter. β¨
All of the following functions and data types are specified in the rrd_client.h header file. No cap.
rrd_client_new(const char *daemon_addr)β Create a new client connection object. If specified, connect to the daemon atdaemon_addr. The connection can later be changed by callingrrd_client_connect. Fresh connection era πrrd_client_destroy(rrd_client_t *client)β Close a client connection and destroy the object by freeing all dynamically allocated memory. After calling this function,clientcan no longer be used. Like, gone gone. π«Άrrd_client_connect(rrd_client_t *client, const char *daemon_addr)/rrdc_connect(const char *daemon_addr)β Connect to a runningrrdcachedinstance, specified viadaemon_addr. Any previous connection will be closed. Ifdaemon_addrisNULL, it defaults to the value of theENV_RRDCACHED_ADDRESSenvironment address. Slay, defaults. πrrd_client_is_connected(rrd_client_t *client)β Return a booleanintif the client is connected to the server.rrd_client_address(rrd_client_t *client)β Returns the server address belonging to the current connection.rrdc_is_connected(const char *daemon_addr)β Return a booleanintto determine if the client is connected to the rrdcache daemon specified by thedaemon_addrparameter.rrd_client_ping(rrd_client_t *client)/rrdc_pingβ Check the client connection by pinging the remote side. Just a lil βu up?β text. πrrdc_is_any_connectedβ Return a booleanintif any daemon connections are connected.rrd_client_disconnect(rrd_client_t *client)/rrdc_disconnectβ Disconnect gracefully from the present daemon connection. No dramatic exit, just closure. π¦rrd_client_update(rrd_client_t *client, const char *filename, int values_num, const char * const *values)/rrdc_update(const char *filename, int values_num, const char * const *values)β Update the RRDfilenamevia the rrdcached. Wherevalues_numis the number of values to update andvaluesare the new values to add.rrd_client_info(rrd_client_t *client, const char *filename)/rrdc_info(const char *filename)β Grab rrd info of the RRDfilenamefrom the connected cache daemon. This function returns anrrd_info_tstructure of the following format:
typedef struct rrd_blob_t {
unsigned long size; /* size of the blob */
unsigned char *ptr; /* pointer */
} rrd_blob_t;
typedef enum rrd_info_type { RD_I_VAL = 0,
RD_I_CNT,
RD_I_STR,
RD_I_INT,
RD_I_BLO
} rrd_info_type_t;
typedef union rrd_infoval {
unsigned long u_cnt;
rrd_value_t u_val;
char *u_str;
int u_int;
rrd_blob_t u_blo;
} rrd_infoval_t;
typedef struct rrd_info_t {
char *key;
rrd_info_type_t type;
rrd_infoval_t value;
struct rrd_info_t *next;
} rrd_info_t;
rrd_client_last(rrd_client_t *client, const char *filename)/rrdc_last(const char *filename)β Grab the unix epoch of the last time RRDfilenamewas updated. Timestamp check, iconic. β°rrd_client_first(rrd_client_t *client, const char *filename, int rraindex)/rrdc_first(const char *filename, int rraindex)β Get the first value of the first sample of the RRDfilename, of therraindexRRA (Round Robin Archive) index number. The RRA index number can be determined by pulling therrd_info_toff the RRD.rrd_client_create(rrd_client_t *client, const char *filename, unsigned long pdp_step, time_t last_up, int no_overwrite, int argc, const char **argv)/rrdc_create(const char *filename, unsigned long pdp_step, time_t last_up, int no_overwrite, int argc, const char **argv)β Create RRD database of pathfilename. The RRD will have a step size ofpfp_step, the unix epoch timestamp to start collecting data from. The number of data sources and RRAsargcand the definitions of the data sources and RRAsargv. Lastly whether or not to overwrite an existing RRD if one is found with the same filename;no_overwrite. New RRD just dropped πrrdc_create_r2(rrd_client_t *client, const char *filename, unsigned long pdp_step, time_t last_up, int no_overwrite, const char **sources, const char *template, int argc, const char **argv)/rrdc_create_r2(const char *filename, unsigned long pdp_step, time_t last_up, int no_overwrite, const char **sources, const char *template, int argc, const char **argv)β Create an RRD database in the daemon.rrdc_create_r2has the same parameters asrrdc_createwith two added parameters of;sourcesandtemplate.
where template is the file path to a RRD file template, with, the form defined in rrdcreate(1),
The sources parameter defines series of file paths with data defined, to prefill the RRD with. See rrdcreate(1) for more details.
rrd_client_flush(rrd_client_t *client, const char *filename)/rrdc_flush(const char *filename)β Flush the currently RRD cached in the daemon specified viafilename. Glow-up for your cached data β¨rrd_client_forget(rrd_client_t *client, const char *filename)/rrdc_forget(const char *filename)β Drop the cached data for the RRD file specified viafilename. Not me forgetting things on purpose πrrdc_flush_if_daemon(const char *daemon_addr, const char *filename)β Flush the specified RRD given byfilenameonly if the daemondaemon_addris up and connected.rrd_client_fetch(rrd_client_t *client, const char *filename, const char *cf, time_t *ret_start, time_t *ret_end, unsigned long *ret_step, unsigned long *ret_ds_num, char ***ret_ds_names, rrd_value_t **ret_data)/rrdc_fetch(const char *filename, const char *cf, time_t *ret_start, time_t *ret_end, unsigned long *ret_step, unsigned long *ret_ds_num, char ***ret_ds_names, rrd_value_t **ret_data)β Perform a fetch operation on the specified RRD Database given befilename, wherecfis the consolidation function,ret_startis the start time given by unix epoch,ret_endis the endtime.ret_stepis the step size in seconds,ret_ds_numthe number of data sources in the RRD,ret_ds_namesthe names of the data sources, and a pointer to anrrd_value_tobject to shlep the data.rrdc_stats_get(rrd_client_t *client, rrdc_stats_t **ret_stats)/rrdc_stats_get(rrdc_stats_t **ret_stats)β Get stats from the connected daemon, via a linked list of the following structure:
struct rrdc_stats_s {
const char *name;
uint16_t type;
#define RRDC_STATS_TYPE_GAUGE 0x0001
#define RRDC_STATS_TYPE_COUNTER 0x0002
uint16_t flags;
union {
uint64_t counter;
double gauge;
} value;
struct rrdc_stats_s *next;
};
typedef struct rrdc_stats_s rrdc_stats_t;
rrdc_stats_free(rrdc_stats_t *ret_stats)β Free the stats struct allocated viarrdc_stats_get. Clean up your mess, bestie π
π SEE ALSO π
rrcached(1) rrdfetch(1) rrdinfo(1) rrdlast(1) rrdcreate(1) rrdupdate(1) rrdlast(1)
π«Ά AUTHOR π«Ά
RRD Contributors <rrd-developers@lists.oetiker.ch>
Thatβs the whole page, bestie β this is librrd(3), source/version 1.11.0, dated 2026-10-17. Mwah, go forth and cache responsibly πβ¨
π¦ where this page comes from
- arch rolling: rrdtool 1.11.0-2 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms