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 if i == 0 && fields[1] == "" {
76 typ = fields[0]
77 } else {
78 args += fmt.Sprintf(" attribute[%s] = %s",
79 proto.EscapeString(fields[0]), proto.EscapeString(fields[1]))
80 }
81 } else {
82 args += fmt.Sprintf(" name =~ %s", proto.EscapeString(tok))
83 }
84 }
86 res, err := s.query("LOOKUP %s MATCHING"+args, identifier(typ))
87 if err != nil {
88 return nil, err
89 }
90 if t, ok := s.results[typ]; ok {
91 return tmpl(t, res)
92 }
93 return nil, fmt.Errorf("Unsupported type %s", typ)
94 }
96 func fetch(req request, s *Server) (*page, error) {
97 if len(req.args) == 0 {
98 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
99 }
101 var res interface{}
102 var err error
103 switch req.cmd {
104 case "host":
105 if len(req.args) != 1 {
106 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
107 }
108 res, err = s.query("FETCH host %s", req.args[0])
109 case "service", "metric":
110 if len(req.args) != 2 {
111 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
112 }
113 res, err = s.query("FETCH %s %s.%s", identifier(req.cmd), req.args[0], req.args[1])
114 if err == nil && req.cmd == "metric" {
115 return metric(req, res, s)
116 }
117 default:
118 panic("Unknown request: fetch(" + req.cmd + ")")
119 }
120 if err != nil {
121 return nil, err
122 }
123 return tmpl(s.results[req.cmd], res)
124 }
126 var datetime = "2006-01-02 15:04:05"
128 func metric(req request, res interface{}, s *Server) (*page, error) {
129 start := time.Now().Add(-24 * time.Hour)
130 end := time.Now()
131 if req.r.Method == "POST" {
132 var err error
133 // Parse the values first to verify their format.
134 if s := req.r.PostForm.Get("start_date"); s != "" {
135 if start, err = time.Parse(datetime, s); err != nil {
136 return nil, fmt.Errorf("Invalid start time %q", s)
137 }
138 }
139 if e := req.r.PostForm.Get("end_date"); e != "" {
140 if end, err = time.Parse(datetime, e); err != nil {
141 return nil, fmt.Errorf("Invalid end time %q", e)
142 }
143 }
144 }
146 p := struct {
147 StartTime string
148 EndTime string
149 URLStart string
150 URLEnd string
151 Data interface{}
152 }{
153 start.Format(datetime),
154 end.Format(datetime),
155 start.Format(urldate),
156 end.Format(urldate),
157 res,
158 }
159 return tmpl(s.results["metric"], &p)
160 }
162 // tokenize split the string s into its tokens where a token is either a quoted
163 // string or surrounded by one or more consecutive whitespace characters.
164 func tokenize(s string) ([]string, error) {
165 scan := scanner{}
166 tokens := []string{}
167 start := -1
168 for i, r := range s {
169 if !scan.inField(r) {
170 if start == -1 {
171 // Skip leading and consecutive whitespace.
172 continue
173 }
174 tok, err := unescape(s[start:i])
175 if err != nil {
176 return nil, err
177 }
178 tokens = append(tokens, tok)
179 start = -1
180 } else if start == -1 {
181 // Found a new field.
182 start = i
183 }
184 }
185 if start >= 0 {
186 // Last (or possibly only) field.
187 tok, err := unescape(s[start:])
188 if err != nil {
189 return nil, err
190 }
191 tokens = append(tokens, tok)
192 }
194 if scan.inQuotes {
195 return nil, errors.New("quoted string not terminated")
196 }
197 if scan.escaped {
198 return nil, errors.New("illegal character escape at end of string")
199 }
200 return tokens, nil
201 }
203 func unescape(s string) (string, error) {
204 var unescaped []byte
205 var i, n int
206 for i = 0; i < len(s); i++ {
207 if s[i] != '\\' {
208 n++
209 continue
210 }
212 if i >= len(s) {
213 return "", errors.New("illegal character escape at end of string")
214 }
215 if s[i+1] != ' ' && s[i+1] != '"' && s[i+1] != '\\' {
216 // Allow simple escapes only for now.
217 return "", fmt.Errorf("illegal character escape \\%c", s[i+1])
218 }
219 if unescaped == nil {
220 unescaped = []byte(s)
221 }
222 copy(unescaped[n:], s[i+1:])
223 }
225 if unescaped != nil {
226 return string(unescaped[:n]), nil
227 }
228 return s, nil
229 }
231 type scanner struct {
232 inQuotes bool
233 escaped bool
234 }
236 func (s *scanner) inField(r rune) bool {
237 if s.escaped {
238 s.escaped = false
239 return true
240 }
241 if r == '\\' {
242 s.escaped = true
243 return true
244 }
245 if s.inQuotes {
246 if r == '"' {
247 s.inQuotes = false
248 return false
249 }
250 return true
251 }
252 if r == '"' {
253 s.inQuotes = true
254 return false
255 }
256 return !unicode.IsSpace(r)
257 }
259 type identifier string
261 func (s *Server) query(cmd string, args ...interface{}) (interface{}, error) {
262 c := <-s.conns
263 defer func() { s.conns <- c }()
265 for i, arg := range args {
266 switch v := arg.(type) {
267 case identifier:
268 // Nothing to do.
269 case string:
270 args[i] = proto.EscapeString(v)
271 case time.Time:
272 args[i] = v.Format(datetime)
273 default:
274 panic(fmt.Sprintf("query: invalid type %T", arg))
275 }
276 }
278 cmd = fmt.Sprintf(cmd, args...)
279 m := &proto.Message{
280 Type: proto.ConnectionQuery,
281 Raw: []byte(cmd),
282 }
283 if err := c.Send(m); err != nil {
284 return nil, fmt.Errorf("Query %q: %v", cmd, err)
285 }
287 for {
288 m, err := c.Receive()
289 if err != nil {
290 return nil, fmt.Errorf("Failed to receive server response: %v", err)
291 }
292 if m.Type == proto.ConnectionLog {
293 log.Println(string(m.Raw[4:]))
294 continue
295 } else if m.Type == proto.ConnectionError {
296 return nil, errors.New(string(m.Raw))
297 }
299 t, err := m.DataType()
300 if err != nil {
301 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
302 }
304 var res interface{}
305 switch t {
306 case proto.HostList:
307 var hosts []sysdb.Host
308 err = proto.Unmarshal(m, &hosts)
309 res = hosts
310 case proto.Host:
311 var host sysdb.Host
312 err = proto.Unmarshal(m, &host)
313 res = host
314 case proto.Timeseries:
315 var ts sysdb.Timeseries
316 err = proto.Unmarshal(m, &ts)
317 res = ts
318 default:
319 return nil, fmt.Errorf("Unsupported data type %d", t)
320 }
321 if err != nil {
322 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
323 }
324 return res, nil
325 }
326 }
328 // vim: set tw=78 sw=4 sw=4 noexpandtab :