Skip to content
This repository was archived by the owner on Jan 22, 2024. It is now read-only.

Commit f7fab01

Browse files
author
Michael Kraus
committed
Add option to load arguments from config file
1 parent 20c2653 commit f7fab01

3 files changed

Lines changed: 55 additions & 20 deletions

File tree

README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ check_nsc_web -k -p "password from nsclient.ini" -u "https://<SERVER_RUNNING_NSC
3737
OK: No entries found|'count'=0;0;0 'problem_count'=0;0;0
3838
```
3939

40+
* Reading parameters and queries from file
41+
```
42+
check_nsc_web -config ./sample.conf
43+
OK: 5m: 0%, 1m: 0%, 5s: 0% |'total 5m'=0%;80;90;; 'total 1m'=0%;80;90;; 'total 5s'=0%;80;90;;
44+
```
45+
46+
Contents of ```sample.conf```:
47+
```
48+
u https://127.0.0.1:28443
49+
p password
50+
k true
51+
query check_cpu show-all
52+
```
53+
54+
Please note, that everything after query will be *appended* to existing query arguments.
55+
4056
## Program help
4157
```
4258
Usage of ./check_nsc_web:
@@ -68,22 +84,24 @@ Usage of ./check_nsc_web:
6884
check_nsc_web can and should be built with CGO_ENABLED=0
6985
7086
Options:
71-
-V Print program version.
87+
-V Print program version.
7288
-a string
73-
API version of NSClient++ (legacy or 1). (default "legacy")
89+
API version of NSClient++ (legacy or 1). (default "legacy")
7490
-f int
75-
Round performance data float values to this number of digits. (default -1)
76-
-j Print out JOSN response body.
77-
-k Insecure mode - skip TLS verification.
91+
Round performance data float values to this number of digits. (default -1)
92+
-j Print out JOSN response body.
93+
-k Insecure mode - skip TLS verification.
7894
-p string
79-
NSClient++ webserver password.
95+
NSClient++ webserver password.
8096
-t int
81-
Connection timeout in seconds, defaults to 10. (default 10)
97+
Connection timeout in seconds, defaults to 10. (default 10)
8298
-u string
83-
NSCLient++ URL, for example https://10.1.2.3:8443.
84-
-v Enable verbose output.
99+
NSCLient++ URL, for example https://10.1.2.3:8443.
100+
-v Enable verbose output.
85101
-x string
86-
Extra text to appear in output.
102+
Extra text to appear in output.
87103
-l string
88-
NSClient++ webserver login. (default "admin")
104+
NSClient++ webserver login. (default "admin")
105+
-config file_location
106+
Location of file with argument/query configuration
89107
```

check_nsc_web.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/tls"
66
"encoding/base64"
77
"encoding/json"
8-
"flag"
98
"fmt"
109
"net"
1110
"net/http"
@@ -15,13 +14,15 @@ import (
1514
"strconv"
1615
"strings"
1716
"time"
17+
18+
"github.com/namsral/flag"
1819
)
1920

2021
// TODO
2122
// - strip trailing / from url
2223
// - what about value being int64 in legacy api in PerfLine struct?
2324

24-
const AppVersion = "0.5.2"
25+
const AppVersion = "0.5.3"
2526

2627
var usage = `
2728
check_nsc_web is a REST client for the NSClient++ webserver for querying
@@ -140,6 +141,7 @@ func main() {
140141
var flagInsecure bool
141142
var flagFloatround int
142143
var flagExtratext string
144+
var flagQuery string
143145

144146
flag.StringVar(&flagURL, "u", "", "NSCLient++ URL, for example https://10.1.2.3:8443.")
145147
flag.StringVar(&flagLogin, "l", "admin", "NSClient++ webserver login.")
@@ -151,9 +153,13 @@ func main() {
151153
flag.BoolVar(&flagVersion, "V", false, "Print program version.")
152154
flag.BoolVar(&flagInsecure, "k", false, "Insecure mode - skip TLS verification.")
153155
flag.IntVar(&flagFloatround, "f", -1, "Round performance data float values to this number of digits.")
154-
flag.StringVar(&flagExtratext, "x", "", "Extra text to appear in output.")
156+
157+
// These flags support loading config from file using "-config FILENAME"
158+
flag.StringVar(&flagQuery, "query", "", "placeholder for query string from config file")
159+
flag.String(flag.DefaultConfigFlagname, "", "path to config file")
155160

156161
flag.Parse()
162+
157163
if flagVersion {
158164
fmt.Fprintln(os.Stderr, "check_nsc_web v"+AppVersion)
159165
os.Exit(0)
@@ -171,6 +177,13 @@ func main() {
171177
}
172178
}
173179

180+
args := flag.Args()
181+
// Has there a flag "query" been provided in the config file? Transform it into slice and append it to Args()
182+
if seen["query"] {
183+
q := strings.Split(flagQuery, " ")
184+
args = append(args, q...)
185+
}
186+
174187
timeout := time.Second * time.Duration(flagTimeout)
175188

176189
urlStruct, err := url.Parse(flagURL)
@@ -179,17 +192,17 @@ func main() {
179192
os.Exit(3)
180193
}
181194

182-
if len(flag.Args()) == 0 {
195+
if len(args) == 0 {
183196
urlStruct.Path += "/"
184197
} else {
185198
if flagAPIVersion == "1" {
186-
urlStruct.Path += "/api/v1/queries/" + flag.Arg(0) + "/commands/execute"
199+
urlStruct.Path += "/api/v1/queries/" + args[0] + "/commands/execute"
187200
} else {
188-
urlStruct.Path += "/query/" + flag.Arg(0)
201+
urlStruct.Path += "/query/" + args[0]
189202
}
190-
if len(flag.Args()) > 1 {
203+
if len(args) > 1 {
191204
var param bytes.Buffer
192-
for i, a := range flag.Args() {
205+
for i, a := range args {
193206
if i == 0 {
194207
continue
195208
} else if i > 1 {
@@ -269,7 +282,7 @@ func main() {
269282
fmt.Printf("RESPONSE:\n%q\n", dumpres)
270283
}
271284

272-
if len(flag.Args()) == 0 {
285+
if len(args) == 0 {
273286
fmt.Println("OK: NSClient API reachable on " + flagURL)
274287
os.Exit(0)
275288
} else {

sample.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
k true
2+
p password
3+
u https://127.0.0.1:28443
4+
query check_cpu show-all

0 commit comments

Comments
 (0)