forked from cernops/golbclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlbclient.go
141 lines (120 loc) · 3.71 KB
/
lbclient.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
package main
import (
"bufio"
"fmt"
"gitlab.cern.ch/lb-experts/golbclient/lbalias"
// This one supports long format and short format. Good for backward-compatibility
"github.com/jessevdk/go-flags"
"log"
"os"
"regexp"
"strconv"
"strings"
)
//Files for configuration
const LBALIASES_FILE = "/usr/local/etc/lbaliases"
const CONFIG_FILE = "/usr/local/etc/lbclient.conf"
type Options struct {
// Example of verbosity with level
CheckOnly bool `short:"c" long:"checkonly" description:"Return code shows if lbclient.conf is correct"`
Debug bool `short:"d" long:"debug" description:"Debug output"`
Syslog bool `short:"s" long:"logsyslog" description:"Log to syslog rather than stdout"`
NoLogin bool `short:"f" logn:"ignorenologin" description:"Ignore nologin files"`
// Example of optional value
GData []string `short:"g" long:"gdata" description:"Data for OID (required for snmp interface)"`
NData []string `short:"n" long:"ndata" description:"Data for OID (required for snmp interface)"`
}
const OID = ".1.3.6.1.4.1.96.255.1"
var options Options
var parser = flags.NewParser(&options, flags.Default)
//
//
//
func readLBAliases(filename string) []lbalias.LBalias {
aliasNames := []string{}
lbAliases := []lbalias.LBalias{}
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
r, _ := regexp.Compile("^lbalias=([^\t\n\f\r ]+)$")
for scanner.Scan() {
line := scanner.Text()
alias := r.FindStringSubmatch(line)
if len(alias) > 0 {
aliasNames = append(aliasNames, alias[1])
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
for i := 0; i < len(aliasNames); i++ {
//Check if the file exist
configFile := ""
aliasName := aliasNames[i]
if _, err := os.Stat(CONFIG_FILE + "." + aliasNames[i]); !os.IsNotExist(err) {
if options.Debug {
fmt.Println("[readLBAliases] The specific configuration file exists for", aliasName)
}
configFile = CONFIG_FILE + "." + aliasNames[i]
} else {
if options.Debug {
fmt.Println("[readLBAliases] The config file does not exist for ", aliasName)
}
continue
//configFile = CONFIG_FILE
}
lbAliases = append(lbAliases, lbalias.LBalias{Name: aliasName,
Debug: options.Debug,
NoLogin: options.NoLogin,
Syslog: options.Syslog,
ConfigFile: configFile,
CheckXsessions: 0})
//fmt.Println(lbAliases)
//fmt.Println(aliasName)
}
if len(lbAliases) == 0 {
lbAliases = append(lbAliases, lbalias.LBalias{Name: "", Debug: options.Debug, NoLogin: options.NoLogin, Syslog: options.Syslog, ConfigFile: CONFIG_FILE, CheckXsessions: 0})
}
return lbAliases
}
func main() {
_, err := parser.Parse()
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
//Arguments parsed. Let's open the configuration file
lbAliases := readLBAliases(LBALIASES_FILE)
if options.Debug {
fmt.Println("[main] The aliases from the configuration file are: ", lbAliases)
}
// Checking the static configuration
for i, _ := range lbAliases {
lbAliases[i].Evaluate()
}
metricType := "integer"
metricValue := ""
if len(lbAliases) == 1 && lbAliases[0].Name == "" {
metricValue = strconv.Itoa(lbAliases[0].Metric)
} else {
keyvaluelist := []string{}
fmt.Println("And let's go to print the results")
for _, lbalias := range lbAliases {
fmt.Println(lbalias)
keyvaluelist = append(keyvaluelist, lbalias.Name+"="+strconv.Itoa(lbalias.Metric))
}
fmt.Println(keyvaluelist)
metricValue = strings.Join(keyvaluelist, ",")
metricType = "string"
}
if options.Debug {
fmt.Printf("[main] metric = %s\n", metricValue)
}
fmt.Printf("%s\n%s\n%s\n", OID, metricType, metricValue)
}