75707ffbdd6540edeb1a4e00330ba7e53f35bd3b
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"
37 "github.com/sysdb/go/proto"
38 "github.com/sysdb/go/sysdb"
39 )
41 func listAll(req request, s *Server) (*page, error) {
42 if len(req.args) != 0 {
43 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
44 }
46 res, err := s.query("LIST %s", identifier(req.cmd))
47 if err != nil {
48 return nil, err
49 }
50 // the template *must* exist
51 return tmpl(s.results[req.cmd], res)
52 }
54 func lookup(req request, s *Server) (*page, error) {
55 if req.r.Method != "POST" {
56 return nil, errors.New("Method not allowed")
57 }
58 q := req.r.FormValue("query")
59 if q == "''" {
60 return nil, errors.New("Empty query")
61 }
63 res, err := s.query("LOOKUP hosts MATCHING name =~ %s", q)
64 if err != nil {
65 return nil, err
66 }
67 return tmpl(s.results["hosts"], res)
68 }
70 func fetch(req request, s *Server) (*page, error) {
71 if len(req.args) == 0 {
72 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
73 }
75 var res interface{}
76 var err error
77 switch req.cmd {
78 case "host":
79 if len(req.args) != 1 {
80 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
81 }
82 res, err = s.query("FETCH host %s", req.args[0])
83 case "service", "metric":
84 if len(req.args) != 2 {
85 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
86 }
87 res, err = s.query("FETCH %s %s.%s", identifier(req.cmd), req.args[0], req.args[1])
88 default:
89 panic("Unknown request: fetch(" + req.cmd + ")")
90 }
91 if err != nil {
92 return nil, err
93 }
94 return tmpl(s.results[req.cmd], res)
95 }
97 type identifier string
99 func (s *Server) query(cmd string, args ...interface{}) (interface{}, error) {
100 c := <-s.conns
101 defer func() { s.conns <- c }()
103 for i, arg := range args {
104 switch v := arg.(type) {
105 case identifier:
106 // Nothing to do.
107 case string:
108 args[i] = proto.EscapeString(v)
109 case time.Time:
110 args[i] = v.Format("2006-01-02 15:04:05")
111 default:
112 panic(fmt.Sprintf("query: invalid type %T", arg))
113 }
114 }
116 cmd = fmt.Sprintf(cmd, args...)
117 m := &proto.Message{
118 Type: proto.ConnectionQuery,
119 Raw: []byte(cmd),
120 }
121 if err := c.Send(m); err != nil {
122 return nil, fmt.Errorf("Query %q: %v", cmd, err)
123 }
125 for {
126 m, err := c.Receive()
127 if err != nil {
128 return nil, fmt.Errorf("Failed to receive server response: %v", err)
129 }
130 if m.Type == proto.ConnectionLog {
131 log.Println(string(m.Raw[4:]))
132 continue
133 } else if m.Type == proto.ConnectionError {
134 return nil, errors.New(string(m.Raw))
135 }
137 t, err := m.DataType()
138 if err != nil {
139 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
140 }
142 var res interface{}
143 switch t {
144 case proto.HostList:
145 var hosts []sysdb.Host
146 err = proto.Unmarshal(m, &hosts)
147 res = hosts
148 case proto.Host:
149 var host sysdb.Host
150 err = proto.Unmarshal(m, &host)
151 res = host
152 case proto.Timeseries:
153 var ts sysdb.Timeseries
154 err = proto.Unmarshal(m, &ts)
155 res = ts
156 default:
157 return nil, fmt.Errorf("Unsupported data type %d", t)
158 }
159 if err != nil {
160 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
161 }
162 return res, nil
163 }
164 }
166 // vim: set tw=78 sw=4 sw=4 noexpandtab :