forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
57 lines (50 loc) · 973 Bytes
/
util.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
// Package util defines utilities for bosun.
package util // import "bosun.org/util"
import (
"os"
"regexp"
"strings"
)
var (
// Hostname is the machine's hostname.
Hostname string
// FullHostname will, if false, uses the hostname upto the first ".". Run Set()
// manually after changing.
FullHostname bool
)
// Clean cleans a hostname based on the current FullHostname setting.
func Clean(s string) string {
if !FullHostname {
s = strings.SplitN(s, ".", 2)[0]
}
return strings.ToLower(s)
}
// Set sets Hostntame based on the current preferences.
func Set() {
h, err := os.Hostname()
if err == nil {
if !FullHostname {
h = strings.SplitN(h, ".", 2)[0]
}
} else {
h = "unknown"
}
Hostname = Clean(h)
}
func NameMatches(name string, regexes []*regexp.Regexp) bool {
for _, r := range regexes {
if r.MatchString(name) {
return true
}
}
return false
}
func Btoi(b bool) int {
if b {
return 1
}
return 0
}
func init() {
Set()
}