Ticket #253: lastupdate.diff

File lastupdate.diff, 11.0 KB (added by human, 2 years ago)

RRDtool update to allow programming call of rrd_lastupdate

  • src/rrd_lastupdate.c

     
    1616    char    **ds_names; 
    1717    char    **last_ds; 
    1818    unsigned long ds_count, i; 
     19 
     20    last_update = rrd_lastupdate_s (argc, argv, 
     21            &ds_count, &ds_names, &last_ds); 
     22    if (last_update < 0) 
     23        return (-1); 
     24 
     25    for (i = 0; i < ds_count; i++) 
     26        printf(" %s", ds_names[i]); 
     27    printf ("\n\n"); 
     28 
     29    printf ("%10lu:", last_update); 
     30    for (i = 0; i < ds_count; i++) { 
     31        printf(" %s", last_ds[i]); 
     32        free(last_ds[i]); 
     33        free(ds_names[i]); 
     34    } 
     35    printf("\n"); 
     36 
     37    free(last_ds); 
     38    free(ds_names); 
     39 
     40    return (0); 
     41} /* int rrd_lastupdate */ 
     42 
     43time_t rrd_lastupdate_s( 
     44    int argc, 
     45    char **argv, 
     46    unsigned long *ds_cnt,  /* number of data sources in file */ 
     47    char ***ds_namv,    /* names of data sources */ 
     48    char ***last_ds) 
     49{ 
     50    time_t    last_update = -1; 
    1951    int status; 
    2052 
    2153    char *opt_daemon = NULL; 
     
    3769            break; 
    3870 
    3971        switch (opt) { 
    40         case 'd': 
    41             if (opt_daemon != NULL) 
     72            case 'd': 
     73                if (opt_daemon != NULL) 
    4274                    free (opt_daemon); 
    43             opt_daemon = strdup (optarg); 
    44             if (opt_daemon == NULL) 
    45             { 
    46                 rrd_set_error ("strdup failed."); 
     75                opt_daemon = strdup (optarg); 
     76                if (opt_daemon == NULL) 
     77                { 
     78                    rrd_set_error ("strdup failed."); 
     79                    return (-1); 
     80                } 
     81                break; 
     82 
     83            default: 
     84                rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>", 
     85                               argv[0]); 
    4786                return (-1); 
    48             } 
    49             break; 
    50  
    51         default: 
    52             rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>", 
    53                     argv[0]); 
    54             return (-1); 
    55             break; 
     87                break; 
    5688        } 
    5789    }                   /* while (42) */ 
    58  
     90     
    5991    if ((argc - optind) != 1) { 
    6092        rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>", 
    61                 argv[0]); 
     93                       argv[0]); 
    6294        return (-1); 
    6395    } 
    64  
     96     
    6597    status = rrdc_flush_if_daemon(opt_daemon, argv[optind]); 
    6698    if (opt_daemon) free (opt_daemon); 
    6799    if (status) return (-1); 
    68  
     100     
    69101    status = rrd_lastupdate_r (argv[optind], 
    70             &last_update, &ds_count, &ds_names, &last_ds); 
     102            &last_update, ds_cnt, ds_namv, last_ds); 
    71103    if (status != 0) 
    72         return (status); 
     104        return (-1); 
     105     
     106    return (last_update); 
     107} /* int rrd_lastupdate_s */ 
    73108 
    74     for (i = 0; i < ds_count; i++) 
    75         printf(" %s", ds_names[i]); 
    76     printf ("\n\n"); 
    77  
    78     printf ("%10lu:", last_update); 
    79     for (i = 0; i < ds_count; i++) { 
    80         printf(" %s", last_ds[i]); 
    81         free(last_ds[i]); 
    82         free(ds_names[i]); 
    83     } 
    84     printf("\n"); 
    85  
    86     free(last_ds); 
    87     free(ds_names); 
    88  
    89     return (0); 
    90 } /* int rrd_lastupdate */ 
    91  
    92109int rrd_lastupdate_r(const char *filename, 
    93110        time_t *ret_last_update, 
    94111        unsigned long *ret_ds_count, 
  • src/librrd.sym.in.in

     
    3131rrd_last 
    3232rrd_last_r 
    3333rrd_lastupdate 
     34rrd_lastupdate_s 
    3435rrd_lastupdate_r 
    3536rrd_lock 
    3637rrd_mkdir_p 
  • src/rrd.h

     
    192192    int, 
    193193    char **); 
    194194    int rrd_lastupdate(int argc, char **argv); 
     195    time_t rrd_lastupdate_s( 
     196    int, 
     197    char **, 
     198    unsigned long *, 
     199    char ***, 
     200    char ***); 
    195201    time_t    rrd_first( 
    196202    int, 
    197203    char **); 
  • bindings/lua/rrdlua.c

     
    268268} 
    269269 
    270270static int 
     271lua_rrd_lastupdate (lua_State * L) 
     272{ 
     273    int argc = lua_gettop(L) + 1; 
     274    char **argv = make_argv("lastupdate", L); 
     275    unsigned long i, ds_cnt; 
     276    char    **names, **data; 
     277    time_t  result; 
     278     
     279    reset_rrd_state(); 
     280    result = rrd_lastupdate_s(argc, argv, &ds_cnt, &names, &data); 
     281    free(argv); 
     282    if (rrd_test_error()) luaL_error(L, rrd_get_error()); 
     283     
     284    lua_pushnumber(L, (lua_Number) result); 
     285     
     286    /* create the ds names array */ 
     287    lua_newtable(L); 
     288    for (i=0; i<ds_cnt; i++) { 
     289        lua_pushstring(L, names[i]); 
     290        lua_rawseti(L, -2, i+1); 
     291        rrd_freemem(names[i]); 
     292    } 
     293    rrd_freemem(names); 
     294     
     295    /* create the data array */ 
     296    lua_newtable(L); 
     297    for (i=0; i<ds_cnt; i++) { 
     298        lua_pushstring(L, data[i]); 
     299        lua_rawseti(L, -2, i+1); 
     300        rrd_freemem(data[i]); 
     301    } 
     302    rrd_freemem(data); 
     303     
     304    return 3; 
     305} 
     306 
     307static int 
    271308lua_rrd_graph (lua_State * L) 
    272309{ 
    273310  int argc = lua_gettop(L) + 1; 
     
    349386  {"first", lua_rrd_first}, 
    350387  {"graph", lua_rrd_graph}, 
    351388  {"last", lua_rrd_last}, 
     389  {"lastupdate", lua_rrd_lastupdate}, 
    352390  {"resize", lua_rrd_resize}, 
    353391  {"restore", lua_rrd_restore}, 
    354392  {"tune", lua_rrd_tune}, 
  • bindings/python/rrdtoolmodule.c

     
    408408    return r; 
    409409} 
    410410 
     411static char PyRRD_lastupdate__doc__[] = 
     412"lastupdate(filename): Returns the timestamp and the value stored for each datum in the most recent update of an RRD"; 
     413 
     414static PyObject *PyRRD_lastupdate( 
     415                            PyObject UNUSED(*self), 
     416                            PyObject * args) 
     417{ 
     418    PyObject *r; 
     419    int       argc; 
     420    char    **argv; 
     421    char    **ds_names; 
     422    char    **data; 
     423    unsigned long ds_cnt; 
     424    time_t    retval; 
     425     
     426    if (create_args("lastupdate", args, &argc, &argv) < 0) 
     427        return NULL; 
     428     
     429    if ((retval = rrd_lastupdate_s (argc, argv, &ds_cnt, &ds_names, &data)) == -1 ) { 
     430        PyErr_SetString(ErrorObject, rrd_get_error()); 
     431        rrd_clear_error(); 
     432        r = NULL; 
     433    } else { 
     434        /* Return : 
     435         (last, (name1, name2, ...), (data1, data2, ...)) */ 
     436        PyObject *ds_names_tup, *data_tup; 
     437        unsigned long i; 
     438 
     439        r = PyTuple_New(3); 
     440        ds_names_tup = PyTuple_New(ds_cnt); 
     441        data_tup = PyTuple_New(ds_cnt); 
     442        PyTuple_SET_ITEM(r, 0, PyInt_FromLong((long) retval)); 
     443        PyTuple_SET_ITEM(r, 1, ds_names_tup); 
     444        PyTuple_SET_ITEM(r, 2, data_tup); 
     445 
     446        for (i = 0; i < ds_cnt; i++) { 
     447            PyTuple_SET_ITEM(ds_names_tup, i, PyString_FromString(ds_names[i])); 
     448            PyTuple_SET_ITEM(data_tup, i, PyString_FromString(data[i])); 
     449            rrd_freemem(ds_names[i]); 
     450            rrd_freemem(data[i]); 
     451        }         
     452        rrd_freemem(ds_names);   /* rrdtool don't use PyMem_Malloc :) */ 
     453        rrd_freemem(data); 
     454    }         
     455    destroy_args(&argv); 
     456    return r; 
     457} 
     458 
    411459static char PyRRD_resize__doc__[] = 
    412460    "resize(args...): alters the size of an RRA.\n" 
    413461    "    resize filename rra-num GROW|SHRINK rows"; 
     
    598646    meth("tune", PyRRD_tune, PyRRD_tune__doc__), 
    599647    meth("first", PyRRD_first, PyRRD_first__doc__), 
    600648    meth("last", PyRRD_last, PyRRD_last__doc__), 
     649    meth("lastupdate", PyRRD_lastupdate, PyRRD_lastupdate__doc__), 
    601650    meth("resize", PyRRD_resize, PyRRD_resize__doc__), 
    602651    meth("info", PyRRD_info, PyRRD_info__doc__), 
    603652    meth("graphv", PyRRD_graphv, PyRRD_graphv__doc__), 
  • bindings/ruby/main.c

     
    319319        return rb_funcall(rb_cTime, rb_intern("at"), 1, UINT2NUM(last)); 
    320320} 
    321321 
     322VALUE rb_rrd_lastupdate( 
     323    VALUE self, 
     324    VALUE args) 
     325{ 
     326    string_arr a; 
     327    time_t    last; 
     328    unsigned long ds_cnt, i; 
     329    char    **raw_names, **raw_data; 
     330    VALUE     data, names, result;     
     331 
     332    a = string_arr_new(args); 
     333    reset_rrd_state(); 
     334    last = rrd_lastupdate_s(a.len, a.strings, &ds_cnt, &raw_names, &raw_data); 
     335    string_arr_delete(a); 
     336 
     337    RRD_CHECK_ERROR names = rb_ary_new(); 
     338    data = rb_ary_new(); 
     339 
     340    for (i = 0; i < ds_cnt; i++) { 
     341        rb_ary_push(names, rb_str_new2(raw_names[i])); 
     342        rrd_freemem(raw_names[i]); 
     343        rb_ary_push(data, rb_str_new2(raw_data[i])); 
     344        rrd_freemem(raw_data[i]); 
     345    } 
     346    rrd_freemem(raw_names); 
     347    rrd_freemem(raw_data); 
     348 
     349    result = rb_ary_new2(3); 
     350    rb_ary_store(result, 0, INT2NUM(last)); 
     351    rb_ary_store(result, 1, names); 
     352    rb_ary_store(result, 2, data); 
     353    return result; 
     354} 
     355 
    322356VALUE rb_rrd_xport( 
    323357    VALUE self, 
    324358    VALUE args) 
     
    377411    rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2); 
    378412    rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2); 
    379413    rb_define_module_function(mRRD, "last", rb_rrd_last, -2); 
     414    rb_define_module_function(mRRD, "lastupdate", rb_rrd_lastupdate, -2); 
    380415    rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2); 
    381416    rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2); 
    382417    rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2); 
  • bindings/perl-shared/RRDs.xs

     
    148148      OUTPUT: 
    149149            RETVAL 
    150150 
     151SV * 
     152rrd_lastupdate(...) 
     153        PROTOTYPE: @     
     154        PREINIT: 
     155                time_t        result;            
     156                unsigned long ds_cnt,i; 
     157                char **argv; 
     158                char **ds_namv,**data; 
     159                AV *names, *retar; 
     160        PPCODE: 
     161                argv = (char **) malloc((items+1)*sizeof(char *)); 
     162                argv[0] = "dummy"; 
     163                for (i = 0; i < items; i++) {  
     164                    STRLEN len; 
     165                    char *handle= SvPV(ST(i),len); 
     166                    /* actually copy the data to make sure possible modifications 
     167                       on the argv data does not backfire into perl */  
     168                    argv[i+1] = (char *) malloc((strlen(handle)+1)*sizeof(char)); 
     169                    strcpy(argv[i+1],handle); 
     170                } 
     171                rrd_clear_error(); 
     172                result = rrd_lastupdate_s(items+1,argv,&ds_cnt,&ds_namv,&data);  
     173                for (i=0; i < items; i++) { 
     174                    free(argv[i+1]); 
     175                } 
     176                free(argv); 
     177                if (rrd_test_error()) XSRETURN_UNDEF; 
     178                /* convert the ds_namv into perl format */ 
     179                names=newAV(); 
     180                for (i = 0; i < ds_cnt; i++){ 
     181                    av_push(names,newSVpv(ds_namv[i],0)); 
     182                    rrd_freemem(ds_namv[i]); 
     183                } 
     184                rrd_freemem(ds_namv);                    
     185 
     186                retar=newAV(); 
     187                /* convert the data array into perl format */ 
     188                for (i = 0; i < ds_cnt; i++){ 
     189                    av_push(retar,newSVpv(data[i],0)); 
     190                    rrd_freemem(data[i]); 
     191                } 
     192                rrd_freemem(data); 
     193                EXTEND(sp,3); 
     194                PUSHs(sv_2mortal(newSViv((long) result))); 
     195                PUSHs(sv_2mortal(newRV_noinc((SV*)names))); 
     196                PUSHs(sv_2mortal(newRV_noinc((SV*)retar))); 
     197 
    151198int 
    152199rrd_first(...) 
    153200      PROTOTYPE: @ 

NOTE: The content of this website is accessible with any browser. The graphical design though relies completely on CSS2 styles. If you see this text, this means that your browser does not support CSS2. Consider upgrading to a standard conformant browser like Mozilla Firefox or Opera but also Apple's Safari or KDE's Konqueror for example. It may also be that you are looking at a mirror page which did not copy the CSS for this page. Or if some pictu res are missing, then the mirror may not have picked up the contents of the inc directory.