16b250c22128551541cf003b5a55de115bb664f7
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(fmt.Sprintf("LIST %s", 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 := proto.EscapeString(req.r.FormValue("query"))
58 if q == "''" {
59 return nil, errors.New("Empty query")
60 }
62 res, err := s.query(fmt.Sprintf("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 q string
75 switch req.cmd {
76 case "host":
77 if len(req.args) != 1 {
78 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
79 }
80 q = fmt.Sprintf("FETCH host %s", proto.EscapeString(req.args[0]))
81 case "service", "metric":
82 if len(req.args) != 2 {
83 return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
84 }
85 host := proto.EscapeString(req.args[0])
86 name := proto.EscapeString(req.args[1])
87 q = fmt.Sprintf("FETCH %s %s.%s", req.cmd, host, name)
88 default:
89 panic("Unknown request: fetch(" + req.cmd + ")")
90 }
92 res, err := s.query(q)
93 if err != nil {
94 return nil, err
95 }
96 return tmpl(s.results[req.cmd], res)
97 }
99 func (s *Server) query(cmd string) (interface{}, error) {
100 c := <-s.conns
101 defer func() { s.conns <- c }()
103 m := &proto.Message{
104 Type: proto.ConnectionQuery,
105 Raw: []byte(cmd),
106 }
107 if err := c.Send(m); err != nil {
108 return nil, fmt.Errorf("Query %q: %v", cmd, err)
109 }
111 for {
112 m, err := c.Receive()
113 if err != nil {
114 return nil, fmt.Errorf("Failed to receive server response: %v", err)
115 }
116 if m.Type == proto.ConnectionLog {
117 log.Println(string(m.Raw[4:]))
118 continue
119 } else if m.Type == proto.ConnectionError {
120 return nil, errors.New(string(m.Raw))
121 }
123 t, err := m.DataType()
124 if err != nil {
125 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
126 }
128 var res interface{}
129 switch t {
130 case proto.HostList:
131 var hosts []sysdb.Host
132 err = proto.Unmarshal(m, &hosts)
133 res = hosts
134 case proto.Host:
135 var host sysdb.Host
136 err = proto.Unmarshal(m, &host)
137 res = host
138 case proto.Timeseries:
139 var ts sysdb.Timeseries
140 err = proto.Unmarshal(m, &ts)
141 res = ts
142 default:
143 return nil, fmt.Errorf("Unsupported data type %d", t)
144 }
145 if err != nil {
146 return nil, fmt.Errorf("Failed to unmarshal response: %v", err)
147 }
148 return res, nil
149 }
150 }
152 // vim: set tw=78 sw=4 sw=4 noexpandtab :