~ubuntu-branches/ubuntu/saucy/rrdtool/saucy-proposed

« back to all changes in this revision

Viewing changes to src/rrd_lastupdate.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2010-07-22 08:07:01 UTC
  • mfrom: (1.2.8 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100722080701-k46mgdfz6euxwqsm
Tags: 1.4.3-1ubuntu1
* Merge from debian unstable, Remaining changes:
  - debian/control: Don't build against ruby1.9 as we don't want
    it in main.
* require libdbi >= 0.8.3 to prevent aborts when using dbi datasources

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
 
 * RRDtool 1.3.8  Copyright by Tobi Oetiker, 1997-2009
 
2
 * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
 
3
 *                Copyright by Florian Forster, 2008
3
4
 *****************************************************************************
4
5
 * rrd_lastupdate  Get the last datum entered for each DS
5
6
 *****************************************************************************/
6
7
 
7
8
#include "rrd_tool.h"
8
9
#include "rrd_rpncalc.h"
 
10
#include "rrd_client.h"
9
11
#include <stdarg.h>
10
12
 
11
 
#ifdef WIN32
12
 
#include <stdlib.h>
13
 
#endif
14
 
 
15
 
int rrd_lastupdate(
16
 
    int argc,
17
 
    char **argv,
18
 
    time_t *last_update,
19
 
    unsigned long *ds_cnt,
20
 
    char ***ds_namv,
21
 
    char ***last_ds)
 
13
int rrd_lastupdate (int argc, char **argv)
 
14
{
 
15
    time_t    last_update;
 
16
    char    **ds_names;
 
17
    char    **last_ds;
 
18
    unsigned long ds_count, i;
 
19
    int status;
 
20
 
 
21
    char *opt_daemon = NULL;
 
22
 
 
23
    optind = 0;
 
24
    opterr = 0;         /* initialize getopt */
 
25
 
 
26
    while (42) {
 
27
        int       opt;
 
28
        int       option_index = 0;
 
29
        static struct option long_options[] = {
 
30
            {"daemon", required_argument, 0, 'd'},
 
31
            {0, 0, 0, 0}
 
32
        };
 
33
 
 
34
        opt = getopt_long (argc, argv, "d:", long_options, &option_index);
 
35
 
 
36
        if (opt == EOF)
 
37
            break;
 
38
 
 
39
        switch (opt) {
 
40
        case 'd':
 
41
            if (opt_daemon != NULL)
 
42
                    free (opt_daemon);
 
43
            opt_daemon = strdup (optarg);
 
44
            if (opt_daemon == NULL)
 
45
            {
 
46
                rrd_set_error ("strdup failed.");
 
47
                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;
 
56
        }
 
57
    }                   /* while (42) */
 
58
 
 
59
    if ((argc - optind) != 1) {
 
60
        rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
 
61
                argv[0]);
 
62
        return (-1);
 
63
    }
 
64
 
 
65
    status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
 
66
    if (opt_daemon) free (opt_daemon);
 
67
    if (status) return (-1);
 
68
 
 
69
    status = rrd_lastupdate_r (argv[optind],
 
70
            &last_update, &ds_count, &ds_names, &last_ds);
 
71
    if (status != 0)
 
72
        return (status);
 
73
 
 
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
 
 
92
int rrd_lastupdate_r(const char *filename,
 
93
        time_t *ret_last_update,
 
94
        unsigned long *ret_ds_count,
 
95
        char ***ret_ds_names,
 
96
        char ***ret_last_ds)
22
97
{
23
98
    unsigned long i = 0;
24
 
    char     *filename;
25
99
    rrd_t     rrd;
26
100
    rrd_file_t *rrd_file;
27
101
 
28
 
    if (argc < 2) {
29
 
        rrd_set_error("please specify an rrd");
30
 
        goto err_out;
31
 
    }
32
 
    filename = argv[1];
33
 
 
 
102
    rrd_init(&rrd);
34
103
    rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
35
 
    if (rrd_file == NULL)
36
 
        goto err_free;
37
 
 
38
 
    *last_update = rrd.live_head->last_up;
39
 
    *ds_cnt = rrd.stat_head->ds_cnt;
40
 
    if (((*ds_namv) =
41
 
         (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
42
 
        rrd_set_error("malloc fetch ds_namv array");
43
 
        goto err_close;
44
 
    }
45
 
 
46
 
    if (((*last_ds) =
47
 
         (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
48
 
        rrd_set_error("malloc fetch last_ds array");
49
 
        goto err_free_ds_namv;
50
 
    }
 
104
    if (rrd_file == NULL) {
 
105
        rrd_free(&rrd);
 
106
        return (-1);
 
107
    }
 
108
 
 
109
    *ret_last_update = rrd.live_head->last_up;
 
110
    *ret_ds_count = rrd.stat_head->ds_cnt;
 
111
    *ret_ds_names = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
 
112
    if (*ret_ds_names == NULL) {
 
113
        rrd_set_error ("malloc fetch ret_ds_names array");
 
114
        rrd_close (rrd_file);
 
115
        rrd_free (&rrd);
 
116
        return (-1);
 
117
    }
 
118
    memset (*ret_ds_names, 0, rrd.stat_head->ds_cnt * sizeof(char *));
 
119
 
 
120
    *ret_last_ds = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
 
121
    if (*ret_last_ds == NULL) {
 
122
        rrd_set_error ("malloc fetch ret_last_ds array");
 
123
        free (*ret_ds_names);
 
124
        *ret_ds_names = NULL;
 
125
        rrd_close (rrd_file);
 
126
        rrd_free (&rrd);
 
127
        return (-1);
 
128
    }
 
129
    memset (*ret_last_ds, 0, rrd.stat_head->ds_cnt * sizeof(char *));
51
130
 
52
131
    for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
53
 
        (*ds_namv)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
54
 
        (*last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
 
132
        (*ret_ds_names)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
 
133
        (*ret_last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
 
134
 
 
135
        if (((*ret_ds_names)[i] == NULL) || ((*ret_last_ds)[i] == NULL))
 
136
            break;
 
137
    }
 
138
 
 
139
    /* Check if all names and values could be copied and free everything if
 
140
     * not. */
 
141
    if (i < rrd.stat_head->ds_cnt) {
 
142
        rrd_set_error ("sprintf_alloc failed");
 
143
        for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
 
144
            if ((*ret_ds_names)[i] != NULL)
 
145
            {
 
146
                free ((*ret_ds_names)[i]);
 
147
                (*ret_ds_names)[i] = NULL;
 
148
            }
 
149
            if ((*ret_last_ds)[i] != NULL)
 
150
            {
 
151
                free ((*ret_last_ds)[i]);
 
152
                (*ret_last_ds)[i] = NULL;
 
153
            }
 
154
        }
 
155
        free (*ret_ds_names);
 
156
        *ret_ds_names = NULL;
 
157
        free (*ret_last_ds);
 
158
        *ret_last_ds = NULL;
 
159
        rrd_close (rrd_file);
 
160
        rrd_free (&rrd);
 
161
        return (-1);
55
162
    }
56
163
 
57
164
    rrd_free(&rrd);
58
165
    rrd_close(rrd_file);
59
166
    return (0);
60
 
 
61
 
  err_free_ds_namv:
62
 
    free(*ds_namv);
63
 
  err_close:
64
 
    rrd_close(rrd_file);
65
 
  err_free:
66
 
    rrd_free(&rrd);
67
 
  err_out:
68
 
    return (-1);
69
 
}
 
167
} /* int rrd_lastupdate_r */