-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm-parser.go
152 lines (139 loc) · 3.94 KB
/
pm-parser.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
package collector
import (
"encoding/xml"
"fmt"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"os"
"strings"
)
var pmDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "pm", "data"),
"THIS IS DESCRIPTION TEXT FOR PROMETHEUS METRICS",
[]string{
"SAMPLETEXT1",
},
nil,
)
type pmCollector struct {
logger log.Logger
}
type measDataFile struct {
XMLName xml.Name `xml:"measDataFile"`
FileHeader fileHeader `xml:"fileHeader"`
MeasData measData `xml:"measData"`
FileFooter fileFooter `xml:"fileFooter"`
}
type fileHeader struct {
XMLName xml.Name `xml:"fileHeader"`
FileFormatVersion string `xml:"fileFormatVersion,attr"`
VendorName string `xml:"vendorName,attr"`
DnPrefix string `xml:"dnPrefix,attr"`
FileSender fileSender `xml:"fileSender"`
MeasData measData `xml:"measData"`
}
type fileSender struct {
XMLName xml.Name `xml:"fileSender"`
SenderName string `xml:"senderName,attr"`
SenderType string `xml:"senderType,attr"`
}
type fileFooter struct {
XMLName xml.Name `xml:"fileFooter"`
MeasData measData `xml:"measData"`
}
type measData struct {
XMLName xml.Name `xml:"measData"`
MeasEntity measEntity `xml:"measEntity"`
MeasInfo []measInfo `xml:"measInfo"`
BeginTime string `xml:"beginTime,attr"`
EndTime string `xml:"endTime,attr"`
}
type measEntity struct {
XMLName xml.Name `xml:"measEntity"`
Key string `xml:"localDn,attr"`
Key2 string `xml:"swVersion,attr"`
}
type measInfo struct {
XMLName xml.Name `xml:"measInfo"`
MeasInfoID string `xml:"measInfoId,attr"`
Job job `xml:"job"`
GranPeriod granPeriod `xml:"granPeriod"`
RepPeriod repPeriod `xml:"repPeriod"`
MeasType []measType `xml:"measType"`
MeasValue measValue `xml:"measValue"`
}
type job struct {
XMLName xml.Name `xml:"job"`
//XMLAttr xml.Attr `xml:"jobId,attr"`
Key string `xml:"jobId,attr"`
}
type granPeriod struct {
XMLName xml.Name `xml:"granPeriod"`
Key string `xml:"duration,attr"`
Key2 string `xml:"endTime,attr"`
}
type repPeriod struct {
XMLName xml.Name `xml:"repPeriod"`
Key string `xml:"duration,attr"`
}
type measType struct {
XMLName xml.Name `xml:"measType"`
Key string `xml:"p,attr"`
Value string `xml:",chardata"`
}
type measValue struct {
XMLName xml.Name `xml:"measValue"`
Key string `xml:"measObjLdn,attr"`
R []r `xml:"r"`
}
type r struct {
XMLName xml.Name `xml:"r"`
Key string `xml:"p,attr"`
Value float64 `xml:",chardata"`
}
func init() {
registerCollector("pm", defaultEnabled, newPmCollector)
}
// newPmCollector returns new pmCollector.
func newPmCollector(logger log.Logger) (Collector, error) {
return &pmCollector{logger}, nil
}
func (c *pmCollector) Update(ch chan<- prometheus.Metric) error {
fmt.Println("THIS IS NEW VERSION OF THE GO PM EXPORTER")
// xml 파일 오픈
fp, err := os.Open("/go/parse_this.xml")
if err != nil {
panic(err)
}
defer fp.Close()
// xml 파일 읽기
data, err := ioutil.ReadAll(fp)
// xml 디코딩
var measDataFile measDataFile
err = xml.Unmarshal(data, &measDataFile)
if err != nil {
panic(err)
}
ch <- prometheus.MustNewConstMetric(pmDesc, prometheus.GaugeValue, measDataFile.MeasData.MeasInfo[0].MeasValue.R[0].Value,
measDataFile.FileFooter.XMLName.Local,
)
measInfoList := measDataFile.MeasData.MeasInfo
measInfoListLen := len(measInfoList)
for i := 0; i < measInfoListLen; i++ {
measTypeList := measDataFile.MeasData.MeasInfo[i].MeasType
//measInfoIdValue := measDataFile.MeasData.MeasInfo[i].MeasInfoID
measTypeListLen := len(measTypeList)
for j := 0; j < measTypeListLen; j++ {
metricKey := strings.ToLower(strings.ReplaceAll(measTypeList[j].Value, ".", "_"))
metricValue := measInfoList[i].MeasValue.R[j].Value
ch <- prometheus.MustNewConstMetric(
pmDesc,
prometheus.GaugeValue,
metricValue,
metricKey,
)
}
}
return nil
}