forked from rif/telegraf-freeswitch
/
sofia_status.go
68 lines (60 loc) · 1.5 KB
/
sofia_status.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
package utils
import (
"bytes"
"encoding/xml"
"errors"
"regexp"
"strings"
"golang.org/x/net/html/charset"
)
var (
dataParser = regexp.MustCompile(`^sip:mod_sofia@(.+):\d+`)
runningParser = regexp.MustCompile(`^RUNNING \((\d+)\)`)
)
type Profile struct {
Name string `xml:"name"`
Type string `xml:"type"`
Data string `xml:"data"`
State string `xml:"state"`
}
type Profiles struct {
Profiles []*Profile `xml:"profile"`
}
type SofiaProfile struct {
Name string
Address string
Running string
}
func (sp *SofiaProfile) loadXMLProfile(p *Profile) error {
sp.Name = p.Name
dataGroupSlice := dataParser.FindStringSubmatch(p.Data)
if len(dataGroupSlice) != 2 {
return errors.New("could not find address info in: " + p.Data)
}
sp.Address = dataGroupSlice[1]
runningGroupSlice := runningParser.FindStringSubmatch(p.State)
if len(runningGroupSlice) != 2 {
return errors.New("cannot find running info in: " + p.State)
}
sp.Running = runningGroupSlice[1]
return nil
}
func ParseSofiaStatus(data string) ([]*SofiaProfile, error) {
data = strings.TrimSpace(data)
dec := xml.NewDecoder(bytes.NewBufferString(data))
dec.CharsetReader = charset.NewReaderLabel
profiles := &Profiles{}
err := dec.Decode(profiles)
if err != nil {
return nil, err
}
sofiaProfiles := make([]*SofiaProfile, len(profiles.Profiles))
for i, p := range profiles.Profiles {
sp := &SofiaProfile{}
if err := sp.loadXMLProfile(p); err != nil {
return nil, err
}
sofiaProfiles[i] = sp
}
return sofiaProfiles, nil
}