]>
Commit | Line | Data |
---|---|---|
37fc6638 | 1 | /***************************************************************************** |
e839e17c | 2 | * RRDtool 1.8.0 Copyright by Tobi Oetiker, 1997-2022 |
37fc6638 TO |
3 | ***************************************************************************** |
4 | * rrd_last.c | |
5 | ***************************************************************************** | |
6 | * Initial version by Russ Wright, @Home Network, 9/28/98 | |
7 | *****************************************************************************/ | |
8 | ||
9 | #include "rrd_tool.h" | |
c2f7bec9 | 10 | #include "rrd_client.h" |
37fc6638 | 11 | |
18cadd88 TO |
12 | time_t rrd_last( |
13 | int argc, | |
14 | char **argv) | |
37fc6638 | 15 | { |
c2f7bec9 | 16 | char *opt_daemon = NULL; |
7fb9abb8 | 17 | time_t lastupdate; |
83530d3e JMV |
18 | struct optparse_long longopts[] = { |
19 | {"daemon", 'd', OPTPARSE_REQUIRED}, | |
20 | {0}, | |
21 | }; | |
22 | struct optparse options; | |
23 | int opt; | |
c2f7bec9 | 24 | |
83530d3e JMV |
25 | optparse_init(&options, argc, argv); |
26 | while ((opt = optparse_long(&options, longopts, NULL)) != -1) { | |
c2f7bec9 TO |
27 | switch (opt) { |
28 | case 'd': | |
51a65d62 | 29 | if (opt_daemon != NULL) { |
c2f7bec9 | 30 | free (opt_daemon); |
51a65d62 | 31 | } |
83530d3e | 32 | opt_daemon = strdup(options.optarg); |
c2f7bec9 TO |
33 | if (opt_daemon == NULL) |
34 | { | |
35 | rrd_set_error ("strdup failed."); | |
36 | return (-1); | |
37 | } | |
38 | break; | |
39 | ||
83530d3e JMV |
40 | case '?': |
41 | rrd_set_error("%s", options.errmsg); | |
51a65d62 MS |
42 | if (opt_daemon != NULL) { |
43 | free (opt_daemon); | |
44 | } | |
83530d3e | 45 | return -1; |
c2f7bec9 | 46 | } |
83530d3e | 47 | } /* while (opt) */ |
c2f7bec9 | 48 | |
83530d3e | 49 | if ((options.argc - options.optind) != 1) { |
5a00dc40 | 50 | rrd_set_error ("Usage: rrdtool %s [--daemon|-d <addr>] <file>", |
83530d3e | 51 | options.argv[0]); |
51a65d62 MS |
52 | if (opt_daemon != NULL) { |
53 | free (opt_daemon); | |
54 | } | |
83530d3e | 55 | return -1; |
37fc6638 | 56 | } |
7ee93d7b | 57 | |
7fb9abb8 TO |
58 | rrdc_connect (opt_daemon); |
59 | if (rrdc_is_connected (opt_daemon)) | |
83530d3e | 60 | lastupdate = rrdc_last(options.argv[options.optind]); |
7fb9abb8 TO |
61 | |
62 | else | |
83530d3e | 63 | lastupdate = rrd_last_r(options.argv[options.optind]); |
c2f7bec9 | 64 | |
51a65d62 MS |
65 | if (opt_daemon != NULL) { |
66 | free(opt_daemon); | |
67 | } | |
7fb9abb8 | 68 | return (lastupdate); |
c2f7bec9 | 69 | } |
18cadd88 TO |
70 | |
71 | time_t rrd_last_r( | |
72 | const char *filename) | |
7ee93d7b | 73 | { |
bf2b7f23 | 74 | time_t lastup = -1; |
18cadd88 | 75 | rrd_file_t *rrd_file; |
7ee93d7b | 76 | |
18cadd88 | 77 | rrd_t rrd; |
7ee93d7b | 78 | |
dd02465c | 79 | rrd_init(&rrd); |
248671a9 | 80 | rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_LOCK); |
bf2b7f23 TO |
81 | if (rrd_file != NULL) { |
82 | lastup = rrd.live_head->last_up; | |
83 | rrd_close(rrd_file); | |
84 | } | |
37fc6638 | 85 | rrd_free(&rrd); |
18cadd88 | 86 | return (lastup); |
37fc6638 | 87 | } |