-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
102 lines (89 loc) · 2.05 KB
/
config.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
package main
import (
"encoding/xml"
"io/ioutil"
"log"
"os"
"strconv"
"time"
)
var GlobalConfig *XMLConfig
type ValueAttr struct {
Value string `xml:"value,attr"`
}
func (va ValueAttr) ToInt() int {
val, err := strconv.Atoi(va.Value)
if err != nil {
panic(err)
}
return val
}
func (va ValueAttr) ToFloat() float64 {
val, err := strconv.ParseFloat(va.Value, 64)
if err != nil {
panic(err)
}
return val
}
func (va ValueAttr) ToString() string {
return va.Value
}
type TCPService struct {
Name ValueAttr
IPAddress ValueAttr
Port ValueAttr
MaxConnections ValueAttr
ImportanceFactor ValueAttr
}
type CPU struct {
ImportanceFactor ValueAttr
ThresholdValue ValueAttr
}
type RAM struct {
ImportanceFactor ValueAttr
ThresholdValue ValueAttr
}
type XMLConfig struct {
XMLName xml.Name `xml:"xml"`
Cpu CPU
Ram RAM
TCPService []TCPService
ReadAgentStatusFromConfig ValueAttr
ReadAgentStatusFromConfigInterval ValueAttr
AgentStatus ValueAttr
Interval ValueAttr
}
func readConfig() {
xmlFile, err := os.Open("G:/assingment/src/Feedback-discovery/config.xml")
if err != nil {
panic(err)
}
defer xmlFile.Close()
content, err := ioutil.ReadAll(xmlFile)
if err != nil {
panic(err)
}
err = xml.Unmarshal(content, &GlobalConfig)
if err != nil {
panic(err)
}
if GlobalConfig.ReadAgentStatusFromConfig.Value == "true" {
go func() {
time.Sleep(time.Second * time.Duration(GlobalConfig.ReadAgentStatusFromConfigInterval.ToInt()))
readConfig()
}()
}
}
func InitConfig() {
f, err := os.OpenFile("G:/assingment/src/Feedback-discovery/testlogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
log.SetOutput(f)
log.Println("This is a test log entry")
readConfig()
go func() {
time.Sleep(time.Second * time.Duration(GlobalConfig.Interval.ToInt()))
initialRun = false
}()
}