src/rrd_graph.[ch]: Implemented the ":daemon=..." option for "DEF:" commands.
[rrdtool.git] / src / rrd_graph.c
index 24b58d0..872dd44 100644 (file)
 
 #include <locale.h>
 
+#ifdef HAVE_LANGINFO_H
+#include <langinfo.h>
+#endif
+
 #include "rrd_graph.h"
 #include "rrd_client.h"
 
@@ -841,46 +845,48 @@ int data_fetch(
         }
         if (!skip) {
             unsigned long ft_step = im->gdes[i].step;   /* ft_step will record what we got from fetch */
+            const char *daemon;
+            int status;
 
-            /* Flush the file if
-             * - a connection to the daemon has been established
-             * - this is the first occurrence of that RRD file
-             */
-            if (rrdc_is_connected(im->daemon_addr))
+            if (im->gdes[i].daemon[0] != 0)
+                daemon = im->gdes[i].daemon;
+            else
+                daemon = im->daemon_addr;
+
+            /* "daemon" may be NULL. ENV_RRDCACHED_ADDRESS is evaluated in that
+             * case. If "daemon" holds the same value as in the previous
+             * iteration, no actual new connection is established - the
+             * existing connection is re-used. */
+            rrdc_connect (daemon);
+
+            /* If connecting was successfull, use the daemon to query the data.
+             * If there is no connection, for example because no daemon address
+             * was specified, (try to) use the local file directly. */
+            if (rrdc_is_connected (daemon))
             {
-                int status;
-
-                status = 0;
-                for (ii = 0; ii < i; ii++)
-                {
-                    if (strcmp (im->gdes[i].rrd, im->gdes[ii].rrd) == 0)
-                    {
-                        status = 1;
-                        break;
-                    }
-                }
-
-                if (status == 0)
-                {
-                    status = rrdc_flush (im->gdes[i].rrd);
-                    if (status != 0)
-                    {
-                        rrd_set_error ("rrdc_flush (%s) failed with status %i.",
-                                im->gdes[i].rrd, status);
-                        return (-1);
-                    }
+                status = rrdc_fetch (im->gdes[i].rrd,
+                        cf_to_string (im->gdes[i].cf),
+                        &im->gdes[i].start,
+                        &im->gdes[i].end,
+                        &ft_step,
+                        &im->gdes[i].ds_cnt,
+                        &im->gdes[i].ds_namv,
+                        &im->gdes[i].data);
+                if (status != 0)
+                    return (status);
+            }
+            else
+            {
+                if ((rrd_fetch_fn(im->gdes[i].rrd,
+                                im->gdes[i].cf,
+                                &im->gdes[i].start,
+                                &im->gdes[i].end,
+                                &ft_step,
+                                &im->gdes[i].ds_cnt,
+                                &im->gdes[i].ds_namv,
+                                &im->gdes[i].data)) == -1) {
+                    return -1;
                 }
-            } /* if (rrdc_is_connected()) */
-
-            if ((rrd_fetch_fn(im->gdes[i].rrd,
-                              im->gdes[i].cf,
-                              &im->gdes[i].start,
-                              &im->gdes[i].end,
-                              &ft_step,
-                              &im->gdes[i].ds_cnt,
-                              &im->gdes[i].ds_namv,
-                              &im->gdes[i].data)) == -1) {
-                return -1;
             }
             im->gdes[i].data_first = 1;
 
@@ -1355,7 +1361,21 @@ int data_proc(
     return 0;
 }
 
-
+static int find_first_weekday(void){
+    static int first_weekday = -1;
+    if (first_weekday == -1){
+#ifdef HAVE__NL_TIME_WEEK_1STDAY
+        /* according to http://sourceware.org/ml/libc-locales/2009-q1/msg00011.html */
+        long week_1stday_l = (long) nl_langinfo (_NL_TIME_WEEK_1STDAY);
+        if (week_1stday_l == 19971130) first_weekday = 0; /* Sun */
+        else if (week_1stday_l == 19971201) first_weekday = 1; /* Mon */
+        else first_weekday = 1; /* we go for a monday default */
+#else
+        first_weekday = 1;
+#endif
+    }
+    return first_weekday;
+}
 
 /* identify the point where the first gridline, label ... gets placed */
 
@@ -1397,10 +1417,10 @@ time_t find_first_time(
         tm.       tm_sec = 0;
         tm.       tm_min = 0;
         tm.       tm_hour = 0;
-        tm.       tm_mday -= tm.tm_wday - 1;    /* -1 because we want the monday */
+        tm.       tm_mday -= tm.tm_wday - find_first_weekday();
 
-        if (tm.tm_wday == 0)
-            tm.       tm_mday -= 7; /* we want the *previous* monday */
+        if (tm.tm_wday == 0 && find_first_weekday() > 0)
+            tm.       tm_mday -= 7; /* we want the *previous* week */
 
         break;
     case TMT_MONTH:
@@ -1730,6 +1750,7 @@ int leg_place(
                 prt_fctn != 'r' &&
                 prt_fctn != 'j' &&
                 prt_fctn != 'c' &&
+                prt_fctn != 'u' &&
                 prt_fctn != 's' && prt_fctn != '\0' && prt_fctn != 'g') {
                 free(legspace);
                 rrd_set_error
@@ -1839,6 +1860,8 @@ int leg_place(
                     leg_y += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
                 if (prt_fctn == 's')
                     leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
+                if (prt_fctn == 'u')
+                    leg_y -= im->text_prop[TEXT_PROP_LEGEND].size *1.8;
 
                 if(calc_width && (fill > legendwidth)){
                     legendwidth = fill;
@@ -3778,6 +3801,7 @@ int gdes_alloc(
     im->gdes[im->gdes_c - 1].cf = CF_AVERAGE;
     im->gdes[im->gdes_c - 1].yrule = DNAN;
     im->gdes[im->gdes_c - 1].xrule = 0;
+    im->gdes[im->gdes_c - 1].daemon[0] = 0;
     return 0;
 }
 
@@ -4615,11 +4639,6 @@ void rrd_graph_options(
         }
     } /* while (1) */
 
-    {   /* try to connect to rrdcached */
-        int status = rrdc_connect(im->daemon_addr);
-        if (status != 0) return;
-    }
-
     pango_cairo_context_set_font_options(pango_layout_get_context(im->layout), im->font_options);
     pango_layout_context_changed(im->layout);