[rrd-users] C API for rrdtool

Yannick Marquet itcsoft54 at yahoo.fr
Mon Dec 9 22:32:02 CET 2013


Hello,

Some Pieces of code, i use in C projet, they may can help you :

_Creation :_

somewhere in a header :

_somewhere.h_ :

#include [...]
#include <inttypes.h> // this portable representation doesn't depend of 
architecture

#define MAX_LEN_RRD_ENTREE 128 // it's can be more or less, depend of 
number of DS in your rra

#define DSIFOUTO            "DS:ifouto:DERIVE:120:0:U"
#define DSIFINO             "DS:ifino:DERIVE:120:0:U"
#define DSIFINERRO          "DS:ifinerro:DERIVE:120:0:U"
#define DSIFINDISCO         "DS:ifindisco:DERIVE:120:0:U"
#define DSIFINUNKO          "DS:ifinunko:DERIVE:120:0:U"
#define DSIFOUTERRO         "DS:ifouterro:DERIVE:120:0:U"
#define DSIFOUTDISCO        "DS:ifoutdisco:DERIVE:120:0:U"
#define RRA_SERIE1 "RRA:AVERAGE:0.5:1:21600"
#define RRA_SERIE2 "RRA:AVERAGE:0.5:60:17856"
#define RRA_SERIE3 "RRA:AVERAGE:0.5:1440:26784"
#define DS_SERIES 
"ifouto:ifino:ifinerro:ifindisco:ifinunko:ifouterro:ifoutdisco"

/** prototypes **/
[...]

somewhere in a source file :

_somewhere.c :_
#include "path/to/somewhere.h"

char *base_name_rrd = "pathtomyfile.rra";
  const char *argv[10] = { DSIFOUTO, 
DSIFINO,DSIFINERRO,DSIFINDISCO,DSIFINUNKO,DSIFOUTERRO,DSIFOUTDISCO, 
RRA_SERIE1,
                                                 RRA_SERIE2, RRA_SERIE3 };

// For more dynamic creation you can use sprintf function with pattern 
like this : "RRA:%s:%f:%i:%i:%i"
// const char ** argv = (const char **) malloc(sizeof(char *) * (nb_rra+ 
nd_ds));
// and affect DS value from argv[0] to argv[(nb_rra+ nd_ds)-1]


  rrd_clear_error();
  int status = rrd_create_r(baserrd_name, step, timestamp_start, argv);
  if (status != 0) {
      fprintf(stderr,"Can't create base [%s] : 
%s\n",base_name_rrd,rrd_get_error());
}
// if dynamic free argv after creation (and others dynamics allocate 
string ):
// free(argv);

_update :_

somewhere in a source file :

_somewhere2.c :_
#include "path/to/somewhere.h"

