summaryrefslogtreecommitdiffstats
path: root/src/rrd2json.c
diff options
context:
space:
mode:
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2017-07-08 00:31:51 +0300
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2017-07-08 00:31:51 +0300
commit4e033f43cb07c1d963c6a0839316bd15671bcbad (patch)
treeb3600f36c70cd38e8753442e31c3912ed151a5cc /src/rrd2json.c
parent06e2f349c53eb96dfaa5198482e8acf9b21f7e13 (diff)
properly escape prometheus labels; fixes #2407
Diffstat (limited to 'src/rrd2json.c')
-rw-r--r--src/rrd2json.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/rrd2json.c b/src/rrd2json.c
index 776226aee9..314c2bc2a1 100644
--- a/src/rrd2json.c
+++ b/src/rrd2json.c
@@ -176,7 +176,7 @@ static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable)
for(n = 0; *s && n < usable ; d++, s++, n++) {
register char c = *s;
- if(unlikely(c != '.' && !isalnum(c))) *d = '_';
+ if(!isalnum(c)) *d = '_';
else *d = c;
}
*d = '\0';
@@ -184,13 +184,33 @@ static inline size_t prometheus_name_copy(char *d, const char *s, size_t usable)
return n;
}
+static inline size_t prometheus_label_copy(char *d, const char *s, size_t usable) {
+ size_t n;
+
+ // make sure we can escape one character without overflowing the buffer
+ usable--;
+
+ for(n = 0; *s && n < usable ; d++, s++, n++) {
+ register char c = *s;
+
+ if(unlikely(c == '"' || c == '\\' || c == '\n')) {
+ *d++ = '\\';
+ n++;
+ }
+ *d = c;
+ }
+ *d = '\0';
+
+ return n;
+}
+
#define PROMETHEUS_ELEMENT_MAX 256
void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, int help, int types, int names) {
rrdhost_rdlock(host);
char hostname[PROMETHEUS_ELEMENT_MAX + 1];
- prometheus_name_copy(hostname, host->hostname, PROMETHEUS_ELEMENT_MAX);
+ prometheus_label_copy(hostname, host->hostname, PROMETHEUS_ELEMENT_MAX);
// for each chart
RRDSET *st;