This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ncd.go
162 lines (147 loc) · 4.39 KB
/
ncd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"log"
"flag"
"http"
"os"
"url"
"strings"
"encoding/base64"
"bytes"
"json"
"syslog"
"old/template"
"goprotobuf.googlecode.com/hg/proto"
)
var (
flagServer = flag.Bool("server", false, "run ncd server")
flagCmdTimeout = flag.Int64("timeout", 10, "check command timeout (in secs)")
flagHostname = flag.String("host", "", "reported hostname")
flagServicename = flag.String("service", "", "reported servicename")
flagURL = flag.String("url", "http://127.0.0.1:8323/ncd/", "url endpoint to post check data to")
flagSilent = flag.Bool("silent", false, "suppress output")
flagPassive = flag.Bool("passive", true, "submit as passive check")
flagCmdlist = flag.Bool("cmdlist", false, "arg is a file containing commands")
flagAddr = flag.String("addr", ":8323", "http service address")
flagEndpoint = flag.String("endpoint", "/ncd/", "http service endpoint")
flagUseSSL = flag.Bool("ssl", false, "use ssl")
flagSSLCert = flag.String("cert", "cert.pem", "ssl cert file")
flagSSLKey = flag.String("key", "key.pem", "ssl key file")
flagSpoolDir = flag.String("spooldir", "/var/nagios/spool/checkresults", "nagios spool directory")
flagSyslog = flag.Bool("syslog", true, "log to syslog- if false, log to stdout")
flagUsername = flag.String("username", "npd", "basic auth username")
flagPassword = flag.String("password", "npd", "basic auth password")
logger = syslog.NewLogger(syslog.LOG_INFO, log.Flags())
)
var templ = template.MustParse(templateStr, nil)
func root(w http.ResponseWriter, r *http.Request) {
// check header
if *flagPassword != "" && *flagUsername != "" {
auth, ok := r.Header["Authorization"]
if ok && strings.HasPrefix(auth[0], "Basic ") {
str := strings.TrimLeft(auth[0], "Basic ")
decode, err := base64.StdEncoding.DecodeString(str)
if err != nil {
log.Print("cannot decode auth string: ", err)
return
}
user, pass, err := url.UnescapeUserinfo(string(decode))
if err != nil {
log.Print("auth: couldn't decode user/pass: ", err)
}
if !(user == *flagUsername && pass == *flagPassword) {
log.Print("auth: wrong user/pass: ", user+"/"+pass, *r)
return
}
/* log.Printf("auth: %#v, user: %s, pass: %s", auth, user, pass)*/
} else {
log.Print("auth: no authorization")
return
}
}
checkpb := new(CheckResultSet)
if r.Method == "POST" {
cout := new(bytes.Buffer)
if _, err := cout.ReadFrom(r.Body); err != nil {
log.Print("error! ", err)
return
}
switch r.Header["Content-Type"][0] {
case "application/x-protobuf":
err := proto.Unmarshal(cout.Bytes(), checkpb)
if err != nil {
log.Printf("unmarshalling error: ", err)
}
case "application/json", "text/plain", "application/x-www-form-urlencoded", "multipart/form-data":
err := json.Unmarshal(cout.Bytes(), checkpb)
if err != nil {
log.Printf("unmarshalling error: ", err)
}
}
logger.Printf("check returned! %s", proto.CompactTextString(checkpb))
for _, v := range checkpb.Results {
_, err := WriteCheck(v, *flagSpoolDir)
if err != nil {
logger.Print("writecheck failed: ", err)
}
}
} else {
/* logger.Printf("NOT POST!! %s", r.Method)*/
}
templ.Execute(w, nil)
}
func main() {
flag.Parse()
cmd := flag.Args()
// set up logging
if !*flagSyslog {
logger = log.New(os.Stderr, log.Prefix(), log.Flags())
}
switch *flagServer {
case true:
http.HandleFunc(*flagEndpoint, root)
logger.Print("bringing up endpoint")
var err os.Error
if *flagUseSSL {
err = http.ListenAndServeTLS(*flagAddr, *flagSSLCert, *flagSSLKey, nil)
} else {
err = http.ListenAndServe(*flagAddr, nil)
}
if err != nil {
log.Fatal("ListenAndServe:", err)
}
case false:
if *flagSilent {
devnull, _ := os.Open(os.DevNull)
log.SetOutput(devnull)
}
switch {
case *flagHostname == "":
log.Fatal("missing hostname")
case *flagServicename == "":
log.Fatal("missing servicename")
case len(cmd) < 1:
log.Fatal("missing command(s)")
}
msg := new(CheckResultSet)
if *flagCmdlist {
for _, v := range cmd {
f, err := os.Open(v)
if err != nil {
log.Print("error: ", err)
} else {
defer f.Close()
runCommandList(f, msg)
}
}
} else {
runSingleCheck(msg, cmd)
}
buf, err := proto.Marshal(msg)
if err != nil {
log.Fatal("marshalling error: ", err)
}
PostToEndpoint(buf, *flagURL)
}
}
const templateStr = `ok`