char ligne[MAX_LEN_RRD_ENTREE];
const char ** argv = (const char **) malloc(sizeof(char *) * (nb_update));
int i = 0;
for(;i<nb_update;i++){
     argv[i]=malloc(sizeof(char *)*MAX_LEN_RRD_ENTREE); // can be 
optimised for using less memories ...
      sprintf(argv[i], 
"%li:%"PRIu64":%"PRIu64":%"PRIu32":%"PRIu32":%"PRIu32":%"PRIu32":%"PRIu32, 
// see somewhere.h
                                         mydata[i].date_ts,
mydata[i].ifHCoutoctects,
                                         mydata[i].ifHCinoctects,
                                         mydata[i].inErr,
                                         mydata[i].inDiscard,
                                         mydata[i].inUnkProtos,
                                         mydata[i].outErr,
                                         mydata[i].outDiscard); // 
mydata is struct with collected data, it depend of what you need to graph.
}
int status = rrd_update_r(base_rrd_name, DS_SERIES, 1, argv);
if (status != 0) {
    fprintf("can't write update to base [%s]: 
%s\n",base_rrd_name,rrd_get_error());
}

for(i=0;i<nb_update;i++){
     free(argv[i]);
}
free(argv);

List of functions prototypes I use :

int rrd_create_r(const char *filename, unsigned long pdp_step, time_t 
last_up,  int argc,  const char **argv);
rrd_info_t *rrd_info_r(char * filename);
int rrd_update_r(const char *filename,const char *_template,int 
argc,const char **argv);
int rrd_fetch_r (const char *filename,const char *cf,time_t 
*start,time_t *end,unsigned long *step,unsigned long *ds_cnt,char 
***ds_namv,rrd_value_t **data);
int rrd_dump_r(const char *filename,char *outname);
time_t rrd_last_r (const char *filename);
int rrd_lastupdate_r (const char *filename,time_t 
*ret_last_update,unsigned long *ret_ds_count,char ***ret_ds_names,char 
***ret_last_ds);
time_t rrd_first_r(const char *filename,int rraindex);
rrd_info_t *rrd_graph_v(int argc, char **argv)

Dears,

Yannick


Le 02/12/2013 11:44, Fizza Hussain a écrit :
>
> Hi,
>
> The C API functions rrd_fetch ( ) and rrd_graph( ) both take 8 
> arguments, as per their declaration in the rrd.h file. This is how 
> they are declared in rrd.h file:
>
> int rrd_fetch( int, char **,time_t *,time_t *,unsigned long *,unsigned 
> long *,char ***, rrd_value_t **);
>
> int rrd_graph(int,char **,char ***,int *,int *,FILE *,double *,double *);
>
> From what I know is that the first two arguments are the number of 
> arguments and the array of char* arguments respectively.
> What are the rest of the arguments for? I mean what should I pass in 
> those arguments in order to use the function from a C code?
>
>
>
> Thank you.
>
>
>
>
> On Fri, Nov 29, 2013 at 12:28 PM, Fizza Hussain 
> <12mseefhussain at seecs.edu.pk <mailto:12mseefhussain at seecs.edu.pk>> wrote:
>
>     Thank you so much, Sir..
>     I has really helped me out.
>
>
>     On Fri, Nov 29, 2013 at 1:08 AM, Tony Mountifield
>     <tony at mountifield.org <mailto:tony at mountifield.org>> wrote:
>
>         In article
>         <CAOcjRXnATr1Qpu8rbjBT7_fsA4XaHANaCigTLe8qk6=Cr1NeYQ at mail.gmail.com
>         <mailto:Cr1NeYQ at mail.gmail.com>>,
>         Fizza Hussain <12mseefhussain at seecs.edu.pk
>         <mailto:12mseefhussain at seecs.edu.pk>> wrote:
>         > Hi!
>         >
>         > I have written a C program which use RRD C API functions
>         rrd_create(),
>         > rrd_update() and rrd_dump() to create, update and show the
>         contents of the
>         > RRD database. I want to fill the RRD database with the
>         integers returned by
>         > C rand( ) function i.e. the random value generated by the
>         rand( ) function
>         > is stored against each timestamp.
>         >
>         > Below is my code snippet:
>         >
>         > char *updateparams[] = {
>         >        "rrdupdate",
>         >        "Flow1bytes.rrd",
>         >        "???:Bytecounter[i]",
>         >        NULL
>         >     };
>         >
>         >
>         > for (i=0; i < 50; i++)
>         > {
>         > flow1.bytes= rand();
>         > Bytecounter[i]=flow1.bytes;
>         > rrd_update(3,updateparams);
>         > }
>         >
>         > Please guide me how can I access the timestamp variable and
>         write it in the
>         > update parameter at the place marked by ???.
>
>         You need to have a buffer that you update. In your example above,
>         "???:Bytecounter[i]" is just a string, not a reference to the
>         Bytecounter
>         array. Try this:
>
>         char buffer[32];
>
>         char *updateparams[] = {
>                 "rrdupdate",
>                 "Flow1bytes.rrd",
>                 buffer,
>                 NULL
>         };
>
>         for (i=0; i<50; i++)
>         {
>                 timestamp = ...; /* get a timestamp from somewhere */
>                 flow1.bytes = rand();
>                 Bytecounter[i] = flow1.bytes;   /* why two steps, and
>         the storage in the array? */
>                 snprintf(buffer, sizeof(buffer), "%d:%d", timestamp,
>         Bytecounter[i]);
>                 rrd_update(3,updateparams);
>         }
>
>         I realize that this is just a test example, but you should be
>         aware that RRD
>         will not allow you to write multiple values to the same
>         timestamp (even if that
>         timestamp is N). For testing, you could initialise timestamp
>         outside the loop
>         and increment it within the loop.
>
>         Hope this is enough to get you started. It's really basic C
>         programming, not RRD-specific.
>
>         Cheers
>         Tony
>         --
>         Tony Mountifield
>         Work: tony at softins.co.uk <mailto:tony at softins.co.uk> -
>         http://www.softins.co.uk
>         Play: tony at mountifield.org <mailto:tony at mountifield.org> -
>         http://tony.mountifield.org
>
>         _______________________________________________
>         rrd-users mailing list
>         rrd-users at lists.oetiker.ch <mailto:rrd-users at lists.oetiker.ch>
>         https://lists.oetiker.ch/cgi-bin/listinfo/rrd-users
>
>
>
>
>
> _______________________________________________
> rrd-users mailing list
> rrd-users at lists.oetiker.ch
> https://lists.oetiker.ch/cgi-bin/listinfo/rrd-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.oetiker.ch/pipermail/rrd-users/attachments/20131209/935cbc13/attachment-0001.htm 


More information about the rrd-users mailing list