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