This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gmon.go
91 lines (81 loc) · 2.3 KB
/
gmon.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
// Package gmon provides read access to the gmon data.
package gmon
import (
"bufio"
"encoding/xml"
"fmt"
"io"
"net"
)
// ExtraElement is one extra on a metric.
type ExtraElement struct {
Name string `xml:"NAME,attr"`
Val string `xml:"VAL,attr"`
}
// ExtraData is the set extras on a metric.
type ExtraData struct {
ExtraElements []ExtraElement `xml:"EXTRA_ELEMENT"`
}
// Metric as returned by gmond.
type Metric struct {
Name string `xml:"NAME,attr"`
Value string `xml:"VAL,attr"`
Unit string `xml:"UNITS,attr"`
Slope string `xml:"SLOPE,attr"`
Tn int `xml:"TN,attr"`
Tmax int `xml:"TMAX,attr"`
Dmax int `xml:"DMAX,attr"`
ExtraData ExtraData `xml:"EXTRA_DATA"`
}
// Host as returned by gmon.
type Host struct {
Name string `xml:"NAME,attr"`
IP string `xml:"IP,attr"`
Tags string `xml:"TAGS,attr"`
Reported int `xml:"REPORTED,attr"`
Tn int `xml:"TN,attr"`
Tmax int `xml:"TMAX,attr"`
Dmax int `xml:"DMAX,attr"`
Location string `xml:"LOCATION,attr"`
GmondStarted int `xml:"GMOND_STARTED,attr"`
Metrics []Metric `xml:"METRIC"`
}
// Cluster as returned by gmon.
type Cluster struct {
Name string `xml:"NAME,attr"`
Owner string `xml:"OWNER,attr"`
LatLong string `xml:"LATLONG,attr"`
URL string `xml:"URL,attr"`
Localtime int `xml:"LOCALTIME,attr"`
Hosts []Host `xml:"HOST"`
}
// Ganglia is the root document returned by gmon.
type Ganglia struct {
XMLNAME xml.Name `xml:"GANGLIA_XML"`
Clusters []Cluster `xml:"CLUSTER"`
}
// Read the gmond XML output.
func Read(r io.Reader) (*Ganglia, error) {
ganglia := Ganglia{}
decoder := xml.NewDecoder(r)
decoder.CharsetReader = charsetReader
if err := decoder.Decode(&ganglia); err != nil {
return nil, err
}
return &ganglia, nil
}
// RemoteRead will connect to the given network/address and read from it.
func RemoteRead(network, addr string) (*Ganglia, error) {
c, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
defer c.Close()
return Read(bufio.NewReader(c))
}
func charsetReader(charset string, input io.Reader) (io.Reader, error) {
if charset != "ISO-8859-1" {
return nil, fmt.Errorf("unsupported charset %s", charset)
}
return input, nil
}