1 //
2 // Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 package server
28 // Helper functions for handling queries.
30 import (
31 "errors"
32 "fmt"
33 "log"
34 "strings"
35 "time"
36 "unicode"
38 "github.com/sysdb/go/proto"
39 "github.com/sysdb/go/sysdb"
40 )
42 func listAll(req request, s *Server) (*page, error) {
43 if len(req.args) != 0 {
44 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
45 }
47 res, err := s.query("LIST %s", identifier(req.cmd))
48 if err != nil {
49 return nil, err
50 }
51 // the template *must* exist
52 return tmpl(s.results[req.cmd], res)
53 }
55 func lookup(req request, s *Server) (*page, error) {
56 if req.r.Method != "POST" {
57 return nil, errors.New("Method not allowed")
58 }
59 tokens, err := tokenize(req.r.PostForm.Get("query"))
60 if err != nil {
61 return nil, err
62 }
63 if len(tokens) == 0 {
64 return nil, errors.New("Empty query")
65 }
67 typ := "hosts"
68 var args string
69 for i, tok := range tokens {
70 if len(args) > 0 {
71 args += " AND"
72 }
74 if fields := strings.SplitN(tok, ":", 2); len(fields) == 2 {
75 // Query: [<type>:] [<sibling-type>.]<attribute>:<value> ...
76 if i == 0 && fields[1] == "" {
77 typ = fields[0]
78 } else if elems := strings.Split(fields[0], "."); len(elems) > 1 {
79 objs := elems[:len(elems)-1]
80 for _, o := range objs {
81 if o != "host" && o != "service" && o != "metric" {
82 return nil, fmt.Errorf("Invalid object type %q", o)
83 }
84 }
85 args += fmt.Sprintf(" %s.attribute[%s] = %s",
86 strings.Join(objs, "."), proto.EscapeString(elems[len(elems)-1]),
87 proto.EscapeString(fields[1]))
88 } else {
89 args += fmt.Sprintf(" attribute[%s] = %s",
90 proto.EscapeString(fields[0]), proto.EscapeString(fields[1]))
91 }
92 } else {
93 args += fmt.Sprintf(" name =~ %s", proto.EscapeString(tok))
94 }
95 }
97 res, err := s.query("LOOKUP %s MATCHING"+args, identifier(typ))
98 if err != nil {
99 return nil, err
100 }
101 if t, ok := s.results[typ]; ok {
102 return tmpl(t, res)
103 }
104 return nil, fmt.Errorf("Unsupported type %s", typ)
105 }
107 func fetch(req request, s *Server) (*page, error) {
108 if len(req.args) == 0 {
109 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
110 }
112 var res interface{}
113 var err error
114 switch req.cmd {
115 case "host":
116 if len(req.args) != 1 {
117 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
118 }
119 res, err = s.query("FETCH host %s", req.args[0])
120 case "service", "metric":
121 if len(req.args) != 2 {
122 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
123 }
124 res, err = s.query("FETCH %s %s.%s", identifier(req.cmd), req.args[0], req.args[1])
125 if err == nil && req.cmd == "metric" {
126 return metric(req, res, s)
127 }
128 default:
129 panic("Unknown request: fetch(" + req.cmd + ")")
130 }
131 if err != nil {
132 return nil, err
133 }
134 return tmpl(s.results[req.cmd], res)
135 }
137 var datetime = "2006-01-02 15:04:05"
139 func metric(req request, res interface{}, s *Server) (*page, error) {
140 start := time.Now().Add(-24 * time.Hour)
141 end := time.Now()
142 if req.r.Method == "POST" {
143 var err error
144 // Parse the values first to verify their format.
145 if s := req.r.PostForm.Get("start_date"); s != "" {
146 if start, err = time.Parse(datetime, s); err != nil {
147 return nil, fmt.Errorf("Invalid start time %q", s)
148 }
149 }
150 if e := req.r.PostForm.Get("end_date"); e != "" {
151 if end, err = time.Parse(datetime, e); err != nil {
152 return nil, fmt.Errorf("Invalid end time %q", e)
153 }
154 }
155 }
157 p := struct {
158 StartTime string
159 EndTime string
160 URLStart string
161 URLEnd string
162 Data interface{}
163 }{
164 start.Format(datetime),
165 end.Format(datetime),
166 start.Format(urldate),
167 end.Format(urldate),
168 res,
169 }
170 return tmpl(s.results["metric"], &p)
171 }
173 // tokenize split the string s into its tokens where a token is either a quoted
174 // string or surrounded by one or more consecutive whitespace characters.
175 func tokenize(s string) ([]string, error) {
176 scan := scanner{}
177 tokens := []string{}
178 start := -1
179 for i, r := range s {
180 if !scan.inField(r) {
181 if start == -1 {
182 // Skip leading and consecutive whitespace.
183 continue
184 }
185 tok, err := unescape(s[start:i])
186 if err != nil {
187 return nil, err
188 }
189 tokens = append(tokens, tok)
190 start = -1
191 } else if start == -1 {
192 // Found a new field.
193 start = i
194 }
195 }
196 if start >= 0 {
197 // Last (or possibly only) field.
198 tok, err := unescape(s[start:])
199 if err != nil {
200 return nil, err
201 }
202 tokens = append(tokens, tok)
203 }
205 if scan.inQuotes {
206 return nil, errors.New("quoted string not terminated")
207 }
208 if scan.escaped {
209 return nil, errors.New("illegal character escape at end of string")
210 }
211 return tokens, nil
212 }
214 func unescape(s string) (string, error) {
215 var unescaped []byte
216 var i, n int
217 for i = 0; i < len(s); i++ {
218 if s[i] != '\\' {
219 n++
220 continue
221 }
223 if i >= len(s) {
224 return "", errors.New("illegal character escape at end of string")
225 }
226 if s[i+1] != ' ' && s[i+1] != '"' && s[i+1] != '\\' {
227 // Allow simple escapes only for now.
228 return "", fmt.Errorf("illegal character escape \\%c", s[i+1])
229 }
230 if unescaped == nil {
231 unescaped = []byte(s)
232 }
233 copy(unescaped[n:], s[i+1:])
234 }
236 if unescaped != nil {
237 return string(unescaped[:n]), nil
238 }
239 return s, nil
240 }
242 type scanner struct {
243 inQuotes bool
244 escaped bool
245 }
247 func (s *scanner) inField(r rune) bool {
248 if s.escaped {
249 s.escaped = false
250 return true
251 }
252 if r == '\\' {
253 s.escaped = true
254 return true
255 }
256 if s.inQuotes {
257 if r == '"' {
258 s.inQuotes = false
259 return false
260 }
261 return true
262 }
263 if r == '"' {
264 s.inQuotes = true
265 return false
266 }
267 return !unicode.IsSpace(r)
268 }
270 type identifier string
272 func (s *Server) query(cmd string, args ...interface{}) (interface{}, error) {
273 c := <-s.conns
274 defer func() { s.conns <- c }()
276 for i, arg := range args {
277 switch v := arg.(type) {
278 case identifier:
279 // Nothing to do.
280 case string:
281 args[i] = proto.EscapeString(v)
282 case time.Time:
283 args[i] = v.Format(datetime)
284 default:
285 panic(fmt.Sprintf("query: invalid type %T", arg))
286 }
287 }
289 cmd = fmt.Sprintf(cmd, args...)
290 m := &proto.Message{
291 Type: proto.ConnectionQuery,
292 Raw: []byte(cmd),
293 }
294 if err := c.Send(m); err != nil {
295 return nil, fmt.Errorf("Query %q: %v", cmd, err)
296 }
298 for {
299 m, err := c.Receive()
300 if err != nil {
301 return nil, fmt.Errorf("Failed to receive server response: %v", err)
302 }
303 if m.Type == proto.ConnectionLog {
304 log.Println(string(m.Raw[4:]))
305 continue
306 } else if m.Type == proto.ConnectionError {
307 return nil, errors.New(string(m.Raw))
308 }
310 t, err := m.DataType()
311 if err != nil {
312 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
313 }
315 var res interface{}
316 switch t {
317 case proto.HostList:
318 var hosts []sysdb.Host
319 err = proto.Unmarshal(m, &hosts)
320 res = hosts
321 case proto.Host:
322 var host sysdb.Host
323 err = proto.Unmarshal(m, &host)
324 res = host
325 case proto.Timeseries:
326 var ts sysdb.Timeseries
327 err = proto.Unmarshal(m, &ts)
328 res = ts
329 default:
330 return nil, fmt.Errorf("Unsupported data type %d", t)
331 }
332 if err != nil {
333 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
334 }
335 return res, nil
336 }
337 }
339 // vim: set tw=78 sw=4 sw=4 noexpandtab :