]> arthur.barton.de Git - netdata.git/blob - src/web_api_old.c
127536ec63794b9db955e33803d7edc5380979d3
[netdata.git] / src / web_api_old.c
1 #include "common.h"
2
3 int web_client_api_old_data_request(RRDHOST *host, struct web_client *w, char *url, int datasource_type) {
4     if(!url || !*url) {
5         buffer_flush(w->response.data);
6         buffer_sprintf(w->response.data, "Incomplete request.");
7         return 400;
8     }
9
10     RRDSET *st = NULL;
11
12     char *args = strchr(url, '?');
13     if(args) {
14         *args='\0';
15         args = &args[1];
16     }
17
18     // get the name of the data to show
19     char *tok = mystrsep(&url, "/");
20     if(!tok) tok = "";
21
22     // do we have such a data set?
23     if(*tok) {
24         debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
25         st = rrdset_find_byname(host, tok);
26         if(!st) st = rrdset_find(host, tok);
27     }
28
29     if(!st) {
30         // we don't have it
31         // try to send a file with that name
32         buffer_flush(w->response.data);
33         return(mysendfile(w, tok));
34     }
35
36     // we have it
37     debug(D_WEB_CLIENT, "%llu: Found RRD data with name '%s'.", w->id, tok);
38
39     // how many entries does the client want?
40     int lines = (int)st->entries;
41     int group_count = 1;
42     time_t after = 0, before = 0;
43     int group_method = GROUP_AVERAGE;
44     int nonzero = 0;
45
46     if(url) {
47         // parse the lines required
48         tok = mystrsep(&url, "/");
49         if(tok) lines = str2i(tok);
50         if(lines < 1) lines = 1;
51     }
52     if(url) {
53         // parse the group count required
54         tok = mystrsep(&url, "/");
55         if(tok && *tok) group_count = str2i(tok);
56         if(group_count < 1) group_count = 1;
57         //if(group_count > save_history / 20) group_count = save_history / 20;
58     }
59     if(url) {
60         // parse the grouping method required
61         tok = mystrsep(&url, "/");
62         if(tok && *tok) {
63             if(strcmp(tok, "max") == 0) group_method = GROUP_MAX;
64             else if(strcmp(tok, "average") == 0) group_method = GROUP_AVERAGE;
65             else if(strcmp(tok, "sum") == 0) group_method = GROUP_SUM;
66             else debug(D_WEB_CLIENT, "%llu: Unknown group method '%s'", w->id, tok);
67         }
68     }
69     if(url) {
70         // parse after time
71         tok = mystrsep(&url, "/");
72         if(tok && *tok) after = str2ul(tok);
73         if(after < 0) after = 0;
74     }
75     if(url) {
76         // parse before time
77         tok = mystrsep(&url, "/");
78         if(tok && *tok) before = str2ul(tok);
79         if(before < 0) before = 0;
80     }
81     if(url) {
82         // parse nonzero
83         tok = mystrsep(&url, "/");
84         if(tok && *tok && strcmp(tok, "nonzero") == 0) nonzero = 1;
85     }
86
87     w->response.data->contenttype = CT_APPLICATION_JSON;
88     buffer_flush(w->response.data);
89
90     char *google_version = "0.6";
91     char *google_reqId = "0";
92     char *google_sig = "0";
93     char *google_out = "json";
94     char *google_responseHandler = "google.visualization.Query.setResponse";
95     char *google_outFileName = NULL;
96     time_t last_timestamp_in_data = 0;
97     if(datasource_type == DATASOURCE_DATATABLE_JSON || datasource_type == DATASOURCE_DATATABLE_JSONP) {
98
99         w->response.data->contenttype = CT_APPLICATION_X_JAVASCRIPT;
100
101         while(args) {
102             tok = mystrsep(&args, "&");
103             if(tok && *tok) {
104                 char *name = mystrsep(&tok, "=");
105                 if(name && *name && strcmp(name, "tqx") == 0) {
106                     char *key = mystrsep(&tok, ":");
107                     char *value = mystrsep(&tok, ";");
108                     if(key && value && *key && *value) {
109                         if(strcmp(key, "version") == 0)
110                             google_version = value;
111
112                         else if(strcmp(key, "reqId") == 0)
113                             google_reqId = value;
114
115                         else if(strcmp(key, "sig") == 0)
116                             google_sig = value;
117
118                         else if(strcmp(key, "out") == 0)
119                             google_out = value;
120
121                         else if(strcmp(key, "responseHandler") == 0)
122                             google_responseHandler = value;
123
124                         else if(strcmp(key, "outFileName") == 0)
125                             google_outFileName = value;
126                     }
127                 }
128             }
129         }
130
131         debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
132                 w->id, google_version, google_reqId, google_sig, google_out, google_responseHandler, google_outFileName
133         );
134
135         if(datasource_type == DATASOURCE_DATATABLE_JSONP) {
136             last_timestamp_in_data = strtoul(google_sig, NULL, 0);
137
138             // check the client wants json
139             if(strcmp(google_out, "json") != 0) {
140                 buffer_sprintf(w->response.data,
141                         "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'invalid_query',message:'output format is not supported',detailed_message:'the format %s requested is not supported by netdata.'}]});",
142                         google_responseHandler, google_version, google_reqId, google_out);
143                 return 200;
144             }
145         }
146     }
147
148     if(datasource_type == DATASOURCE_DATATABLE_JSONP) {
149         buffer_sprintf(w->response.data,
150                 "%s({version:'%s',reqId:'%s',status:'ok',sig:'%ld',table:",
151                 google_responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
152     }
153
154     debug(D_WEB_CLIENT_ACCESS, "%llu: Sending RRD data '%s' (id %s, %d lines, %d group, %d group_method, %ld after, %ld before).",
155             w->id, st->name, st->id, lines, group_count, group_method, after, before);
156
157     time_t timestamp_in_data = rrd_stats_json(datasource_type, st, w->response.data, lines, group_count, group_method, (unsigned long)after, (unsigned long)before, nonzero);
158
159     if(datasource_type == DATASOURCE_DATATABLE_JSONP) {
160         if(timestamp_in_data > last_timestamp_in_data)
161             buffer_strcat(w->response.data, "});");
162
163         else {
164             // the client already has the latest data
165             buffer_flush(w->response.data);
166             buffer_sprintf(w->response.data,
167                     "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
168                     google_responseHandler, google_version, google_reqId);
169         }
170     }
171
172     return 200;
173 }
174
175 inline int web_client_api_old_data_request_json(RRDHOST *host, struct web_client *w, char *url) {
176     return web_client_api_old_data_request(host, w, url, DATASOURCE_JSON);
177 }
178
179 inline int web_client_api_old_data_request_jsonp(RRDHOST *host, struct web_client *w, char *url) {
180     return web_client_api_old_data_request(host, w, url, DATASOURCE_DATATABLE_JSONP);
181 }
182
183 inline int web_client_api_old_graph_request(RRDHOST *host, struct web_client *w, char *url) {
184     // get the name of the data to show
185     char *tok = mystrsep(&url, "/?&");
186     if(tok && *tok) {
187         debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
188
189         // do we have such a data set?
190         RRDSET *st = rrdset_find_byname(host, tok);
191         if(!st) st = rrdset_find(host, tok);
192         if(!st) {
193             // we don't have it
194             // try to send a file with that name
195             buffer_flush(w->response.data);
196             return mysendfile(w, tok);
197         }
198
199         debug(D_WEB_CLIENT_ACCESS, "%llu: Sending %s.json of RRD_STATS...", w->id, st->name);
200         w->response.data->contenttype = CT_APPLICATION_JSON;
201         buffer_flush(w->response.data);
202         rrd_stats_graph_json(st, url, w->response.data);
203         return 200;
204     }
205
206     buffer_flush(w->response.data);
207     buffer_strcat(w->response.data, "Graph name?\r\n");
208     return 400;
209 }
210
211 inline int web_client_api_old_list_request(RRDHOST *host, struct web_client *w, char *url) {
212     (void)url;
213
214     buffer_flush(w->response.data);
215     RRDSET *st;
216
217     rrdhost_rdlock(host);
218     rrdset_foreach_read(st, host) buffer_sprintf(w->response.data, "%s\n", st->name);
219     rrdhost_unlock(host);
220
221     return 200;
222 }
223
224 inline int web_client_api_old_all_json(RRDHOST *host, struct web_client *w, char *url) {
225     (void)url;
226
227     w->response.data->contenttype = CT_APPLICATION_JSON;
228     buffer_flush(w->response.data);
229     rrd_stats_all_json(host, w->response.data);
230     return 200;
231 }