From 7f045f968c341402edeb2d9ed00f74ecd9b452da Mon Sep 17 00:00:00 2001 From: fluepke Date: Fri, 4 Dec 2020 20:55:14 +0100 Subject: [PATCH] Add scraping of call log --- collector/api.go | 43 +++++++++++++++++++++++++++++++++++------- collector/collector.go | 25 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/collector/api.go b/collector/api.go index add3127..46eb6a7 100644 --- a/collector/api.go +++ b/collector/api.go @@ -146,6 +146,32 @@ type StationStatusData struct { IpPrefixClass string `json:"IpPrefixClass"` } +type CallLog struct { + Lines map[string]*PhoneNumberCallLog + Line0 *PhoneNumberCallLog `json:"0"` + Line1 *PhoneNumberCallLog `json:"1"` + Token string `json:"token"` +} + +type PhoneNumberCallLog struct { + Error string `json:"error"` + Message string `json:"message"` + Data *CallLogData `json:"data"` +} + +type CallLogData struct { + Entries []*CallLogEntry `json:"CallTbl"` +} + +type CallLogEntry struct { + Id string `json:"__id"` + EndTime string `json:"endTime"` + StartTime string `json:"startTime"` + ExternalNumber string `json:"externalNumber"` + Direction string `json:"Direction"` + Type string `json:"type"` +} + func NewVodafoneStation(stationUrl, password string) *VodafoneStation { cookieJar, err := cookiejar.New(nil) parsedUrl, err := url.Parse(stationUrl) @@ -216,9 +242,6 @@ func (v *VodafoneStation) GetDocsisStatus() (*DocsisStatusResponse, error) { } func (v *VodafoneStation) GetStationStatus() (*StationStatusReponse, error) { - if err := v.CheckTimeout(); err != nil { - return nil, err - } responseBody, err := v.doRequest("GET", v.URL+"/api/v1/sta_status?_="+strconv.FormatInt(makeTimestamp(), 10), "") if err != nil { return nil, err @@ -227,12 +250,18 @@ func (v *VodafoneStation) GetStationStatus() (*StationStatusReponse, error) { return stationStatusReponse, json.Unmarshal(responseBody, stationStatusReponse) } -func (v *VodafoneStation) CheckTimeout() error { - _, err := v.doRequest("GET", v.URL+"/api/v1/CheckTimeOut?_="+strconv.FormatInt(makeTimestamp(), 10), "") +func (v *VodafoneStation) GetCallLog() (*CallLog, error) { + responseBody, err := v.doRequest("GET", v.URL+"/api/v1/phone_calllog/1,2/CallTbl?_="+strconv.FormatInt(makeTimestamp(), 10), "") if err != nil { - return err + return nil, err } - return nil + callLog := &CallLog{} + err = json.Unmarshal(responseBody, callLog) + if err != nil { + return nil, err + } + callLog.Lines = map[string]*PhoneNumberCallLog{"0": callLog.Line0, "1": callLog.Line1} + return callLog, nil } func makeTimestamp() int64 { diff --git a/collector/collector.go b/collector/collector.go index e7f488e..946d542 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -62,6 +62,9 @@ var ( ipAddressRTDesc *prometheus.Desc ipPrefixClassDesc *prometheus.Desc + callEndTimeDesc *prometheus.Desc + callStartTimeDesc *prometheus.Desc + logoutSuccessDesc *prometheus.Desc logoutMessageDesc *prometheus.Desc ) @@ -121,6 +124,9 @@ func init() { ipAddressRTDesc = prometheus.NewDesc(prefix+"ip_address_rt_info", "IP address RT", []string{"ip"}, nil) ipPrefixClassDesc = prometheus.NewDesc(prefix+"ip_prefix_class_info", "IP prefix class info", []string{"prefix_class"}, nil) + callEndTimeDesc = prometheus.NewDesc(prefix+"call_end_time_epoch", "Call endtime as unix epoch", []string{"port", "id", "external_number", "direction", "type"}, nil) + callStartTimeDesc = prometheus.NewDesc(prefix+"call_start_time_epoch", "Call starttime as unix epoch", []string{"port", "id", "external_number", "direction", "type"}, nil) + logoutSuccessDesc = prometheus.NewDesc(prefix+"logout_success_bool", "1 if the logout was successfull", nil, nil) logoutMessageDesc = prometheus.NewDesc(prefix+"logout_message_info", "Logout message returned by the web interface", []string{"message"}, nil) } @@ -176,6 +182,9 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) { ch <- ipAddressRTDesc ch <- ipPrefixClassDesc + ch <- callEndTimeDesc + ch <- callStartTimeDesc + ch <- logoutSuccessDesc ch <- logoutMessageDesc } @@ -272,6 +281,22 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric(ipPrefixClassDesc, prometheus.GaugeValue, 1, stationStatusResponse.Data.IpPrefixClass) } + callLog, err := c.Station.GetCallLog() + if err != nil { + log.With("error", err.Error()).Error("Failed to get call log") + } else { + for port, phoneNumberCallLog := range callLog.Lines { + if phoneNumberCallLog.Data == nil { + continue + } + for _, callLogEntry := range phoneNumberCallLog.Data.Entries { //port", "id", "external_number", "direction", "type + labels := []string{port, callLogEntry.Id, callLogEntry.ExternalNumber, callLogEntry.Direction, callLogEntry.Type} + ch <- prometheus.MustNewConstMetric(callEndTimeDesc, prometheus.GaugeValue, parse2float(callLogEntry.EndTime), labels...) + ch <- prometheus.MustNewConstMetric(callStartTimeDesc, prometheus.GaugeValue, parse2float(callLogEntry.StartTime), labels...) + } + } + } + logoutresponse, err := c.Station.Logout() if logoutresponse != nil { ch <- prometheus.MustNewConstMetric(logoutMessageDesc, prometheus.GaugeValue, 1, logoutresponse.Message)